Lordicon
  • Icons
  • Docs
  • Resources
  • Pricing
Log in Sign up Log in
React
  • Foundations
  • Icon styles
  • Animation
  • Formats
  • Best practices
  • Integrations
  • Web
  • React
  • React Native
  • Plugins
  • WordPress
  • Figma
  • Google Docs & Slides
  • Support
  • Licenses
  • Account and Billing
  • Library
  • Troubleshooting

React

Lordicon React makes it easy to add and control animated icons in your React application. It uses the <LordIcon> component to display and interact with icons in Lottie JSON format, with support for React 19 and popular frameworks like Next.js, Vite, and React Router.

Under the hood, the component uses the <lord-icon> custom HTML element described in our Web Player documentation, bringing the same animation features and controls to React.

To get started, you need the Lordicon React package and your icon's Lottie JSON file.

Building a mobile app with React Native? It has its own player, @lordicon/react-native. See the React Native guide.

Quick start

Install the package:

npm install @lordicon/react

Download the icon's JSON from its page on Lordicon and put it in your public folder (public/ in most setups). Then use it:

import { LordIcon } from "@lordicon/react";

export function Hero() {
    return <LordIcon src="/icons/lock.json" trigger="hover" />;
}

Move the pointer over it and it plays. There's nothing to set up first: the component registers the element itself.

The icon is 32 × 32 px by default. Size it like any other element, with className, style or CSS:

<LordIcon
    src="/icons/lock.json"
    trigger="hover"
    style={{ width: 48, height: 48 }}
/>

Set the size to 1em and the icon grows with the text around it, like icons from an icon font.

Quick start

See it running in your browser.

States

Most icons hold more than one animation. We call them states, and their names tell you what they are for:

  • in-… for an entrance, like in-reveal
  • hover-… for a short effect that plays once, like hover-jump
  • loop-… for an animation that repeats nicely
  • morph-… for a change into another look and back

You can see an icon's states on its page on Lordicon. Pick one with state:

<LordIcon src="/icons/coins.json" trigger="loop" state="loop-spin" />

Without state the icon plays its default one.

By default Lordicon downloads the icon with only the animation you picked, to keep the file small. If you want to use several states of one icon (for an intro, a sequence or follow), or change its stroke on the page, choose the "Raw" format when you download it. Colors work either way.

Triggers

The trigger prop decides when the icon plays:

Trigger

Plays

hover

once, when the pointer enters the icon (or it gets keyboard focus)

click

once per click

loop

over and over

loop-on-hover

over and over, while the pointer is on the icon

in

once, when the icon scrolls into view

morph

to a second look on hover, and back when the pointer leaves

boomerang

there and straight back, on hover

follow

in step with your React state (see below)

sequence

a short script of steps you write

Some triggers take an option in parentheses, like a loop with a one-second pause: trigger="loop(1000)". Every option of every trigger is in the Web guide.

Play on the whole card

To make the icon react to the button or card around it, point target at that parent:

<a className="card" href="/pricing">
    <LordIcon src="/icons/coins.json" trigger="hover" target=".card" />
    Pricing
</a>

target also takes a ref, so the icon can follow an element that isn't around it:

import { useRef } from "react";
import { LordIcon } from "@lordicon/react";

function Unlock() {
    const button = useRef(null);

    return (
        <>
            <LordIcon src="/icons/lock.json" trigger="hover" target={button} />
            <button ref={button}>Unlock</button>
        </>
    );
}

Follow React state

This is where follow shines. Your state stays in React; the button carries it as aria-pressed, and the icon follows:

import { useState } from "react";
import { LordIcon } from "@lordicon/react";

function LockToggle() {
    const [on, setOn] = useState(false);

    return (
        <button aria-pressed={on} onClick={() => setOn(!on)}>
            <LordIcon
                src="/icons/lock.json"
                trigger="follow"
                target="button"
                state="morph-unlocked"
            />
        </button>
    );
}

No effects, no refs: when on changes, the icon morphs there or back.

Many UI libraries (Radix, shadcn/ui, Headless UI, React Aria) already set these attributes. A menu button has aria-expanded, so an icon inside it with trigger="follow(aria-expanded)" target="button" animates as the menu opens and closes.

For a task with several stages, put the stage on an element and give each value a state:

// stage is "idle", "busy" or "done", kept in your state
<div data-stage={stage}>
    <LordIcon
        src="/icons/coins.json"
        trigger="follow(data-stage, busy=loop-spin, done=hover-jump)"
        target="[data-stage]"
    />
</div>

Play an entrance first

intro plays an entrance the first time the icon comes into view; then the trigger takes over. It's a good fit for items that appear as your state changes:

function Cards({ cards }) {
    return cards.map((card) => (
        <div className="card" key={card.id}>
            <LordIcon
                src="/icons/coins.json"
                intro="in-reveal, after=.card"
                trigger="hover"
                target=".card"
            />
            {card.title}
        </div>
    ));
}

after=.card waits until the card has finished sliding in.

Colors and stroke

Colors have names, such as primary and secondary. Pass them as an object:

<LordIcon
    src="/icons/lock.json"
    trigger="hover"
    colors={{ primary: "#e5484d", secondary: "#3e63dd" }}
/>

colors, stroke and speed are props like any other. Change them from state and the icon updates while it plays, without loading again:

  • stroke sets the line width: light, regular or bold.
  • speed speeds it up or slows it down: 2 is twice as fast, 0.5 half as fast.

To make the whole icon one color, the same as the text around it, add currentColor:

<a href="/account" style={{ color: "tomato" }}>
    <LordIcon src="/icons/lock.json" trigger="hover" currentColor />
    Account
