Skip to content

Commit

Permalink
feat(web): 🎸 add autoResize to render config (#341)
Browse files Browse the repository at this point in the history
* feat(web): 🎸 add autoResize to render config
  • Loading branch information
theashraf authored Sep 2, 2024
1 parent 5efca2a commit 1900885
Show file tree
Hide file tree
Showing 25 changed files with 501 additions and 205 deletions.
5 changes: 5 additions & 0 deletions .changeset/lemon-mangos-agree.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@lottiefiles/dotlottie-web': minor
---

feat(web): 🎸 add autoResize to render config
12 changes: 11 additions & 1 deletion apps/dotlottie-next-example/src/pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,17 @@ export default function Home(): JSX.Element {
<link rel="icon" href="/favicon.ico" />
</Head>
<main className={`${styles.main} ${inter.className}`}>
<DotLottieReact src={src} loop autoplay />
<DotLottieReact
style={{
minWidth: '100px',
}}
src={src}
loop
autoplay
renderConfig={{
autoResize: true,
}}
/>
</main>
</>
);
Expand Down
4 changes: 3 additions & 1 deletion apps/dotlottie-react-example/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,9 @@ function App() {
loop={loop}
speed={speed}
playOnHover={playOnHover}
autoResizeCanvas={autoResizeCanvas}
renderConfig={{
autoResize: autoResizeCanvas,
}}
marker={marker}
style={{
margin: '2px',
Expand Down
4 changes: 3 additions & 1 deletion apps/dotlottie-solid-example/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,9 @@ const App: Component = () => {
themeId={themeId()}
speed={speed()}
playOnHover={playOnHover()}
autoResizeCanvas={autoResizeCanvas()}
renderConfig={{
autoResize: autoResizeCanvas(),
}}
style={{
height: '500px',
}}
Expand Down
23 changes: 13 additions & 10 deletions apps/dotlottie-web-example/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

import './styles.css';
import type { Fit, Mode } from '@lottiefiles/dotlottie-web';
import { DotLottieWorker as DotLottie } from '@lottiefiles/dotlottie-web';
// import { DotLottie } from '@lottiefiles/dotlottie-web';
// import { DotLottieWorker as DotLottie } from '@lottiefiles/dotlottie-web';
import { DotLottie } from '@lottiefiles/dotlottie-web';

import wasmUrl from '../../../packages/web/dist/dotlottie-player.wasm?url';

Expand Down Expand Up @@ -162,16 +162,19 @@ allCanvas.forEach((canvas) => {
loop: true,
autoplay: true,
backgroundColor,
// useFrameInterpolation: false,
renderConfig: {
autoResize: true,
},
useFrameInterpolation: false,
});

dotLottie.addEventListener('loadError', console.error);

Check warning on line 171 in apps/dotlottie-web-example/src/main.ts

View workflow job for this annotation

GitHub Actions / validate

Unexpected console statement
dotLottie.addEventListener('freeze', console.log);

Check warning on line 172 in apps/dotlottie-web-example/src/main.ts

View workflow job for this annotation

GitHub Actions / validate

Unexpected console statement
dotLottie.addEventListener('unfreeze', console.log);

Check warning on line 173 in apps/dotlottie-web-example/src/main.ts

View workflow job for this annotation

GitHub Actions / validate

Unexpected console statement

window.addEventListener('resize', () => {
dotLottie.resize();
});
// window.addEventListener('resize', () => {
// dotLottie.resize();
// });
});

fetch(
Expand Down Expand Up @@ -236,7 +239,7 @@ fetch(
const alignSelect = document.getElementById('align') as HTMLSelectElement;
const stateMachinesSelect = document.getElementById('states') as HTMLSelectElement;

const resizeButton = document.getElementById('resize') as HTMLButtonElement;
// const resizeButton = document.getElementById('resize') as HTMLButtonElement;

let animationIdx = 0;

Expand All @@ -256,9 +259,9 @@ fetch(
}
});

resizeButton.addEventListener('click', () => {
dotLottie.resize();
});
// resizeButton.addEventListener('click', () => {
// dotLottie.resize();
// });

freezeButton.addEventListener('click', () => {
dotLottie.freeze();
Expand Down
8 changes: 3 additions & 5 deletions apps/dotlottie-web-example/src/styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,15 @@ body {
}

canvas {
height: 200px;
border: 2px solid #333;
margin-bottom: 20px;
box-shadow: 4px 4px 10px rgba(0, 0, 0, 0.1);
width: 100%; /* Full width of the container */
aspect-ratio: 1/1;
}

.grid {
display: grid;
grid-template-rows: repeat(1, 1fr);
grid-template-columns: repeat(6, 1fr);
grid-template-rows: repeat(2, 1fr);
grid-template-columns: repeat(9, 1fr);
gap: 10px;
}

Expand Down
3 changes: 1 addition & 2 deletions packages/react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@
"react-dom": "^16.8.0 || ^17.0.0 || ^18.0.0"
},
"dependencies": {
"@lottiefiles/dotlottie-web": "workspace:*",
"debounce": "^2.0.0"
"@lottiefiles/dotlottie-web": "workspace:*"
},
"devDependencies": {
"@types/react": "^18.2.37",
Expand Down
3 changes: 0 additions & 3 deletions packages/react/src/dotlottie-worker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import useStableCallback from './use-stable-callback';
export type DotLottieWorkerReactProps = Omit<Config, 'canvas'> &
ComponentProps<'canvas'> & {
animationId?: string;
autoResizeCanvas?: boolean;
dotLottieRefCallback?: RefCallback<DotLottieWorker | null>;
playOnHover?: boolean;
themeData?: string;
Expand All @@ -20,7 +19,6 @@ export type DotLottieWorkerReactProps = Omit<Config, 'canvas'> &

export const DotLottieWorkerReact = ({
animationId,
autoResizeCanvas = true,
autoplay,
backgroundColor,
data,
Expand Down Expand Up @@ -52,7 +50,6 @@ export const DotLottieWorkerReact = ({
backgroundColor,
useFrameInterpolation,
playOnHover,
autoResizeCanvas,
marker,
themeId,
animationId,
Expand Down
3 changes: 0 additions & 3 deletions packages/react/src/dotlottie.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import useStableCallback from './use-stable-callback';
export type DotLottieReactProps = Omit<Config, 'canvas'> &
ComponentProps<'canvas'> & {
animationId?: string;
autoResizeCanvas?: boolean;
dotLottieRefCallback?: RefCallback<DotLottie>;
playOnHover?: boolean;
themeData?: string;
Expand All @@ -19,7 +18,6 @@ export type DotLottieReactProps = Omit<Config, 'canvas'> &

export const DotLottieReact = ({
animationId,
autoResizeCanvas = true,
autoplay,
backgroundColor,
data,
Expand Down Expand Up @@ -49,7 +47,6 @@ export const DotLottieReact = ({
backgroundColor,
useFrameInterpolation,
playOnHover,
autoResizeCanvas,
marker,
themeId,
animationId,
Expand Down
34 changes: 2 additions & 32 deletions packages/react/src/use-dotlottie-worker.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import type { Config } from '@lottiefiles/dotlottie-web';
import { DotLottieWorker } from '@lottiefiles/dotlottie-web';
import debounce from 'debounce';
import React, { useCallback, useState, useMemo, useEffect, useRef } from 'react';
import React, { useCallback, useState, useEffect, useRef } from 'react';
import type { ComponentProps, RefCallback } from 'react';

interface DotLottieWorkerComponentProps {
Expand Down Expand Up @@ -42,7 +41,6 @@ function DotLottieWorkerComponent({

export type DotLottieWorkerConfig = Omit<Config, 'canvas'> & {
animationId?: string;
autoResizeCanvas?: boolean;
playOnHover?: boolean;
themeData?: string;
themeId?: string;
Expand All @@ -58,8 +56,6 @@ export interface UseDotLottieWorkerResult {
setContainerRef: RefCallback<HTMLDivElement>;
}

const isServerSide = (): boolean => typeof window === 'undefined';

export const useDotLottieWorker = (config?: DotLottieWorkerConfig): UseDotLottieWorkerResult => {
const [dotLottie, setDotLottie] = useState<DotLottieWorker | null>(null);

Expand All @@ -82,18 +78,6 @@ export const useDotLottieWorker = (config?: DotLottieWorkerConfig): UseDotLottie
}
}, []);

const resizeObserver = useMemo(() => {
if (isServerSide()) return null;

const observerCallback = debounce(() => {
if (configRef.current?.autoResizeCanvas) {
dotLottieRef.current?.resize();
}
}, 150);

return new ResizeObserver(observerCallback);
}, []);

const setCanvasRef = useCallback((canvas: HTMLCanvasElement | null) => {
canvasRef.current = canvas;
}, []);
Expand All @@ -120,9 +104,6 @@ export const useDotLottieWorker = (config?: DotLottieWorkerConfig): UseDotLottie
canvas,
});

if (config?.autoResizeCanvas) {
resizeObserver?.observe(canvas);
}
canvas.addEventListener('mouseenter', hoverHandler);
canvas.addEventListener('mouseleave', hoverHandler);

Expand All @@ -132,11 +113,10 @@ export const useDotLottieWorker = (config?: DotLottieWorkerConfig): UseDotLottie
return () => {
dotLottieInstance?.destroy();
setDotLottie(null);
resizeObserver?.disconnect();
canvas?.removeEventListener('mouseenter', hoverHandler);
canvas?.removeEventListener('mouseleave', hoverHandler);
};
}, [resizeObserver, hoverHandler]);
}, [hoverHandler]);

// speed reactivity
useEffect(() => {
Expand Down Expand Up @@ -242,16 +222,6 @@ export const useDotLottieWorker = (config?: DotLottieWorkerConfig): UseDotLottie
}
}, [config?.marker]);

useEffect(() => {
if (!resizeObserver) return;

if (config?.autoResizeCanvas && canvasRef.current) {
resizeObserver.observe(canvasRef.current);
} else {
resizeObserver.disconnect();
}
}, [config?.autoResizeCanvas, resizeObserver]);

// animationId reactivity
useEffect(() => {
if (!dotLottieRef.current) return;
Expand Down
34 changes: 2 additions & 32 deletions packages/react/src/use-dotlottie.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import type { Config } from '@lottiefiles/dotlottie-web';
import { DotLottie } from '@lottiefiles/dotlottie-web';
import debounce from 'debounce';
import React, { useCallback, useState, useMemo, useEffect, useRef } from 'react';
import React, { useCallback, useState, useEffect, useRef } from 'react';
import type { ComponentProps, RefCallback } from 'react';

interface DotLottieComponentProps {
Expand Down Expand Up @@ -42,7 +41,6 @@ function DotLottieComponent({

export type DotLottieConfig = Omit<Config, 'canvas'> & {
animationId?: string;
autoResizeCanvas?: boolean;
playOnHover?: boolean;
themeData?: string;
themeId?: string;
Expand All @@ -57,8 +55,6 @@ export interface UseDotLottieResult {
setContainerRef: RefCallback<HTMLDivElement>;
}

const isServerSide = (): boolean => typeof window === 'undefined';

export const useDotLottie = (config?: DotLottieConfig): UseDotLottieResult => {
const [dotLottie, setDotLottie] = useState<DotLottie | null>(null);

Expand All @@ -81,18 +77,6 @@ export const useDotLottie = (config?: DotLottieConfig): UseDotLottieResult => {
}
}, []);

const resizeObserver = useMemo(() => {
if (isServerSide()) return null;

const observerCallback = debounce(() => {
if (configRef.current?.autoResizeCanvas) {
dotLottieRef.current?.resize();
}
}, 150);

return new ResizeObserver(observerCallback);
}, []);

const setCanvasRef = useCallback((canvas: HTMLCanvasElement | null) => {
canvasRef.current = canvas;
}, []);
Expand All @@ -119,9 +103,6 @@ export const useDotLottie = (config?: DotLottieConfig): UseDotLottieResult => {
canvas,
});

if (config?.autoResizeCanvas) {
resizeObserver?.observe(canvas);
}
canvas.addEventListener('mouseenter', hoverHandler);
canvas.addEventListener('mouseleave', hoverHandler);

Expand All @@ -131,11 +112,10 @@ export const useDotLottie = (config?: DotLottieConfig): UseDotLottieResult => {
return () => {
dotLottieInstance?.destroy();
setDotLottie(null);
resizeObserver?.disconnect();
canvas?.removeEventListener('mouseenter', hoverHandler);
canvas?.removeEventListener('mouseleave', hoverHandler);
};
}, [resizeObserver, hoverHandler]);
}, [hoverHandler]);

// speed reactivity
useEffect(() => {
Expand Down Expand Up @@ -241,16 +221,6 @@ export const useDotLottie = (config?: DotLottieConfig): UseDotLottieResult => {
}
}, [config?.marker]);

useEffect(() => {
if (!resizeObserver) return;

if (config?.autoResizeCanvas && canvasRef.current) {
resizeObserver.observe(canvasRef.current);
} else {
resizeObserver.disconnect();
}
}, [config?.autoResizeCanvas, resizeObserver]);

// animationId reactivity
useEffect(() => {
if (!dotLottieRef.current) return;
Expand Down
3 changes: 1 addition & 2 deletions packages/solid/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,7 @@
"solid-js": "^1.6.12"
},
"dependencies": {
"@lottiefiles/dotlottie-web": "workspace:*",
"debounce": "^2.0.0"
"@lottiefiles/dotlottie-web": "workspace:*"
},
"devDependencies": {
"cross-env": "7.0.3",
Expand Down
2 changes: 0 additions & 2 deletions packages/solid/src/dotlottie.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ export type DotLottieSolidProps = Omit<Config, 'canvas'> &
ComponentProps<'canvas'> &
Partial<{
animationId?: string;
autoResizeCanvas: boolean;
dotLottieRefCallback: (dotLottie: DotLottie) => void;
playOnHover: boolean;
themeData?: string;
Expand All @@ -30,7 +29,6 @@ export const DotLottieSolid = (props: DotLottieSolidProps): JSX.Element => {
'playOnHover',
'animationId',
'renderConfig',
'autoResizeCanvas',
'dotLottieRefCallback',
'useFrameInterpolation',
]);
Expand Down
Loading

0 comments on commit 1900885

Please sign in to comment.