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 / constrainNav block orientation via block supports #32596

Closed
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
3 changes: 2 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,8 @@
"__experimentalFontFamily": true,
"__experimentalTextDecoration": true
},
"color": true
"color": true,
"__experimentalAllowedOrientations": ["horizontal", "vertical"]
},
"editorStyle": "wp-block-navigation-editor",
"style": "wp-block-navigation"
Expand Down
18 changes: 17 additions & 1 deletion packages/block-library/src/navigation/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import classnames from 'classnames';
/**
* WordPress dependencies
*/
import { useState, useMemo } from '@wordpress/element';
import { useState, useMemo, useEffect } from '@wordpress/element';
import {
InnerBlocks,
__experimentalUseInnerBlocksProps as useInnerBlocksProps,
Expand All @@ -20,6 +20,7 @@ import { useDispatch, withSelect, withDispatch } from '@wordpress/data';
import { PanelBody, ToggleControl, ToolbarGroup } from '@wordpress/components';
import { compose } from '@wordpress/compose';
import { __ } from '@wordpress/i18n';
import { getBlockSupport } from '@wordpress/blocks';

/**
* Internal dependencies
Expand Down Expand Up @@ -55,6 +56,7 @@ function Navigation( {
className,
hasSubmenuIndicatorSetting = true,
hasItemJustificationControls = true,
allowedOrientations,
} ) {
const [ isPlaceholderShown, setIsPlaceholderShown ] = useState(
! hasExistingNavItems
Expand All @@ -79,6 +81,14 @@ function Navigation( {

const placeholder = useMemo( () => <PlaceholderPreview />, [] );

useEffect( () => {
if ( ! allowedOrientations?.includes( attributes.orientation ) ) {
setAttributes( {
orientation: allowedOrientations[ 0 ],
} );
}
}, [ allowedOrientations ] );

const innerBlocksProps = useInnerBlocksProps(
{
className: 'wp-block-navigation__container',
Expand Down Expand Up @@ -183,6 +193,11 @@ function Navigation( {

export default compose( [
withSelect( ( select, { clientId } ) => {
const allowedOrientations = getBlockSupport(
'core/navigation',
'__experimentalAllowedOrientations'
);

const innerBlocks = select( blockEditorStore ).getBlocks( clientId );
const {
getClientIdsOfDescendants,
Expand All @@ -206,6 +221,7 @@ export default compose( [
// This prop is already available but computing it here ensures it's
// fresh compared to isImmediateParentOfSelectedBlock
isSelected: selectedBlockId === clientId,
allowedOrientations,
};
} ),
withDispatch( ( dispatch, { clientId } ) => {
Expand Down