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

Use data-wp-on-async directives in core blocks when handler does not need synchronous access to event #62160

Merged
merged 3 commits into from
Jun 6, 2024
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
16 changes: 8 additions & 8 deletions packages/block-library/src/navigation/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -495,15 +495,15 @@ private static function get_responsive_container_markup( $attributes, $inner_blo
$close_button_directives = '';
if ( $is_interactive ) {
$open_button_directives = '
data-wp-on--click="actions.openMenuOnClick"
data-wp-on-async--click="actions.openMenuOnClick"
data-wp-on--keydown="actions.handleMenuKeydown"
';
$responsive_container_directives = '
data-wp-class--has-modal-open="state.isMenuOpen"
data-wp-class--is-menu-open="state.isMenuOpen"
data-wp-watch="callbacks.initMenu"
data-wp-on--keydown="actions.handleMenuKeydown"
data-wp-on--focusout="actions.handleMenuFocusout"
data-wp-on-async--focusout="actions.handleMenuFocusout"
tabindex="-1"
';
$responsive_dialog_directives = '
Expand All @@ -512,7 +512,7 @@ private static function get_responsive_container_markup( $attributes, $inner_blo
data-wp-bind--role="state.roleAttribute"
';
$close_button_directives = '
data-wp-on--click="actions.closeMenuOnClick"
data-wp-on-async--click="actions.closeMenuOnClick"
';
$responsive_container_content_directives = '
data-wp-watch="callbacks.focusFirstElement"
Expand Down Expand Up @@ -826,7 +826,7 @@ function block_core_navigation_add_directives_to_submenu( $tags, $block_attribut
$tags->set_attribute( 'data-wp-interactive', 'core/navigation' );
$tags->set_attribute( 'data-wp-context', '{ "submenuOpenedBy": { "click": false, "hover": false, "focus": false }, "type": "submenu" }' );
$tags->set_attribute( 'data-wp-watch', 'callbacks.initMenu' );
$tags->set_attribute( 'data-wp-on--focusout', 'actions.handleMenuFocusout' );
$tags->set_attribute( 'data-wp-on-async--focusout', 'actions.handleMenuFocusout' );
$tags->set_attribute( 'data-wp-on--keydown', 'actions.handleMenuKeydown' );

// This is a fix for Safari. Without it, Safari doesn't change the active
Expand All @@ -836,8 +836,8 @@ function block_core_navigation_add_directives_to_submenu( $tags, $block_attribut
$tags->set_attribute( 'tabindex', '-1' );

if ( ! isset( $block_attributes['openSubmenusOnClick'] ) || false === $block_attributes['openSubmenusOnClick'] ) {
$tags->set_attribute( 'data-wp-on--mouseenter', 'actions.openMenuOnHover' );
$tags->set_attribute( 'data-wp-on--mouseleave', 'actions.closeMenuOnHover' );
$tags->set_attribute( 'data-wp-on-async--mouseenter', 'actions.openMenuOnHover' );
$tags->set_attribute( 'data-wp-on-async--mouseleave', 'actions.closeMenuOnHover' );
}

// Add directives to the toggle submenu button.
Expand All @@ -847,7 +847,7 @@ function block_core_navigation_add_directives_to_submenu( $tags, $block_attribut
'class_name' => 'wp-block-navigation-submenu__toggle',
)
) ) {
$tags->set_attribute( 'data-wp-on--click', 'actions.toggleMenuOnClick' );
$tags->set_attribute( 'data-wp-on-async--click', 'actions.toggleMenuOnClick' );
$tags->set_attribute( 'data-wp-bind--aria-expanded', 'state.isMenuOpen' );
// The `aria-expanded` attribute for SSR is already added in the submenu block.
}
Expand All @@ -858,7 +858,7 @@ function block_core_navigation_add_directives_to_submenu( $tags, $block_attribut
'class_name' => 'wp-block-navigation__submenu-container',
)
) ) {
$tags->set_attribute( 'data-wp-on--focus', 'actions.openMenuOnFocus' );
$tags->set_attribute( 'data-wp-on-async--focus', 'actions.openMenuOnFocus' );
}

// Iterate through subitems if exist.
Expand Down
2 changes: 1 addition & 1 deletion packages/block-library/src/navigation/view.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ const { state, actions } = store(
// If focus is outside modal, and in the document, close menu
// event.target === The element losing focus
// event.relatedTarget === The element receiving focus (if any)
// When focusout is outsite the document,
// When focusout is outside the document,
// `window.document.activeElement` doesn't change.

// The event.relatedTarget is null when something outside the navigation menu is clicked. This is only necessary for Safari.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ function render_block_core_query_pagination_next( $attributes, $content, $block
) ) {
$p->set_attribute( 'data-wp-key', 'query-pagination-next' );
$p->set_attribute( 'data-wp-on--click', 'core/query::actions.navigate' );
$p->set_attribute( 'data-wp-on--mouseenter', 'core/query::actions.prefetch' );
$p->set_attribute( 'data-wp-on-async--mouseenter', 'core/query::actions.prefetch' );
$p->set_attribute( 'data-wp-watch', 'core/query::callbacks.prefetch' );
$content = $p->get_updated_html();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ function render_block_core_query_pagination_previous( $attributes, $content, $bl
) ) {
$p->set_attribute( 'data-wp-key', 'query-pagination-previous' );
$p->set_attribute( 'data-wp-on--click', 'core/query::actions.navigate' );
$p->set_attribute( 'data-wp-on--mouseenter', 'core/query::actions.prefetch' );
$p->set_attribute( 'data-wp-on-async--mouseenter', 'core/query::actions.prefetch' );
$p->set_attribute( 'data-wp-watch', 'core/query::callbacks.prefetch' );
$content = $p->get_updated_html();
}
Expand Down
4 changes: 2 additions & 2 deletions packages/block-library/src/search/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -191,8 +191,8 @@ function render_block_core_search( $attributes ) {
data-wp-interactive="core/search"'
. $form_context .
'data-wp-class--wp-block-search__searchfield-hidden="!context.isSearchInputVisible"
data-wp-on--keydown="actions.handleSearchKeydown"
data-wp-on--focusout="actions.handleSearchFocusout"
data-wp-on-async--keydown="actions.handleSearchKeydown"
data-wp-on-async--focusout="actions.handleSearchFocusout"
';
}

