Skip to content
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 unregister a block style variation #10654

Merged
merged 8 commits into from
Oct 27, 2018

Conversation

ocean90
Copy link
Member

@ocean90 ocean90 commented Oct 16, 2018

This PR is a follow-up to #7997 and adds a helper function to remove an existing block style variation.

Related: #7551

* @param {string} styleVariationName Name of class applied to the block.
*/
export const unregisterBlockStyle = ( blockName, styleVariationName ) => {
addFilter( 'blocks.registerBlockType', `${ blockName }/${ styleVariationName }/unregister`, ( settings, name ) => {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we have to rethink the namespace for this and registerBlockStyle.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FWIW, I'd personally like to see if we could avoid the filter altogether, and instead make style a proper data type of its own than force-fit it into the block definition.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be nice to avoid using the filter in the first place.

In regards to the propose PR, why not use buil-int removeFilter instead?

removeFilter( 'blocks.registerBlockType', `${ blockName }/${ styleVariation.name }` );

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gziolo Because you can't remove built-in style variations with this.

@mtias mtias modified the milestones: API Freeze, 4.2 - API freeze Oct 16, 2018
@mtias mtias added the [Feature] Block API API that allows to express the block paradigm. label Oct 16, 2018

return {
...settings,
styles: remove( get( settings, [ 'styles' ], [] ), ( style ) => styleVariationName !== style.name ),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove is mutative, which we should avoid. Consider reject or filter instead:

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reading this, it seems like it should be the opposite equality check, removing those variations where the style name matches the function argument.

* @param {string} styleVariationName Name of class applied to the block.
*/
export const unregisterBlockStyle = ( blockName, styleVariationName ) => {
addFilter( 'blocks.registerBlockType', `${ blockName }/${ styleVariationName }/unregister`, ( settings, name ) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FWIW, I'd personally like to see if we could avoid the filter altogether, and instead make style a proper data type of its own than force-fit it into the block definition.

packages/blocks/src/api/registration.js Show resolved Hide resolved
@swissspidy
Copy link
Member

A paragraph or two in https://github.com/WordPress/gutenberg/blob/377c222ebaecdab3771dfd3e66f940aa1fe94954/docs/extensibility/extending-blocks.md about this function can't hurt.

Copy link
Member

@aduth aduth left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On the documentation front, we should consider also adding a note of this new addition to packages/blocks/CHANGELOG.md.

https://github.com/WordPress/gutenberg/blob/master/CONTRIBUTING.md#maintaining-changelogs

Otherwise, code-wise, this is looking good, with notes for future iterations.

packages/blocks/src/api/test/registration.js Show resolved Hide resolved
@WordPress WordPress deleted a comment from gziolo Oct 22, 2018
Copy link
Contributor

@youknowriad youknowriad left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 👍 Feel free to rebase and merge.

@youknowriad youknowriad merged commit aec9408 into master Oct 27, 2018
@youknowriad youknowriad deleted the add/unregister-style-variations-api branch October 27, 2018 13:07
@mtias
Copy link
Member

mtias commented Oct 30, 2018

Thanks for the useful addition!

@designsimply designsimply added the [Feature] Theme Style Variations Related to style variations provided by block themes label Dec 17, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Feature] Block API API that allows to express the block paradigm. [Feature] Theme Style Variations Related to style variations provided by block themes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants