Skip to content

Commit

Permalink
Search: Hide label when inserted in Navigation block
Browse files Browse the repository at this point in the history
  • Loading branch information
Mamaduka committed Oct 28, 2021
1 parent 0054ce9 commit 8e05f25
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 10 deletions.
1 change: 1 addition & 0 deletions packages/block-library/src/search/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"default": false
}
},
"usesContext": [ "showSubmenuIcon" ],
"supports": {
"align": [ "left", "center", "right" ],
"color": {
Expand Down
41 changes: 31 additions & 10 deletions packages/block-library/src/search/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
__experimentalUseBorderProps as useBorderProps,
__experimentalUnitControl as UnitControl,
__experimentalUseColorProps as useColorProps,
store as blockEditorStore,
} from '@wordpress/block-editor';
import {
ToolbarDropdownMenu,
Expand All @@ -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';

Expand Down Expand Up @@ -54,6 +57,7 @@ const DEFAULT_INNER_PADDING = '4px';

export default function SearchEdit( {
className,
context,
attributes,
setAttributes,
toggleSelection,
Expand All @@ -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.
Expand Down Expand Up @@ -254,16 +273,18 @@ export default function SearchEdit( {
<>
<BlockControls>
<ToolbarGroup>
<ToolbarButton
title={ __( 'Toggle search label' ) }
icon={ toggleLabel }
onClick={ () => {
setAttributes( {
showLabel: ! showLabel,
} );
} }
className={ showLabel ? 'is-pressed' : undefined }
/>
{ ! isNavigationChild && (
<ToolbarButton
title={ __( 'Toggle search label' ) }
icon={ toggleLabel }
onClick={ () => {
setAttributes( {
showLabel: ! showLabel,
} );
} }
className={ showLabel ? 'is-pressed' : undefined }
/>
) }
<ToolbarDropdownMenu
icon={ getButtonPositionIcon() }
label={ __( 'Change button position' ) }
Expand Down

0 comments on commit 8e05f25

Please sign in to comment.