Skip to content

Commit

Permalink
feat(pie-lottie-player): DSW-2365 replace utility method with isServer
Browse files Browse the repository at this point in the history
  • Loading branch information
fernandofranca committed Sep 4, 2024
1 parent 798df0c commit 2cf382c
Showing 1 changed file with 9 additions and 11 deletions.
20 changes: 9 additions & 11 deletions packages/components/pie-lottie-player/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { LitElement, html, unsafeCSS } from 'lit';
import {
LitElement, html, isServer, unsafeCSS,
} from 'lit';
import { property, query } from 'lit/decorators.js';
import { LottiePlayer, AnimationItem } from 'lottie-web';

Expand Down Expand Up @@ -29,24 +31,20 @@ export class PieLottiePlayer extends LitElement implements LottiePlayerProps {

protected async firstUpdated (): Promise<void> {
// Check if the code is running in a browser environment
if (this._canUseDOM()) {
const { default: lottieModule } = await import('lottie-web/build/player/lottie_light_canvas.min.js'); // Dynamically import the 'lottie-web' library to avoid SSR issues
this._lottie = lottieModule;
this._loadAnimation();
}
if (isServer) return;

const { default: lottieModule } = await import('lottie-web/build/player/lottie_light_canvas.min.js'); // Dynamically import the 'lottie-web' library to avoid SSR issues
this._lottie = lottieModule;
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 (isServer) return;
if (!this._hostElement) return;
if (!this._lottie) return;

Expand Down

0 comments on commit 2cf382c

Please sign in to comment.