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

Product Gallery: Add keyboard access #11373

Merged
merged 5 commits into from
Oct 23, 2023
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
59 changes: 59 additions & 0 deletions assets/js/blocks/product-gallery/frontend.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ interface Actions {
thumbnails: {
handleClick: ( context: Context ) => void;
};
handlePreviousImageButtonClick: {
( store: Store ): void;
};
handleNextImageButtonClick: {
( store: Store ): void;
};
};
}

Expand All @@ -41,10 +47,63 @@ interface Store {
ref?: HTMLElement;
}

interface Event {
keyCode: number;
}

type SelectorsStore = Pick< Store, 'context' | 'selectors' | 'ref' >;

enum Keys {
ESC = 27,
LEFT_ARROW = 37,
RIGHT_ARROW = 39,
}

interactivityApiStore( {
state: {},
effects: {
woocommerce: {
keyboardAccess: ( store: Store ) => {
const { context, actions } = store;
let allowNavigation = true;

const handleKeyEvents = ( event: Event ) => {
if (
! allowNavigation ||
! context.woocommerce?.isDialogOpen
) {
return;
}

// Disable navigation for a brief period to prevent spamming.
allowNavigation = false;

requestAnimationFrame( () => {
allowNavigation = true;
} );

// Check if the esc key is pressed.
if ( event.keyCode === Keys.ESC ) {
context.woocommerce.isDialogOpen = false;
}

// Check if left arrow key is pressed.
if ( event.keyCode === Keys.LEFT_ARROW ) {
actions.woocommerce.handlePreviousImageButtonClick(
store
);
}

// Check if right arrow key is pressed.
if ( event.keyCode === Keys.RIGHT_ARROW ) {
actions.woocommerce.handleNextImageButtonClick( store );
}
};

document.addEventListener( 'keydown', handleKeyEvents );

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we remove this event listener when the gallery is not open?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes we probably should but I haven't found an elegant way to do that in this context. Any suggestions?

Copy link
Member Author

@roykho roykho Oct 20, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For some reasons it is only showing 3 images in the gallery for me. Is this expected?

Yes that's expected and being worked on else where.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes we probably should but I haven't found an elegant way to do that in this context. Any suggestions?

I'm also not sure, maybe somewhere we're handling the click. I'll take a deep dive into it and get back to you.

},
},
},
selectors: {
woocommerce: {
isSelected: ( { context }: Store ) => {
Expand Down
2 changes: 1 addition & 1 deletion src/BlockTypes/ProductGallery.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ function( $carry, $item ) {

$gallery_dialog = strtr(
'
<div class="wc-block-product-gallery-dialog__overlay" hidden data-wc-bind--hidden="!selectors.woocommerce.isDialogOpen">
<div class="wc-block-product-gallery-dialog__overlay" hidden data-wc-bind--hidden="!selectors.woocommerce.isDialogOpen" data-wc-effect="effects.woocommerce.keyboardAccess">
<dialog data-wc-bind--open="selectors.woocommerce.isDialogOpen">
<div class="wc-block-product-gallery-dialog__header">
<div class="wc-block-product-galler-dialog__header-right">
Expand Down