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

Viewport: Fix editing when default viewport is set #26360

Merged
merged 3 commits into from
Mar 7, 2024
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
15 changes: 13 additions & 2 deletions code/addons/viewport/src/Tool.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ export const ViewportTool: FC = memo(

useEffect(() => {
registerShortcuts(api, globals, updateGlobals, Object.keys(viewports));
}, [viewports, globals.viewport]);
}, [viewports, globals, globals.viewport, updateGlobals, api]);

useEffect(() => {
const defaultRotated = defaultOrientation === 'landscape';
Expand All @@ -150,7 +150,18 @@ export const ViewportTool: FC = memo(
viewportRotated: defaultRotated,
});
}
}, [defaultOrientation, defaultViewport, globals, updateGlobals]);
// NOTE: we don't want to re-run this effect when `globals` changes
// due to https://github.com/storybookjs/storybook/issues/26334
//
// Also, this *will* rerun every time you change story as the parameter is briefly `undefined`.
// This behaviour is intentional, if a bit of a happy accident in implementation.
//
// Ultimately this process of "locking in" a parameter value should be
// replaced by https://github.com/storybookjs/storybook/discussions/23347
// or something similar.
//
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [defaultOrientation, defaultViewport, updateGlobals]);

const item =
list.find((i) => i.id === globals.viewport) ||
Expand Down
22 changes: 22 additions & 0 deletions code/e2e-tests/addon-viewport.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,26 @@ test.describe('addon-viewport', () => {
// Compare the two widths
await expect(adjustedDimensions?.width).not.toBe(originalDimensions?.width);
});

test('viewport should be editable when a default viewport is set', async ({ page }) => {
const sbPage = new SbPage(page);

// Story parameters/selected is set to small mobile
await sbPage.navigateToStory('addons/viewport/parameters', 'selected');

// Measure the original dimensions of previewRoot
const originalDimensions = await sbPage.getCanvasBodyElement().boundingBox();
await expect(originalDimensions?.width).toBeDefined();

// Manually select "large mobile" and give it time to adjust
await sbPage.selectToolbar('[title="Change the size of the preview"]', '#list-item-mobile2');
await new Promise((r) => setTimeout(r, 200));

// Measure the adjusted dimensions of previewRoot after clicking the mobile item.
const adjustedDimensions = await sbPage.getCanvasBodyElement().boundingBox();
await expect(adjustedDimensions?.width).toBeDefined();

// Compare the two widths
await expect(adjustedDimensions?.width).not.toBe(originalDimensions?.width);
});
});
Loading