1
1
import lottie , { AnimationItem } from 'lottie-web' ;
2
2
import * as React from 'react' ;
3
3
4
- import { LOTTIE_PLAYER_VERSION , LOTTIE_WEB_VERSION } from './versions' ;
4
+ import { LOTTIE_WEB_VERSION , REACT_LOTTIE_PLAYER_VERSION } from './versions' ;
5
5
6
6
/**
7
7
* Parse a resource into a JSON object or a URL string
@@ -139,7 +139,7 @@ export class Player extends React.Component<IPlayerProps, IPlayerState> {
139
139
public getVersions ( ) : Versions {
140
140
return {
141
141
lottieWebVersion : LOTTIE_WEB_VERSION ,
142
- lottiePlayerVersion : LOTTIE_PLAYER_VERSION ,
142
+ lottiePlayerVersion : REACT_LOTTIE_PLAYER_VERSION ,
143
143
} ;
144
144
}
145
145
@@ -311,8 +311,17 @@ export class Player extends React.Component<IPlayerProps, IPlayerState> {
311
311
let animationData = parseSrc ( src ) ;
312
312
313
313
if ( typeof animationData === 'string' ) {
314
- const fetchResult = await fetch ( animationData as string ) ;
315
- animationData = await fetchResult . json ( ) ;
314
+ const fetchResult = await fetch ( animationData as string ) . catch ( ( ) => {
315
+ this . setState ( { playerState : PlayerState . Error } ) ;
316
+ this . triggerEvent ( PlayerEvent . Error ) ;
317
+ throw new Error ( 'Error @LottieFiles/lottie-react: Animation data could not be fetched.' ) ;
318
+ } ) ;
319
+
320
+ animationData = await fetchResult . json ( ) . catch ( ( ) => {
321
+ this . setState ( { playerState : PlayerState . Error } ) ;
322
+ this . triggerEvent ( PlayerEvent . Error ) ;
323
+ throw new Error ( 'Error @LottieFiles/lottie-react: Animation data could not be fetched.' ) ;
324
+ } ) ;
316
325
}
317
326
318
327
// Clear previous animation, if any
@@ -334,83 +343,88 @@ export class Player extends React.Component<IPlayerProps, IPlayerState> {
334
343
}
335
344
this . setState ( { animationData } ) ;
336
345
337
- // Handle new frame event
338
- newInstance . addEventListener ( 'enterFrame' , ( ) => {
339
- this . triggerEvent ( PlayerEvent . Frame ) ;
346
+ this . setState ( { instance : newInstance } , ( ) => {
347
+ if ( typeof lottieRef === 'function' ) {
348
+ lottieRef ( newInstance ) ;
349
+ }
350
+
351
+ // Handle new frame event
352
+ newInstance . addEventListener ( 'enterFrame' , ( ) => {
353
+ this . triggerEvent ( PlayerEvent . Frame ) ;
340
354
341
- this . setState ( {
342
- seeker : Math . floor ( ( newInstance as any ) . currentFrame ) ,
355
+ this . setState ( {
356
+ seeker : Math . floor ( ( newInstance as any ) . currentFrame ) ,
357
+ } ) ;
343
358
} ) ;
344
- } ) ;
345
359
346
- // Handle lottie-web ready event
347
- newInstance . addEventListener ( 'DOMLoaded' , ( ) => {
348
- this . triggerEvent ( PlayerEvent . Load ) ;
349
- } ) ;
360
+ // Handle lottie-web ready event
361
+ newInstance . addEventListener ( 'DOMLoaded' , ( ) => {
362
+ this . triggerEvent ( PlayerEvent . Load ) ;
350
363
351
- // Handle animation data load complete
352
- newInstance . addEventListener ( 'data_ready' , ( ) => {
353
- this . triggerEvent ( PlayerEvent . Ready ) ;
354
- } ) ;
364
+ if ( autoplay ) {
365
+ this . play ( ) ;
366
+ }
367
+ } ) ;
355
368
356
- // Set error state when animation load fail event triggers
357
- newInstance . addEventListener ( 'data_failed ' , ( ) => {
358
- this . setState ( { playerState : PlayerState . Error } ) ;
359
- } ) ;
369
+ // Handle animation data load complete
370
+ newInstance . addEventListener ( 'data_ready ' , ( ) => {
371
+ this . triggerEvent ( PlayerEvent . Ready ) ;
372
+ } ) ;
360
373
361
- // Handle new loop event
362
- newInstance . addEventListener ( 'loopComplete' , ( ) => {
363
- this . triggerEvent ( PlayerEvent . Loop ) ;
364
- } ) ;
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
+ } ) ;
365
379
366
- // Set state to paused if loop is off and anim has completed
367
- newInstance . addEventListener ( 'complete ' , ( ) => {
368
- this . triggerEvent ( PlayerEvent . Complete ) ;
369
- this . setState ( { playerState : PlayerState . Paused } ) ;
380
+ // Handle new loop event
381
+ newInstance . addEventListener ( 'loopComplete ' , ( ) => {
382
+ this . triggerEvent ( PlayerEvent . Loop ) ;
383
+ } ) ;
370
384
371
- if ( ! this . props . keepLastFrame || this . props . loop ) {
372
- this . setSeeker ( 0 ) ;
373
- }
374
- } ) ;
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 } ) ;
375
389
376
- // Set handlers to auto play animation on hover if enabled
377
- this . container . addEventListener ( 'mouseenter' , ( ) => {
378
- if ( hover && this . state . playerState !== PlayerState . Playing ) {
379
- if ( this . props . keepLastFrame ) {
380
- this . stop ( ) ;
390
+ if ( ! this . props . keepLastFrame || this . props . loop ) {
391
+ this . setSeeker ( 0 ) ;
381
392
}
382
- this . play ( ) ;
383
- }
384
- } ) ;
385
- this . container . addEventListener ( 'mouseleave' , ( ) => {
386
- if ( hover && this . state . playerState === PlayerState . Playing ) {
387
- this . stop ( ) ;
388
- }
389
- } ) ;
390
-
391
- // Set initial playback speed and direction
392
- if ( speed ) {
393
- this . setPlayerSpeed ( speed ) ;
394
- }
393
+ } ) ;
395
394
396
- if ( direction ) {
397
- this . setPlayerDirection ( direction ) ;
398
- }
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 ) {
407
+ this . stop ( ) ;
408
+ }
409
+ } ) ;
410
+ }
399
411
400
- if ( background ) {
401
- this . setState ( { background } ) ;
402
- }
412
+ // Set initial playback speed and direction
413
+ if ( speed ) {
414
+ this . setPlayerSpeed ( speed ) ;
415
+ }
403
416
404
- this . setState ( { instance : newInstance } , ( ) => {
405
- if ( typeof lottieRef === 'function' ) {
406
- lottieRef ( newInstance ) ;
417
+ if ( direction ) {
418
+ this . setPlayerDirection ( direction ) ;
407
419
}
408
- if ( autoplay ) {
409
- this . play ( ) ;
420
+
421
+ if ( background ) {
422
+ this . setState ( { background } ) ;
410
423
}
411
424
} ) ;
412
425
} catch ( e ) {
413
426
this . setState ( { playerState : PlayerState . Error } ) ;
427
+ this . triggerEvent ( PlayerEvent . Error ) ;
414
428
}
415
429
}
416
430
0 commit comments