</a>

Colors can also come from CSS, which is handy for dark mode:

.dark lord-icon {
    --lord-icon-primary: #ffffff;
}

Loading

With many icons on a page, for example in a long list, load each one only when it's needed:

<LordIcon src="/icons/lock.json" trigger="hover" loading="lazy" />

lazy loads the icon when it scrolls into view, interaction on the first hover, click or focus, and delay:1000 a moment after the page.

Children show until the icon is ready. A static SVG of the same icon (you can download it from Lordicon too) makes the switch almost invisible:

<LordIcon src="/icons/lock.json" trigger="hover" loading="interaction">
    <img src="/icons/lock.svg" alt="" />
</LordIcon>

Give each item in a list its key as usual. When the list is reordered, the icons move with their rows and keep playing, without loading again.

Ref and events

A ref gives you the element, so you can play the icon from your code:

import { useRef } from "react";
import { LordIcon } from "@lordicon/react";

function Save() {
    const icon = useRef(null);

    return (
        <>
            <LordIcon ref={icon} src="/icons/coins.json" />
            <button onClick={() => icon.current.play({ state: "hover-jump" })}>
                Save
            </button>
        </>
    );
}

You don't have to wait for the icon to load first; the calls wait for it. Besides play() there are pause(), stop() and seek(). play() returns a promise, so you can play one state after another:

await icon.current.play({ state: "in-reveal" });
icon.current.play({ state: "hover-jump" });

Events have props of their own: onReady, onComplete and onError.

<LordIcon src="/icons/coins.json" onComplete={() => console.log("finished")} />

In TypeScript, the ref's type is LordIconElement: useRef<LordIconElement>(null), imported from @lordicon/react.

Icon data instead of a URL

If you'd rather import the JSON than serve it as a file, pass it as icon:

import lock from "./icons/lock.json";

<LordIcon icon={lock} trigger="hover" />;

This puts the icon inside your JavaScript bundle, so prefer src when you can. Keep the same object between renders; importing it at the top of the file, as above, does that.

Server rendering

<LordIcon> works with server rendering, Next.js Server Components included. You don't need 'use client' for the icon itself; the package takes care of that. Your own components that pass it a ref or an event prop still need it, as usual. The server sends <lord-icon> with its attributes and placeholder, and the icon comes to life in the browser.

Until then, the browser doesn't know the element's size. To keep the page from jumping, add this to your global CSS:

lord-icon {
    display: inline-block;
    width: 48px;
    height: 48px;
}

lord-icon:not(:defined) > * {
    width: 100%;
    height: 100%;
}

With server rendering, prefer src to icon: the URL is already in the HTML, so the icon starts loading before React hydrates.

The Next.js demo shows all of this in a complete app.

Your own trigger

When none of the built-in triggers fits, write one. Register it once, in code that runs in the browser before an icon uses it (your app's entry file, for example), and use its name in trigger:

import { BaseTrigger, defineElement } from "@lordicon/react";

class DoubleClick extends BaseTrigger {
    constructor(context) {
        super(context);
        this.listen(this.target, "dblclick", () =>
            this.player.play({ from: "start" }),
        );
    }
}

defineElement({ triggers: { "double-click": DoubleClick } });
<LordIcon src="/icons/lock.json" trigger="double-click" />

How triggers work is covered in the element README.

Using the Web guide

<LordIcon> is a thin layer over <lord-icon>, so the Web guide and its examples apply here too. Read them with this in mind:

In the Web guide

In React

<lord-icon trigger="hover" …>

<LordIcon trigger="hover" … />

current-color

currentColor

colors="primary:#121331"

colors={{ primary: '#121331' }} (the string works too)

icon.target = element

target={ref}

icon.play(), icon.states

ref.current.play(), ref.current.states

icon.addEventListener('complete', …)

onComplete={…}

import { … } from '@lordicon/element'

import { … } from '@lordicon/react'

CSS works the same way on lord-icon: color variables, :state(ready) and so on.

You don't install @lordicon/element yourself; it comes with @lordicon/react.

Examples

Every example runs in your browser on StackBlitz, so you can change the code and see what happens.

Getting started

src, trigger and a placeholder

Follow React state

Toggle buttons through aria-pressed

A process

An icon that follows the stage of a task kept in state

A target from a ref

A button elsewhere on the page plays the icon

Looks from React state

Colors, stroke, speed and currentColor changed live

Intro on mount

Cards added from state, each icon playing after its card

A list

Lazy loading, and rows that move without reloading

A placeholder

A still image until the icon is ready

States in CSS

Fading in with :state(ready), styling with :state(playing)

Methods and events

A ref, onReady and onComplete

Icon data

The icon prop instead of src

A custom trigger

BaseTrigger and defineElement

Apps

Next.js

Icons in Server Components, icons that follow state, server rendering without a jump

More examples of the element itself, from sequences to scroll-driven triggers, are in the Web guide.

Best practices

One animation or all of them

When you download an icon, the default format keeps only the animation you picked. The file is small and that's usually all you need. Choose "Raw" only when you want to switch between the icon's states, or change its stroke, in your app.

Reduced motion

When a visitor has asked their system for less motion, icons respect it: they show their final frame instead of playing, and loops stand still. To animate a particular icon anyway, add motion="always" to it.

Screen readers

Screen readers skip icons. If an icon means something on its own, without text next to it, give it an aria-label.

  • Intro
  • Quick start
  • States
  • Triggers
  • Colors and stroke
  • Loading
  • Ref and events
  • Server rendering
  • Your own trigger
  • Using the Web guide
  • Examples
  • Best practices
Web React Native