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
Merged
Show file tree
Hide file tree
Changes from 3 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
23 changes: 14 additions & 9 deletions packages/block-editor/src/components/block-mover/README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
# Block mover
# BlockMover

Block movers allow moving blocks inside the editor using up and down buttons.
BlockMover allows moving blocks inside the editor using up and down buttons.

miminari marked this conversation as resolved.
Show resolved Hide resolved
![Block mover screenshot](https://make.wordpress.org/core/files/2020/08/block-mover-screenshot.png)

## Development guidelines

### Usage
## Usage

Shows the block mover buttons in the block toolbar.

Expand All @@ -15,13 +13,20 @@ import { BlockMover } from '@wordpress/block-editor';
const MyMover = () => <BlockMover clientIds={ [ clientId ] } />;
```

### Props
## Props

t-hamano marked this conversation as resolved.
Show resolved Hide resolved
### `clientIds`: `Array`

Blocks IDs.

- Required: Yes

#### clientIds
### `hideDragHandle`: `boolean`

Blocks IDs
If this property is true, the drag handle is hidden.

- Type: `Array`
- Required: No
- Default: `false`

## Related components

Expand Down

This file was deleted.

mirka marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
/**
* External dependencies
*/
import type { Meta, StoryFn } from '@storybook/react';

/**
* WordPress dependencies
*/
import { useEffect } from '@wordpress/element';
import { createBlock } from '@wordpress/blocks';
import { registerCoreBlocks } from '@wordpress/block-library';
import { useDispatch } from '@wordpress/data';
import { Toolbar } from '@wordpress/components';

/**
* Internal dependencies
*/
import BlockMover from '../';
import { ExperimentalBlockEditorProvider } from '../../provider';
import { store as blockEditorStore } from '../../../store';

// For the purpose of this story, we need to register the core blocks samples.
registerCoreBlocks();
const blocks = [
// vertical
createBlock( 'core/group', { layout: { type: 'flex' } }, [
createBlock( 'core/paragraph' ),
createBlock( 'core/paragraph' ),
createBlock( 'core/paragraph' ),
] ),
// horizontal
createBlock( 'core/buttons', {}, [
createBlock( 'core/button' ),
createBlock( 'core/button' ),
createBlock( 'core/button' ),
] ),
];

// Provider component to wrap the BlockEditorProvider
function Provider( { children } ) {
return (
<ExperimentalBlockEditorProvider value={ blocks }>
{ children }
</ExperimentalBlockEditorProvider>
);
}

function BlockMoverStoryHorizontal() {
const { updateBlockListSettings } = useDispatch( blockEditorStore );

useEffect( () => {
/**
* This shouldn't be needed but unfortunatley
miminari marked this conversation as resolved.
Show resolved Hide resolved
* 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 (
<div>
mirka marked this conversation as resolved.
Show resolved Hide resolved
<Toolbar label="Block Mover">
<BlockMover
clientIds={
blocks.length
? [ blocks[ 1 ].innerBlocks[ 1 ].clientId ]
: []
}
/>
</Toolbar>
</div>
);
}

const meta: Meta< typeof BlockMover > = {
title: 'BlockEditor/BlockMover',
component: BlockMover,
parameters: {
controls: { expanded: true },
docs: { canvas: { sourceState: 'shown' } },
},
};
export default meta;

const Template: StoryFn< typeof BlockMover > = ( props ) => {
return (
<Provider>
mirka marked this conversation as resolved.
Show resolved Hide resolved
<div>
<Toolbar label="Block Mover">
<BlockMover
{ ...props }
clientIds={
blocks.length
? [ blocks[ 1 ].innerBlocks[ 1 ].clientId ]
: []
}
/>
</Toolbar>
</div>
</Provider>
);
};

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,
+	},
 };

Default.args = {
clientIds: [
blocks.length ? [ 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: StoryFn< typeof BlockMover > = ( props ) => {
return (
<Provider>
<BlockMoverStoryHorizontal { ...props } />
</Provider>
);
};
Horizontal.args = {
clientIds: [
blocks.length ? [ blocks[ 1 ].innerBlocks[ 1 ].clientId ] : [],
],
};
Horizontal.parameters = {
docs: { canvas: { sourceState: 'hidden' } },
};

/**
* You can hide the drag handle by `hideDragHandle` attribute.
*/
export const HideDragHandle: StoryFn< typeof BlockMover > = ( props ) => {
return (
<Provider>
<div>
<Toolbar label="Block Mover">
<BlockMover
{ ...props }
clientIds={
blocks.length
? [ blocks[ 1 ].innerBlocks[ 1 ].clientId ]
: []
}
hideDragHandle
/>
</Toolbar>
</div>
</Provider>
);
};
HideDragHandle.args = {
...Default.args,
hideDragHandle: true,
};
Loading