• Stars
    star
    445
  • Rank 98,085 (Top 2 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 4 years ago
  • Updated about 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Fully declarative React Lottie player

Fully declarative React Lottie player

Inspired by several existing packages wrapping lottie-web for React, I created this package because I wanted something that just works and is easy to use. None of the alternatives properly handle changes of props like playing/pausing/segments. This lead to lots of hacks to get the animations to play correctly.

react-lottie-player is a complete rewrite using hooks 🎣 for more readable code, easy to use, seamless and fully declarative control of the lottie player.

Tests NPM JavaScript Style Guide

Features

Install

npm install --save react-lottie-player

Usage

import React from 'react'

import Lottie from 'react-lottie-player'
// Alternatively:
// import Lottie from 'react-lottie-player/dist/LottiePlayerLight'

import lottieJson from './my-lottie.json'

export default function Example() {
  return (
    <Lottie
      loop
      animationData={lottieJson}
      play
      style={{ width: 150, height: 150 }}
    />
  )
}

Example

Screenshot

πŸŽ› Live demo

πŸ‘©πŸΏβ€πŸ’» Example code

Lazy loading

Option 1: React code splitting (React.lazy)

Extract your Lottie animation into a separate component, then lazy load it:

// MyLottieAnimation.jsx

import Lottie from 'react-lottie-player';
import animation from './animation.json';

export default function MyLottieAnimation(props) {
  return <Lottie animationData={animation} {...props} />;
}

// MyComponent.jsx

import React from 'react';
const MyLottieAnimation = React.lazy(() => import('./MyLottieAnimation'));

export default function MyComponent() {
  return <MyLottieAnimation play />;
}

Option 2: dynamic import with state

const Example = () => {
  const [animationData, setAnimationData] = useState<object>();

  useEffect(() => {
    import('./animation.json').then(setAnimationData);
  }, []);

  if (!animationData) return <div>Loading...</div>;
  return <Lottie animationData={animationData} />;
}

Option 3: path URL

const Example = () => <Lottie path="https://example.com/lottie.json" />;

Imperative API (ref)

const lottieRef = useRef();

useEffect(() => {
  console.log(lottieRef.current.currentFrame);
}, [])

return <Lottie ref={lottieRef} />;

See also #11

LottiePlayerLight

The default lottie player uses eval. If you don't want eval to be used in your code base, you can instead import react-lottie-player/dist/LottiePlayerLight. For more discussion see #39.

Lottie animation track scrolling div

See example/App.jsx (ScrollTest) in live example.

Resize mode: cover

If you want the animation to fill the whole container, you can pass this prop. See also #55:

<Lottie rendererSettings={{ preserveAspectRatio: 'xMidYMid slice' }} />

API

See PropTypes and lottie-web.

Releasing

  • Commit & wait for CI tests
  • np

Credits

License

MIT Β© mifi


Made with ❀️ in πŸ‡³πŸ‡΄

More apps by mifi.no

Follow me on GitHub, YouTube, IG, Twitter for more awesome content!

More Repositories

1

lossless-cut

The swiss army knife of lossless video/audio editing
JavaScript
19,775
star
2

editly

Slick, declarative command line video editing & API
JavaScript
4,389
star
3

instauto

Instagram bot / automation library written in Javascript for Node.js
JavaScript
735
star
4

SimpleInstaBot

Simple Instagram bot GUI
JavaScript
532
star
5

ezshare

Easily share files, folders and clipboard over LAN - Like Google Drive but without internet
JavaScript
458
star
6

hls-vod

HTTP Live Streaming with on-the-fly encoding of any video file for Web/Apple TV/iPhone/iPad/iPod
JavaScript
264
star
7

cognito-backup

CLI for backing up AWS Cogntito User Pools
JavaScript
141
star
8

reactive-video

Create videos using React!
JavaScript
118
star
9

dynamodump

Node CLI for backing up and restoring schema+data from DynamoDB tables
JavaScript
115
star
10

stacktracify

CLI to make unreadable production stack traces readable using source maps
JavaScript
111
star
11

in-app-subscription-example

Example of implementing auto-renewing in-app subscriptions for Android & iOS using React Native / Node.js
JavaScript
96
star
12

ical-expander

ICS / iCal / iCalendar parser / expander
JavaScript
63
star
13

commonify

Recursively publish ESM packages as CommonJS!
JavaScript
31
star
14

libxcomfort

Open source library for controlling Moeller XComfort devices through the USB/RS232 dongle
C
31
star
15

build-electron

Use ES modules in Electron
JavaScript
25
star
16

telldus-api

Node.js interface for Telldus Live and Telldus Local API
JavaScript
18
star
17

mjml-dynamic

Dynamic JSON content for MJML templates
TypeScript
16
star
18

VideoGrader

JavaScript
14
star
19

lossless-cut-fixtures

Test fixtures (videos) for manual testing of LosslessCut
13
star
20

cordova-xml

Edit cordova config.xml from the command line or via API
JavaScript
7
star
21

file2kodi

Plays a local file on Kodi/XBMC
JavaScript
5
star
22

ffmpeg-builds

FFMPEG static builds for use with LosslessCut
4
star
23

s3fuse

Simple and stable Node.js implementation of read-only S3 file system
JavaScript
4
star
24

rpi-touch-ui

Basic control of a Raspberry Pi with a touch screen
JavaScript
4
star
25

telldus-local-auth

Node CLI for assisting set up of local auth for Telldus Live local devices
JavaScript
4
star
26

fork-worker

Makes it easier to fork off a separate Node.js job
JavaScript
4
star
27

sleekboard

Sleekboard is a slick on-screen-keyboard built on Cairo and X11
Shell
3
star
28

cloudwatch-winston

Robust and simple Winston transport for AWS CloudWatch Logs
JavaScript
2
star
29

form-encode-object

Convert a deep object structure to be sent as urlencoded or form data (PHP style).
JavaScript
2
star
30

facetracker

C
2
star
31

digital-media-frame

Making a digital media frame running on Linux with Chromium
CSS
2
star
32

scripts

Some handy scripts
Shell
2
star
33

jquery-joystick

jQuery Joystick
JavaScript
2
star
34

google-photos-timestamp-fix

Attempt to write a script that automatically sets the correct timestamp for videos uploaded to google photos, see: https://productforums.google.com/forum/#!topic/photos/oj96JZK14Fs
JavaScript
1
star
35

mifi

mifi npm package
1
star
36

chrome-to-xbmc

Chrome plugin for detecting video streams on pages and sending them to XBMC
1
star
37

restplass-alerts

Service that polls finn.no restplass and gives notifications when something changes for the better
JavaScript
1
star
38

github-action-push-static

Example GitHub Action workflow that will automatically push all files in a specified directory to a different GitHub repo
JavaScript
1
star
39

mifi.no

JavaScript
1
star
40

editly-assets

Assets for editly examples
GLSL
1
star
41

xbmc-vsxu

VSXu visualization for XBMC
C
1
star