Storybook: Add BlockCanvas Component#68589
Storybook: Add BlockCanvas Component#68589Rishit30G wants to merge 4 commits intoWordPress:trunkfrom
Conversation
|
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 If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message. To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
| styles: { | ||
| border: '1px solid #ccc', | ||
| backgroundColor: '#f9f9f9', | ||
| }, |
There was a problem hiding this comment.
styles is an array of objects and the actual CSS is a string. A basic example is the variable used in the playground stories.
As for the type, I'm not sure how thoroughly it can/should be documented so maybe for now Array suffices. The full type would be something like: { css?: string; assets?: string; isGlobalStyles?: boolean; __unstableType: string; }[]. Example usage is here and you can see how it’s consumed in EditorStyles
There was a problem hiding this comment.
Thanks for sharing the feedback and resources @stokesman
Here is the updated code snippet:
meta object:
styles: {
control: 'object',
description: 'The styles to apply to the canvas.',
table: {
type: {
summary:
'[{ css?: string; assets?: string; isGlobalStyles?: boolean; __unstableType: string; }]',
},
},
},Default object:
export const Default = {
args: {
height: '100px',
styles: [ { css: `body{font-size: 16px;}` } ],
children: <BlockList />,
},
render: function Template( args ) {
return (
<BlockCanvas { ...args } />
);
},
};Screenshot of the storybook
Let me know if this looks good, I'll update the PR accordingly 🙇🏻
There was a problem hiding this comment.
Thanks! The change to Default looks good to me.
For the styles prop’s type summary, I'm not sure because I haven’t see guidance on how specific it should be. Most other examples I have seen are pretty generic. So perhaps just Array, or Array<{}>. If it’s better to fully specify the type then the last update you posted isn’t quite right—I’ll take some responsibility—and it should be:
{ css?: string; assets?: string; isGlobalStyles?: boolean; __unstableType?: string; }[]
or alternatively using the Array generic:
Array<{ css?: string; assets?: string; isGlobalStyles?: boolean; __unstableType?: string; }>
I’ll ping @WordPress/gutenberg-components and @t-hamano in case they know the guidance around this (and if not maybe we can establish it). One argument in favor of a more general type here would be easier maintenance I think 🤷♂️. It’s also notable EditorStyles is where the type is actually important as this component just passes it through.
There was a problem hiding this comment.
Got it, thanks for sharing the updates and feedback! 🙇🏻
I have updated the PR ✅
There was a problem hiding this comment.
There are no strict rules for now 😅 Personally, I think a simple array type is fine, following the JSDoc here:
gutenberg/packages/block-editor/src/components/block-canvas/index.js
Lines 124 to 128 in 2abe6fa
There was a problem hiding this comment.
From #67165 (comment):
I'd like to emphasize that we should write the stories in a way that keeps maintenance cost at a minimum.
I think from that general guidance the choice here should be to use the simple Array type.
t-hamano
left a comment
There was a problem hiding this comment.
Thanks for the PR!
The BlockCanvas component doesn't draw anything visually by itself, so exposing it to Storybook may not be useful.
Generally, this component is expected to render blocks based on the data from a provider. Therefore, how about wrapping it in a provider and managing the blocks as a state?
Example (Quotation from this document):
function MyEditorComponent() {
const [ blocks, updateBlocks ] = useState( [] );
return (
<BlockEditorProvider
value={ blocks }
onInput={ ( blocks ) => updateBlocks( blocks ) }
onChange={ ( blocks ) => updateBlocks( blocks ) }
>
<BlockCanvas />
</BlockEditorProvider>
);
}| args: { | ||
| height: '100px', | ||
| styles: [ { css: `body{font-size: 16px;}` } ], | ||
| children: <BlockList />, | ||
| }, |
There was a problem hiding this comment.
I don't think these args are necessary, as this component should work fine with the default props.
|
Thanks for the feedback and resources @t-hamano! 🙇🏻 Screen.Recording.2025-01-28.at.1.28.36.PM.mov |
|
@Rishit30G thanks for the update! In the I think this is because the On the other hand, in a story similar to this PR (Block Editor > Box), the Enter key works correctly. I don't know why this is happening 🤷 I haven't been able to find a solution yet, so I'll add the "Needs Technical Feedback" label. |
|
Thanks for testing it out @t-hamano Even I could replicate this issue, I tried the same logic from the |
| <BlockEditorProvider | ||
| value={ blocks } | ||
| onInput={ ( newBlocks ) => updateBlocks( newBlocks ) } | ||
| onChange={ ( newBlocks ) => updateBlocks( newBlocks ) } | ||
| > | ||
| <BlockCanvas { ...args } /> | ||
| </BlockEditorProvider> |
There was a problem hiding this comment.
Adding the provider seems necessary yet I believe it would be considered more appropriate to do so by way of a decorator in order to keep the story itself focused on BlockCanvas. For example, how it was done is in the PR for the BlockTitle story.
A difference with that story is its source is outside the block-editor package and so it imports from that package by name instead of the local path. It doesn’t seem like it should matter yet I tried changing the import here to |
|
Thanks for sharing the feedback @stokesman! 🙇🏻 I have updated the PR accordingly ✅
Yup, that's true, for time being I have updated the imports in the PR so that it doesn't break |


Part of #67165
What?
This PR adds Story for Block Canvas Component
Testing Instructions
Screenshots or screencast