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 8 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: 15 additions & 8 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 component allows moving blocks inside the editor using up and down buttons.

![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,22 @@ 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
### clientIds

Blocks IDs
The IDs of the blocks to move.

- Type: `Array`
- Required: Yes

### hideDragHandle

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

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

## Related components

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ 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
Expand All @@ -30,81 +31,113 @@ const blocks = [
] ),
];

function Provider( { children } ) {
const wrapperStyle = { margin: '24px', position: 'relative' };

return (
<div style={ wrapperStyle }>
<ExperimentalBlockEditorProvider value={ blocks }>
{ children }
</ExperimentalBlockEditorProvider>
</div>
);
}

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

useEffect( () => {
/**
* This shouldn't be needed but unfortunatley
* 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.
* to render the blocks to update the block settings in the state.
*/
updateBlockListSettings( blocks[ 1 ].clientId, {
orientation: 'horizontal',
} );
}, [] );

return (
<div>
<p>The mover by default is vertical</p>
<Toolbar label="Block Mover">
<BlockMover
clientIds={
blocks.length
? [ blocks[ 0 ].innerBlocks[ 1 ].clientId ]
: []
}
/>
</Toolbar>

<p style={ { marginTop: 36 } }>
But it can also accommodate horizontal blocks.
</p>
<Toolbar label="Block Mover">
<BlockMover
clientIds={
blocks.length
? [ blocks[ 1 ].innerBlocks[ 1 ].clientId ]
: []
}
/>
</Toolbar>

<p style={ { marginTop: 36 } }>We can also hide the drag handle.</p>
<Toolbar label="Block Mover">
<BlockMover
clientIds={
blocks.length
? [ blocks[ 1 ].innerBlocks[ 0 ].clientId ]
: []
}
hideDragHandle
/>
</Toolbar>
</div>
<BlockMover
clientIds={
blocks.length ? [ blocks[ 1 ].innerBlocks[ 1 ].clientId ] : []
}
/>
);
}

export default {
/**
* BlockMover component allows moving blocks inside the editor using up and down buttons.
*/
const meta = {
title: 'BlockEditor/BlockMover',
component: BlockMover,
parameters: {
docs: { canvas: { sourceState: 'shown' } },
},
decorators: [
( Story ) => (
<ExperimentalBlockEditorProvider value={ blocks }>
<Toolbar label="Block Mover">
<Story />
</Toolbar>
</ExperimentalBlockEditorProvider>
),
],
argTypes: {
clientIds: {
control: {
type: 'array',
},
description: 'The client IDs of the blocks to move.',
},
hideDragHandle: {
control: {
type: 'boolean',
},
description: 'If this property is true, the drag handle is hidden.',
},
},
};
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 ] : [],
],
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))

};

/**
* 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 ] : [],
],
};
Horizontal.parameters = {
docs: { canvas: { sourceState: 'hidden' } },
};

export const _default = () => {
/**
* You can hide the drag handle by `hideDragHandle` attribute.
*/
export const HideDragHandle = ( props ) => {
return (
<Provider>
<BlockMoverStory />
</Provider>
<BlockMover
{ ...props }
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.

/>
);
};
HideDragHandle.args = {
...Default.args,
hideDragHandle: true,
};
Loading