Skip to content

Commit d71a178

Browse files
committed
fix: added new instanceloaded event
1 parent c8107ea commit d71a178

File tree

2 files changed

+69
-62
lines changed

2 files changed

+69
-62
lines changed

.changeset/few-tables-notice.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@lottiefiles/react-lottie-player': patch
3+
---
4+
5+
added instanceloaded event

src/Player.tsx

Lines changed: 64 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ export enum PlayerState {
4747
// Define player events
4848
export enum PlayerEvent {
4949
Load = 'load',
50+
InstanceSaved = 'instanceSaved',
5051
Error = 'error',
5152
Ready = 'ready',
5253
Play = 'play',
@@ -314,13 +315,13 @@ export class Player extends React.Component<IPlayerProps, IPlayerState> {
314315
const fetchResult = await fetch(animationData as string).catch(() => {
315316
this.setState({ playerState: PlayerState.Error });
316317
this.triggerEvent(PlayerEvent.Error);
317-
throw new Error('Error @LottieFiles/lottie-react: Animation data could not be fetched.');
318+
throw new Error('@LottieFiles/lottie-react: Animation data could not be fetched.');
318319
});
319320

320321
animationData = await fetchResult.json().catch(() => {
321322
this.setState({ playerState: PlayerState.Error });
322323
this.triggerEvent(PlayerEvent.Error);
323-
throw new Error('Error @LottieFiles/lottie-react: Animation data could not be fetched.');
324+
throw new Error('@LottieFiles/lottie-react: Animation data could not be fetched.');
324325
});
325326
}
326327

@@ -344,84 +345,85 @@ export class Player extends React.Component<IPlayerProps, IPlayerState> {
344345
this.setState({ animationData });
345346

346347
this.setState({ instance: newInstance }, () => {
348+
this.triggerEvent(PlayerEvent.InstanceSaved);
349+
347350
if (typeof lottieRef === 'function') {
348351
lottieRef(newInstance);
349352
}
353+
if (autoplay) {
354+
this.play();
355+
}
356+
});
350357

351-
// Handle new frame event
352-
newInstance.addEventListener('enterFrame', () => {
353-
this.triggerEvent(PlayerEvent.Frame);
358+
// Handle new frame event
359+
newInstance.addEventListener('enterFrame', () => {
360+
this.triggerEvent(PlayerEvent.Frame);
354361

355-
this.setState({
356-
seeker: Math.floor((newInstance as any).currentFrame),
357-
});
362+
this.setState({
363+
seeker: Math.floor((newInstance as any).currentFrame),
358364
});
365+
});
359366

360-
// Handle lottie-web ready event
361-
newInstance.addEventListener('DOMLoaded', () => {
362-
this.triggerEvent(PlayerEvent.Load);
363-
364-
if (autoplay) {
365-
this.play();
366-
}
367-
});
367+
// Handle lottie-web ready event
368+
newInstance.addEventListener('DOMLoaded', () => {
369+
this.triggerEvent(PlayerEvent.Load);
370+
});
368371

369-
// Handle animation data load complete
370-
newInstance.addEventListener('data_ready', () => {
371-
this.triggerEvent(PlayerEvent.Ready);
372-
});
372+
// Handle animation data load complete
373+
newInstance.addEventListener('data_ready', () => {
374+
this.triggerEvent(PlayerEvent.Ready);
375+
});
373376

374-
// Set error state when animation load fail event triggers
375-
newInstance.addEventListener('data_failed', () => {
376-
this.setState({ playerState: PlayerState.Error });
377-
this.triggerEvent(PlayerEvent.Error);
378-
});
377+
// Set error state when animation load fail event triggers
378+
newInstance.addEventListener('data_failed', () => {
379+
this.setState({ playerState: PlayerState.Error });
380+
this.triggerEvent(PlayerEvent.Error);
381+
});
379382

380-
// Handle new loop event
381-
newInstance.addEventListener('loopComplete', () => {
382-
this.triggerEvent(PlayerEvent.Loop);
383-
});
383+
// Handle new loop event
384+
newInstance.addEventListener('loopComplete', () => {
385+
this.triggerEvent(PlayerEvent.Loop);
386+
});
384387

385-
// Set state to paused if loop is off and anim has completed
386-
newInstance.addEventListener('complete', () => {
387-
this.triggerEvent(PlayerEvent.Complete);
388-
this.setState({ playerState: PlayerState.Paused });
388+
// Set state to paused if loop is off and anim has completed
389+
newInstance.addEventListener('complete', () => {
390+
this.triggerEvent(PlayerEvent.Complete);
391+
this.setState({ playerState: PlayerState.Paused });
389392

390-
if (!this.props.keepLastFrame || this.props.loop) {
391-
this.setSeeker(0);
392-
}
393-
});
393+
if (!this.props.keepLastFrame || this.props.loop) {
394+
this.setSeeker(0);
395+
}
396+
});
394397

395-
// Set handlers to auto play animation on hover if enabled
396-
if (this.container) {
397-
this.container.addEventListener('mouseenter', () => {
398-
if (hover && this.state.playerState !== PlayerState.Playing) {
399-
if (this.props.keepLastFrame) {
400-
this.stop();
401-
}
402-
this.play();
403-
}
404-
});
405-
this.container.addEventListener('mouseleave', () => {
406-
if (hover && this.state.playerState === PlayerState.Playing) {
398+
// Set handlers to auto play animation on hover if enabled
399+
if (this.container) {
400+
this.container.addEventListener('mouseenter', () => {
401+
if (hover && this.state.playerState !== PlayerState.Playing) {
402+
if (this.props.keepLastFrame) {
407403
this.stop();
408404
}
409-
});
410-
}
405+
this.play();
406+
}
407+
});
408+
this.container.addEventListener('mouseleave', () => {
409+
if (hover && this.state.playerState === PlayerState.Playing) {
410+
this.stop();
411+
}
412+
});
413+
}
411414

412-
// Set initial playback speed and direction
413-
if (speed) {
414-
this.setPlayerSpeed(speed);
415-
}
415+
// Set initial playback speed and direction
416+
if (speed) {
417+
this.setPlayerSpeed(speed);
418+
}
416419

417-
if (direction) {
418-
this.setPlayerDirection(direction);
419-
}
420+
if (direction) {
421+
this.setPlayerDirection(direction);
422+
}
420423

421-
if (background) {
422-
this.setState({ background });
423-
}
424-
});
424+
if (background) {
425+
this.setState({ background });
426+
}
425427
} catch (e) {
426428
this.setState({ playerState: PlayerState.Error });
427429
this.triggerEvent(PlayerEvent.Error);

0 commit comments

Comments
 (0)