Skip to content

Commit

Permalink
Use withSyncEvent() in all built-in actions that require it.
Browse files Browse the repository at this point in the history
  • Loading branch information
felixarntz committed Jan 21, 2025
1 parent a582f01 commit 5d68fcb
Show file tree
Hide file tree
Showing 9 changed files with 59 additions and 29 deletions.
15 changes: 10 additions & 5 deletions packages/block-library/src/image/view.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
/**
* WordPress dependencies
*/
import { store, getContext, getElement } from '@wordpress/interactivity';
import {
store,
getContext,
getElement,
withSyncEvent,
} from '@wordpress/interactivity';

/**
* Tracks whether user is touching screen; used to differentiate behavior for
Expand Down Expand Up @@ -128,7 +133,7 @@ const { state, actions, callbacks } = store(
}, 450 );
}
},
handleKeydown( event ) {
handleKeydown: withSyncEvent( ( event ) => {
if ( state.overlayEnabled ) {
// Focuses the close button when the user presses the tab key.
if ( event.key === 'Tab' ) {
Expand All @@ -141,8 +146,8 @@ const { state, actions, callbacks } = store(
actions.hideLightbox();
}
}
},
handleTouchMove( event ) {
} ),
handleTouchMove: withSyncEvent( ( event ) => {
// On mobile devices, prevents triggering the scroll event because
// otherwise the page jumps around when it resets the scroll position.
// This also means that closing the lightbox requires that a user
Expand All @@ -152,7 +157,7 @@ const { state, actions, callbacks } = store(
if ( state.overlayEnabled ) {
event.preventDefault();
}
},
} ),
handleTouchStart() {
isTouching = true;
},
Expand Down
11 changes: 8 additions & 3 deletions packages/block-library/src/navigation/view.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
/**
* WordPress dependencies
*/
import { store, getContext, getElement } from '@wordpress/interactivity';
import {
store,
getContext,
getElement,
withSyncEvent,
} from '@wordpress/interactivity';

const focusableSelectors = [
'a[href]',
Expand Down Expand Up @@ -106,7 +111,7 @@ const { state, actions } = store(
actions.openMenu( 'click' );
}
},
handleMenuKeydown( event ) {
handleMenuKeydown: withSyncEvent( ( event ) => {
const { type, firstFocusableElement, lastFocusableElement } =
getContext();
if ( state.menuOpenedBy.click ) {
Expand Down Expand Up @@ -137,7 +142,7 @@ const { state, actions } = store(
}
}
}
},
} ),
handleMenuFocusout( event ) {
const { modal, type } = getContext();
// If focus is outside modal, and in the document, close menu
Expand Down
11 changes: 8 additions & 3 deletions packages/block-library/src/query/view.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
/**
* WordPress dependencies
*/
import { store, getContext, getElement } from '@wordpress/interactivity';
import {
store,
getContext,
getElement,
withSyncEvent,
} from '@wordpress/interactivity';

const isValidLink = ( ref ) =>
ref &&
Expand All @@ -22,7 +27,7 @@ store(
'core/query',
{
actions: {
*navigate( event ) {
navigate: withSyncEvent( function* ( event ) {
const ctx = getContext();
const { ref } = getElement();
const queryRef = ref.closest(
Expand All @@ -42,7 +47,7 @@ store(
const firstAnchor = `.wp-block-post-template a[href]`;
queryRef.querySelector( firstAnchor )?.focus();
}
},
} ),
*prefetch() {
const { ref } = getElement();
if ( isValidLink( ref ) ) {
Expand Down
11 changes: 8 additions & 3 deletions packages/block-library/src/search/view.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
/**
* WordPress dependencies
*/
import { store, getContext, getElement } from '@wordpress/interactivity';
import {
store,
getContext,
getElement,
withSyncEvent,
} from '@wordpress/interactivity';

const { actions } = store(
'core/search',
Expand Down Expand Up @@ -31,15 +36,15 @@ const { actions } = store(
},
},
actions: {
openSearchInput( event ) {
openSearchInput: withSyncEvent( ( event ) => {
const ctx = getContext();
const { ref } = getElement();
if ( ! ctx.isSearchInputVisible ) {
event.preventDefault();
ctx.isSearchInputVisible = true;
ref.parentElement.querySelector( 'input' ).focus();
}
},
} ),
closeSearchInput() {
const ctx = getContext();
ctx.isSearchInputVisible = false;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
/**
* WordPress dependencies
*/
import { store, getContext, getServerContext } from '@wordpress/interactivity';
import {
store,
getContext,
getServerContext,
withSyncEvent,
} from '@wordpress/interactivity';

store( 'test/get-server-context', {
actions: {
*navigate( e ) {
navigate: withSyncEvent( function* ( e ) {
e.preventDefault();
const { actions } = yield import(
'@wordpress/interactivity-router'
);
yield actions.navigate( e.target.href );
},
} ),
attemptModification() {
try {
getServerContext().prop = 'updated from client';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
/**
* WordPress dependencies
*/
import { store, getServerState, getContext } from '@wordpress/interactivity';
import {
store,
getServerState,
getContext,
withSyncEvent,
} from '@wordpress/interactivity';

const { state } = store( 'test/get-server-state', {
actions: {
*navigate( e ) {
navigate: withSyncEvent( function* ( e ) {
e.preventDefault();
const { actions } = yield import(
'@wordpress/interactivity-router'
);
yield actions.navigate( e.target.href );
},
} ),
attemptModification() {
try {
getServerState().prop = 'updated from client';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* WordPress dependencies
*/
import { store } from '@wordpress/interactivity';
import { store, withSyncEvent } from '@wordpress/interactivity';

const { state } = store( 'router', {
state: {
Expand All @@ -18,7 +18,7 @@ const { state } = store( 'router', {
},
},
actions: {
*navigate( e ) {
navigate: withSyncEvent( function* ( e ) {
e.preventDefault();

state.navigations.count += 1;
Expand All @@ -38,7 +38,7 @@ const { state } = store( 'router', {
if ( state.navigations.pending === 0 ) {
state.status = 'idle';
}
},
} ),
toggleTimeout() {
state.timeout = state.timeout === 10000 ? 0 : 10000;
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* WordPress dependencies
*/
import { store, getContext } from '@wordpress/interactivity';
import { store, getContext, withSyncEvent } from '@wordpress/interactivity';

const { state } = store( 'router-regions', {
state: {
Expand All @@ -17,13 +17,13 @@ const { state } = store( 'router-regions', {
},
actions: {
router: {
*navigate( e ) {
navigate: withSyncEvent( function* ( e ) {
e.preventDefault();
const { actions } = yield import(
'@wordpress/interactivity-router'
);
yield actions.navigate( e.target.href );
},
} ),
back() {
history.back();
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
/**
* WordPress dependencies
*/
import { store } from '@wordpress/interactivity';
import { store, withSyncEvent } from '@wordpress/interactivity';

const { state } = store( 'test/router-styles', {
state: {
clientSideNavigation: false,
},
actions: {
*navigate( e ) {
navigate: withSyncEvent( function* ( e ) {
e.preventDefault();
const { actions } = yield import(
'@wordpress/interactivity-router'
);
yield actions.navigate( e.target.href );
state.clientSideNavigation = true;
},
} ),
},
} );

0 comments on commit 5d68fcb

Please sign in to comment.