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: Storybook 7 viewport menu not working #22829

Merged
merged 3 commits into from
May 30, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
20 changes: 17 additions & 3 deletions code/addons/viewport/src/Tool.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,25 @@ const getStyles = (
styles: Styles,
isRotated: boolean
): ViewportStyles | undefined => {
if (!styles || !prevStyles) {
if (styles === null) {
return undefined;
}
const result = typeof styles === 'function' ? styles(prevStyles) : styles;
return isRotated ? flip(result) : result;

let result: ViewportStyles | undefined;

if (typeof styles === 'function') {
if (prevStyles) {
result = styles(prevStyles);
}
} else {
result = styles;
}
Copy link
Contributor

Choose a reason for hiding this comment

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

But what now if styles is a function and prevStyles is not yet defined. Then result will also be always undefined right? I think that maybe the right fix would be to rollback what we had before, and change the type to be:

export type Styles = ViewportStyles | ((previous: ViewportStyles | null) => ViewportStyles) | null;

So that the user has to explicitly handle previous being still null.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

to rollback what we had before

You mean reverting back to before fe02579, right?

And yes you're right the nested if if (prevStyles) was only there because of the type Styles.

And what about the type for prevStyles? The actual value for prevStyles argument comes from const ref = useRef<ViewportStyles>(); which allows it to be undefined, so you either have to give it the initial value or accept undefined for prevStyles arg of getStyles.

Copy link
Contributor

@kasperpeulen kasperpeulen May 30, 2023

Choose a reason for hiding this comment

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

You mean reverting back to before fe02579, right?

Yes!

And yes, widening previous in the type of Styles to undefined instead of null sounds also good to me indeed!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hey um one more thing about the return type. Since the ref only takes ViewportStyles | undefined I adjusted the return value to be ViewportStyles | undefined rather than ViewportStyles | null. I pushed a revised commit so could you check if everything's alright?


if (result && isRotated) {
return flip(result);
}

return result;
};

export const ViewportTool: FC = memo(
Expand Down
20 changes: 20 additions & 0 deletions code/e2e-tests/addon-viewport.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,24 @@ test.describe('addon-viewport', () => {
// Check that Button story is still displayed
await expect(sbPage.previewRoot()).toContainText('Button');
});

test('iframe width should be changed when a mobile viewport is selected', async ({ page }) => {
const sbPage = new SbPage(page);

// Click on viewport button and select small mobile
await sbPage.navigateToStory('example/button', 'primary');

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

await sbPage.selectToolbar('[title="Change the size of the preview"]', '#list-item-mobile1');

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

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