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.
React
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.
This content requires cookies. You must accept cookies to view it.
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, likein-revealhover-…for a short effect that plays once, likehover-jumploop-…for an animation that repeats nicelymorph-…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 |
|---|---|
|
|
once, when the pointer enters the icon (or it gets keyboard focus) |
|
|
once per click |
|
|
over and over |
|
|
over and over, while the pointer is on the icon |
|
|
once, when the icon scrolls into view |
|
|
to a second look on hover, and back when the pointer leaves |
|
|
there and straight back, on hover |
|
|
in step with your React state (see below) |
|
|
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:
strokesets the line width:light,regularorbold.speedspeeds it up or slows it down:2is twice as fast,0.5half 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 |
|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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.
src, trigger and a placeholder
Toggle buttons through aria-pressed
An icon that follows the stage of a task kept in state
A button elsewhere on the page plays the icon
Colors, stroke, speed and currentColor changed live
Cards added from state, each icon playing after its card
Lazy loading, and rows that move without reloading
A still image until the icon is ready
Fading in with :state(ready), styling with :state(playing)
A ref, onReady and onComplete
The icon prop instead of src
BaseTrigger and defineElement
Apps
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.