Skip to content
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

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
10 changes: 9 additions & 1 deletion packages/block-library/src/navigation/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,15 @@
"__experimentalFontFamily": true,
"__experimentalTextDecoration": true
},
"color": true
"color": true,
"__experimentalAllowedBlocks": [
Copy link
Contributor

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. 🤔

"core/navigation-link",
"core/search",
"core/social-links",
"core/page-list",
"core/spacer",
"core/home-link"
]
},
"editorStyle": "wp-block-navigation-editor",
"style": "wp-block-navigation"
Expand Down
20 changes: 9 additions & 11 deletions packages/block-library/src/navigation/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
useBlockProps,
store as blockEditorStore,
} from '@wordpress/block-editor';
import { getBlockSupport } from '@wordpress/blocks';
import { useDispatch, withSelect, withDispatch } from '@wordpress/data';
import { PanelBody, ToggleControl, ToolbarGroup } from '@wordpress/components';
import { compose } from '@wordpress/compose';
Expand All @@ -29,15 +30,6 @@ import NavigationPlaceholder from './placeholder';
import PlaceholderPreview from './placeholder-preview';
import ResponsiveWrapper from './responsive-wrapper';

const ALLOWED_BLOCKS = [
'core/navigation-link',
'core/search',
'core/social-links',
'core/page-list',
'core/spacer',
'core/home-link',
];

const LAYOUT = {
type: 'default',
alignments: [],
Expand All @@ -55,6 +47,7 @@ function Navigation( {
className,
hasSubmenuIndicatorSetting = true,
hasItemJustificationControls = true,
allowedBlocks,
} ) {
const [ isPlaceholderShown, setIsPlaceholderShown ] = useState(
! hasExistingNavItems
Expand Down Expand Up @@ -84,7 +77,7 @@ function Navigation( {
className: 'wp-block-navigation__container',
},
{
allowedBlocks: ALLOWED_BLOCKS,
allowedBlocks,
Copy link
Contributor

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?

Copy link
Contributor Author

@getdave getdave Jun 10, 2021

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.

function UncontrolledInnerBlocks( props ) {
const {
clientId,
allowedBlocks,
template,

I'm unsure how we'd determine priority between

  1. allowedBlocks passed to InnerBlock directly as a prop.
  2. the allowedBlocks setting in block.json.

I guess always honour #1 and expect block developers not to use both?

Copy link
Contributor

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.

orientation: attributes.orientation || 'horizontal',
renderAppender:
( isImmediateParentOfSelectedBlock &&
Expand Down Expand Up @@ -183,6 +176,11 @@ function Navigation( {

export default compose( [
withSelect( ( select, { clientId } ) => {
const allowedBlocks = getBlockSupport(
getdave marked this conversation as resolved.
Show resolved Hide resolved
'core/navigation',
'__experimentalAllowedBlocks'
);

const innerBlocks = select( blockEditorStore ).getBlocks( clientId );
const {
getClientIdsOfDescendants,
Expand All @@ -202,7 +200,7 @@ export default compose( [
isImmediateParentOfSelectedBlock,
selectedBlockHasDescendants,
hasExistingNavItems: !! innerBlocks.length,

allowedBlocks,
// This prop is already available but computing it here ensures it's
// fresh compared to isImmediateParentOfSelectedBlock
isSelected: selectedBlockId === clientId,
Expand Down