npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

lottie-solid

v1.1.0

Published

Lottie Web Player as a solid.js component

Downloads

322

Readme

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

| Prop | Description | Type | Default | |----------------------|------------------------------------------------------------------------|---------------------|-------------| | lottieRef | Get lottie animation object | function | undefined | | onEvent | Listen for events | function | undefined | | onStateChange | Play state changes | function | undefined | | onBackgroundChange | Listen for bg changes | function | undefined | | autoplay | Autoplay animation on load. | boolean | false | | background | Background color. | string | undefined | | controls | Show controls. | boolean | false | | direction | Direction of animation. | number | 1 | | hover | Whether to play on mouse hover. | boolean | false | | click | Whether to play on mouse click. | boolean | false | | keepLastFrame | Stop animation on the last frame.Has no effect if loop is true. | boolean | false | | loop | Whether to loop animation. | boolean | false | | renderer | Renderer to use. | "svg" \| "canvas" | 'svg' | | speed | Animation speed. | number | 1 | | style | The style for the container. | object | undefined | | buttons | The buttons to show. | Buttons[] | undefined | | theme | The theme to use. | Theme | undefined | | src (required) | Bodymovin JSON data or URL to JSON. | object | string |

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.

| Name | Description | |------------|---------------------------------------------------------------------------| | load | Animation data is loaded. | | error | An animation source cannot be parsed, fails to load or has format errors. | | ready | Animation data is loaded and player is ready. | | play | Animation starts playing. | | pause | Animation is paused. | | stop | Animation is stopped. | | loop | An animation loop is completed. | | complete | Animation is complete (all loops completed). | | frame | A 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

| Name | Type | Description | |---------|----------|-------------------| | value | number | Direction values. |

Returns

Type: void

setPlayerSpeed(speed?: number) => void

Sets animation play speed.

Parameters

| Name | Type | Description | |---------|----------|-----------------| | value | number | Playback 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