@@ -14,6 +14,19 @@ jQuery( function ( $ ) {
14
14
}
15
15
} ) ;
16
16
17
+ // Global accessibility handler for all links with role="button"
18
+ // This ensures both spacebar and enter keypresses trigger click events, as per ARIA specification
19
+ document . body . addEventListener ( 'keydown' , function ( event ) {
20
+ if ( ! event . target . matches ( 'a[role="button"]' ) ) {
21
+ return ;
22
+ }
23
+
24
+ if ( event . key === ' ' || event . key === 'Enter' ) {
25
+ event . preventDefault ( ) ;
26
+ event . target . click ( ) ;
27
+ }
28
+ } ) ;
29
+
17
30
var noticeID = $ ( '.woocommerce-store-notice' ) . data ( 'noticeId' ) || '' ,
18
31
cookieName = 'store_notice' + noticeID ;
19
32
@@ -22,31 +35,22 @@ jQuery( function ( $ ) {
22
35
$ ( '.woocommerce-store-notice' ) . hide ( ) ;
23
36
} else {
24
37
$ ( '.woocommerce-store-notice' ) . show ( ) ;
25
- /**
26
- * After adding the role="button" attribute to the
27
- * .woocommerce-store-notice__dismiss-link element,
28
- * we need to add the keydown event listener to it.
29
- */
30
- function store_notice_keydown_handler ( event ) {
31
- if ( [ 'Enter' , ' ' ] . includes ( event . key ) ) {
32
- event . preventDefault ( ) ;
33
- $ ( '.woocommerce-store-notice__dismiss-link' ) . click ( ) ;
34
- }
35
- }
36
38
37
39
// Set a cookie and hide the store notice when the dismiss button is clicked
38
40
function store_notice_click_handler ( event ) {
39
41
Cookies . set ( cookieName , 'hidden' , { path : '/' } ) ;
40
42
$ ( '.woocommerce-store-notice' ) . hide ( ) ;
41
43
event . preventDefault ( ) ;
42
- $ ( '.woocommerce-store-notice__dismiss-link' )
43
- . off ( 'click' , store_notice_click_handler )
44
- . off ( 'keydown' , store_notice_keydown_handler ) ;
44
+ $ ( '.woocommerce-store-notice__dismiss-link' ) . off (
45
+ 'click' ,
46
+ store_notice_click_handler
47
+ ) ;
45
48
}
46
-
47
- $ ( '.woocommerce-store-notice__dismiss-link' )
48
- . on ( 'click' , store_notice_click_handler )
49
- . on ( 'keydown' , store_notice_keydown_handler ) ;
49
+
50
+ $ ( '.woocommerce-store-notice__dismiss-link' ) . on (
51
+ 'click' ,
52
+ store_notice_click_handler
53
+ ) ;
50
54
}
51
55
52
56
// Make form field descriptions toggle on focus.
@@ -182,27 +186,12 @@ jQuery( function ( $ ) {
182
186
} ) ;
183
187
} ) ;
184
188
185
- // If the "Enable AJAX add to cart buttons on archives" setting is disabled
186
- // the add-to-cart.js file won't be loaded, so we need to add the event listener here.
187
- if ( typeof wc_add_to_cart_params === 'undefined' ) {
188
- $ ( document . body ) . on ( 'keydown' , '.remove_from_cart_button' , on_keydown_remove_from_cart ) ;
189
- }
190
-
191
- $ ( document . body ) . on ( 'item_removed_from_classic_cart updated_wc_div' , focus_populate_live_region ) ;
189
+ $ ( document . body ) . on (
190
+ 'item_removed_from_classic_cart updated_wc_div' ,
191
+ focus_populate_live_region
192
+ ) ;
192
193
} ) ;
193
194
194
- /**
195
- * Handle when pressing the Space key on the remove item link.
196
- * This is necessary because the link has the role="button" attribute
197
- * and needs to act like a button.
198
- */
199
- function on_keydown_remove_from_cart ( event ) {
200
- if ( event . key === ' ' ) {
201
- event . preventDefault ( ) ;
202
- event . currentTarget . click ( ) ;
203
- }
204
- }
205
-
206
195
/**
207
196
* Focus on the first notice element on the page.
208
197
*
@@ -248,10 +237,10 @@ function refresh_sorted_by_live_region() {
248
237
249
238
if ( sorted_by_live_region ) {
250
239
var text = sorted_by_live_region . innerHTML ;
251
- sorted_by_live_region . setAttribute ( 'aria-hidden' , 'true' ) ;
252
-
240
+ sorted_by_live_region . setAttribute ( 'aria-hidden' , 'true' ) ;
241
+
253
242
var sorted_by_live_region_id = setTimeout ( function ( ) {
254
- sorted_by_live_region . setAttribute ( 'aria-hidden' , 'false' ) ;
243
+ sorted_by_live_region . setAttribute ( 'aria-hidden' , 'false' ) ;
255
244
sorted_by_live_region . innerHTML = '' ;
256
245
sorted_by_live_region . innerHTML = text ;
257
246
clearTimeout ( sorted_by_live_region_id ) ;
0 commit comments