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

Menu: migrate Storybook examples to CSF3 #68204

Open
wants to merge 5 commits into
base: trunk
Choose a base branch
from

Conversation

ciampo
Copy link
Contributor

@ciampo ciampo commented Dec 20, 2024

What?

Migrate Menu's storybook examples to the latest story format

Why?

Keeping aligned with latest Storybook specs and features, potentially better performance

How?

Followed docs at https://storybook.js.org/docs/api/csf

Testing Instructions

Build storybook locally (npm run storybook:dev), make sure that all Menu stories work as on trunk

@ciampo ciampo requested a review from ajitbohra as a code owner December 20, 2024 16:11
Copy link

github-actions bot commented Dec 20, 2024

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Co-authored-by: ciampo <[email protected]>
Co-authored-by: tyxla <[email protected]>
Co-authored-by: mirka <[email protected]>

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hiding whitespace changes removes a lot of noise while reviewing

@ciampo ciampo requested a review from a team December 20, 2024 16:12
@ciampo ciampo self-assigned this Dec 20, 2024
@ciampo ciampo added [Type] Code Quality Issues or PRs that relate to code quality [Package] Components /packages/components labels Dec 20, 2024
Copy link

github-actions bot commented Dec 20, 2024

Flaky tests detected in 980c20e.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/12455042305
📝 Reported issues:

Copy link
Member

@tyxla tyxla left a comment

Choose a reason for hiding this comment

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

This looks good, thanks for the migration @ciampo!

Just left a few questions and things to discuss as I'm getting familiar with the syntax.

packages/components/src/menu/stories/index.story.tsx Outdated Show resolved Hide resolved
Comment on lines +73 to +72
args: {
children: (
Copy link
Member

Choose a reason for hiding this comment

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

The csf-2-to-3 codemod would use render: () => { ... } instead of args.children. What's the difference? Seems like it allows you to specify the props type inline when passing it to render:

export const Default: StoryObj< typeof Menu > = {
	render: ( props: MenuProps ) => (
		<Menu { ...props }>
			/// ...
		</Menu>
	),

	args: {},
};

One difference to note is that the codemod uses render: but also specifically adds this to all stories that inherit from Default:

	args: {
		...Default.args,
	},

Copy link
Member

Choose a reason for hiding this comment

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

When I was working on some of the TypeScript compilation hotspots, I noticed that not having a render function is better for performance, since it can bypass a lot of work. So we should avoid render and prefer children when possible.

Copy link
Contributor Author

@ciampo ciampo Dec 22, 2024

Choose a reason for hiding this comment

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

As Lena points out, the reason why I used children at the beginning was because I thought it'd be more idiomatic / more performant.

string[]
>( [ 'b' ] );
export const WithCheckboxes: Story = {
render: function WithCheckboxes( props: MenuProps ) {
Copy link
Member

Choose a reason for hiding this comment

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

Why do we need to provide a named function here? Can't we just do:

	render: ( props: MenuProps ) => (
		<>
			//...
		</>
	),

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I used named functions to prevent warnings about using hooks outside of a component.

packages/components/src/menu/stories/index.story.tsx Outdated Show resolved Hide resolved
Comment on lines +73 to +72
args: {
children: (
Copy link
Member

Choose a reason for hiding this comment

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

When I was working on some of the TypeScript compilation hotspots, I noticed that not having a render function is better for performance, since it can bypass a lot of work. So we should avoid render and prefer children when possible.

string[]
>( [ 'b' ] );
export const WithCheckboxes: Story = {
render: function WithCheckboxes( props: MenuProps ) {
Copy link
Member

Choose a reason for hiding this comment

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

These parameter types shouldn't be necessary, since that is already handled through the StoryObj<> generic.

Suggested change
render: function WithCheckboxes( props: MenuProps ) {
render: function WithCheckboxes( props ) {

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I did that initially, but then I added back the explicit props because it seems to improve TypeScript performance (TS doesn't need to compute the prop types since they are explicitly declared)

</Menu>
);
Default.args = {};
type Story = StoryObj< MenuProps >;
Copy link
Member

Choose a reason for hiding this comment

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

Any reason it's not this?

Suggested change
type Story = StoryObj< MenuProps >;
type Story = StoryObj< typeof Menu >;

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Same as #68204 (comment) — performance reasons.

Although I'm happy to revert if we don't think performance is improved with this change.

@ciampo ciampo force-pushed the feat/menu-storybook-csf3 branch from 2514423 to fd4b118 Compare December 22, 2024 14:15
@ciampo ciampo requested review from tyxla and mirka December 22, 2024 14:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Package] Components /packages/components [Type] Code Quality Issues or PRs that relate to code quality
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants