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 Block: Add icon only option with expandable search field #31719

Closed
wants to merge 36 commits into from
Closed
Show file tree
Hide file tree
Changes from 35 commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
ae41e8c
Reinstate the icon only option in the UI.
apeatling May 11, 2021
52572f2
Add basic select control to set button behavior.
apeatling May 11, 2021
aa0a932
Handle different display modes for button only view.
apeatling May 11, 2021
f4d1a5e
Some work on show/hide of search field.
apeatling May 11, 2021
0877d86
Very rough proof of concept for animating in and out the search field.
apeatling May 12, 2021
a36fe39
Hide the searchfield on first selection of the button only view.
apeatling May 14, 2021
a825bb8
Move functions.
apeatling May 17, 2021
e5dfc6b
Shift code to css from js.
apeatling May 17, 2021
69f2639
Set the closed class on a delay and use state.
apeatling May 17, 2021
827805a
Remove state
apeatling May 17, 2021
d484ebf
Manually set the field hidden settings.
apeatling May 18, 2021
12e3299
Unhide the search field when the width is adjusted.
apeatling May 18, 2021
91355c5
Align the search block to the right when floated right. This is likel…
apeatling May 18, 2021
f5d19a0
Make sure search field hides on first load.
apeatling May 18, 2021
d262b41
Clean up animation transitions
apeatling May 18, 2021
336a1e0
Match hide/show functions
apeatling May 18, 2021
22c4fd2
Hide drag handle for resize when searchfield is hidden.
apeatling May 18, 2021
64f8082
Don't clip drag handle when searchfield is not hidden.
apeatling May 18, 2021
4d2b6e5
Add frontend class rendering
apeatling May 20, 2021
cc6458b
Move search/hide functions to utils.js and make use of them in the ed…
apeatling May 20, 2021
16d469c
Only load the frontend JS if it's set for button only mode, and expan…
apeatling May 20, 2021
2dac480
Get frontend js correctly working withb the show and hide of the text…
apeatling May 25, 2021
ce1917f
Fix issues with changing top and bottom padding.
apeatling May 26, 2021
1c9674c
Fix issues with the button with and flex, and make sure animations us…
apeatling May 31, 2021
6553d1c
Remove the UI for selecting button only behavior since there is no de…
apeatling May 31, 2021
f87d3d1
Update frontend code so that clicking outside of the searchfield clos…
apeatling May 31, 2021
dae56ff
Make sure that the frontend toggle works when using the SVG icon for …
apeatling May 31, 2021
5817463
Keep searchfield hidden on editor load, and expand field when a diffe…
apeatling May 31, 2021
27346ef
Update fixtures.
apeatling May 31, 2021
d7d97b4
Fix PHP lint issues.
apeatling May 31, 2021
e80abd2
Remove commented out UI since we can just add this back in when the o…
apeatling May 31, 2021
e6cb904
Remove margin on the input field when collapsed.
apeatling Jun 1, 2021
b72f668
Fix line height, font size. Fix padding overrides.
apeatling Jun 1, 2021
0560249
Make sure the search block is still useable with JS turned off.
apeatling Jun 1, 2021
847e9b3
Simplify frontend script for button-only search (#32387)
aristath Jun 3, 2021
55ad50a
Move const outside foreach loop.
apeatling Jun 7, 2021
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
14 changes: 12 additions & 2 deletions packages/block-library/src/search/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@
"__experimentalRole": "content"
},
"width": {
"type": "number"
"type": "number",
"default": 100
},
"widthUnit": {
"type": "string"
"type": "string",
"default": "%"
},
"buttonText": {
"type": "string",
Expand All @@ -37,6 +39,14 @@
"buttonUseIcon": {
"type": "boolean",
"default": false
},
"buttonBehavior": {
"type": "string",
"default": "expand-searchfield"
},
"isSearchFieldHidden": {
"type": "boolean",
"default": false
}
},
"supports": {
Expand Down
84 changes: 81 additions & 3 deletions packages/block-library/src/search/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import {
import { useInstanceId } from '@wordpress/compose';
import { search } from '@wordpress/icons';
import { __ } from '@wordpress/i18n';
import { useRef, useEffect } from '@wordpress/element';

/**
* Internal dependencies
Expand All @@ -51,6 +52,7 @@ import {
// Used to calculate border radius adjustment to avoid "fat" corners when
// button is placed inside wrapper.
const DEFAULT_INNER_PADDING = 4;
const BUTTON_BEHAVIOR_EXPAND = 'expand-searchfield';

export default function SearchEdit( {
className,
Expand All @@ -69,18 +71,40 @@ export default function SearchEdit( {
buttonText,
buttonPosition,
buttonUseIcon,
buttonBehavior,
isSearchFieldHidden,
style,
} = attributes;

const borderRadius = style?.border?.radius;
const unitControlInstanceId = useInstanceId( UnitControl );
const unitControlInputId = `wp-block-search__width-${ unitControlInstanceId }`;
const searchFieldRef = useRef();
const buttonRef = useRef();

const units = useCustomUnits( {
availableUnits: [ '%', 'px' ],
defaultValues: { '%': PC_WIDTH_DEFAULT, px: PX_WIDTH_DEFAULT },
} );

useEffect( () => {
if ( 'button-only' === buttonPosition && ! isSelected ) {
setAttributes( {
isSearchFieldHidden: true,
} );
}
}, [ isSelected ] );

useEffect( () => {
if ( 'button-only' !== buttonPosition || ! isSelected ) {
return;
}

setAttributes( {
isSearchFieldHidden: false,
} );
}, [ width ] );

const getBlockClassNames = () => {
return classnames(
className,
Expand All @@ -96,6 +120,13 @@ export default function SearchEdit( {
'button-only' === buttonPosition
? 'wp-block-search__button-only'
: undefined,
'button-only' === buttonPosition &&
BUTTON_BEHAVIOR_EXPAND === buttonBehavior
? 'wp-block-search__button-behavior-expand'
: undefined,
'button-only' === buttonPosition && isSearchFieldHidden
? 'wp-block-search__searchfield-hidden'
: undefined,
! buttonUseIcon && 'no-button' !== buttonPosition
? 'wp-block-search__text-button'
: undefined,
Expand All @@ -119,7 +150,10 @@ export default function SearchEdit( {
};

const getResizableSides = () => {
if ( 'button-only' === buttonPosition ) {
if (
'button-only' === buttonPosition &&
( 'search-page-link' === buttonBehavior || isSearchFieldHidden )
) {
return {};
}

Expand All @@ -145,6 +179,7 @@ export default function SearchEdit( {
onChange={ ( event ) =>
setAttributes( { placeholder: event.target.value } )
}
ref={ searchFieldRef }
/>
);
};
Expand All @@ -157,6 +192,17 @@ export default function SearchEdit( {
icon={ search }
className="wp-block-search__button"
style={ { borderRadius } }
onClick={ () => {
if (
'button-only' === buttonPosition &&
BUTTON_BEHAVIOR_EXPAND === buttonBehavior
) {
setAttributes( {
isSearchFieldHidden: ! isSearchFieldHidden,
} );
}
} }
ref={ buttonRef }
/>
) }

Expand All @@ -171,6 +217,17 @@ export default function SearchEdit( {
onChange={ ( html ) =>
setAttributes( { buttonText: html } )
}
onClick={ () => {
if (
'button-only' === buttonPosition &&
BUTTON_BEHAVIOR_EXPAND === buttonBehavior
) {
setAttributes( {
isSearchFieldHidden: ! isSearchFieldHidden,
} );
}
} }
ref={ buttonRef }
/>
) }
</>
Expand Down Expand Up @@ -202,6 +259,7 @@ export default function SearchEdit( {
onClick={ () => {
setAttributes( {
buttonPosition: 'no-button',
isSearchFieldHidden: false,
} );
onClose();
} }
Expand All @@ -213,6 +271,7 @@ export default function SearchEdit( {
onClick={ () => {
setAttributes( {
buttonPosition: 'button-outside',
isSearchFieldHidden: false,
} );
onClose();
} }
Expand All @@ -224,12 +283,26 @@ export default function SearchEdit( {
onClick={ () => {
setAttributes( {
buttonPosition: 'button-inside',
isSearchFieldHidden: false,
} );
onClose();
} }
>
{ __( 'Button Inside' ) }
</MenuItem>
<MenuItem
icon={ buttonOnly }
onClick={ () => {
setAttributes( {
buttonPosition: 'button-only',
isSearchFieldHidden: true,
} );

onClose();
} }
>
{ __( 'Button Only' ) }
</MenuItem>
</MenuGroup>
) }
</DropdownMenu>
Expand All @@ -241,6 +314,7 @@ export default function SearchEdit( {
onClick={ () => {
setAttributes( {
buttonUseIcon: ! buttonUseIcon,
isSearchFieldHidden: true,
} );
} }
className={
Expand Down Expand Up @@ -375,14 +449,18 @@ export default function SearchEdit( {
showHandle={ isSelected }
>
{ ( 'button-inside' === buttonPosition ||
'button-outside' === buttonPosition ) && (
'button-outside' === buttonPosition ||
( 'button-only' === buttonPosition &&
BUTTON_BEHAVIOR_EXPAND === buttonBehavior ) ) && (
<>
{ renderTextField() }
{ renderButton() }
</>
) }

{ 'button-only' === buttonPosition && renderButton() }
{ 'button-only' === buttonPosition &&
BUTTON_BEHAVIOR_EXPAND !== buttonBehavior &&
renderButton() }
{ 'no-button' === buttonPosition && renderTextField() }
</ResizableBox>
</div>
Expand Down
7 changes: 6 additions & 1 deletion packages/block-library/src/search/editor.scss
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
// This needs high specificity because it otherwise inherits styles from `components-button`.
// stylelint-disable-line no-duplicate-selectors
&.wp-block-search__button.wp-block-search__button {
padding: 6px 10px;
display: flex;
align-items: center;
}
Expand All @@ -31,4 +30,10 @@
&__components-button-group {
margin-top: 10px;
}

&.wp-block-search__button-behavior-expand {
.wp-block-search__input {
transition-duration: 300ms;
}
}
}
63 changes: 63 additions & 0 deletions packages/block-library/src/search/frontend.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// eslint-disable-next-line @wordpress/no-global-event-listener
document.addEventListener( 'DOMContentLoaded', () => {
const transitionDuration = 300;

Array.from(
document.getElementsByClassName(
'wp-block-search__button-behavior-expand'
)
).forEach( ( block ) => {
const hiddenClass = 'wp-block-search__searchfield-hidden';
const wrapperClass = '.wp-block-search__inside-wrapper';
const buttonClass = '.wp-block-search__button';
apeatling marked this conversation as resolved.
Show resolved Hide resolved

const wrapper = block.querySelector( wrapperClass );
const searchField = block.querySelector( '.wp-block-search__input' );
const button = block.querySelector( buttonClass );

// Hide search on init.
block.classList.add( hiddenClass );
setTimeout(
() =>
( searchField.style.transitionDuration = `${ transitionDuration }ms` ),
transitionDuration
);

const toggleSearchField = ( e ) => {
if ( e.target !== button && ! e.target.closest( buttonClass ) ) {
return false;
}

e.preventDefault();

return block.classList.contains( hiddenClass )
? doShowSearchField()
: doHideSearchField();
};

const doShowSearchField = () => {
block.classList.remove( hiddenClass );
searchField.focus();

wrapper.removeEventListener( 'click', toggleSearchField );
document.body.addEventListener( 'click', doSearch );
};

const doHideSearchField = () => {
block.classList.add( hiddenClass );
};

const doSearch = ( e ) => {
if ( e.target.closest( wrapperClass ) ) {
return false;
}

doHideSearchField();

document.body.removeEventListener( 'click', doSearch );
wrapper.addEventListener( 'click', toggleSearchField );
};

wrapper.addEventListener( 'click', toggleSearchField );
} );
} );
35 changes: 24 additions & 11 deletions packages/block-library/src/search/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ function render_block_core_search( $attributes ) {
$classnames = classnames_for_block_core_search( $attributes );
$show_label = ( ! empty( $attributes['showLabel'] ) ) ? true : false;
$use_icon_button = ( ! empty( $attributes['buttonUseIcon'] ) ) ? true : false;
$show_input = ( ! empty( $attributes['buttonPosition'] ) && 'button-only' === $attributes['buttonPosition'] ) ? false : true;
$show_button = ( ! empty( $attributes['buttonPosition'] ) && 'no-button' === $attributes['buttonPosition'] ) ? false : true;
$label_markup = '';
$input_markup = '';
Expand All @@ -54,15 +53,13 @@ function render_block_core_search( $attributes ) {
}
}

if ( $show_input ) {
$input_markup = sprintf(
'<input type="search" id="%s" class="wp-block-search__input" name="s" value="%s" placeholder="%s" %s required />',
$input_id,
esc_attr( get_search_query() ),
esc_attr( $attributes['placeholder'] ),
$inline_styles['shared']
);
}
$input_markup = sprintf(
'<input type="search" id="%s" class="wp-block-search__input" name="s" value="%s" placeholder="%s" %s required />',
$input_id,
esc_attr( get_search_query() ),
esc_attr( $attributes['placeholder'] ),
$inline_styles['shared']
);

if ( $show_button ) {
$button_internal_markup = '';
Expand Down Expand Up @@ -95,6 +92,12 @@ function render_block_core_search( $attributes ) {
);
$wrapper_attributes = get_block_wrapper_attributes( array( 'class' => $classnames ) );

if ( ! empty( $attributes['buttonPosition'] ) && ! empty( $attributes['buttonBehavior'] ) ) {
if ( 'button-only' === $attributes['buttonPosition'] && 'expand-searchfield' === $attributes['buttonBehavior'] ) {
wp_enqueue_script( 'wp-block-library-search', plugins_url( 'search/frontend.js', __FILE__ ) );
}
}

return sprintf(
'<form role="search" method="get" action="%s" %s>%s</form>',
esc_url( home_url( '/' ) ),
Expand Down Expand Up @@ -141,6 +144,16 @@ function classnames_for_block_core_search( $attributes ) {

if ( 'button-only' === $attributes['buttonPosition'] ) {
$classnames[] = 'wp-block-search__button-only';

if ( ! empty( $attributes['buttonBehavior'] ) ) {
if ( 'expand-searchfield' === $attributes['buttonBehavior'] ) {
$classnames[] = 'wp-block-search__button-behavior-expand';
}

if ( 'search-page-link' === $attributes['buttonBehavior'] ) {
$classnames[] = 'wp-block-search__button-behavior-link';
}
}
}
}

Expand Down Expand Up @@ -176,7 +189,7 @@ function styles_for_block_core_search( $attributes ) {
$has_width = ! empty( $attributes['width'] ) && ! empty( $attributes['widthUnit'] );
$button_only = ! empty( $attributes['buttonPosition'] ) && 'button-only' === $attributes['buttonPosition'];

if ( $has_width && ! $button_only ) {
if ( $has_width ) {
$wrapper_styles[] = sprintf(
'width: %d%s;',
esc_attr( $attributes['width'] ),
Expand Down
Loading