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: Prevent canvas resolution resizing if user passes in resolution values on canvas #116

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
28 changes: 23 additions & 5 deletions src/hooks/useRive.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ function RiveComponent({
setCanvasRef,
className = '',
style,
width,
height,
...rest
}: RiveComponentProps & ComponentProps<'canvas'>) {
const containerStyle = {
Expand All @@ -39,7 +41,13 @@ function RiveComponent({
className={className}
{...(!className && { style: containerStyle })}
>
<canvas ref={setCanvasRef} style={{ verticalAlign: 'top' }} {...rest} />
<canvas
ref={setCanvasRef}
style={{ verticalAlign: 'top' }}
{...(width !== undefined && {width, 'data-rive-width-prop': width})}
{...(height !== undefined && {height, 'data-rive-height-prop': height})}
{...rest}
/>
</div>
);
}
Expand Down Expand Up @@ -130,18 +138,28 @@ export default function useRive(
const boundsChanged =
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updateBounds be called at all if we have width & height fixed?

width !== dimensions.width || height !== dimensions.height;
if (canvasRef.current && rive && boundsChanged) {
const widthProp = canvasRef.current.getAttribute('data-rive-width-prop');
const heightProp = canvasRef.current.getAttribute('data-rive-height-prop');
if (options.fitCanvasToArtboardHeight) {
containerRef.current.style.height = height + 'px';
}
if (options.useDevicePixelRatio) {
const dpr = window.devicePixelRatio || 1;
canvasRef.current.width = dpr * width;
canvasRef.current.height = dpr * height;
if (!widthProp) {
canvasRef.current.width = dpr * width;
}
if (!heightProp) {
canvasRef.current.height = dpr * height;
}
canvasRef.current.style.width = width + 'px';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we still set the style to these new widths / heights?

canvasRef.current.style.height = height + 'px';
} else {
canvasRef.current.width = width;
canvasRef.current.height = height;
if (!widthProp) {
canvasRef.current.width = width;
}
if (!heightProp) {
canvasRef.current.height = height;
}
}
setDimensions({ width, height });

Expand Down
31 changes: 31 additions & 0 deletions test/useRive.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -376,4 +376,35 @@ describe('useRive', () => {
);
expect(container.firstChild).not.toHaveStyle('width: 50%');
});

it('container bounds do not override user-provided canvas resolutions', async () => {
const params = {
src: 'file-src',
};

global.devicePixelRatio = 2;

const riveMock = {
...baseRiveMock,
resizeToCanvas: jest.fn(),
};
// @ts-ignore
mocked(rive.Rive).mockImplementation(() => riveMock);

const containerSpy = document.createElement('div');
jest.spyOn(containerSpy, 'clientWidth', 'get').mockReturnValue(600);
jest.spyOn(containerSpy, 'clientHeight', 'get').mockReturnValue(100);
const { result } = renderHook(() => useRive(params));

const { RiveComponent: RiveTestComponent } = result.current;
render(
<RiveTestComponent width={300} height={300} />
);
await act(async () => {
result.current.setContainerRef(containerSpy);
controlledRiveloadCb();
});

expect(result.current.canvas).toHaveAttribute('width', '300');
});
});