diff --git a/packages/block-library/src/navigation/block.json b/packages/block-library/src/navigation/block.json index 2822b03081f02..c015e7b35e7df 100644 --- a/packages/block-library/src/navigation/block.json +++ b/packages/block-library/src/navigation/block.json @@ -62,6 +62,10 @@ }, "customOverlayTextColor": { "type": "string" + }, + "searchBlockLabel": { + "type": "boolean", + "default": false } }, "providesContext": { @@ -78,7 +82,8 @@ "showSubmenuIcon": "showSubmenuIcon", "openSubmenusOnClick": "openSubmenusOnClick", "style": "style", - "orientation": "orientation" + "orientation": "orientation", + "searchBlockLabel": "searchBlockLabel" }, "supports": { "align": [ diff --git a/packages/block-library/src/search/block.json b/packages/block-library/src/search/block.json index d2b3e08e946cb..fbdce40dda2a4 100644 --- a/packages/block-library/src/search/block.json +++ b/packages/block-library/src/search/block.json @@ -53,5 +53,8 @@ "html": false }, "editorStyle": "wp-block-search-editor", - "style": "wp-block-search" + "style": "wp-block-search", + "usesContext": [ + "searchBlockLabel" + ] } diff --git a/packages/block-library/src/search/edit.js b/packages/block-library/src/search/edit.js index 3bf965cd0f5f4..b5a9b703f1b65 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 { useSelect } from '@wordpress/data'; +import { useRef } from '@wordpress/element'; import { Icon, search } from '@wordpress/icons'; import { __ } from '@wordpress/i18n'; @@ -54,6 +57,8 @@ const DEFAULT_INNER_PADDING = '4px'; export default function SearchEdit( { className, + clientId, + context, attributes, setAttributes, toggleSelection, @@ -76,6 +81,24 @@ export default function SearchEdit( { const borderColor = style?.border?.color; const borderProps = useBorderProps( attributes ); + const isNewBlock = useSelect( ( select ) => { + const { wasBlockJustInserted } = select( blockEditorStore ); + + return wasBlockJustInserted( clientId ); + } ); + + // Save a ref to this value, as it would become outdated + // if any other blocks are added. + const isNewBlockRef = useRef( isNewBlock ); + + if ( + 'undefined' !== typeof context.searchBlockLabel && + true === isNewBlockRef.current + ) { + setAttributes( { showLabel: context.searchBlockLabel } ); + isNewBlockRef.current = false; + } + // 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. diff --git a/test/integration/fixtures/blocks/core__navigation.json b/test/integration/fixtures/blocks/core__navigation.json index a770861fd7824..77c5cb56e9566 100644 --- a/test/integration/fixtures/blocks/core__navigation.json +++ b/test/integration/fixtures/blocks/core__navigation.json @@ -7,7 +7,8 @@ "orientation": "horizontal", "showSubmenuIcon": true, "openSubmenusOnClick": false, - "overlayMenu": "never" + "overlayMenu": "never", + "searchBlockLabel": false }, "innerBlocks": [], "originalContent": ""