Skip to content

Commit

Permalink
Use queryRef to store a reference of the query block
Browse files Browse the repository at this point in the history
  • Loading branch information
DAreRodz committed Jan 24, 2024
1 parent 2cda3f5 commit fe2c923
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 9 deletions.
1 change: 1 addition & 0 deletions packages/block-library/src/query/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ function render_block_core_query( $attributes, $content, $block ) {
// Add the necessary directives.
$p->set_attribute( 'data-wp-interactive', '{"namespace":"core/query"}' );
$p->set_attribute( 'data-wp-router-region', 'query-' . $attributes['queryId'] );
$p->set_attribute( 'data-wp-init', 'callbacks.setQueryRef' );
// Use context to send translated strings.
$p->set_attribute(
'data-wp-context',
Expand Down
20 changes: 11 additions & 9 deletions packages/block-library/src/query/view.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,12 @@ store( 'core/query', {
*navigate( event ) {
const ctx = getContext();
const { ref } = getElement();
const isDisabled = ref.closest( '[data-wp-router-region]' )?.dataset
.wpNavigationDisabled;
const { queryRef } = ctx;
const isDisabled = queryRef?.dataset.wpNavigationDisabled;

if ( isValidLink( ref ) && isValidEvent( event ) && ! isDisabled ) {
event.preventDefault();

const id = ref.closest( '[data-wp-router-region]' ).dataset
.wpNavigationId;

// Don't announce the navigation immediately, wait 400 ms.
const timeout = setTimeout( () => {
ctx.message = ctx.loadingText;
Expand All @@ -65,14 +62,14 @@ store( 'core/query', {
ctx.url = ref.href;

// Focus the first anchor of the Query block.
const firstAnchor = `[data-wp-router-region=${ id }] .wp-block-post-template a[href]`;
document.querySelector( firstAnchor )?.focus();
const firstAnchor = `.wp-block-post-template a[href]`;
queryRef.querySelector( firstAnchor )?.focus();
}
},
*prefetch() {
const { queryRef } = getContext();
const { ref } = getElement();
const isDisabled = ref.closest( '[data-wp-router-region]' )?.dataset
.wpNavigationDisabled;
const isDisabled = queryRef?.dataset.wpNavigationDisabled;
if ( isValidLink( ref ) && ! isDisabled ) {
const { actions } = yield import(
'@wordpress/interactivity-router'
Expand All @@ -92,5 +89,10 @@ store( 'core/query', {
yield actions.prefetch( ref.href );
}
},
setQueryRef() {
const ctx = getContext();
const { ref } = getElement();
ctx.queryRef = ref;
},
},
} );

0 comments on commit fe2c923

Please sign in to comment.