Skip to content

Commit

Permalink
[Block Library - Seach]: Add contextual defaults when inserted in Nav…
Browse files Browse the repository at this point in the history
…igation block (#36125)
  • Loading branch information
ntsekouras authored Nov 2, 2021
1 parent bf13d39 commit 9614106
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 2 deletions.
4 changes: 3 additions & 1 deletion packages/block-library/src/navigation-link/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,9 @@ function navStripHTML( html ) {
function LinkControlTransforms( { block, transforms, replace } ) {
return (
<div className="link-control-transform">
<h3 className="link-control-transform__subheading">Transform</h3>
<h3 className="link-control-transform__subheading">
{ __( 'Transform' ) }
</h3>
<div className="link-control-transform__items">
{ transforms.map( ( item, index ) => {
return (
Expand Down
6 changes: 5 additions & 1 deletion packages/block-library/src/navigation-link/transforms.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,11 @@ const transforms = {
type: 'block',
blocks: [ 'core/search' ],
transform: () => {
return createBlock( 'core/search' );
return createBlock( 'core/search', {
showLabel: false,
buttonUseIcon: true,
buttonPosition: 'button-inside',
} );
},
},
],
Expand Down
29 changes: 29 additions & 0 deletions packages/block-library/src/search/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ import {
__experimentalUseBorderProps as useBorderProps,
__experimentalUnitControl as UnitControl,
__experimentalUseColorProps as useColorProps,
store as blockEditorStore,
} from '@wordpress/block-editor';
import { useDispatch, useSelect } from '@wordpress/data';
import { useEffect } from '@wordpress/element';
import {
ToolbarDropdownMenu,
ToolbarGroup,
Expand Down Expand Up @@ -58,6 +61,7 @@ export default function SearchEdit( {
setAttributes,
toggleSelection,
isSelected,
clientId,
} ) {
const {
label,
Expand All @@ -72,6 +76,31 @@ export default function SearchEdit( {
style,
} = attributes;

const insertedInNavigationBlock = useSelect(
( select ) => {
const { getBlockParentsByBlockName, wasBlockJustInserted } = select(
blockEditorStore
);
return (
!! getBlockParentsByBlockName( clientId, 'core/navigation' )
?.length && wasBlockJustInserted( clientId )
);
},
[ clientId ]
);
const { __unstableMarkNextChangeAsNotPersistent } = useDispatch(
blockEditorStore
);
useEffect( () => {
if ( ! insertedInNavigationBlock ) return;
// This side-effect should not create an undo level.
__unstableMarkNextChangeAsNotPersistent();
setAttributes( {
showLabel: false,
buttonUseIcon: true,
buttonPosition: 'button-inside',
} );
}, [ insertedInNavigationBlock ] );
const borderRadius = style?.border?.radius;
const borderColor = style?.border?.color;
const borderProps = useBorderProps( attributes );
Expand Down

0 comments on commit 9614106

Please sign in to comment.