Make WordPress Core

Changeset 62878


Ignore:
Timestamp:
07/28/2026 08:05:34 PM (4 months ago)
Author:
joedolson
Message:

Accessibility: In modal collections use alt plus arrow for navigation.

The theme browsers and the media attachment browser supported left and right arrow keys to trigger navigation. These interfered with screen reader reading commands, which also use the left and right arrow keys, forcing navigation instead of allowing the screen reader user to read additional content.

Require that the alt key is also pressed to trigger navigation. Also add debounced screen reader announcements to notify users of the new modal context.

Developed in ​https://github.com/WordPress/wordpress-develop/pull/11560

Props joedolson, sukhendu2002, afercia.
Fixes #63760.

Location:
trunk
Files:
9 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/js/_enqueues/wp/customize/controls.js

    r62431 r62878  
    17021702                headerContainer: null,
    17031703                updateCountDebounced: null,
     1704                announceThemeDebounced: null,
    17041705
    17051706                /**
    … …  
    17251726                        api.Section.prototype.initialize.call( section, id, options );
    17261727                        section.updateCountDebounced = _.debounce( section.updateCount, 500 );
     1728                        section.announceThemeDebounced = _.debounce( function( name ) {
     1729                                if ( ! name ) {
     1730                                        return;
     1731                                }
     1732
     1733                                wp.a11y.speak( api.settings.l10n.announceThemeDetails.replace( '%s', name ) );
     1734                        }, 500 );
    17271735                },
    17281736
    … …  
    17781786                                }
    17791787
     1788                                // Require the alt key for arrow events.
     1789                                if ( 27 !== event.keyCode && ! event.altKey ) {
     1790                                        return;
     1791                                }
     1792
    17801793                                // Pressing the right arrow key fires a theme:next event.
    17811794                                if ( 39 === event.keyCode ) {
     1795                                        event.preventDefault(); // Prevent browser from triggering history shortcuts.
    17821796                                        section.nextTheme();
    17831797                                }
    … …  
    17851799                                // Pressing the left arrow key fires a theme:previous event.
    17861800                                if ( 37 === event.keyCode ) {
     1801                                        event.preventDefault(); // Prevent browser from triggering history shortcuts.
    17871802                                        section.previousTheme();
    17881803                                }
    … …  
    26032618                        section.containFocus( section.overlay );
    26042619                        section.updateLimits();
    2605                         wp.a11y.speak( api.settings.l10n.announceThemeDetails.replace( '%s', theme.name ) );
     2620
     2621                        section.announceThemeDebounced( theme.name );
    26062622                        if ( callback ) {
    26072623                                callback();
    … …  
    26212637                        section.overlay.fadeOut( 'fast' );
    26222638                        api.control( section.params.action + '_theme_' + section.currentTheme ).container.find( '.theme' ).focus();
     2639                        // Cancel any pending navigation announcement.
     2640                        section.announceThemeDebounced.cancel();
    26232641                },
    26242642
  • trunk/src/js/_enqueues/wp/theme.js

    r62869 r62878  
    1616themes.data = _wpThemeSettings;
    1717l10n = themes.data.l10n;
     18
     19/**
     20 * Announces to screen readers the theme shown after previous/next navigation.
     21 *
     22 * @since 7.1.0
     23 *
     24 * @param {Object} model The theme model.
     25 * @return {void}
     26 */
     27themes.announceThemeDebounced = _.debounce( function( model ) {
     28        var name;
     29
     30        if ( ! model ) {
     31                return;
     32        }
     33
     34        name = model.get( 'name' ) || model.get( 'id' );
     35
     36        if ( ! name ) {
     37                return;
     38        }
     39
     40        wp.a11y.speak( l10n.themeViewed.replace( '%s', name ) );
     41}, 500 );
    1842
    1943// Shortcut for isInstall check.
    … …  
    550574                        this.setNavButtonsState();
    551575                        $( '.next-theme' ).trigger( 'focus' );
     576                        themes.announceThemeDebounced( self.current );
    552577                })
    553578                .listenTo( preview, 'theme:previous', function() {
    … …  
    580605                        this.setNavButtonsState();
    581606                        $( '.previous-theme' ).trigger( 'focus' );
     607                        themes.announceThemeDebounced( self.current );
    582608                });
    583609
    … …  
    770796                        });
    771797                }
     798
     799                // Cancel any pending navigation announcement.
     800                themes.announceThemeDebounced.cancel();
    772801        },
    773802
    … …  
    910939                'click .previous-theme': 'previousTheme',
    911940                'click .next-theme': 'nextTheme',
    912                 'keyup': 'keyEvent',
     941                'keydown': 'keyEvent',
    913942                'click .theme-install': 'installTheme'
    914943        },
    … …  
    968997                this.undelegateEvents();
    969998                this.unbind();
     999
     1000                // Cancel any pending navigation announcement.
     1001                themes.announceThemeDebounced.cancel();
    9701002                return false;
    9711003        },
    … …  
    10131045                }
    10141046
    1015                 // Return if Ctrl + Shift or Shift key pressed
    1016                 if ( event.shiftKey || ( event.ctrlKey && event.shiftKey ) ) {
     1047                // Arrow key navigation requires Alt key to avoid interfering with screen reader navigation.
     1048                if ( ! event.altKey ) {
    10171049                        return;
    10181050                }
    … …  
    10201052                // The right arrow key, next theme.
    10211053                if ( event.keyCode === 39 ) {
    1022                         _.once( this.nextTheme() );
     1054                        event.preventDefault();
     1055                        this.nextTheme();
    10231056                }
    10241057
    10251058                // The left arrow key, previous theme.
    10261059                if ( event.keyCode === 37 ) {
     1060                        event.preventDefault();
    10271061                        this.previousTheme();
    10281062                }
    … …  
    11121146
    11131147                // Bind keyboard events.
    1114                 $( 'body' ).on( 'keyup', function( event ) {
     1148                $( 'body' ).on( 'keydown.wp-themes', function( event ) {
    11151149                        if ( ! self.overlay ) {
    11161150                                return;
    … …  
    11221156                        }
    11231157
    1124                         // Return if Ctrl + Shift or Shift key pressed
    1125                         if ( event.shiftKey || ( event.ctrlKey && event.shiftKey ) ) {
    1126                                 return;
    1127                         }
    1128 
    1129                         // Pressing the right arrow key fires a theme:next event.
    1130                         if ( event.keyCode === 39 ) {
    1131                                 self.overlay.nextTheme();
    1132                         }
    1133 
    1134                         // Pressing the left arrow key fires a theme:previous event.
    1135                         if ( event.keyCode === 37 ) {
    1136                                 self.overlay.previousTheme();
    1137                         }
    1138 
    11391158                        // Pressing the escape key fires a theme:collapse event.
    11401159                        if ( event.keyCode === 27 ) {
    11411160                                self.overlay.collapse( event );
     1161                        }
     1162
     1163                        // Arrow key navigation requires Alt key to avoid interfering with screen reader navigation.
     1164                        if ( ! event.altKey ) {
     1165                                return;
     1166                        }
     1167
     1168                        // Pressing Alt + right arrow key fires a theme:next event.
     1169                        if ( event.keyCode === 39 ) {
     1170                                event.preventDefault();
     1171                                self.overlay.nextTheme();
     1172                        }
     1173
     1174                        // Pressing Alt + left arrow key fires a theme:previous event.
     1175                        if ( event.keyCode === 37 ) {
     1176                                event.preventDefault();
     1177                                self.overlay.previousTheme();
    11421178                        }
    11431179                });
    … …  
    13231359                        // Trigger a route update for the current model.
    13241360                        self.theme.trigger( 'theme:expand', nextModel.cid );
    1325 
     1361                        themes.announceThemeDebounced( nextModel );
    13261362                }
    13271363        },
    … …  
    13581394                        // Trigger a route update for the current model.
    13591395                        self.theme.trigger( 'theme:expand', previousModel.cid );
    1360 
     1396                        themes.announceThemeDebounced( previousModel );
    13611397                }
    13621398        },
  • trunk/src/js/media/views/frame/edit-attachments.js

    r60516 r62878  
    11var Frame = wp.media.view.Frame,
    22        MediaFrame = wp.media.view.MediaFrame,
     3        l10n = wp.media.view.l10n,
    34
    45        $ = jQuery,
    … …  
    3435        },
    3536
     37        /**
     38         * Announces to screen readers the attachment shown after previous/next navigation.
     39         *
     40         * @since 7.1.0
     41         *
     42         * @param {Object} model The attachment model.
     43         * @return {void}
     44         */
     45        announceMediaItemDebounced: _.debounce( function( model ) {
     46                var title;
     47
     48                if ( ! model ) {
     49                        return;
     50                }
     51
     52                title = model.get( 'title' ) || model.get( 'filename' ) || model.get( 'id' );
     53
     54                if ( ! title ) {
     55                        return;
     56                }
     57
     58                wp.a11y.speak( l10n.mediaItemViewed.replace( '%s', title ) );
     59        }, 500 ),
     60
    3661        initialize: function() {
    3762                Frame.prototype.initialize.apply( this, arguments );
    … …  
    97122                                $( 'li.attachment[data-id="' + this.model.get( 'id' ) +'"]' ).trigger( 'focus' );
    98123                                this.resetRoute();
     124                                // Cancel any pending navigation announcement.
     125                                this.announceMediaItemDebounced.cancel();
    99126                        }, this ) );
    100127
    … …  
    203230         */
    204231        previousMediaItem: function() {
     232                var model;
     233
    205234                if ( ! this.hasPrevious() ) {
    206235                        return;
    207236                }
    208237
    209                 this.trigger( 'refresh', this.library.at( this.getCurrentIndex() - 1 ) );
     238                model = this.library.at( this.getCurrentIndex() - 1 );
     239                this.trigger( 'refresh', model );
    210240                // Move focus to the Previous button. When there are no more items, to the Next button.
    211241                this.focusNavButton( this.hasPrevious() ? '.left' : '.right' );
     242                this.announceMediaItemDebounced( model );
    212243        },
    213244
    … …  
    216247         */
    217248        nextMediaItem: function() {
     249                var model;
     250
    218251                if ( ! this.hasNext() ) {
    219252                        return;
    220253                }
    221254
    222                 this.trigger( 'refresh', this.library.at( this.getCurrentIndex() + 1 ) );
     255                model = this.library.at( this.getCurrentIndex() + 1 );
     256                this.trigger( 'refresh', model );
    223257                // Move focus to the Next button. When there are no more items, to the Previous button.
    224258                this.focusNavButton( this.hasNext() ? '.right' : '.left' );
     259                this.announceMediaItemDebounced( model );
    225260        },
    226261
    … …  
    248283        },
    249284        /**
    250          * Respond to the keyboard events: right arrow, left arrow, except when
    251          * focus is in a textarea or input field.
     285         * Respond to the keyboard events: Alt + right arrow, Alt + left arrow,
     286         * except when focus is in a form field. Requires the Alt modifier key to
     287         * avoid interfering with screen reader navigation.
    252288         */
    253289        keyEvent: function( event ) {
    254                 if ( ( 'INPUT' === event.target.nodeName || 'TEXTAREA' === event.target.nodeName ) && ! event.target.disabled ) {
    255                         return;
    256                 }
    257 
    258                 // Return if Ctrl + Shift or Shift key pressed
    259                 if ( event.shiftKey || ( event.ctrlKey && event.shiftKey ) ) {
    260                         return;
    261                 }
    262 
    263                 // The right arrow key.
     290                if ( ( 'INPUT' === event.target.nodeName || 'TEXTAREA' === event.target.nodeName || 'SELECT' === event.target.nodeName ) && ! event.target.disabled ) {
     291                        return;
     292                }
     293
     294                // Arrow key navigation requires Alt key to avoid interfering with screen reader navigation.
     295                if ( ! event.altKey ) {
     296                        return;
     297                }
     298
     299                // Alt + right arrow key.
    264300                if ( 39 === event.keyCode ) {
     301                        event.preventDefault();
    265302                        this.nextMediaItem();
    266303                }
    267                 // The left arrow key.
     304                // Alt + left arrow key.
    268305                if ( 37 === event.keyCode ) {
     306                        event.preventDefault();
    269307                        this.previousMediaItem();
    270308                }
  • trunk/src/wp-admin/theme-install.php

    r62516 r62878  
    6868                        'themesFound'         => __( 'Number of Themes found: %d' ),
    6969                        'noThemesFound'       => __( 'No themes found. Try a different search.' ),
     70                        /* translators: %s: Theme name. */
     71                        'themeViewed'         => __( 'Theme details: %s' ),
    7072                        'collapseSidebar'     => __( 'Collapse Sidebar' ),
    7173                        'expandSidebar'       => __( 'Expand Sidebar' ),
  • trunk/src/wp-admin/themes.php

    r62516 r62878  
    132132        $help_overview = '<p>' . __( 'This screen is used for managing your installed themes. Aside from the default theme(s) included with your WordPress installation, themes are designed and developed by third parties.' ) . '</p>' .
    133133                '<p>' . __( 'From this screen you can:' ) . '</p>' .
    134                 '<ul><li>' . __( 'Hover or tap to see Activate and Live Preview buttons' ) . '</li>' .
    135                 '<li>' . __( 'Click on the theme to see the theme name, version, author, description, tags, and the Delete link' ) . '</li>' .
    136                 '<li>' . __( 'Click Customize for the active theme or Live Preview for any other theme to see a live preview' ) . '</li></ul>' .
     134                '<ul><li>' . __( 'Hover or tap to see Activate and Live Preview buttons.' ) . '</li>' .
     135                '<li>' . __( 'Click Customize for the active theme or Live Preview for any other theme to see a live preview.' ) . '</li>' .
     136                '<li>' . __( 'Click on a theme to open the Theme Details dialog and see the theme name, version, author, description, tags, and the Delete link.' ) . '</li>' .
     137                '<li>' . __( 'Use the buttons at the top of the dialog, or <code>alt</code>/<code>option</code> plus the left or right arrow keys on your keyboard, to navigate between themes quickly.' ) . '</li></ul>' .
    137138                '<p>' . __( 'The active theme is displayed highlighted as the first theme.' ) . '</p>' .
    138139                '<p>' . __( 'The search for installed themes will search for terms in their name, description, author, or tag.' ) . ' <span id="live-search-desc">' . __( 'The search results will be updated as you type.' ) . '</span></p>';
    … …  
    237238                        'themesFound'   => __( 'Number of Themes found: %d' ),
    238239                        'noThemesFound' => __( 'No themes found. Try a different search.' ),
     240                        /* translators: %s: Theme name. */
     241                        'themeViewed'   => __( 'Theme details: %s' ),
    239242                ),
    240243        )
  • trunk/src/wp-admin/upload.php

    r61453 r62878  
    191191                        'content' =>
    192192                                '<p>' . __( 'Clicking an item will display an Attachment Details dialog, which allows you to preview media and make quick edits. Any changes you make to the attachment details will be automatically saved.' ) . '</p>' .
    193                                 '<p>' . __( 'Use the arrow buttons at the top of the dialog, or the left and right arrow keys on your keyboard, to navigate between media items quickly.' ) . '</p>' .
     193                                '<p>' . __( 'Use the buttons at the top of the dialog, or <code>alt</code>/<code>option</code> plus the left or right arrow keys on your keyboard, to navigate between media items quickly.' ) . '</p>' .
    194194                                '<p>' . __( 'You can also delete individual items and access the extended edit screen from the details dialog.' ) . '</p>',
    195195                )
  • trunk/src/wp-includes/class-wp-customize-manager.php

    r62763 r62878  
    49604960                                'announceThemeCount'   => __( 'Displaying %d themes' ),
    49614961                                /* translators: %s: Theme name. */
    4962                                 'announceThemeDetails' => __( 'Showing details for theme: %s' ),
     4962                                'announceThemeDetails' => __( 'Theme details: %s' ),
    49634963                        ),
    49644964                );
  • trunk/src/wp-includes/media.php

    r62807 r62878  
    51905190                'noMedia'                     => __( 'No media items found.' ),
    51915191                'noMediaTryNewSearch'         => __( 'No media items found. Try a different search.' ),
     5192                /* translators: %s: Media item title or file name. */
     5193                'mediaItemViewed'             => __( 'Viewing media item: %s' ),
    51925194
    51935195                // Library Details.
  • trunk/tests/qunit/wp-admin/js/theme.js

    r61629 r62878  
    1313                                previousTheme: function() { prevCalled++; },
    1414                                keyEvent: function( event ) {
    15                                         if ( event.shiftKey || event.ctrlKey || event.altKey || event.metaKey ) {
     15                                        if ( event.shiftKey || event.ctrlKey || event.metaKey ) {
    1616                                                return;
    1717                                        }
    1818
    1919                                        // Right arrow
    20                                         if ( event.keyCode === 39 ) {
     20                                        if ( event.altKey && event.keyCode === 39 ) {
     21                                                event.preventDefault();
    2122                                                this.nextTheme();
    2223                                        }
    2324                                        // Left arrow
    24                                         else if ( event.keyCode === 37 ) {
     25                                        else if ( event.altKey && event.keyCode === 37 ) {
     26                                                event.preventDefault();
    2527                                                this.previousTheme();
    2628                                        }
    … …  
    3537                });
    3638
    37                 QUnit.test( 'Arrow keys without modifiers', function( assert ) {
     39                QUnit.test( 'Arrow keys with Alt modifier', function( assert ) {
    3840                        // Right arrow
    3941                        themePreview.keyEvent( $.Event( 'keydown', {
    4042                                keyCode: 39,
     43                                altKey: true,
    4144                                shiftKey: false,
    4245                                ctrlKey: false
    4346                        }) );
    44                         assert.equal( nextCalled, 1, 'Right arrow triggers nextTheme' );
     47                        assert.equal( nextCalled, 1, 'Alt + Right arrow triggers nextTheme' );
    4548
    4649                        // Left arrow
    4750                        themePreview.keyEvent( $.Event( 'keydown', {
    4851                                keyCode: 37,
     52                                altKey: true,
    4953                                shiftKey: false,
    5054                                ctrlKey: false
    5155                        }) );
    52                         assert.equal( prevCalled, 1, 'Left arrow triggers previousTheme' );
     56                        assert.equal( prevCalled, 1, 'Alt + Left arrow triggers previousTheme' );
    5357                } );
     58
     59                QUnit.test( 'Arrow keys without Alt do nothing', function( assert ) {
     60                        // Right arrow without Alt - should NOT call nextTheme
     61                        themePreview.keyEvent( $.Event( 'keydown', {
     62                                keyCode: 39,
     63                                altKey: false,
     64                                shiftKey: false,
     65                                ctrlKey: false
     66                        }) );
     67                        assert.equal( nextCalled, 0, 'Right arrow without Alt does nothing' );
     68
     69                        // Left arrow without Alt - should NOT call previousTheme
     70                        themePreview.keyEvent( $.Event( 'keydown', {
     71                                keyCode: 37,
     72                                altKey: false,
     73                                shiftKey: false,
     74                                ctrlKey: false
     75                        }) );
     76                        assert.equal( prevCalled, 0, 'Left arrow without Alt does nothing' );
     77                } );
     78
     79                QUnit.test( 'PreventDefault is called for arrow keys with Alt', function( assert ) {
     80                        // This test would need to check if preventDefault was called
     81                        var event = $.Event( 'keydown', {
     82                                keyCode: 39,
     83                                altKey: true,
     84                                shiftKey: false,
     85                                ctrlKey: false
     86                        });
     87
     88                        // Mock the preventDefault method to track if it's called
     89                        var preventDefaultCalled = false;
     90                        event.preventDefault = function() {
     91                                preventDefaultCalled = true;
     92                        };
     93
     94                        themePreview.keyEvent( event );
     95                        assert.ok( preventDefaultCalled, 'preventDefault is called for arrow keys with Alt' );
     96                });
    5497
    5598                QUnit.test( 'Shift+Arrow keys do nothing', function( assert ) {
    … …  
    57100                        themePreview.keyEvent( $.Event( 'keydown', {
    58101                                keyCode: 39,
     102                                altKey: false,
    59103                                shiftKey: true,
    60104                                ctrlKey: false
    … …  
    65109                        themePreview.keyEvent( $.Event( 'keydown', {
    66110                                keyCode: 37,
     111                                altKey: false,
    67112                                shiftKey: true,
    68113                                ctrlKey: false
    … …  
    75120                        themePreview.keyEvent( $.Event( 'keydown', {
    76121                                keyCode: 39,
     122                                altKey: false,
    77123                                ctrlKey: true,
    78124                                shiftKey: false
    … …  
    83129                        themePreview.keyEvent( $.Event( 'keydown', {
    84130                                keyCode: 37,
     131                                altKey: false,
    85132                                ctrlKey: true,
    86133                                shiftKey: false
Note: See TracChangeset for help on using the changeset viewer.