diff --git a/packages/block-library/src/search/edit.js b/packages/block-library/src/search/edit.js index 3bf965cd0f5f49..48b97e157c2ccc 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, useSelect } from '@wordpress/data'; +import { useEffect, useRef } 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, + clientId, attributes, setAttributes, toggleSelection, @@ -76,6 +80,42 @@ export default function SearchEdit( { const borderColor = style?.border?.color; const borderProps = useBorderProps( attributes ); + const { isNavigationChild } = useSelect( + ( select ) => { + const { getBlockParentsByBlockName } = select( blockEditorStore ); + + return { + isNavigationChild: !! getBlockParentsByBlockName( + clientId, + 'core/navigation' + )?.length, + }; + }, + [ clientId ] + ); + const { __unstableMarkNextChangeAsNotPersistent } = useDispatch( + blockEditorStore + ); + + const hasChangedDefaults = useRef( false ); + useEffect( () => { + if ( hasChangedDefaults.current ) { + return; + } + + if ( isNavigationChild && showLabel && ! buttonUseIcon ) { + // This side-effect should not create an undo level. + __unstableMarkNextChangeAsNotPersistent(); + setAttributes( { + showLabel: false, + buttonUseIcon: true, + buttonPosition: 'button-inside', + } ); + + hasChangedDefaults.current = true; + } + }, [ isNavigationChild, showLabel, buttonUseIcon ] ); + // 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.