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

Simplify frontend script for button-only search #32387

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
19 changes: 0 additions & 19 deletions packages/block-library/src/search/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,6 @@ import {
PX_WIDTH_DEFAULT,
MIN_WIDTH,
MIN_WIDTH_UNIT,
showSearchField,
hideSearchField,
} from './utils.js';

// Used to calculate border radius adjustment to avoid "fat" corners when
Expand Down Expand Up @@ -89,23 +87,6 @@ export default function SearchEdit( {
defaultValues: { '%': PC_WIDTH_DEFAULT, px: PX_WIDTH_DEFAULT },
} );

useEffect( () => {
if ( isSearchFieldHidden ) {
hideSearchField(
searchFieldRef.current.offsetParent,
searchFieldRef.current,
buttonRef.current
);
} else {
showSearchField(
searchFieldRef.current.offsetParent,
searchFieldRef.current,
width,
widthUnit
);
}
}, [ isSearchFieldHidden, buttonUseIcon ] );

useEffect( () => {
if ( 'button-only' === buttonPosition && ! isSelected ) {
setAttributes( {
Expand Down
6 changes: 6 additions & 0 deletions packages/block-library/src/search/editor.scss
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,10 @@
&__components-button-group {
margin-top: 10px;
}

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

const wpBlockSearch = ( block ) => {
const attributeContainer = block.querySelector(
'.wp-block-search__attributes'
);
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';

if ( ! attributeContainer ) {
return;
}
const wrapper = block.querySelector( wrapperClass );
const searchField = block.querySelector( '.wp-block-search__input' );
const button = block.querySelector( buttonClass );

let attributes;
try {
attributes = JSON.parse( attributeContainer.text );
} catch ( e ) {
return;
}
attributeContainer.remove();
// Hide search on init.
block.classList.add( hiddenClass );
setTimeout(
() =>
( searchField.style.transitionDuration = `${ transitionDuration }ms` ),
transitionDuration
);

const hiddenClass = 'wp-block-search__searchfield-hidden';
const wrapperClass = '.wp-block-search__inside-wrapper';
const buttonClass = '.wp-block-search__button';
const toggleSearchField = ( e ) => {
if ( e.target !== button && ! e.target.closest( buttonClass ) ) {
return false;
}

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

const toggleSearchField = ( e ) => {
if ( e.target !== button && ! e.target.closest( buttonClass ) ) {
return false;
}
return block.classList.contains( hiddenClass )
? doShowSearchField()
: doHideSearchField();
};

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

return block.classList.contains( hiddenClass )
? doShowSearchField()
: doHideSearchField();
};
wrapper.removeEventListener( 'click', toggleSearchField );
document.body.addEventListener( 'click', doSearch );
};

const doShowSearchField = () => {
showSearchField(
wrapper,
searchField,
attributes.width,
attributes.widthUnit
);
block.classList.remove( hiddenClass );
searchField.focus();
const doHideSearchField = () => {
block.classList.add( hiddenClass );
};

wrapper.removeEventListener( 'click', toggleSearchField );
document.body.addEventListener( 'click', doBlur );
};
const doSearch = ( e ) => {
if ( e.target.closest( wrapperClass ) ) {
return false;
}

const doHideSearchField = ( animate = true ) => {
hideSearchField( wrapper, searchField, button, animate );
block.classList.add( hiddenClass );
};
doHideSearchField();

const doBlur = ( e ) => {
if ( e.target.closest( wrapperClass ) ) {
return false;
}
document.body.removeEventListener( 'click', doSearch );
wrapper.addEventListener( 'click', toggleSearchField );
};

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

wrapper.addEventListener( 'click', toggleSearchField );
doHideSearchField( false );
};

// eslint-disable-next-line @wordpress/no-global-event-listener
document.addEventListener( 'DOMContentLoaded', () => {
Array.from(
document.getElementsByClassName(
'wp-block-search__button-behavior-expand'
)
).forEach( ( block ) => {
wpBlockSearch( block );
} );
} );
12 changes: 2 additions & 10 deletions packages/block-library/src/search/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,17 +91,9 @@ function render_block_core_search( $attributes ) {
$input_markup . $button_markup
);
$wrapper_attributes = get_block_wrapper_attributes( array( 'class' => $classnames ) );
$block_attributes = '';

if ( ! empty( $attributes['buttonPosition'] ) && ! empty( $attributes['buttonBehavior'] ) ) {
if ( 'button-only' === $attributes['buttonPosition'] && 'expand-searchfield' === $attributes['buttonBehavior'] ) {
$block_attributes = '<script type="application/json" class="wp-block-search__attributes">' .
wp_json_encode( (object) array(
'width' => $attributes['width'],
'widthUnit' => $attributes['widthUnit']
) ) .
'</script>';

wp_enqueue_script( 'wp-block-library-search', plugins_url( 'search/frontend.js', __FILE__ ) );
}
}
Expand All @@ -110,7 +102,7 @@ function render_block_core_search( $attributes ) {
'<form role="search" method="get" action="%s" %s>%s</form>',
esc_url( home_url( '/' ) ),
$wrapper_attributes,
$label_markup . $field_markup . $block_attributes
$label_markup . $field_markup
);
}

Expand Down Expand Up @@ -197,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
7 changes: 7 additions & 0 deletions packages/block-library/src/search/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,12 @@
min-width: 0 !important;
}

.wp-block-search__input {
transition-property: all;
Copy link
Member Author

Choose a reason for hiding this comment

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

This needs to be all and not flex-basis because using flex-basis some versions of Safari don't properly apply the transition.

transition-duration: 0;
flex-basis: 100%;
}

// !important here to override inline styles on button only deselected view.
&.wp-block-search__searchfield-hidden {
overflow: hidden;
Expand All @@ -85,6 +91,7 @@
border-right-width: 0 !important;
flex-grow: 0;
margin: 0;
flex-basis: 0 !important;
}
}
}
Expand Down
30 changes: 0 additions & 30 deletions packages/block-library/src/search/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ export const PC_WIDTH_DEFAULT = 50;
export const PX_WIDTH_DEFAULT = 350;
export const MIN_WIDTH = 220;
export const MIN_WIDTH_UNIT = 'px';
export const SEARCHFIELD_ANIMATION_DURATION = 300;

/**
* Returns a boolean whether passed unit is percentage
Expand All @@ -17,32 +16,3 @@ export const SEARCHFIELD_ANIMATION_DURATION = 300;
export function isPercentageUnit( unit ) {
return unit === '%';
}

export function hideSearchField( wrapper, searchField, button, animate = true ) {
const duration = animate ? SEARCHFIELD_ANIMATION_DURATION : 0;

searchField.style.transitionDuration = `${ duration }ms`;
wrapper.style.transitionDuration = `${ duration }ms`;
wrapper.style.width = `${ button.offsetWidth }px`;

const removeTransitions = setTimeout( () => {
wrapper.style.transitionDuration = 'unset';

clearTimeout( removeTransitions );
}, duration );
}

export function showSearchField( wrapper, searchField, width, widthUnit, animate = true ) {
const duration = animate ? SEARCHFIELD_ANIMATION_DURATION : 0;

searchField.style.transitionDuration = `${ duration }ms`;
wrapper.style.transitionDuration = `${ duration }ms`;
wrapper.style.width = `${ width }${ widthUnit }`;

const removeTransitions = setTimeout( () => {
searchField.style.width = `${ width }${ widthUnit }`;
wrapper.style.transitionDuration = 'unset';

clearTimeout( removeTransitions );
}, duration );
}