Skip to content

Commit

Permalink
refactor(react): simplify DotLottie and DotLottieWorker initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
theashraf committed Oct 16, 2024
1 parent 77e8e59 commit 2915f38
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 37 deletions.
18 changes: 6 additions & 12 deletions packages/react/src/use-dotlottie-worker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,12 @@ export const useDotLottieWorker = (config: DotLottieWorkerConfig): UseDotLottieW
useEffect(() => {
let dotLottieInstance: DotLottieWorker | null = null;

if (config.canvasRef.current) {
const canvas = configRef.current?.canvasRef.current;

if (canvas) {
dotLottieInstance = new DotLottieWorker({
...config,
canvas: config.canvasRef.current,
canvas,
});

setDotLottie(dotLottieInstance);
Expand All @@ -54,15 +56,7 @@ export const useDotLottieWorker = (config: DotLottieWorkerConfig): UseDotLottieW
setDotLottie(null);
}
};
}, [config.canvasRef]);

// hover reactivity
useEffect(() => {
if (config.canvasRef.current) {
config.canvasRef.current.addEventListener('mouseenter', hoverHandler);
config.canvasRef.current.addEventListener('mouseleave', hoverHandler);
}
}, [config.canvasRef, hoverHandler]);
}, []);

// hover reactivity
useEffect(() => {
Expand All @@ -77,7 +71,7 @@ export const useDotLottieWorker = (config: DotLottieWorkerConfig): UseDotLottieW
config.canvasRef.current.removeEventListener('mouseleave', hoverHandler);
}
};
}, [config.canvasRef]);
}, [config.canvasRef, hoverHandler]);

// speed reactivity
useEffect(() => {
Expand Down
35 changes: 10 additions & 25 deletions packages/react/src/use-dotlottie.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,22 +38,15 @@ export const useDotLottie = (config: DotLottieConfig): UseDotLottieResult => {
useEffect(() => {
let dotLottieInstance: DotLottie | null = null;

if (config.canvasRef.current) {
// wait for canvas to be ready before initializing dotLottie
const interval = setInterval(() => {
if (
config.canvasRef.current &&
config.canvasRef.current.offsetWidth > 0 &&
config.canvasRef.current.offsetHeight > 0
) {
clearInterval(interval);
dotLottieInstance = new DotLottie({
...config,
canvas: config.canvasRef.current,
});
setDotLottie(dotLottieInstance);
}
}, 1);
const canvas = configRef.current?.canvasRef.current;

if (canvas) {
dotLottieInstance = new DotLottie({
...config,
canvas,
});

setDotLottie(dotLottieInstance);
}

return () => {
Expand All @@ -62,15 +55,7 @@ export const useDotLottie = (config: DotLottieConfig): UseDotLottieResult => {
setDotLottie(null);
}
};
}, [config.canvasRef]);

// hover reactivity
useEffect(() => {
if (config.canvasRef.current) {
config.canvasRef.current.addEventListener('mouseenter', hoverHandler);
config.canvasRef.current.addEventListener('mouseleave', hoverHandler);
}
}, [config.canvasRef, hoverHandler]);
}, []);

// hover reactivity
useEffect(() => {
Expand Down

0 comments on commit 2915f38

Please sign in to comment.