Expand Down
4 changes: 2 additions & 2 deletions phpunit/blocks/render-query-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,13 +94,13 @@ public function test_rendering_query_with_enhanced_pagination() {
$p->next_tag( array( 'class_name' => 'wp-block-query-pagination-previous' ) );
$this->assertSame( 'query-pagination-previous', $p->get_attribute( 'data-wp-key' ) );
$this->assertSame( 'core/query::actions.navigate', $p->get_attribute( 'data-wp-on--click' ) );
$this->assertSame( 'core/query::actions.prefetch', $p->get_attribute( 'data-wp-on--mouseenter' ) );
$this->assertSame( 'core/query::actions.prefetch', $p->get_attribute( 'data-wp-on-async--mouseenter' ) );
$this->assertSame( 'core/query::callbacks.prefetch', $p->get_attribute( 'data-wp-watch' ) );

$p->next_tag( array( 'class_name' => 'wp-block-query-pagination-next' ) );
$this->assertSame( 'query-pagination-next', $p->get_attribute( 'data-wp-key' ) );
$this->assertSame( 'core/query::actions.navigate', $p->get_attribute( 'data-wp-on--click' ) );
$this->assertSame( 'core/query::actions.prefetch', $p->get_attribute( 'data-wp-on--mouseenter' ) );
$this->assertSame( 'core/query::actions.prefetch', $p->get_attribute( 'data-wp-on-async--mouseenter' ) );
$this->assertSame( 'core/query::callbacks.prefetch', $p->get_attribute( 'data-wp-watch' ) );

$router_config = wp_interactivity_config( 'core/router' );
Expand Down
Loading