Install react-useanimations
masterYou can install the react-useanimations package using either Yarn or NPM.
Using Yarn:
yarn add react-useanimationsUsing NPM:
npm install -S react-useanimationsyarn add react-useanimationsrepository·master·Indexed 22 days ago
https://github.com/useanimations/react-useanimationsA collection of free, animated, open-source Lottie icons designed specifically for React.js applications. It provides the UseAnimations component to manage the Lottie lifecycle, customize appearance via props like strokeColor and fillColor, and implement interactive UI elements using a render prop pattern.
You can install the react-useanimations package using either Yarn or NPM.
Using Yarn:
yarn add react-useanimationsUsing NPM:
npm install -S react-useanimationsyarn add react-useanimationsstrokeColor, fillColor, or pathCss. These props inject CSS rules into a managed stylesheet (#useAnimationsSheet) targeting the animation's unique ID. This allows you to change the color of the icon without re-loading the entire animation data.To use an animation, you must first import the UseAnimations component and the specific animation file you want to use. Note that animations are imported from react-useanimations/lib/[animation-name]. This ensures your bundle only includes the animations you actually use.
import React from 'react';
import UseAnimations from 'react-useanimations';
// EVERY ANIMATION NEEDS TO BE IMPORTED FIRST -> YOUR BUNDLE WILL INCLUDE ONLY WHAT IT NEEDS
import github from 'react-useanimations/lib/github'
const App = () => <UseAnimations animation={github} />;
export default App;import React from 'react';
import UseAnimations from 'react-useanimations';
// EVERY ANIMATION NEEDS TO BE IMPORTED FIRST -> YOUR BUNDLE WILL INCLUDE ONLY WHAT IT NEEDS
import github from 'react-useanimations/lib/github'
const App = () => <UseAnimations animation={github} />;
export default App;If you need to wrap the animation inside another element (like a <button>), use the render prop. The render function provides two sets of props:
eventProps: Contains DOM events like onClick and mouseOver. These should be spread onto your wrapping element.animationProps: Contains the actual animation properties. These should be spread onto a simple <div> inside your wrapper.export const WrapperElement = () => {
return (
<UseAnimation
animation={heart}
size={60}
onClick={() => {
// eslint-disable-next-line
console.log('additional onClick cb is working');
}}
render={(eventProps, animationProps) => (
<button style={{ padding: '20px' }} type="button" {...eventProps}>
<div {...animationProps} />
</button>
)}
/>
);
};export const WrapperElement = () => {
return (
<UseAnimation
animation={heart}
size={60}
onClick={() => {
// eslint-disable-next-line
console.log('additional onClick cb is working');
}}
render={(eventProps, animationProps) => (
<button style={{ padding: '20px' }} type="button" {...eventProps}>
<div {...animationProps} />
</button>
)}
/>
);
};The UseAnimations component accepts several props to customize the appearance and behavior of the icon.
| Prop | Default | Definition |
|---|---|---|
animation | / | The animation file to use |
size | 24 | The animation size |
strokeColor | 'inherit' | The animation stroke color |
fillColor | '' | The animation fill color |
wrapperStyle | {} | Styles for the wrapper div |
pathCss | '' | CSS string for the animation path element |
reverse | false | Set to true when an element (like a checkbox) should be in the 'checked' state initially |
autoplay | false* | Whether the animation plays automatically (false except in animations like loading, etc.) |
loop | false* | Whether the animation loops (false except in animations like loading, etc.) |
options | {} | Custom options that override default ones |
speed | 1 | A number to determine the speed of lottie (1 is normal speed) |
*Note: autoplay and loop behavior depends on the specific animation type (e.g., loading animations).
render prop, you can access eventProps. These props include event handlers (like onClick) that are synchronized with the animation's state (e.g., whether it is playing, looping, or reversed). This is useful for building interactive components where the UI must react to the animation's lifecycle.The UseAnimations component is the primary way to render Lottie-based animations in a React application. It manages the Lottie lifecycle (initialization and destruction), handles animation speed, and provides event hooks.
By default, it renders a div container. However, you can use the render prop to provide a custom rendering function if you need to wrap the animation in specific UI elements.
The AnimationKey type defines the set of supported icon identifiers available in the library. Use these keys to select specific animations.
type AnimationKey =
| 'activity'
| 'airplay'
| 'alertCircle'
| 'alertOctagon'
| 'alertTriangle'
| 'archive'
| 'arrowDown'
| 'arrowDownCircle'
| 'arrowLeftCircle'
| 'arrowRightCircle'
| 'arrowUp'
| 'arrowUpCircle'
| 'bookmark'
| 'behance'
| 'calendar'
| 'checkBox'
| 'checkmark'
| 'codepen'
| 'copy'
| 'download'
| 'dribbble'
| 'edit'
| 'error'
| 'explore'
| 'facebook'
| 'folder'
| 'github'
| 'heart'
| 'help'
| 'home'
| 'infinity'
| 'info'
| 'instagram'
| 'linkedin'
| 'loading'
| 'loading2'
| 'loading3'
| 'lock'
| 'mail'
| 'maximizeMinimize'
| 'maximizeMinimize2'
| 'menu'
| 'menu2'
| 'menu3'
| 'menu4'
| 'microphone'
| 'microphone2'
| 'notification'
| 'notification2'
| 'playPause'
| 'playPauseCircle'
| 'plusToX'
| 'pocket'
| 'radioButton'
| 'scrollDown'
| 'searchToX'
| 'settings'
| 'settings2'
| 'share'
| 'skipBack'
| 'skipForward'
| 'star'
| 'thumbUp'
| 'toggle'
| 'trash'
| 'trash2'
| 'twitter'
| 'userMinus'
| 'userPlus'
| 'userX'
| 'video'
| 'video2'
| 'visibility'
| 'visibility2'
| 'volume'
| 'youtube'
| 'youtube2'
| 'zoomIn'
| 'zoomOut';When configuring how an animation responds to user interaction or playback, you can use one of the following AnimationEffect string literals:
CLICK_PLAY_AND_BACKWARDS: Plays the animation on click and then plays it in reverse.HOVER_PLAY_AND_STOP: Plays the animation when hovered and stops at the end.HOVER_PLAY_AND_BACKWARDS: Plays the animation on hover and then plays it in reverse.CLICK_PLAY: Plays the animation once on click.LOOP_PLAY: Continuously loops the animation.CLICK_PLAY_AND_SEGMENTS: Plays specific segments of the animation on click.type AnimationEffect =
| 'CLICK_PLAY_AND_BACKWARDS'
| 'HOVER_PLAY_AND_STOP'
| 'HOVER_PLAY_AND_BACKWARDS'
| 'CLICK_PLAY'
| 'LOOP_PLAY'
| 'CLICK_PLAY_AND_SEGMENTS';checkBox icon is an exported animation object compatible with the useanimations library. It contains the animationData (Lottie JSON) and a unique animationKey of 'checkBox'. This object can be passed directly to animation components or hooks that consume Animation types.menu3 icon is an animated icon exported as an Animation object. It contains the animationData (loaded from menu3.json) and a unique animationKey of 'menu3'. This object is intended to be used with the useAnimations hook to trigger Lottie animations for a menu icon.volume animation is an exported animation object compatible with the useAnimations hook. It contains the animationData (loaded from a JSON file) and an animationKey set to 'volume'. This is used to trigger a volume-related animation on a component.