-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
Table of Contents: Try maintaining block example attributes #65549
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. |
const blockType = useSelect( | ||
( select ) => | ||
select( blocksStore ).getBlockType( 'core/table-of-contents' ), | ||
[] | ||
); | ||
const isExample = | ||
JSON.stringify( blockType.example.attributes.headings ) === | ||
JSON.stringify( headings ); |
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.
I struggled to find a better option to avoid the useObserveHeadings
hook overriding arbitrary block example attributes. More than happy to hear better approaches if you have them 🙏
Size Change: +74 B (0%) Total Size: 1.77 MB
ℹ️ View Unchanged
|
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.
Might be seeing things, but adding innerBlocks to the example works for me
@@ -19,6 +19,28 @@ export const settings = { | |||
icon, | |||
edit, | |||
save, | |||
example: { |
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.
Could this be moved to block.json?
I think the example check above might be unnecessary if we use innerBlocks
(?)
"example": {
"innerBlocks": [
{
"name": "core/heading",
"attributes": {
"level": 2,
"content": "Heading"
}
},
{
"name": "core/heading",
"attributes": {
"level": 3,
"content": "Subheading"
}
},
{
"name": "core/heading",
"attributes": {
"level": 2,
"content": "Heading"
}
},
{
"name": "core/heading",
"attributes": {
"level": 3,
"content": "Subheading"
}
}
],
"attributes": {
"headings": [
{
"content": "Heading",
"level": 2
},
{
"content": "Subheading",
"level": 3
},
{
"content": "Heading",
"level": 2
},
{
"content": "Subheading",
"level": 3
}
]
}
},
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.
Yep, that works, thanks 🙇
I take it that the definition of the innerBlocks
and them being rendered initially for the example means there are heading blocks in the registry. That then leads the useObserveHeadings
hook to find those before updating the block's headings
attribute.
If I remove the attributes.headings
value, you can see the block example start in the placeholder state then update to match the provided inner blocks.
I've pushed a commit using the suggested approach.
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.
I take it that the definition of the innerBlocks and them being rendered initially for the example means there are heading blocks in the registry. That then leads the useObserveHeadings hook to find those before updating the block's headings attribute.
That sounds legit. My brain stops functioning when I see something working. 😄
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.
Co-authored-by: aaronrobertshaw <[email protected]> Co-authored-by: ramonjd <[email protected]>
Part of: #64707
Related: #65430
What?
Adds block example for the Table of Contents.
Why?
The Style Book is being iterated on and part of that effort is to ensure the desired blocks are shown there. Currently, the Table of Contents blocks only shows a placeholder which isn't a great experience when the point of the Style Book is to see how it would actually look.
How?
Defines a block example containing both a
headings
attribute value and corresponding inner blocks.The
innerBlocks
definition means there are heading blocks matching theheadings
attribute so theuseObserveHeadings
hook doesn't override the example attributes with an empty set.Testing Instructions
Screenshots or screencast