-
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
Define Nav block allowed inner blocks via block supports mechanism #32426
Conversation
Size Change: +781 B (0%) Total Size: 1.03 MB
ℹ️ View Unchanged
|
This was designed to define which items should show in the inserter. |
316062f
to
20e0a7b
Compare
@@ -72,7 +72,15 @@ | |||
"__experimentalFontFamily": true, | |||
"__experimentalTextDecoration": true | |||
}, | |||
"color": true | |||
"color": true, | |||
"__experimentalAllowedBlocks": [ |
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.
I wonder if it should be part of supports
or just a normal block property. 🤔
@@ -84,7 +77,7 @@ function Navigation( { | |||
className: 'wp-block-navigation__container', | |||
}, | |||
{ | |||
allowedBlocks: ALLOWED_BLOCKS, | |||
allowedBlocks, |
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.
If it's specified in the block.json, my feeling is a block developer shouldn't have to add some logic to the block edit function. Is there a way to make it work without it having to pass it through as a prop?
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.
Yeh I mean this implementation is pretty naive and rough. More a POC really.
That said, if we wanted to pursue this API we could intercept the allowedBlocks
here in InnerBlock
and get the block support to determine the value.
gutenberg/packages/block-editor/src/components/inner-blocks/index.js
Lines 37 to 41 in f3ad70f
function UncontrolledInnerBlocks( props ) { | |
const { | |
clientId, | |
allowedBlocks, | |
template, |
I'm unsure how we'd determine priority between
allowedBlocks
passed toInnerBlock
directly as a prop.- the
allowedBlocks
setting inblock.json
.
I guess always honour #1
and expect block developers not to use both?
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.
I imagine block.json could override the prop, with the prop deprecated.
Description
In #30007 we are discussing how best to enable/remove features of the Nav block to make it easier for the Nav Editor screen to configure the block (via some unknown mechanic).
This PR is a PoC to explore if/how it's possible to customise the blocks that are allowed as child blocks of the Nav block using the block supports mechanic.
This area is largely unknown to me at this stage so the PR will evolve as my understanding grows.
Currently, it uses a new field called
__experimentalAllowedBlocks
inblock.json
to define which blocks are allowed as child blocks. This is then used in the block'sedit.js
to define which blocks are passed asallowedBlocks
to<InnerBlocks>
.Problems
parent
inblock.json
causes theallowedBlocks
prop of<InnerBlocks>
to be ignored. Seecore/navigation-link
andcore/home-link
which both specifycore/navigation
asparent
and are therefore always shown in the inspector.How has this been tested?
packages/block-library/src/navigation/block.json
.__experimentalAllowedBlocks
field and note the available blocks.block.json
remove a block from the list (which does not haveparent
defined in its ownblock.json
. Why? Because currently defining aparent
will overide theallowedBlocks
API ofInnerBlocks
which will mean it will always appear no matter what blocks we list here). I'd recommend removingcore/social-links
as this does not define aparent
field in itsblock.json
.Note if you try removing other blocks you will find they remain in the inserter. To make this work you'll need to find the block in question (eg:
core/navigation-link
) and find itsblock.json
file and remove theparent
field entry. Once you reload you'll find the__experimentalAllowedBlocks
API is now working as expected.Screenshots
Types of changes
New feature (non-breaking change which adds functionality)
Checklist:
*.native.js
files for terms that need renaming or removal).