Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(react): type error caused by passing empty or incomplete layout prop #490

Merged
merged 1 commit into from
Mar 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/curvy-jokes-matter.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@lottiefiles/dotlottie-web': minor
---

feat: make `Layout` properties optional with default values. `align` defaults to `[0.5, 0.5]` and `fit` defaults to `contain`
5 changes: 5 additions & 0 deletions .changeset/tough-roses-pay.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@lottiefiles/dotlottie-react': patch
---

fix: TypeError caused by passing empty or incomplete `layout` props to `DotLottieReact` component
7 changes: 2 additions & 5 deletions packages/react/src/base-dotlottie-react.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -226,11 +226,8 @@ export const BaseDotLottieReact = <T extends DotLottie | DotLottieWorker>({
}, [themeData]);

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

return (
<div
Expand Down
39 changes: 39 additions & 0 deletions packages/react/tests/dotlottie-react.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -728,4 +728,43 @@ describe.each([

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

test('calls dotLottie.setLayout properly when layout prop changes with fit only', 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={{ fit: 'cover' }} dotLottieRefCallback={dotLottieRefCallback} />,
);

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

expect(setLayout).toHaveBeenCalledWith({ fit: 'cover' });

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

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

expect(setLayout).toHaveBeenCalledWith({ fit: 'cover' });
});
});
32 changes: 15 additions & 17 deletions packages/web/src/dotlottie.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,17 @@ const createCoreSegment = (segment: number[], module: MainModule): VectorFloat =
return coresegment;
};

const createCoreLayout = (layout: Layout | undefined, module: MainModule): { align: VectorFloat; fit: CoreFit } => {
if (!layout) {
return module.createDefaultLayout();
}

return {
align: createCoreAlign(layout.align ?? [0.5, 0.5], module),
fit: createCoreFit(layout.fit ?? 'contain', module),
};
};

export class DotLottie {
private readonly _canvas: HTMLCanvasElement | OffscreenCanvas;

Expand Down Expand Up @@ -115,12 +126,7 @@ export class DotLottie {
speed: config.speed ?? 1,
useFrameInterpolation: config.useFrameInterpolation ?? true,
marker: config.marker ?? '',
layout: config.layout
? {
align: createCoreAlign(config.layout.align, module),
fit: createCoreFit(config.layout.fit, module),
}
: module.createDefaultLayout(),
layout: createCoreLayout(config.layout, module),
});

this._eventManager.dispatch({ type: 'ready' });
Expand Down Expand Up @@ -448,12 +454,7 @@ export class DotLottie {
speed: config.speed ?? 1,
useFrameInterpolation: config.useFrameInterpolation ?? true,
marker: config.marker ?? '',
layout: config.layout
? {
align: createCoreAlign(config.layout.align, DotLottie._wasmModule),
fit: createCoreFit(config.layout.fit, DotLottie._wasmModule),
}
: DotLottie._wasmModule.createDefaultLayout(),
layout: createCoreLayout(config.layout, DotLottie._wasmModule),
});

if (config.data) {
Expand All @@ -471,7 +472,7 @@ export class DotLottie {
const rendered = this._dotLottieCore.render();

if (rendered) {
const buffer = this._dotLottieCore.buffer() as Uint8Array;
const buffer = this._dotLottieCore.buffer() as ArrayBuffer;
const clampedBuffer = new Uint8ClampedArray(buffer, 0, this._canvas.width * this._canvas.height * 4);

let imageData = null;
Expand Down Expand Up @@ -840,10 +841,7 @@ export class DotLottie {

this._dotLottieCore.setConfig({
...this._dotLottieCore.config(),
layout: {
align: createCoreAlign(layout.align, DotLottie._wasmModule),
fit: createCoreFit(layout.fit, DotLottie._wasmModule),
},
layout: createCoreLayout(layout, DotLottie._wasmModule),
});
}

Expand Down
4 changes: 2 additions & 2 deletions packages/web/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ export type Data = string | ArrayBuffer | Record<string, unknown>;
export type Fit = 'contain' | 'cover' | 'fill' | 'none' | 'fit-width' | 'fit-height';

export interface Layout {
align: [number, number];
fit: Fit;
align?: [number, number];
fit?: Fit;
}

export interface Config {
Expand Down
Loading