Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

expose div ref and update types for Lottie.ref #111

Merged
merged 1 commit into from
Jan 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@ declare module 'react-lottie-player' {
AnimationConfig,
AnimationDirection,
AnimationEventCallback,
AnimationItem,
AnimationSegment,
RendererType
} from 'lottie-web'

export type LottieProps = React.DetailedHTMLProps<
export type LottieProps = React.PropsWithoutRef<React.DetailedHTMLProps<
React.HTMLAttributes<HTMLDivElement>,
HTMLDivElement
> &
Partial<Pick<AnimationConfig<RendererType>, 'loop' | 'renderer' | 'rendererSettings' | 'audioFactory'>> & {
>> &
Partial<Pick<AnimationConfig<RendererType>, 'path', 'animationData', 'loop' | 'renderer' | 'rendererSettings' | 'audioFactory'>> & {
play?: boolean
goTo?: number
speed?: number
Expand All @@ -23,6 +24,9 @@ declare module 'react-lottie-player' {
onLoopComplete?: AnimationEventCallback
onEnterFrame?: AnimationEventCallback
onSegmentStart?: AnimationEventCallback

/** Lottie `AnimationItem` Instance */
ref?: React.Ref<AnimationItem | undefined>
} & ({ path?: string } | { animationData?: object })

const Lottie: React.FC<LottieProps>
Expand Down
29 changes: 24 additions & 5 deletions src/makeLottiePlayer.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// @ts-check
/// <reference types="./index" />
// eslint-disable-next-line no-unused-vars
import React, {
memo, useRef, useEffect, useState, forwardRef, useCallback,
Expand All @@ -10,8 +12,17 @@ import propTypes from './propTypes';
const emptyObject = {};
const noOp = () => {};

/**
* @param {import('lottie-web').LottiePlayer} args
* @returns {React.FC<import('react-lottie-player').LottieProps>}
*/
const makeLottiePlayer = ({ loadAnimation }) => {
const Lottie = memo(forwardRef((params, forwardedRef) => {
const Lottie = memo(forwardRef((
/** @type {import('react-lottie-player').LottieProps} */
params,
/** @type {React.ForwardedRef<import('lottie-web').AnimationItem>} */
forwardedRef,
) => {
const {
animationData = null,
path = null,
Expand All @@ -23,21 +34,25 @@ const makeLottiePlayer = ({ loadAnimation }) => {
goTo = null,
useSubframes = true,

// props picked to match from Lottie's config
renderer = 'svg',
loop = true,
rendererSettings: rendererSettingsIn = emptyObject,

audioFactory = null,

onLoad = noOp,
onComplete = noOp,
onLoopComplete = noOp,
onEnterFrame = noOp,
onSegmentStart = noOp,

// htmlProps remain and will pass on to the div element
...props
} = params;

/** @type {React.MutableRefObject<HTMLDivElement | undefined>} */
const animElementRef = useRef();
/** @type {React.MutableRefObject<import('lottie-web').AnimationItem | undefined>} */
const animRef = useRef();

const [ready, setReady] = useState(false);
Expand Down Expand Up @@ -65,9 +80,13 @@ const makeLottiePlayer = ({ loadAnimation }) => {

const setLottieRefs = useCallback((newRef) => {
animRef.current = newRef;
// eslint-disable-next-line no-param-reassign
if (forwardedRef) forwardedRef.current = newRef;
}, []); // eslint-disable-line react-hooks/exhaustive-deps
if (typeof forwardedRef === 'function') {
forwardedRef(newRef);
} else if (forwardedRef !== undefined && forwardedRef !== null) {
// eslint-disable-next-line no-param-reassign -- mutating a ref is intended
forwardedRef.current = newRef;
}
}, [forwardedRef]);

useEffect(() => {
function parseAnimationData() {
Expand Down