Separator Block: Apply default block variation when inserting via --- shortcut#77135
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. |
| // Check for default variation to preserve attributes. | ||
| const blockType = getBlockType( 'core/separator' ); |
There was a problem hiding this comment.
You could use the getBlockVariations method here. See packages/blocks/src/api/registration.js.
ramonjd
left a comment
There was a problem hiding this comment.
This change makes sense to me. Thank you!
Let a couple of comments, then I think it looks good to go 🙇🏻
| const attributes = defaultVariation | ||
| ? defaultVariation.attributes | ||
| : {}; |
There was a problem hiding this comment.
Variations could omit attributes, so we may just use defaultVariation.attributes ?? {} in the call directly, e.g.,
createBlock( 'core/separator', defaultVariation.attributes ?? {} ),| createBlock( getDefaultBlockName() ), | ||
| ], | ||
| transform: () => { | ||
| // Check for default variation to preserve attributes. |
There was a problem hiding this comment.
| // Check for default variation to preserve attributes. | |
| // Check for default variation to apply default variation attributes. |
ramonjd
left a comment
There was a problem hiding this comment.
Appreciate the quick follow up! 🚀
What?
Closes #75259
Apply default block variation attributes when creating a Separator block via the
---markdown shortcut.Why?
When a default block variation is registered for the Separator block, it is correctly applied when inserting via the block inserter UI. But when inserting via the
---shortcut, it creates the Separator block with no attributes, so the default variation is never used and the block falls back to its hardcoded defaults.How?
Updated the
inputtransform to check the registered default variation for the Separator block before callingcreateBlock, and merge its attributes in.If no default variation is registered,
attributesfalls back to an empty object andcreateBlockbehaves exactly as before with no registered default variation.Testing Instructions
tagName: 'div'(could use the snippet from the issue)div---to insert a Separator. Before the fix it useshr, after the fix it correctly usesdiv.hrbehaviour.Screenshots or screencast