@@ -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+
4454type SelectorsStore = Pick < Store , 'context' | 'selectors' | 'ref' > ;
4555
56+ enum Keys {
57+ ESC = 27 ,
58+ LEFT_ARROW = 37 ,
59+ RIGHT_ARROW = 39 ,
60+ }
61+
4662interactivityApiStore ( {
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 ) => {
0 commit comments