Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
allowedBlocks list setting is not deprecated, can be used for dynamic…
… behavior
  • Loading branch information
jsnajdr committed Jan 29, 2024
commit 198bc749696e47739099df863c04c8c884c013ff
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,17 @@ registerBlockType( 'gutenberg-examples/example-06', {
} );
```

## Allowed blocks (deprecated)
## Allowed blocks

Using the `allowedBlocks` property, you can define the set of blocks allowed in your `InnerBlocks`. This restricts the blocks that can be included only to those listed, all other blocks will not show in the inserter.
Using the `allowedBlocks` prop, you can further limit, in addition to the `children` field in `block.json`, which blocks can be inserted as direct descendants of this block. It is useful to determine the list of allowed blocks dynamically, individually for each block. For example, determined by a block attribute:

```js
const ALLOWED_BLOCKS = [ 'core/image', 'core/paragraph' ];
const { allowedBlocks } = attributes;
//...
<InnerBlocks allowedBlocks={ ALLOWED_BLOCKS } />;
<InnerBlocks allowedBlocks={ allowedBlocks } />;
```

This prop is now deprecated in favor of the [`children` block setting](#defining-a-children-block-relationship).
If the list of allowed blocks is always the same, prefer the [`children` block setting](#defining-a-children-block-relationship) instead.

## Orientation

Expand Down
14 changes: 7 additions & 7 deletions packages/block-editor/src/store/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -1565,20 +1565,20 @@ const canInsertBlockTypeUnmemoized = (

const parentName = getBlockName( state, rootClientId );
const parentBlockType = getBlockType( parentName );

// Look at the `blockType.children` field to determine whether this is an allowed child block.
const parentAllowedChildBlocks = parentBlockType?.children;
let hasParentAllowedBlock = checkAllowList(
parentAllowedChildBlocks,
blockName
);

// If decision can't be made with `blockType.children` (i.e., it's not there)
// decide using the `allowedBlocks` block list setting (as a legacy fallback).
if ( hasParentAllowedBlock === null ) {
// The `allowedBlocks` block list setting can further limit which blocks are allowed children.
if ( hasParentAllowedBlock !== false ) {
const parentAllowedBlocks = parentBlockListSettings?.allowedBlocks;
hasParentAllowedBlock = checkAllowList(
parentAllowedBlocks,
blockName
);
if ( checkAllowList( parentAllowedBlocks, blockName ) === false ) {
hasParentAllowedBlock = false;
}
}

const blockAllowedParentBlocks = blockType.parent;
Expand Down