Skip to content

Commit

Permalink
feat(pie-lottie-player): DSW-2365 make it async!
Browse files Browse the repository at this point in the history
feat(pie-lottie-player): DSW-2365 fix async issues
  • Loading branch information
fernandofranca committed Aug 30, 2024
1 parent f006129 commit a863cfb
Showing 1 changed file with 17 additions and 9 deletions.
26 changes: 17 additions & 9 deletions packages/components/pie-lottie-player/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { LitElement, PropertyValues, html } from 'lit';
import { property, query } from 'lit/decorators.js';
// @ts-expect-error - Type is defined below
import lottieLight from 'lottie-web/build/player/esm/lottie_light_canvas.min.js';
import Lottie, { AnimationItem } from 'lottie-web';
import { LottiePlayer, AnimationItem } from 'lottie-web';

import { defineCustomElement } from '@justeattakeaway/pie-webc-core';
import { LottiePlayerProps, defaultProps, AnimationDirection } from './defs';
Expand All @@ -12,9 +10,6 @@ export * from './defs';

const componentSelector = 'pie-lottie-player';

// Use type assertion to assign the LottiePlayer type to the lottie import
const lottie = lottieLight as typeof Lottie;

/**
* @tagname pie-lottie-player
*/
Expand All @@ -24,31 +19,44 @@ export class PieLottiePlayer extends LitElement implements LottiePlayerProps {

private animationInstance?: AnimationItem;

private _lottie?:LottiePlayer;
private _disableAutoPlay = defaultProps.disableAutoPlay;
private _disableLoop = defaultProps.disableLoop;
private _speed = defaultProps.speed;
private _direction = defaultProps.direction;
private _animationSrc = '';
private _animationData?:object;

protected firstUpdated (_changedProperties: PropertyValues): void {
this._loadAnimation();
protected async firstUpdated (_changedProperties: PropertyValues): Promise<void> {

Check warning on line 30 in packages/components/pie-lottie-player/src/index.ts

View workflow job for this annotation

GitHub Actions / lint-js

'_changedProperties' is defined but never used
// Check if the code is running in a browser environment
if (this.canUseDOM()) {
// TODO: Solve TS issue
const asyncLottieModule = await import('lottie-web/build/player/esm/lottie_light_canvas.min.js'); // Dynamically import the 'lottie-web' library to avoid SSR issues
this._lottie = asyncLottieModule.default;
this._loadAnimation();
}
}

disconnectedCallback () {
super.disconnectedCallback();
this._destroyAnimation();
}

private canUseDOM () {
return typeof window !== 'undefined' && 'document' in window && 'createElement' in window.document;
}

private _loadAnimation (): void {
if (!this.canUseDOM()) return;
if (!this.hostElement) return;
if (!this._lottie) return;

this._destroyAnimation();

if (!this.animationSrc && !this.animationData) return;

try {
this.animationInstance = lottie.loadAnimation({
this.animationInstance = this._lottie.loadAnimation({
container: this.hostElement, // the dom element that will contain the animation
renderer: 'canvas',
loop: !this.disableLoop,
Expand Down

0 comments on commit a863cfb

Please sign in to comment.