-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Storybook: Add stories for AlignmentToolbar and AlignmentControl components #67046
Merged
mirka
merged 6 commits into
WordPress:trunk
from
miminari:add/alignment-control-stories
Nov 27, 2024
Merged
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
4ad2b0c
Add stories for AlignmentToolbar and AlignmentControl components
miminari 462e54a
Remove unneccesery args
miminari f4b16f5
package.json: remove packageManager entry
miminari 154adaa
Rename the story file and remove unnecessary args from AlignmentContr…
miminari de208e2
Update packages/block-editor/src/components/alignment-control/stories…
miminari 628eac7
Add render function
miminari File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
47 changes: 47 additions & 0 deletions
47
packages/block-editor/src/components/alignment-control/stories/aliginment-toolbar.story.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useState } from '@wordpress/element'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { AlignmentToolbar } from '..'; | ||
|
||
/** | ||
* The `AlignmentToolbar` component renders a dropdown menu that displays alignment options for the selected block in `Toolbar`. | ||
*/ | ||
const meta = { | ||
title: 'BlockEditor/AlignmentToolbar', | ||
component: AlignmentToolbar, | ||
argTypes: { | ||
value: { | ||
control: { type: null }, | ||
defaultValue: 'undefined', | ||
description: 'The current value of the alignment setting.', | ||
}, | ||
onChange: { | ||
action: 'onChange', | ||
control: { type: null }, | ||
description: | ||
"A callback function invoked when the toolbar's alignment value is changed via an interaction with any of the toolbar's buttons. Called with the new alignment value (ie: `left`, `center`, `right`, `undefined`) as the only argument.", | ||
}, | ||
}, | ||
}; | ||
export default meta; | ||
|
||
const Template = ( { onChange, ...args } ) => { | ||
const [ value, setValue ] = useState(); | ||
return ( | ||
<AlignmentToolbar | ||
{ ...args } | ||
onChange={ ( ...changeArgs ) => { | ||
onChange( ...changeArgs ); | ||
setValue( ...changeArgs ); | ||
} } | ||
value={ value } | ||
/> | ||
); | ||
}; | ||
|
||
export const Default = Template.bind(); | ||
51 changes: 51 additions & 0 deletions
51
packages/block-editor/src/components/alignment-control/stories/index.story.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useState } from '@wordpress/element'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { AlignmentControl } from '../'; | ||
|
||
/** | ||
* The `AlignmentControl` component renders a dropdown menu that displays alignment options for the selected block. | ||
* | ||
* This component is mostly used for blocks that display text, such as Heading, Paragraph, Post Author, Post Comments, Verse, Quote, Post Title, etc... And the available alignment options are `left`, `center` or `right` alignment. | ||
* | ||
* If you want to use the alignment control in a toolbar, you should use the `AlignmentToolbar` component instead. | ||
*/ | ||
const meta = { | ||
title: 'BlockEditor/AlignmentControl', | ||
component: AlignmentControl, | ||
argTypes: { | ||
value: { | ||
control: { type: null }, | ||
defaultValue: 'undefined', | ||
description: 'The current value of the alignment setting.', | ||
}, | ||
onChange: { | ||
action: 'onChange', | ||
control: { type: null }, | ||
description: | ||
"A callback function invoked when the toolbar's alignment value is changed via an interaction with any of the toolbar's buttons. Called with the new alignment value (ie: `left`, `center`, `right`, `undefined`) as the only argument.", | ||
}, | ||
}, | ||
}; | ||
export default meta; | ||
|
||
const Template = ( { onChange, ...args } ) => { | ||
const [ value, setValue ] = useState(); | ||
return ( | ||
<AlignmentControl | ||
{ ...args } | ||
onChange={ ( ...changeArgs ) => { | ||
onChange( ...changeArgs ); | ||
setValue( ...changeArgs ); | ||
} } | ||
value={ value } | ||
/> | ||
); | ||
}; | ||
|
||
export const Default = Template.bind(); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is fine, but just as a tip for easier authoring and maintenance, it will be a lot simpler to showcase the component in uncontrolled mode if the component supports it. It will literally be a one-liner 😄
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wow, so easy! And more ideal. Thanks for letting me know!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But then we can't reproduce the
onChange
moving on Storybook, but that's not a problem, right? Or I guess I might miss something...There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
onChange
should still be observable in the Actions panel as before. Is it not working in your environment? The only reason we need to merge the incomingonChange
function is when it's used in controlled mode and you need to call thesetValue
as well.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In my environment, the story of the
AlignmentToolbar
component does not work properly. TheonChange
event is fired, but the text alignment is not updated. Does this component not support uncontrolled mode?d5e018c76f3413012303cbe0189ea76b.mp4
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah right, my bad! I guess it doesn't support uncontrolled. I'd still recommend using CSF 3 format (instead of the
Template.bind()
) for new stories though:There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you two! I updated as you wrote.