-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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 block nesting governance for theme.json #43796
Conversation
Add hardcoded nesting rules
Add `useSetting()` hook tests
This works by baking in all the possible blocks, under each block when constructing the settings.block schema. It works one layer deep at the moment.
Add useSetting multiple level nesting support, specificity, and tests
…ested blocks can be supported
…port Add support for parsing nested block settings from the theme.json
…k names that we already know
…pport-multi-depth-blocks
Fix useSetting in double-nested parent blocks
…d-block-settings Add new e2e test for nested block settings
…-code Clean up the code a bit more
Remove the extra since tags
👋 Thanks for your first Pull Request and for helping build the future of Gutenberg and WordPress, @alecgeatches! In case you missed it, we'd love to have you join us in our Slack community, where we hold regularly weekly meetings open to anyone to coordinate with each other. If you want to learn more about WordPress development in general, check out the Core Handbook full of helpful information. |
See related |
cc @jorgefilipecosta and @mtias as this seems to greatly relate to the patterns as sections work. In particular, this PR #40318 From the FSE Outreach Program perspective, this would be a welcome area of control for agency types based on feedback I've heard and would fit in nicely with curation options as I know you all are well aware. |
Per feedback from @mcsf I'm going to close this issue. @ingeniumed has released a much smaller PR in #45089 that will allow us to address nesting governance within a plugin. Thank you! |
What?
Combined PR from @ingeniumed and @alecgeatches.
We have added the ability to govern individual block settings via
theme.json
when they are nested inside each other. This means a second set of settings can be applied to blocks based on the context in which it is used.Why?
@dabowman has a great explanation and examples of the reasoning behind these changes in #41547. We used this issue and our discussions with David as the basis for this PR. In short, we'd like to extend Gutenberg to support governance options used by design systems.
How?
We've made changes to the way
theme.json
parsing and validation is handled, and extendeduseSetting
to take advantage of nested block settings:Switch the validation of
theme.json → settings.blocks
to recurse through each block and validate inner nested blocks rather than removing them. It now validates each block individually against the settings schema, and then assembles it all together. After parsing, all existing code continues to work but passes nested block settings relative to their parent into the block editor.Switch the parsing/generation of the
get_setting_nodes()
function to recurse through each block, and keep the parent in mind when generating selectors and paths to the respective block. This way, the settings output takes into account the nested blocks, appliesPRESETS_METADATA
transforms to sub-blocks correctly, and doesn't just process the top level parents.useSetting
has been extended to apply settings in the block editor for nested blocks. This works by processing a block's parents in the block editor, finding nested settings, and applying those to the current block onuseSetting
calls. A few notes on the implementation:useSetting
based on the number of nested parents, but this impact should be small. The internal functiongetNestedSetting
runs through each of a block's parents and only recurses further if relevant nested settings has been added totheme.json
. For normal blocks nested a few times, the performance impact will be the same as the existing__experimentalSettings
code that checks a block's parents.useSetting
have been added inpackages/block-editor/src/components/use-setting/test/index.js
. These tests document the current behavior and may be helpful in understanding the implementation.Testing Instructions
A theme.json resolver test has been added in
test_theme_with_nested_block_settings()
which uses this exampletheme.json
. It can be run via the standard PHPUnit tests:An end-to-end test has been added here to test nested options from a theme show up correctly in the block editor. This can be run via e2e tests:
The test suite for
useSetting
can be run via:For manual testing, the
theme.json
linked above and in the Demo section below can be used as a starting point. Generally, you should be able to take any set of regularsettings.block
settings, add them nested below another block, and those settings should take priority in the block editor when a block is nested in the same way.Demo
Here is an example of a
core/heading
block with different color palette options at different levels of nesting:core/heading
core/quote
→core/heading
core/quote
→core/media-text
→core/heading
Screen.Recording.2022-09-01.at.15.24.45.mov
A pared down version of the theme.json used for the demo can be found here.