1.1.0 • Published 4 months ago

lottie-solid v1.1.0

Weekly downloads
-
License
MIT
Repository
github
Last release
4 months ago

LottiePlayer Solid Component

This is a Solid.js component for the Lottie Web Player.Based on the Lottie Player Web Component.

Demo

screencast

Usage

Installation

  1. Install package using npm or yarn.
npm install lottie-solid
  1. Import package in your code.
import { Player, Buttons, Theme } from 'lottie-solid';

Player component

Add the element Player and set the src prop to a URL pointing to a valid Lottie JSON.

<Player
  autoplay
  loop
  controls
  src="https://assets3.lottiefiles.com/packages/lf20_UJNc2t.json"
  style={{ height: '300px', width: '300px' }}
  buttons={[Buttons.Play, Buttons.Repeat, Buttons.Frame]}
  theme={Theme.Transparent}
/>

Props

PropDescriptionTypeDefault
lottieRefGet lottie animation objectfunctionundefined
onEventListen for eventsfunctionundefined
onStateChangePlay state changesfunctionundefined
onBackgroundChangeListen for bg changesfunctionundefined
autoplayAutoplay animation on load.booleanfalse
backgroundBackground color.stringundefined
controlsShow controls.booleanfalse
directionDirection of animation.number1
hoverWhether to play on mouse hover.booleanfalse
clickWhether to play on mouse click.booleanfalse
keepLastFrameStop animation on the last frame.Has no effect if loop is true.booleanfalse
loopWhether to loop animation.booleanfalse
rendererRenderer to use."svg" \| "canvas"'svg'
speedAnimation speed.number1
styleThe style for the container.objectundefined
buttonsThe buttons to show.Buttons[]undefined
themeThe theme to use.Themeundefined
src (required)Bodymovin JSON data or URL to JSON.objectstring

Get Player instance

To call methods on the instance of the Player component. You may get a reference to the component and call the methods on ref.current. That's a solid way of doing a document.getElementById(); You may then use this ref i.e.: player in the example below to call methods that are described in this documentation.

import { createSignal, createEffect } from 'solid-js';
import { Player } from 'lottie-solid';

export default function App() {
  const [playerRef, setPlayerRef] = createSignal<HTMLDivElement>();

  return (
    <Player
      ref={setPlayerRef} // set the ref to your component
      loop
      src="https://assets3.lottiefiles.com/packages/lf20_XZ3pkn.json"
      style={{ height: '300px', width: '300px' }}
    />
  );
}

Get Lottie instance

The lottieRef prop returns the Lottie instance which you can use to set data and call methods as described in the bodymovin documentation.

import { createSignal, createEffect } from 'solid-js';
import { Player, AnimationItem } from 'lottie-solid';

export default function App() {
  const [lottieRef, setLottieRef] = createSignal<AnimationItem>();

  // example of calling a method on the lottie instance
  // this will pause the animation after 1 second
  createEffect(() => {
    const lottie = lottieRef();
    if (lottie) {
      setTimeout(() => lottie.pause(), 1000);
    }
  });
  
  return (
    <Player
      lottieRef={setLottieRef} // the lottie instance is returned in the argument of this prop. set it to your local state
      loop
      src="https://assets3.lottiefiles.com/packages/lf20_XZ3pkn.json"
      style={{ height: '300px', width: '300px' }}
    />
  );
}

Listening for events

import { createSignal, createEffect } from 'solid-js';
import { Player, AnimationItem } from 'lottie-solid';

export default function App() {
  const [lottieRef, setLottieRef] = createSignal<AnimationItem>();


  const doSomething = () => {
    lottieRef()?.play(); // make use of the lottie instance and call methods
  }
  
  return (
    <Player
      onEvent={(event) => {
        // check event type and do something
        if (event === 'load') {
          this.doSomething();
        }
      }}
      lottieRef={setLottieRef}
      loop
      src="https://assets3.lottiefiles.com/packages/lf20_XZ3pkn.json"
      style={{ height: '300px', width: '300px' }}
    />
  );
}

Events

The following events are exposed and can be listened to via addEventListener calls.

NameDescription
loadAnimation data is loaded.
errorAn animation source cannot be parsed, fails to load or has format errors.
readyAnimation data is loaded and player is ready.
playAnimation starts playing.
pauseAnimation is paused.
stopAnimation is stopped.
loopAn animation loop is completed.
completeAnimation is complete (all loops completed).
frameA new frame is entered.

Methods

pause() => void

Pause animation play.

Returns

Type: void

play() => void

Start playing animation.

Returns

Type: void

setPlayerDirection(direction: 1 | -1 ) => void

Animation play direction.

Parameters

NameTypeDescription
valuenumberDirection values.

Returns

Type: void

setPlayerSpeed(speed?: number) => void

Sets animation play speed.

Parameters

NameTypeDescription
valuenumberPlayback speed.

Returns

Type: void

stop() => void

Stops animation play.

Returns

Type: void

setSeeker(frame: number, play: boolean) => void

Seek to a given frame.

Returns

Type: void

License

MIT License © Neulen.dev

1.1.0

4 months ago

1.0.0

4 months ago