Skip to content

Commit e2547a7

Browse files
Prevent Esc from executing changes in category and archive widgets. (#72889)
* Initial draft. Updates the block archives and category widgets to match the behavior in core. * Change function name to match PHPCS requirements * Update data for archives js * In block widget, data is not an integer. * Update packages/block-library/src/archives/index.php Co-authored-by: Weston Ruter <weston@ruter.net> --------- Co-authored-by: Weston Ruter <weston@ruter.net>
1 parent ff75a25 commit e2547a7

File tree

2 files changed

+78
-8
lines changed

2 files changed

+78
-8
lines changed

packages/block-library/src/archives/index.php

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,12 @@ function render_block_core_archives( $attributes ) {
6666
$show_label = empty( $attributes['showLabel'] ) ? ' screen-reader-text' : '';
6767

6868
$block_content = '<label for="' . $dropdown_id . '" class="wp-block-archives__label' . $show_label . '">' . esc_html( $title ) . '</label>
69-
<select id="' . $dropdown_id . '" name="archive-dropdown" onchange="document.location.href=this.options[this.selectedIndex].value;">
69+
<select id="' . esc_attr( $dropdown_id ) . '" name="archive-dropdown">
7070
<option value="">' . esc_html( $label ) . '</option>' . $archives . '</select>';
7171

72+
// Inject the dropdown script immediately after the select dropdown.
73+
$block_content .= block_core_archives_build_dropdown_script( $dropdown_id );
74+
7275
return sprintf(
7376
'<div %1$s>%2$s</div>',
7477
$wrapper_attributes,
@@ -106,6 +109,55 @@ function render_block_core_archives( $attributes ) {
106109
);
107110
}
108111

112+
/**
113+
* Generates the inline script for an archives dropdown field.
114+
*
115+
* @since 6.9.0
116+
*
117+
* @param string $dropdown_id ID of the dropdown field.
118+
*
119+
* @return string Returns the dropdown onChange redirection script.
120+
*/
121+
function block_core_archives_build_dropdown_script( $dropdown_id ) {
122+
ob_start();
123+
124+
$exports = array( $dropdown_id, home_url() );
125+
?>
126+
<script>
127+
( ( [ dropdownId, homeUrl ] ) => {
128+
const dropdown = document.getElementById( dropdownId );
129+
function onSelectChange() {
130+
setTimeout( () => {
131+
if ( 'escape' === dropdown.dataset.lastkey ) {
132+
return;
133+
}
134+
if ( dropdown.value ) {
135+
location.href = dropdown.value;
136+
}
137+
}, 250 );
138+
}
139+
function onKeyUp( event ) {
140+
if ( 'Escape' === event.key ) {
141+
dropdown.dataset.lastkey = 'escape';
142+
} else {
143+
delete dropdown.dataset.lastkey;
144+
}
145+
}
146+
function onClick() {
147+
delete dropdown.dataset.lastkey;
148+
}
149+
dropdown.addEventListener( 'keyup', onKeyUp );
150+
dropdown.addEventListener( 'click', onClick );
151+
dropdown.addEventListener( 'change', onSelectChange );
152+
} )( <?php echo wp_json_encode( $exports, JSON_HEX_TAG | JSON_UNESCAPED_SLASHES ); ?> );
153+
</script>
154+
<?php
155+
return wp_get_inline_script_tag(
156+
trim( str_replace( array( '<script>', '</script>' ), '', ob_get_clean() ) ) .
157+
"\n//# sourceURL=" . rawurlencode( __FUNCTION__ )
158+
);
159+
}
160+
109161
/**
110162
* Register archives block.
111163
*

packages/block-library/src/categories/index.php

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -104,14 +104,32 @@ function build_dropdown_script_block_core_categories( $dropdown_id ) {
104104
?>
105105
<script>
106106
( ( [ dropdownId, homeUrl ] ) => {
107-
document.getElementById( dropdownId ).addEventListener( 'change', ( event ) => {
108-
const dropdown = /** @type {HTMLSelectElement} */ ( event.target );
109-
if ( dropdown.value && dropdown.value !== '-1' ) {
110-
const url = new URL( homeUrl );
111-
url.searchParams.set( dropdown.name, dropdown.value );
112-
location.href = url.href;
107+
const dropdown = document.getElementById( dropdownId );
108+
function onSelectChange() {
109+
setTimeout( () => {
110+
if ( 'escape' === dropdown.dataset.lastkey ) {
111+
return;
112+
}
113+
if ( dropdown.value && dropdown instanceof HTMLSelectElement ) {
114+
const url = new URL( homeUrl );
115+
url.searchParams.set( dropdown.name, dropdown.value );
116+
location.href = url.href;
117+
}
118+
}, 250 );
119+
}
120+
function onKeyUp( event ) {
121+
if ( 'Escape' === event.key ) {
122+
dropdown.dataset.lastkey = 'escape';
123+
} else {
124+
delete dropdown.dataset.lastkey;
113125
}
114-
} );
126+
}
127+
function onClick() {
128+
delete dropdown.dataset.lastkey;
129+
}
130+
dropdown.addEventListener( 'keyup', onKeyUp );
131+
dropdown.addEventListener( 'click', onClick );
132+
dropdown.addEventListener( 'change', onSelectChange );
115133
} )( <?php echo wp_json_encode( $exports, JSON_HEX_TAG | JSON_UNESCAPED_SLASHES ); ?> );
116134
</script>
117135
<?php

0 commit comments

Comments
 (0)