diff --git a/packages/block-library/src/search/block.json b/packages/block-library/src/search/block.json index d2b3e08e946cb2..4c0b63fb0dc528 100644 --- a/packages/block-library/src/search/block.json +++ b/packages/block-library/src/search/block.json @@ -39,6 +39,7 @@ "default": false } }, + "usesContext": [ "showSubmenuIcon" ], "supports": { "align": [ "left", "center", "right" ], "color": { diff --git a/packages/block-library/src/search/edit.js b/packages/block-library/src/search/edit.js index 3bf965cd0f5f49..4e04590a49a69d 100644 --- a/packages/block-library/src/search/edit.js +++ b/packages/block-library/src/search/edit.js @@ -14,6 +14,7 @@ import { __experimentalUseBorderProps as useBorderProps, __experimentalUnitControl as UnitControl, __experimentalUseColorProps as useColorProps, + store as blockEditorStore, } from '@wordpress/block-editor'; import { ToolbarDropdownMenu, @@ -27,6 +28,8 @@ import { __experimentalUseCustomUnits as useCustomUnits, } from '@wordpress/components'; import { useInstanceId } from '@wordpress/compose'; +import { useDispatch } from '@wordpress/data'; +import { useEffect } from '@wordpress/element'; import { Icon, search } from '@wordpress/icons'; import { __ } from '@wordpress/i18n'; @@ -54,6 +57,7 @@ const DEFAULT_INNER_PADDING = '4px'; export default function SearchEdit( { className, + context, attributes, setAttributes, toggleSelection, @@ -76,6 +80,21 @@ export default function SearchEdit( { const borderColor = style?.border?.color; const borderProps = useBorderProps( attributes ); + const { __unstableMarkNextChangeAsNotPersistent } = useDispatch( + blockEditorStore + ); + + // Check if Navigation contains the block. + const isNavigationChild = 'showSubmenuIcon' in context; + + useEffect( () => { + if ( isNavigationChild && showLabel ) { + // This side-effect should not create an undo level. + __unstableMarkNextChangeAsNotPersistent(); + setAttributes( { showLabel: false } ); + } + }, [ isNavigationChild, showLabel ] ); + // Check for old deprecated numerical border radius. Done as a separate // check so that a borderRadius style won't overwrite the longhand // per-corner styles. @@ -254,16 +273,18 @@ export default function SearchEdit( { <> - { - setAttributes( { - showLabel: ! showLabel, - } ); - } } - className={ showLabel ? 'is-pressed' : undefined } - /> + { ! isNavigationChild && ( + { + setAttributes( { + showLabel: ! showLabel, + } ); + } } + className={ showLabel ? 'is-pressed' : undefined } + /> + ) }