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

Update BlockMover Stories and README #66519

Merged
merged 14 commits into from
Nov 15, 2024

Conversation

miminari
Copy link
Member

@miminari miminari commented Oct 28, 2024

What?

Update the BlockMover component's Storybook and README

Why?

I feel Block Editor components's Storybook should also be updated.
We should also have all component's stories, but I started to update the component having stories.
related #22891

How?

Almost Component's stories already have been updated, and use Storybook 7's features.
I changed the Stories based on them, and added and changed the minimum README.

And, I would like to use the automatic citation from the README to the Storybook, but it did not work...

Testing Instructions

npm run storybook:dev and check the BlockMover stories.

Testing Instructions for Keyboard

Screenshots or screencast

ss_2024-10-29 10 25 30

@miminari miminari self-assigned this Oct 29, 2024
@miminari miminari added Storybook Storybook and its stories for components [Type] Code Quality Issues or PRs that relate to code quality [Type] Developer Documentation Documentation for developers and removed [Type] Code Quality Issues or PRs that relate to code quality labels Oct 29, 2024
@miminari miminari marked this pull request as ready for review October 29, 2024 01:36
@miminari miminari requested a review from ellatrix as a code owner October 29, 2024 01:36
Copy link

github-actions bot commented Oct 29, 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: miminari <[email protected]>
Co-authored-by: mirka <[email protected]>
Co-authored-by: t-hamano <[email protected]>

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

@miminari miminari requested review from mirka, adamziel and t-hamano and removed request for ellatrix October 29, 2024 01:36
Copy link
Member

@mirka mirka left a comment

Choose a reason for hiding this comment

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

Thanks for improving our Block Editor stories!

I would like to use the automatic citation from the README to the Storybook, but it did not work

We currently don't have any systems to extract README data into Storybook. I think you're referring to how @wordpress/components stories extract TypeScript/TSDoc data into Storybook 🙂 I don't think we'll be able to automate this part until Block Editor components are annotated with TypeScript.

);
};

export const Default: StoryFn< typeof BlockMover > = Template.bind( {} );
Copy link
Member

Choose a reason for hiding this comment

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

It might be nice to use CSF 3 format, since we're overhauling anyway.

Copy link
Member Author

Choose a reason for hiding this comment

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

I just removed the types, is it OK?

Copy link
Member

@mirka mirka Nov 12, 2024

Choose a reason for hiding this comment

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

I do recommend it in this case, as we can eliminate a lot of unnecessary code.

The main points of this suggested diff being:

  1. The Template component is unnecessary because it's just the plain component, and we only use it once. Plus, the implementation is incorrect because the clientIds prop cannot be overridden by the stories.
  2. The useEffect in the Horizontal story can also be moved to a decorator.
  3. The conditionals that check for blocks.length seem unnecessary because we are assigning the blocks variable statically.
diff --git a/packages/block-editor/src/components/block-mover/stories/index.story.js b/packages/block-editor/src/components/block-mover/stories/index.story.js
index 97d8fce96a..241c62c878 100644
--- a/packages/block-editor/src/components/block-mover/stories/index.story.js
+++ b/packages/block-editor/src/components/block-mover/stories/index.story.js
@@ -31,29 +31,6 @@ const blocks = [
 	] ),
 ];
 
-function BlockMoverStoryHorizontal() {
-	const { updateBlockListSettings } = useDispatch( blockEditorStore );
-
-	useEffect( () => {
-		/**
-		 * This shouldn't be needed but unfortunately
-		 * the layout orientation is not declarative, we need
-		 * to render the blocks to update the block settings in the state.
-		 */
-		updateBlockListSettings( blocks[ 1 ].clientId, {
-			orientation: 'horizontal',
-		} );
-	}, [] );
-
-	return (
-		<BlockMover
-			clientIds={
-				blocks.length ? [ blocks[ 1 ].innerBlocks[ 1 ].clientId ] : []
-			}
-		/>
-	);
-}
-
 /**
  * BlockMover component allows moving blocks inside the editor using up and down buttons.
  */
@@ -89,55 +66,49 @@ const meta = {
 };
 export default meta;
 
