@@ -6,42 +6,16 @@ function generateAriaId() {
66 return `_aria_auto_id_${ ariaIdCounter ++ } ` ;
77}
88
9- // make the item has role=option, and add an id if there wasn't one yet.
10- function prepareMenuItem ( $item ) {
11- if ( ! $item . attr ( 'id' ) ) $item . attr ( 'id' , generateAriaId ( ) ) ;
12- $item . attr ( { 'role' : 'menuitem' , 'tabindex' : '-1' } ) ;
13- $item . find ( 'a' ) . attr ( 'tabindex' , '-1' ) ; // as above, the elements inside the dropdown menu item should not be focusable, the focus should always be on the dropdown primary element.
14- }
15-
16- // when the menu items are loaded from AJAX requests, the items are created dynamically
17- const defaultCreateDynamicMenu = $ . fn . dropdown . settings . templates . menu ;
18- $ . fn . dropdown . settings . templates . menu = function ( response , fields , preserveHTML , className ) {
19- const ret = defaultCreateDynamicMenu ( response , fields , preserveHTML , className ) ;
20- const $wrapper = $ ( '<div>' ) . append ( ret ) ;
21- const $items = $wrapper . find ( '> .item' ) ;
22- $items . each ( ( _ , item ) => {
23- prepareMenuItem ( $ ( item ) ) ;
24- } ) ;
25- return $wrapper . html ( ) ;
26- } ;
27-
289function attachOneDropdownAria ( $dropdown ) {
29- if ( $dropdown . attr ( 'data-aria-attached' ) ) return ;
10+ if ( $dropdown . attr ( 'data-aria-attached' ) || $dropdown . hasClass ( 'custom' ) ) return ;
3011 $dropdown . attr ( 'data-aria-attached' , 1 ) ;
3112
32- const $textSearch = $dropdown . find ( 'input.search' ) . eq ( 0 ) ;
33- const $focusable = $textSearch . length ? $textSearch : $dropdown ; // see comment below
34- if ( ! $focusable . length ) return ;
35-
36- // prepare menu list
37- const $menu = $dropdown . find ( '> .menu' ) ;
38- if ( ! $menu . attr ( 'id' ) ) $menu . attr ( 'id' , generateAriaId ( ) ) ;
39-
40- // dropdown has 2 different focusing behaviors
41- // * with search input: the input is focused, and it works perfectly with aria-activedescendant pointing another sibling element.
13+ // Dropdown has 2 different focusing behaviors
14+ // * with search input: the input is focused, and it works with aria-activedescendant pointing another sibling element.
4215 // * without search input (but the readonly text), the dropdown itself is focused. then the aria-activedescendant points to the element inside dropdown
16+ // Some desktop screen readers may change the focus, but dropdown requires that the focus must be on its primary element, then they don't work well.
4317
44- // expected user interactions for dropdown with aria support:
18+ // Expected user interactions for dropdown with aria support:
4519 // * user can use Tab to focus in the dropdown, then the dropdown menu (list) will be shown
4620 // * user presses Tab on the focused dropdown to move focus to next sibling focusable element (but not the menu item)
4721 // * user can use arrow key Up/Down to navigate between menu items
@@ -51,31 +25,83 @@ function attachOneDropdownAria($dropdown) {
5125
5226 // TODO: multiple selection is not supported yet.
5327
54- $focusable . attr ( {
55- 'role' : 'menu' ,
56- 'aria-haspopup' : 'menu' ,
57- 'aria-controls' : $menu . attr ( 'id' ) ,
58- 'aria-expanded' : 'false' ,
59- } ) ;
28+ const $textSearch = $dropdown . find ( 'input.search' ) . eq ( 0 ) ;
29+ const $focusable = $textSearch . length ? $textSearch : $dropdown ; // the primary element for focus, see comment above
30+ if ( ! $focusable . length ) return ;
6031
61- if ( $dropdown . attr ( 'data-content' ) && ! $dropdown . attr ( 'aria-label' ) ) {
32+ // There are 2 possible solutions about the role: combobox or menu.
33+ // The idea is that if there is an input, then it's a combobox, otherwise it's a menu.
34+ // Since #19861 we have prepared the "combobox" solution, but didn't get enough time to put it into practice and test before.
35+ const isComboBox = $dropdown . find ( 'input' ) . length > 0 ;
36+
37+ const focusableRole = isComboBox ? 'combobox' : 'button' ;
38+ const listPopupRole = isComboBox ? 'listbox' : 'menu' ;
39+ const listItemRole = isComboBox ? 'option' : 'menuitem' ;
40+
41+ // make the item has role=option/menuitem, add an id if there wasn't one yet, make items as non-focusable
42+ // the elements inside the dropdown menu item should not be focusable, the focus should always be on the dropdown primary element.
43+ function prepareMenuItem ( $item ) {
44+ if ( ! $item . attr ( 'id' ) ) $item . attr ( 'id' , generateAriaId ( ) ) ;
45+ $item . attr ( { 'role' : listItemRole , 'tabindex' : '-1' } ) ;
46+ $item . find ( 'a' ) . attr ( 'tabindex' , '-1' ) ;
47+ }
48+
49+ // delegate the dropdown's template function to add aria attributes.
50+ // the "template" functions are used for dynamic creation (eg: AJAX)
51+ const dropdownTemplates = { ...$dropdown . dropdown ( 'setting' , 'templates' ) } ;
52+ const dropdownTemplatesMenuOld = dropdownTemplates . menu ;
53+ dropdownTemplates . menu = function ( response , fields , preserveHTML , className ) {
54+ // when the dropdown menu items are loaded from AJAX requests, the items are created dynamically
55+ const menuItems = dropdownTemplatesMenuOld ( response , fields , preserveHTML , className ) ;
56+ const $wrapper = $ ( '<div>' ) . append ( menuItems ) ;
57+ const $items = $wrapper . find ( '> .item' ) ;
58+ $items . each ( ( _ , item ) => prepareMenuItem ( $ ( item ) ) ) ;
59+ return $wrapper . html ( ) ;
60+ } ;
61+ $dropdown . dropdown ( 'setting' , 'templates' , dropdownTemplates ) ;
62+
63+ // use tooltip's content as aria-label if there is no aria-label
64+ if ( $dropdown . hasClass ( 'tooltip' ) && $dropdown . attr ( 'data-content' ) && ! $dropdown . attr ( 'aria-label' ) ) {
6265 $dropdown . attr ( 'aria-label' , $dropdown . attr ( 'data-content' ) ) ;
6366 }
6467
68+ // prepare dropdown menu list popup
69+ const $menu = $dropdown . find ( '> .menu' ) ;
70+ if ( ! $menu . attr ( 'id' ) ) $menu . attr ( 'id' , generateAriaId ( ) ) ;
6571 $menu . find ( '> .item' ) . each ( ( _ , item ) => {
6672 prepareMenuItem ( $ ( item ) ) ;
6773 } ) ;
74+ // this role could only be changed after its content is ready, otherwise some browsers+readers (like Chrome+AppleVoice) crash
75+ $menu . attr ( 'role' , listPopupRole ) ;
6876
69- // update aria attributes according to current active/selected item
70- const refreshAria = ( ) => {
71- const isMenuVisible = ! $menu . is ( '.hidden' ) && ! $menu . is ( '.animating.out' ) ;
72- $focusable . attr ( 'aria-expanded' , isMenuVisible ? 'true' : 'false' ) ;
77+ // make the primary element (focusable) aria-friendly
78+ $focusable . attr ( {
79+ 'role' : $focusable . attr ( 'role' ) ?? focusableRole ,
80+ 'aria-haspopup' : listPopupRole ,
81+ 'aria-controls' : $menu . attr ( 'id' ) ,
82+ 'aria-expanded' : 'false' ,
83+ } ) ;
7384
74- let $active = $menu . find ( '> .item.active' ) ;
75- if ( ! $active . length ) $active = $menu . find ( '> .item.selected' ) ; // it's strange that we need this fallback at the moment
85+ // when showing, it has class: ".animating.in"
86+ // when hiding, it has class: ".visible.animating.out"
87+ const isMenuVisible = ( ) => ( $menu . hasClass ( 'visible' ) && ! $menu . hasClass ( 'out' ) ) || $menu . hasClass ( 'in' ) ;
7688
77- // if there is an active item, use its id. if no active item, then the empty string is set
78- $focusable . attr ( 'aria-activedescendant' , $active . attr ( 'id' ) ) ;
89+ // update aria attributes according to current active/selected item
90+ const refreshAria = ( ) => {
91+ const menuVisible = isMenuVisible ( ) ;
92+ $focusable . attr ( 'aria-expanded' , menuVisible ? 'true' : 'false' ) ;
93+
94+ // if there is an active item, use it (the user is navigating between items)
95+ // otherwise use the "selected" for combobox (for the last selected item)
96+ const $active = $menu . find ( '> .item.active, > .item.selected' ) ;
97+ // if the popup is visible and has an active/selected item, use its id as aria-activedescendant
98+ if ( menuVisible ) {
99+ $focusable . attr ( 'aria-activedescendant' , $active . attr ( 'id' ) ) ;
100+ } else if ( ! isComboBox ) {
101+ // for menu, when the popup is hidden, no need to keep the aria-activedescendant, and clear the active/selected item
102+ $focusable . removeAttr ( 'aria-activedescendant' ) ;
103+ $active . removeClass ( 'active' ) . removeClass ( 'selected' ) ;
104+ }
79105 } ;
80106
81107 $dropdown . on ( 'keydown' , ( e ) => {
@@ -85,16 +111,51 @@ function attachOneDropdownAria($dropdown) {
85111 if ( ! $item ) $item = $menu . find ( '> .item.selected' ) ; // when dropdown filters items by input, there is no "value", so query the "selected" item
86112 // if the selected item is clickable, then trigger the click event.
87113 // we can not click any item without check, because Fomantic code might also handle the Enter event. that would result in double click.
88- if ( $item && ( $item . is ( 'a' ) || $item . is ( '. js-aria-clickable') ) ) $item [ 0 ] . click ( ) ;
114+ if ( $item && ( $item . is ( 'a' ) || $item . hasClass ( ' js-aria-clickable') ) ) $item [ 0 ] . click ( ) ;
89115 }
90116 } ) ;
91117
92118 // use setTimeout to run the refreshAria in next tick (to make sure the Fomantic UI code has finished its work)
93- const deferredRefreshAria = ( ) => { setTimeout ( refreshAria , 0 ) } ; // do not return any value, jQuery has return-value related behaviors.
94- $focusable . on ( 'focus' , deferredRefreshAria ) ;
95- $focusable . on ( 'mouseup' , deferredRefreshAria ) ;
96- $focusable . on ( 'blur' , deferredRefreshAria ) ;
119+ // do not return any value, jQuery has return-value related behaviors.
120+ // when the popup is hiding, it's better to have a small "delay", because there is a Fomantic UI animation
121+ // without the delay for hiding, the UI will be somewhat laggy and sometimes may get stuck in the animation.
122+ const deferredRefreshAria = ( delay = 0 ) => { setTimeout ( refreshAria , delay ) } ;
97123 $dropdown . on ( 'keyup' , ( e ) => { if ( e . key . startsWith ( 'Arrow' ) ) deferredRefreshAria ( ) ; } ) ;
124+
125+ // if the dropdown has been opened by focus, do not trigger the next click event again.
126+ // otherwise the dropdown will be closed immediately, especially on Android with TalkBack
127+ // * desktop event sequence: mousedown -> focus -> mouseup -> click
128+ // * mobile event sequence: focus -> mousedown -> mouseup -> click
129+ // Fomantic may stop propagation of blur event, use capture to make sure we can still get the event
130+ let ignoreClickPreEvents = 0 , ignoreClickPreVisible = 0 ;
131+ $dropdown [ 0 ] . addEventListener ( 'mousedown' , ( ) => {
132+ ignoreClickPreVisible += isMenuVisible ( ) ? 1 : 0 ;
133+ ignoreClickPreEvents ++ ;
134+ } , true ) ;
135+ $dropdown [ 0 ] . addEventListener ( 'focus' , ( ) => {
136+ ignoreClickPreVisible += isMenuVisible ( ) ? 1 : 0 ;
137+ ignoreClickPreEvents ++ ;
138+ deferredRefreshAria ( ) ;
139+ } , true ) ;
140+ $dropdown [ 0 ] . addEventListener ( 'blur' , ( ) => {
141+ ignoreClickPreVisible = ignoreClickPreEvents = 0 ;
142+ deferredRefreshAria ( 100 ) ;
143+ } , true ) ;
144+ $dropdown [ 0 ] . addEventListener ( 'mouseup' , ( ) => {
145+ setTimeout ( ( ) => {
146+ ignoreClickPreVisible = ignoreClickPreEvents = 0 ;
147+ deferredRefreshAria ( 100 ) ;
148+ } , 0 ) ;
149+ } , true ) ;
150+ $dropdown [ 0 ] . addEventListener ( 'click' , ( e ) => {
151+ if ( isMenuVisible ( ) &&
152+ ignoreClickPreVisible !== 2 && // dropdown is switch from invisible to visible
153+ ignoreClickPreEvents === 2 // the click event is related to mousedown+focus
154+ ) {
155+ e . stopPropagation ( ) ; // if the dropdown menu has been opened by focus, do not trigger the next click event again
156+ }
157+ ignoreClickPreEvents = ignoreClickPreVisible = 0 ;
158+ } , true ) ;
98159}
99160
100161export function attachDropdownAria ( $dropdowns ) {
0 commit comments