Skip to content

Commit

Permalink
More contextual defaults
Browse files Browse the repository at this point in the history
  • Loading branch information
Mamaduka committed Oct 28, 2021
1 parent 8e05f25 commit b37f9f5
Showing 1 changed file with 25 additions and 16 deletions.
41 changes: 25 additions & 16 deletions packages/block-library/src/search/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import {
} from '@wordpress/components';
import { useInstanceId } from '@wordpress/compose';
import { useDispatch } from '@wordpress/data';
import { useEffect } from '@wordpress/element';
import { useEffect, useRef } from '@wordpress/element';
import { Icon, search } from '@wordpress/icons';
import { __ } from '@wordpress/i18n';

Expand Down Expand Up @@ -86,14 +86,25 @@ export default function SearchEdit( {

// Check if Navigation contains the block.
const isNavigationChild = 'showSubmenuIcon' in context;
const hasChangedDefaults = useRef( false );

useEffect( () => {
if ( isNavigationChild && showLabel ) {
if ( hasChangedDefaults.current ) {
return;
}

if ( isNavigationChild ) {
// This side-effect should not create an undo level.
__unstableMarkNextChangeAsNotPersistent();
setAttributes( { showLabel: false } );
setAttributes( {
showLabel: false,
buttonUseIcon: true,
buttonPosition: 'button-inside',
} );

hasChangedDefaults.current = true;
}
}, [ isNavigationChild, showLabel ] );
}, [ isNavigationChild ] );

// Check for old deprecated numerical border radius. Done as a separate
// check so that a borderRadius style won't overwrite the longhand
Expand Down Expand Up @@ -273,18 +284,16 @@ export default function SearchEdit( {
<>
<BlockControls>
<ToolbarGroup>
{ ! isNavigationChild && (
<ToolbarButton
title={ __( 'Toggle search label' ) }
icon={ toggleLabel }
onClick={ () => {
setAttributes( {
showLabel: ! showLabel,
} );
} }
className={ showLabel ? 'is-pressed' : undefined }
/>
) }
<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 b37f9f5

Please sign in to comment.