-
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
Add a function to register a block style variation #7997
Conversation
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.
Code looks good! But I'm getting wp.blocks.registerBlockStyleVariation is not a function
when I try to test this. I think we need to export the new function from blocks/api/index.js
.
blocks/api/registration.js
Outdated
@@ -330,3 +330,25 @@ export const getChildBlockNames = ( blockName ) => { | |||
export const hasChildBlocks = ( blockName ) => { | |||
return select( 'core/blocks' ).hasChildBlocks( blockName ); | |||
}; | |||
|
|||
/** | |||
* Registers a new block style variation for the given block |
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.
Missing a period (.
) at the end of the sentence here.
blocks/api/registration.js
Outdated
* Registers a new block style variation for the given block | ||
* | ||
* @param {string} blockName Block type name. | ||
* @param {Object} styleVariation Block style variation. |
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.
Should we document what properties this object has?
@param {Object} styleVariation Object containing `name` which is the class name applied to the block and `label` which identifies the variation to the user.
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.
Yes we should 😅
blocks/api/registration.js
Outdated
* @param {string} blockName Block type name. | ||
* @param {Object} styleVariation Block style variation. | ||
*/ | ||
export const registerBlockStyleVariation = ( blockName, styleVariation ) => { |
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.
registerBlockStyle
seems simpler to me
blocks/api/registration.js
Outdated
/** | ||
* Registers a new block style variation for the given block | ||
* | ||
* @param {string} blockName Block type name. |
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.
Block type name might actually be slightly confusing comment here because it implies blocks can have types and that’s what this is? That’s how I read it. It would be better to just say “name of block”. Maybe even provide an example like “Name of block (example: “latest-posts”).”
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.
Works for me. 👍
blocks/api/registration.js
Outdated
* @param {Object} styleVariation Object containing `name` which is the class name applied to the block and `label` which identifies the variation to the user. | ||
*/ | ||
export const registerBlockStyle = ( blockName, styleVariation ) => { | ||
addFilter( 'blocks.registerBlockType', blockName + '/' + styleVariation.name, ( settings, name ) => { |
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.
Nitpick: We could use template strings here.
We should add this to the docs, see #8145 |
2431e02
to
bd0c215
Compare
This PR adds a helper function to easily add a new block style variation as this is a common extensibility pattern.
Testing instructions
wp.blocks. registerBlockStyle( 'core/paragraph', { name: 'big', label: 'Big Style' })
registerCoreBlocks
)Related #7551