Skip to content

Commit 6244184

Browse files
committed
Accessibility: Require Alt key for arrow navigation in theme and media modals
1 parent aa72dfe commit 6244184

2 files changed

Lines changed: 28 additions & 21 deletions

File tree

src/js/_enqueues/wp/theme.js

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -909,7 +909,7 @@ themes.view.Preview = themes.view.Details.extend({
909909
'click .devices button': 'previewDevice',
910910
'click .previous-theme': 'previousTheme',
911911
'click .next-theme': 'nextTheme',
912-
'keyup': 'keyEvent',
912+
'keydown': 'keyEvent',
913913
'click .theme-install': 'installTheme'
914914
},
915915

@@ -1012,18 +1012,20 @@ themes.view.Preview = themes.view.Details.extend({
10121012
this.close();
10131013
}
10141014

1015-
// Return if Ctrl + Shift or Shift key pressed
1016-
if ( event.shiftKey || ( event.ctrlKey && event.shiftKey ) ) {
1015+
// Arrow key navigation requires Alt key to avoid interfering with screen reader navigation.
1016+
if ( ! event.altKey || event.repeat ) {
10171017
return;
10181018
}
10191019

10201020
// The right arrow key, next theme.
10211021
if ( event.keyCode === 39 ) {
1022-
_.once( this.nextTheme() );
1022+
event.preventDefault();
1023+
this.nextTheme();
10231024
}
10241025

10251026
// The left arrow key, previous theme.
10261027
if ( event.keyCode === 37 ) {
1028+
event.preventDefault();
10271029
this.previousTheme();
10281030
}
10291031
},
@@ -1111,7 +1113,7 @@ themes.view.Themes = wp.Backbone.View.extend({
11111113
} );
11121114

11131115
// Bind keyboard events.
1114-
$( 'body' ).on( 'keyup', function( event ) {
1116+
$( 'body' ).on( 'keydown.wp-themes', function( event ) {
11151117
if ( ! self.overlay ) {
11161118
return;
11171119
}
@@ -1121,25 +1123,27 @@ themes.view.Themes = wp.Backbone.View.extend({
11211123
return;
11221124
}
11231125

1124-
// Return if Ctrl + Shift or Shift key pressed
1125-
if ( event.shiftKey || ( event.ctrlKey && event.shiftKey ) ) {
1126+
// Pressing the escape key fires a theme:collapse event.
1127+
if ( event.keyCode === 27 ) {
1128+
self.overlay.collapse( event );
1129+
}
1130+
1131+
// Arrow key navigation requires Alt key to avoid interfering with screen reader navigation.
1132+
if ( ! event.altKey || event.repeat ) {
11261133
return;
11271134
}
11281135

1129-
// Pressing the right arrow key fires a theme:next event.
1136+
// Pressing Alt + right arrow key fires a theme:next event.
11301137
if ( event.keyCode === 39 ) {
1138+
event.preventDefault();
11311139
self.overlay.nextTheme();
11321140
}
11331141

1134-
// Pressing the left arrow key fires a theme:previous event.
1142+
// Pressing Alt + left arrow key fires a theme:previous event.
11351143
if ( event.keyCode === 37 ) {
1144+
event.preventDefault();
11361145
self.overlay.previousTheme();
11371146
}
1138-
1139-
// Pressing the escape key fires a theme:collapse event.
1140-
if ( event.keyCode === 27 ) {
1141-
self.overlay.collapse( event );
1142-
}
11431147
});
11441148
},
11451149

src/js/media/views/frame/edit-attachments.js

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -247,25 +247,28 @@ EditAttachments = MediaFrame.extend(/** @lends wp.media.view.MediaFrame.EditAtta
247247
return ( this.getCurrentIndex() - 1 ) > -1;
248248
},
249249
/**
250-
* Respond to the keyboard events: right arrow, left arrow, except when
251-
* focus is in a textarea or input field.
250+
* Respond to the keyboard events: Alt + right arrow, Alt + left arrow,
251+
* except when focus is in an interactive field. Requires the Alt modifier
252+
* key to avoid interfering with screen reader navigation.
252253
*/
253254
keyEvent: function( event ) {
254-
if ( ( 'INPUT' === event.target.nodeName || 'TEXTAREA' === event.target.nodeName ) && ! event.target.disabled ) {
255+
if ( ( 'INPUT' === event.target.nodeName || 'TEXTAREA' === event.target.nodeName || 'SELECT' === event.target.nodeName || 'BUTTON' === event.target.nodeName || 'A' === event.target.nodeName ) && ! event.target.disabled ) {
255256
return;
256257
}
257258

258-
// Return if Ctrl + Shift or Shift key pressed
259-
if ( event.shiftKey || ( event.ctrlKey && event.shiftKey ) ) {
259+
// Arrow key navigation requires Alt key to avoid interfering with screen reader navigation.
260+
if ( ! event.altKey || event.repeat ) {
260261
return;
261262
}
262263

263-
// The right arrow key.
264+
// Alt + right arrow key.
264265
if ( 39 === event.keyCode ) {
266+
event.preventDefault();
265267
this.nextMediaItem();
266268
}
267-
// The left arrow key.
269+
// Alt + left arrow key.
268270
if ( 37 === event.keyCode ) {
271+
event.preventDefault();
269272
this.previousMediaItem();
270273
}
271274
},

0 commit comments

Comments
 (0)