@dotlottie/react-player

中文文档
1.6.19发布于 2024/03/21MITTypeScriptES Module近 30 天下载 17.2万

dotlottie animation player react component.

此软件包已被标记为弃用

This package is superceded by @lottiefiles/dotlottie-react

原始使用文档
文档目录11 节
  1. What is dotLottie?
  2. Documentation
  3. Installation
  4. Usage
  5. Props
  6. Events
  7. Examples
  8. Methods
  9. Contributing
  10. Our Other Lottie-related Libraries
  11. License

DotLottiePlayer React Component

This is a React Component for easily embedding and playing dotLottie animations in your react projects.

What is dotLottie?

dotLottie is an open-source file format that combines one or more Lottie files and their associated resources into a single file. These files are ZIP archives compressed with the Deflate compression method and have the file extension ".lottie".

Read more about dotLottie here!

Documentation

View the full documentation

Installation

You can install @dotlottie/react-player using npm:

npm install @dotlottie/react-player

or yarn:

yarn add @dotlottie/react-player

Usage

To use @dotlottie/react-player in your React project, import the component and use it as follows:

import React from 'react';
import { DotLottiePlayer, Controls } from '@dotlottie/react-player';
import '@dotlottie/react-player/dist/index.css';

const App = () => {
  return (
    <div>
      <DotLottiePlayer
        src="/path-to-lottie.lottie"
        autoplay
        loop
      >
        <Controls />
      </DotLottiePlayer>
    </div>
  );
};

export default App;

Props

The DotLottiePlayer component accepts the following props:

PropDescriptionTypeDefault
src (required)Animation data or URLRecord<string, unknown> | stringundefined
lottieRefGet player objectMutableRefObjectundefined
autoplayAutoplay animation on loadbooleanfalse
backgroundBackground colorstringtransparent
directionPlay direction1 | -11
hoverWhether to play on mouse hoverbooleanfalse
intermissionPause between loopsnumber0
loopWhether to loop animationbooleanfalse
playModePlay mode'normal' | 'bounce'normal
rendererHow to render'svg' | 'html' | 'canvas'svg
speedPlay speednumber1
onEventListen to player eventsfunctionundefined
defaultThemeDefault .lottie theme to usestringundefined

Events

The following events are exposed via the onEvent function:

NameDescription
completeAnimation completed playing
errorAn animation source cannot be parsed, fails to load, or has format errors
frameA new frame is entered
freezeAnimation is frozen
loopCompleteLoop animation is complete
pauseAnimation is paused
readyAnimation data is loaded and player is ready
stopAnimation is stopped

Examples

Listening for events:

import React, { useState } from 'react';
import { DotLottiePlayer, Controls, PlayerEvents } from '@dotlottie/react-player';
import '@dotlottie/react-player/dist/index.css';

const App = () => {
  const [loading, setLoading] = useState(true);

  return (
    <div>
      {loading ? (
        <div>loading....</div>
      ) : (
        <DotLottiePlayer
          src="/path-to-lottie.lottie"
          onEvent={(event: PlayerEvents) => {
            if (event === PlayerEvents.Ready) {
              setLoading(false);
            }
          }}
          autoplay
          loop
        >
          <Controls />
        </DotLottiePlayer>
      )}
    </div>
  );
};

export default App;

Getting player instance and calling methods:

import React, { useRef } from 'react';
import { DotLottiePlayer, Controls, PlayerEvents } from '@dotlottie/react-player';
import '@dotlottie/react-player/dist/index.css';

const App = () => {
  const lottieRef = useRef<DotLottieRefProps>();

  return (
    <div>
      <DotLottiePlayer
        lottieRef={lottieRef}
        src="/path-to-lottie.lottie"
        onEvent={(event: PlayerEvents) => {
          if (event === PlayerEvents.Ready) {
            // Play next animation.
            lottieRef.current?.next();
          }
        }}
        autoplay
        loop
      >
        <Controls />
      </DotLottiePlayer>
    </div>
  );
};

export default App;

Methods

