GithubHelp home page GithubHelp logo

joshuabaker / react-lottie Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 246 KB

🪝 React hook to render Lottie animations.

Home Page: https://github.com/joshuabaker/react-lottie

License: MIT License

TypeScript 61.35% JavaScript 38.65%
lottie lottie-animation lottie-web react react-hooks reactjs

react-lottie's Introduction

React Lottie

React Lottie

A React hook to render Lottie animations using Airbnb’s JavaScript library.

Install

npm install @joshuabaker/react-lottie
yarn add @joshuabaker/react-lottie

Usage

The useLottie hook takes two arguments:

  1. A container ref for the HTML element that you want to use as the canvas
  2. A set of params that are passed to lottie-web

Important

You must pass either path to a Lottie JSON file or the raw JSON via animationData.

Please see the lottie-web docs for all argument params, methods and events.

Simple Example

import { useLottie } from "@joshuabaker/react-lottie";

function Animation(props) {
  const containerRef = useRef();

  const animationItem = useLottie(containerRef, {
    path: "/path/to/lottie.json",
    autoplay: false,
    loop: false,
  });

  return <div ref={containerRef} {...props} />
}

Controls & Events

import { useLottie } from "@joshuabaker/react-lottie";
import HeartAnimationData from "../lottie-animations/heart.json";

function Animation(props) {
  const containerRef = useRef();

  const animationItem = useLottie(containerRef, {
    animationData: HeartAnimationData,
  });

  useEffect(() => {
    if (animationItem) {
      // Add event listeners here
    }
  }, [animationItem]);

  return (
    <div
      ref={containerRef}
      onClick={() => animationItem?.togglePause()}
      {...props}
    />
  );
}

Warning

Do not call addEventListener or removeEventListener using optional chaining. Instead wrap with a simple if condition.

useEffect(() => {
  if (animationItem) {
    animationItem.addEventListener("complete", completeHandler); // Do this
  }

  animationItem?.addEventListener("complete", completeHandler); // Don’t do this
}, [animationItem]);

This is due to how lottie-web internally handles events.

Why?

Other hooks output a <div> or a ref, both of which reduce the flexibility hooks should provide. This hook acts like a hook should, accepting a ref and animation params.

react-lottie's People

Contributors

joshuabaker avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.