Skip to content

Commit

Permalink
fix(react): add missing layout prop
Browse files Browse the repository at this point in the history
  • Loading branch information
theashraf committed Dec 17, 2024
1 parent fad1e71 commit f5d330b
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/sixty-shoes-roll.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@lottiefiles/dotlottie-react': patch
---

fix: add missing layout prop
9 changes: 9 additions & 0 deletions packages/react/src/base-dotlottie-react.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ export const BaseDotLottieReact = <T extends DotLottie | DotLottieWorker>({
createDotLottie,
data,
dotLottieRefCallback,
layout,
loop,
mode,
playOnHover,
Expand Down Expand Up @@ -89,6 +90,7 @@ export const BaseDotLottieReact = <T extends DotLottie | DotLottieWorker>({
workerId,
src,
data,
layout,
renderConfig,
};

Expand Down Expand Up @@ -223,6 +225,13 @@ export const BaseDotLottieReact = <T extends DotLottie | DotLottieWorker>({
dotLottieRef.current?.setThemeData(themeData ?? '');
}, [themeData]);

useEffect(() => {
dotLottieRef.current?.setLayout({
align: layout?.align ?? [0.5, 0.5],
fit: layout?.fit ?? 'contain',
});
}, [layout?.align[0], layout?.align[1], layout?.fit]);

return (
<div
className={className}
Expand Down
44 changes: 44 additions & 0 deletions packages/react/tests/dotlottie-react.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -684,4 +684,48 @@ describe.each([
speed: 2,
});
});

test('calls dotLottie.setLayout when layout prop changes', async () => {
const onLoad = vi.fn();
const dotLottieRefCallback = vi.fn();

const { rerender } = render(<Component src={dotLottieSrc} autoplay dotLottieRefCallback={dotLottieRefCallback} />);

await vi.waitFor(() => {
expect(dotLottieRefCallback).toHaveBeenCalledTimes(1);
});

const dotLottie = dotLottieRefCallback.mock.calls[0]?.[0];

dotLottie?.addEventListener('load', onLoad);

await vi.waitFor(() => {
expect(onLoad).toHaveBeenCalledTimes(1);
});

const setLayout = vi.spyOn(dotLottie, 'setLayout');

rerender(
<Component
src={dotLottieSrc}
autoplay
layout={{ align: [0.5, 0.5], fit: 'contain' }}
dotLottieRefCallback={dotLottieRefCallback}
/>,
);

await vi.waitFor(() => {
expect(setLayout).toHaveBeenCalledTimes(1);
});

expect(setLayout).toHaveBeenCalledWith({ align: [0.5, 0.5], fit: 'contain' });

rerender(<Component src={dotLottieSrc} autoplay dotLottieRefCallback={dotLottieRefCallback} />);

await vi.waitFor(() => {
expect(setLayout).toHaveBeenCalledTimes(2);
});

expect(setLayout).toHaveBeenCalledWith({ align: [0.5, 0.5], fit: 'contain' });
});
});

0 comments on commit f5d330b

Please sign in to comment.