You can access these methods using the player instance:

  • getLottie(): AnimationItem | undefined Returns the lottie-web instance.

  • getManifest(): Manifest | undefined Returns the .lottie Manifest.

  • next(getOptions?: (currPlaybackOptions?: PlaybackOptions, manifestPlaybackOptions?: PlaybackOptions) => PlaybackOptions): void Plays the next animation in the manifest.

  • play(indexOrId?: string | number, getOptions?: (currPlaybackOptions?: PlaybackOptions, manifestPlaybackOptions?: PlaybackOptions) => PlaybackOptions): void Plays the current animation or a specified animation with passed PlaybackOptions.

  • previous(getOptions?: (currPlaybackOptions?: PlaybackOptions, manifestPlaybackOptions?: PlaybackOptions) => PlaybackOptions): void Plays the previous animation in the manifest.

  • reset(): void Resets or goes back to the default/initial animation.

  • resize(): void Resizes the canvas if canvas renderer.

  • seek(frame: number): void Seeks to a specific frame.

  • setBackground(background: string): void Sets the container background.

  • setDefaultTheme(defaultTheme: string): void Sets the default theme (applies all animations).

  • setDirection(direction: AnimationDirection): void Sets the player direction. (applies all animations)

  • setHover(hover: boolean): void Sets whether to play on hover (applies to all animations).

  • setIntermission(intermission: number): void Sets the pause between loops (applies to all animations).

  • setLoop(loop: number | boolean): void Sets loop behavior (applies to all animations).

  • setPlayMode(mode: PlayMode): void Sets the player play mode (e.g., bounce or normal) for all animations.

  • setSpeed(speed: number): void Sets the play speed (applies all animations).

  • togglePlay(): void Toggles the play state.

  • pause(): void Pauses the animation.

  • stop(): void Stops the animation.

  • getContainer(): HTMLDivElement | undefined Gets the animation container.

Contributing

We use changesets to maintain a changelog for this repository. When making any changes to the codebase that impact functionality or performance, we require a changeset to be present.

To add a changeset, run:

pnpm changeset add

And select the type of version bump you'd like (major, minor, patch).

You can document the changes in detail and format them properly using Markdown by opening the ".md" file that the "pnpm changeset" command created in the ".changeset" folder. Open the file, and it should look something like this:

---
"@dotlottie/common": minor
"@dotlottie/react-player": major
---

This is where you document your **changes** using Markdown.

- You can write
- However you'd like
- In as much detail as you'd like

Aim to provide enough details so that teammates and future you can understand the changes and the context of

 the change.

Commit your changes and the changeset to your branch, and then create a pull request on the develop branch.

Here are some of our other Lottie-related libraries:

  • lottie-react: A React component for the Lottie Web player.
  • lottie-vue: A Vue component for the Lottie player.
  • svelte-lottie-player: Lottie player component for use with Svelte.
  • jLottie: jLottie is suitable as a general-purpose Lottie player, though it implements a subset of the features in the core player. This approach leads to a tiny footprint and great performance.
  • lottie-interactivity: This is a small library to add scrolling, cursor interactivity, and interaction chaining to your Lottie animations.
  • lottie-js: The library consists of methods to map the Lottie JSON to the object model and interact with properties, as well as manipulate them.

License

MIT License © LottieFiles.com

版本与发布记录

@dotlottie/react-player 共发布 45 个版本,首次发布于 2023/06/21;过去 12 个月发布了 0 个版本。

版本发布日期距今
1.6.19latest2024/03/212 年前
1.6.182024/03/052 年前
1.6.172024/02/162 年前
1.6.162024/02/132 年前
1.6.152024/01/192 年前
1.6.142024/01/192 年前

其他发布通道:next → 0.0.0,beta → 0.1.0-beta.9。

关于 @dotlottie/react-player 的常见问题

@dotlottie/react-player 是什么?

dotlottie animation player react component.注意:该包已被作者标记为弃用——This package is superceded by @lottiefiles/dotlottie-react

如何安装 @dotlottie/react-player?

在项目目录运行 npm install @dotlottie/react-player,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:react(^16.8.0 || ^17.0.0 || ^18.0.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@dotlottie/react-player 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@dotlottie/react-player 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@dotlottie/react-player 可以商用吗?

@dotlottie/react-player 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@dotlottie/react-player 的下载量有多少?

根据 npm 官方下载统计,@dotlottie/react-player 在 2026/10/13 – 2026/11/11 这 30 天内被下载 171,636 次,日均约 5,721 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@dotlottie/react-player 还在维护吗?

最新版本 1.6.19 发布于 2024/03/21(2 年前);至今共发布 45 个版本,其中过去 12 个月发布了 0 个;首次发布于 2023/06/21。作者已将其标记为弃用,新项目建议寻找替代方案。