-
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
Establishing a common, 'official' method to allow blocks to be extended by themes in specific contexts #18892
Comments
We've now enhanced this to allow block extensions (optionally) to be conditional, for example on post type, taxonomy or metadata. To achieve this, instead of passing in an object when registering an extension, we can alternatively pass a function which will receive the This example disables colour and alignment features based on the current post type:
The main file
The npm package is updated. |
@andrewstaffell Not sure if you saw this post, but it seems very relevant to your proposal: #15450 seems like the other most relevant issue. |
BTW, this isn't often very effective. Often what happens is one issue gains a lot of attention, and other issues that are similar end up ignored (github's UI doesn't make it easy to combine issues on a similar topic into a sort of 'epic'). I'd recommend commenting on one of the existing issues if you can. That would also ensure those who're already involved in the conversation get to see your suggestion. Usually this issue would be closed as a duplicate, but I respect that amount of effort you went into with the description 😄 |
@talldan Thanks for the comments Dan, I've made a comment on the post now. |
@andrewstaffell, re-sharing comment from @youknowriad added to your comment under https://make.wordpress.org/core/2020/01/23/controlling-the-block-editor/:
In addition to that, I wanted to highlight that there seem to be 2 different cases for making block easier to customize. The one proposed in the post linked by @talldan addresses visual aspects and it should cover the use cases included in this issue as soon as it is granular enough to make it possible to let apply customizations on the block level. It's definitely on the table, and I plan to investigate it in the upcoming weeks. There is also an aspect to make the existing controls easier to customize when they are targeting the content creation (less visual), e.g. the number of columns, heading levels, etc. It's tracked in #15450. Thank you for sharing your use case, it definitely should help to drive the development efforts for the block editor features API that initially was meant to work only on the post type level. Do you think we can close this issue in favor of the two linked existing issues? Do I miss anything? |
Let's close and consolidate on a single issue. Once the API in place we can continue discussing specific keys in dedicated issues. |
There have already been some discussions about block extensibility (eg. #6023, #15450, #7931); the current issue is intended to be considered alongside those.
Blocks need to be agnostic to the theme they're used in; yet in some cases style or presentation options are only known to (or named by) the theme, and it is sometimes desirable to offer these as choices / controls within a block, possibly at a granular level affecting only specific elements.
Examples: a choice of (theme) font for a particular element within a block; a choice of (theme) colour for specific elements; a choice of photo filter for individual photos in a gallery.
The
editor.blockEdit
filter already allows us to pass additional parameters into a block'sedit()
function, so the underlying mechanism for this block-theme communication exists, but there doesn't seem to be a recommended / official method for it, so we've implemented a simple but versatile interface which we're proposing here for discussion.We're already using this technique effectively in our own blocks / themes, but if consensus were reached on an official method, block developers could start using it consistently to expose extensible contexts to themes, and would only need to document the name and formats of their extensions (instead of citing long-winded hooks to be copy-pasted); while theme developers would have a consistent, tidy and stable API to register all their block extensions in one place.
Our proposed method consists of just a few additional lines of JS (currently this is an npm package which we import into theme JS enqueued for the block editor) which provide:
registerBlockExtensions()
function for themes to register their extensions for specific blocksThis file (
fabrica-block-extensions/index.js
) looks like this:Example usage:
Register extensions in the theme:
Receive and act upon extensions in block:
This example shows an additional radio control allowing font selection for a specific element within the block, but it could be a dropdown, toolbar or any other UI device – it's completely up to the block. The key is that the
extensions
property sent to the edit function contains all the block's registered extensions.Of course it will fall to block developers to accurately and transparently document the contexts in which they are receptive to extensions, as well as the data format they expect in each case; similarly it falls to them to escape safely if data passed in is malformed (hence the conditional checks wrapping the
<RadioControl>
in the last example above). They should also escape gracefully if extension data is missing (which simply means a theme has opted not to pass in any extensions).Anyone interested in testing this can install the npm package
fabrica-block-extensions
.Looking forward to reading thoughts and feedback.
The text was updated successfully, but these errors were encountered: