Skip to content
This repository was archived by the owner on Feb 23, 2024. It is now read-only.

Commit 25cc415

Browse files
authored
Product Gallery: Add keyboard access (#11373)
* Product Gallery: Add keyboard access * Add optional chaining to guard against undefines and update delay logic * Use enums for key codes
1 parent e470bdb commit 25cc415

File tree

2 files changed

+60
-1
lines changed

2 files changed

+60
-1
lines changed

assets/js/blocks/product-gallery/frontend.tsx

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@ interface Actions {
3030
thumbnails: {
3131
handleClick: ( context: Context ) => void;
3232
};
33+
handlePreviousImageButtonClick: {
34+
( store: Store ): void;
35+
};
36+
handleNextImageButtonClick: {
37+
( store: Store ): void;
38+
};
3339
};
3440
}
3541

@@ -41,10 +47,63 @@ interface Store {
4147
ref?: HTMLElement;
4248
}
4349

50+
interface Event {
51+
keyCode: number;
52+
}
53+
4454
type SelectorsStore = Pick< Store, 'context' | 'selectors' | 'ref' >;
4555

56+
enum Keys {
57+
ESC = 27,
58+
LEFT_ARROW = 37,
59+
RIGHT_ARROW = 39,
60+
}
61+
4662
interactivityApiStore( {
4763
state: {},
64+
effects: {
65+
woocommerce: {
66+
keyboardAccess: ( store: Store ) => {
67+
const { context, actions } = store;
68+
let allowNavigation = true;
69+
70+
const handleKeyEvents = ( event: Event ) => {
71+
if (
72+
! allowNavigation ||
73+
! context.woocommerce?.isDialogOpen
74+
) {
75+
return;
76+
}
77+
78+
// Disable navigation for a brief period to prevent spamming.
79+
allowNavigation = false;
80+
81+
requestAnimationFrame( () => {
82+
allowNavigation = true;
83+
} );
84+
85+
// Check if the esc key is pressed.
86+
if ( event.keyCode === Keys.ESC ) {
87+
context.woocommerce.isDialogOpen = false;
88+
}
89+
90+
// Check if left arrow key is pressed.
91+
if ( event.keyCode === Keys.LEFT_ARROW ) {
92+
actions.woocommerce.handlePreviousImageButtonClick(
93+
store
94+
);
95+
}
96+
97+
// Check if right arrow key is pressed.
98+
if ( event.keyCode === Keys.RIGHT_ARROW ) {
99+
actions.woocommerce.handleNextImageButtonClick( store );
100+
}
101+
};
102+
103+
document.addEventListener( 'keydown', handleKeyEvents );
104+
},
105+
},
106+
},
48107
selectors: {
49108
woocommerce: {
50109
isSelected: ( { context }: Store ) => {

src/BlockTypes/ProductGallery.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ function( $carry, $item ) {
7777

7878
$gallery_dialog = strtr(
7979
'
80-
<div class="wc-block-product-gallery-dialog__overlay" hidden data-wc-bind--hidden="!selectors.woocommerce.isDialogOpen">
80+
<div class="wc-block-product-gallery-dialog__overlay" hidden data-wc-bind--hidden="!selectors.woocommerce.isDialogOpen" data-wc-effect="effects.woocommerce.keyboardAccess">
8181
<dialog data-wc-bind--open="selectors.woocommerce.isDialogOpen">
8282
<div class="wc-block-product-gallery-dialog__header">
8383
<div class="wc-block-product-galler-dialog__header-right">

0 commit comments

Comments
 (0)