-const Template = ( props ) => {
-	return (
-		<BlockMover
-			{ ...props }
-			clientIds={
-				blocks.length ? [ blocks[ 1 ].innerBlocks[ 1 ].clientId ] : []
-			}
-		/>
-	);
-};
-
-export const Default = Template.bind( {} );
-Default.args = {
-	clientIds: [
-		blocks.length ? [ blocks[ 0 ].innerBlocks[ 1 ].clientId ] : [],
-	],
+export const Default = {
+	args: {
+		clientIds: [ blocks[ 0 ].innerBlocks[ 1 ].clientId ],
+	},
 };
 
 /**
  * This story shows the block mover with horizontal orientation.
  * It is necessary to render the blocks to update the block settings in the state.
  */
-export const Horizontal = ( props ) => {
-	return <BlockMoverStoryHorizontal { ...props } />;
-};
-Horizontal.args = {
-	clientIds: [
-		blocks.length ? [ blocks[ 1 ].innerBlocks[ 1 ].clientId ] : [],
+export const Horizontal = {
+	decorators: [
+		( Story ) => {
+			const { updateBlockListSettings } = useDispatch( blockEditorStore );
+
+			useEffect( () => {
+				/**
+				 * This shouldn't be needed but unfortunately
+				 * the layout orientation is not declarative, we need
+				 * to render the blocks to update the block settings in the state.
+				 */
+				updateBlockListSettings( blocks[ 1 ].clientId, {
+					orientation: 'horizontal',
+				} );
+			}, [] );
+
+			return <Story />;
+		},
 	],
-};
-Horizontal.parameters = {
-	docs: { canvas: { sourceState: 'hidden' } },
+	args: {
+		clientIds: [ blocks[ 1 ].innerBlocks[ 1 ].clientId ],
+	},
+	parameters: {
+		docs: { canvas: { sourceState: 'hidden' } },
+	},
 };
 
 /**
  * You can hide the drag handle by `hideDragHandle` attribute.
  */
-export const HideDragHandle = ( props ) => {
-	return (
-		<BlockMover
-			{ ...props }
-			clientIds={
-				blocks.length ? [ blocks[ 1 ].innerBlocks[ 1 ].clientId ] : []
-			}
-			hideDragHandle
-		/>
-	);
-};
-HideDragHandle.args = {
-	...Default.args,
-	hideDragHandle: true,
+export const HideDragHandle = {
+	args: {
+		...Default.args,
+		hideDragHandle: true,
+	},
 };

Copy link
Contributor

@t-hamano t-hamano left a comment

Choose a reason for hiding this comment

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

Thanks for the PR!

Each story is working as expected, but I think one thing we need to address is making the hideDragHandle prop controllable:

672766caf0147a73c5f9aa049172bbf2.mp4

We'll probably need to run updateBlockListSettings in response to changes in the control.

@miminari
Copy link
Member Author

miminari commented Nov 7, 2024

@mirka

I would like to use the automatic citation from the README to the Storybook, but it did not work

We currently don't have any systems to extract README data into Storybook. I think you're referring to how @wordpress/components stories extract TypeScript/TSDoc data into Storybook 🙂 I don't think we'll be able to automate this part until Block Editor components are annotated with TypeScript.

I see, thanks for letting me know.
When I tried this, I saw the text rendered in the stories like "Renders a link control. ...", it seems same as the text in the README, so I mistakenly thought I could.

I think I updated all. Could you please review again?

@miminari miminari requested review from mirka and t-hamano November 7, 2024 05:24
Copy link
Member

@mirka mirka left a comment

Choose a reason for hiding this comment

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

I suggested some additional cleanup and bug fixes in #66519 (comment), but all the other tweaks look good!

Comment on lines 105 to 107
clientIds: [
blocks.length ? [ blocks[ 0 ].innerBlocks[ 1 ].clientId ] : [],
],
Copy link
Member

Choose a reason for hiding this comment

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

Noting that this would result in a nested array, not a flat one as it should be. (These are also fixed in the suggested diff in #66519 (comment))

Comment on lines 133 to 136
clientIds={
blocks.length ? [ blocks[ 1 ].innerBlocks[ 1 ].clientId ] : []
}
hideDragHandle
Copy link
Member

Choose a reason for hiding this comment

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

Noting that these hardcoded props would override any of the prop values passed from args or the Storybook controls.

@miminari
Copy link
Member Author

@mirka I appreciate you taking the time to tell me.
I finally got it, I hope.
It turns out at least that I didn't understand 70% of the storybook's functionality...

One point, I was wondering if we should pass the clientIds directly for the Horizontal story because BlockMover can't be rendered horizontally view by passing clientIds via args.
How is it?

@miminari miminari requested a review from mirka November 14, 2024 00:03
Copy link
Contributor

@t-hamano t-hamano left a comment

Choose a reason for hiding this comment

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

Thanks for the update.

Finally, in order for the "Horizontal" story to work properly (to get the hideDragHandle controls to work), it looks like the following changes are necessary:

diff --git a/packages/block-editor/src/components/block-mover/stories/index.story.js b/packages/block-editor/src/components/block-mover/stories/index.story.js
index 5b2fdd4ee6..fd56a4cded 100644
--- a/packages/block-editor/src/components/block-mover/stories/index.story.js
+++ b/packages/block-editor/src/components/block-mover/stories/index.story.js
@@ -80,7 +80,7 @@ export const Default = {
  */
 export const Horizontal = {
        decorators: [
-               () => {
+               ( Story ) => {
                        const { updateBlockListSettings } = useDispatch( blockEditorStore );
                        useEffect( () => {
                                /**
@@ -92,13 +92,12 @@ export const Horizontal = {
                                        orientation: 'horizontal',
                                } );
                        }, [] );
-                       return (
-                               <BlockMover
-                                       clientIds={ [ blocks[ 1 ].innerBlocks[ 1 ].clientId ] }
-                               />
-                       );
+                       return <Story />;
                },
        ],
+       args: {
+               clientIds: [ blocks[ 1 ].innerBlocks[ 1 ].clientId ],
+       },
        parameters: {
                docs: { canvas: { sourceState: 'hidden' } },
        },

@miminari miminari requested a review from t-hamano November 14, 2024 22:29
@miminari
Copy link
Member Author

@mirka @t-hamano Sorry, but I missed the point that the implementation of the args was incorrect.
Could you please review again?

Copy link
Contributor

@t-hamano t-hamano left a comment

Choose a reason for hiding this comment

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

LGTM!

431b4dbb370ed576feeea5242ada0dd9.mp4

package.json Outdated Show resolved Hide resolved
@t-hamano t-hamano merged commit 2028fd8 into WordPress:trunk Nov 15, 2024
62 checks passed
@github-actions github-actions bot added this to the Gutenberg 19.8 milestone Nov 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Storybook Storybook and its stories for components [Type] Developer Documentation Documentation for developers
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants