-
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
Possibility to extend any block settings from the plugin? #2474
Comments
Extensibility is an unsolved problem right now, there are several explorations but nothing settled yet:
|
Just following this. This would essentially allow me to deprecate both a meta box and shortcode in one plugin in exchange for something far cooler and more powerful for users. |
In considering #2507, I've come to realize where we currently have shared functionality with ad-hoc implementations (generated class names, custom class names, anchor) would be a good opportunity for proving a pattern of extensibility. For example, rather than injecting classes into a block from the serializer, we could consider the class names behavior an extension or mixin of the block. There are a few variations and considerations here:
I think there's an option to special-case specific built-in extensions such as the class names and anchors behavior we already have. As explored in #2507, these could live on the block definition itself under a Externally, how could this look? We could take an object-oriented inheritance pattern of extending a specific block. Block types are not proper JavaScript // wp.blocks.extendBlocks( ( originalBlock ) => {
wp.blocks.extendBlock( 'core/image', ( originalBlock ) => {
attributes: {
className: {
type: 'string',
},
},
edit( props ) {
const element = originalBlock.save( props );
const { attributes, setAttributes } = props;
const { className } = attributes;
return [
element,
<InspectorControls>
<TextControl
label={ __( 'Additional CSS Class' ) }
value={ className }
onChange={ ( nextClassName ) => {
setAttributes( { className: nextClassName } );
} }
/>
</InspectorControls>
];
},
save( props ) {
const element = originalBlock.save( props );
const { className } = props.attributes;
return cloneElement( element, {
className: [ element.props.className || '', className ].join( ' ' ),
} );
},
} ); The idea here being:
For a less object-oriented approach, another alternative could be to consider a middleware / filters type model, where an extension could hook before or after particular events in the lifecycle of a block to override the default behavior. Some inspiration and prior art here could be:
Filtering a block prior to registration: addFilter( 'blocks.register', ( blockType ) => {
if ( ! includes( blockType.supports, 'className' ) {
return blockType;
}
return {
...blockType,
attributes: {
...blockType.attributes,
className: {
type: 'string',
},
},
edit( props ) {
const element = blockType.save( props );
const { attributes, setAttributes } = props;
const { className } = attributes;
return [
element,
<InspectorControls>
<TextControl
label={ __( 'Additional CSS Class' ) }
value={ className }
onChange={ ( nextClassName ) => {
setAttributes( { className: nextClassName } );
} }
/>
</InspectorControls>
];
},
save( props ) {
const element = blockType.save( props );
const { className } = props.attributes;
return cloneElement( element, {
className: [ element.props.className || '', className ].join( ' ' ),
} );
},
};
} ); More granular, adding props to a block before serializing: (Note: Unclear how we detect support for or maintain the value of the addFilter( 'blocks.serialize.props', ( props, block ) => {
const { className } = block.attributes;
if ( className ) {
props = { ...props, className };
}
return props;
} ); |
Hooks/filters like behaviour is clear for most of WP developers. |
@aduth any new ideas or movements on blocks extensibility? I have a few plugin ideas in mind and all of them can't be implemented because we have no access to the already defined blocks from the outside. If extensibility is a big and hard task can we at least start from the Additional Custom Class for every default block? Do you have any plans to solve block extensibility issues before the editor release or it's a task for later (after Gutenberg merge)? Thank you very much! |
@lumberman My understanding is that improved extensibility is still aimed to be included in an initial Gutenberg release, but must be balanced against priority of the basic content authoring experience. Internally, we still see a need for a consolidated "block supports" approach, recently reiterated in #3037. Since my previous comment, the JavaScript hooks package has been published to npm and is seeing continued iterations at WordPress/packages#40. Hooks are in need of a proven real-world use-case and these block supports be a good near-term opportunity to explore. |
Following this one too :) |
Following |
I'm wondering if you have in plans anything like filters or hooks we have in php?
For example I want to extend any block settings with my custom dropdown selector that will show/hide a block from mobile. This dropdown will add/remove additional class to the block classes. Is there a way to do that?
Thanks for your work.
The text was updated successfully, but these errors were encountered: