Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Search: Add contextual defaults when inserted in Navigation block #36125

Merged
merged 1 commit into from
Nov 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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' ) }
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unrelated, but needed 😄

</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