@@ -86,13 +86,21 @@ function attachOneDropdownAria($dropdown) {
86
86
87
87
// update aria attributes according to current active/selected item
88
88
const refreshAria = ( ) => {
89
- $focusable . attr ( 'aria-expanded' , isMenuVisible ( ) ? 'true' : 'false' ) ;
90
-
91
- let $active = $menu . find ( '> .item.active' ) ;
92
- if ( ! $active . length ) $active = $menu . find ( '> .item.selected' ) ; // it's strange that we need this fallback at the moment
93
-
94
- // if there is an active item, use its id. if no active item, then the empty string is set
95
- $focusable . attr ( 'aria-activedescendant' , $active . attr ( 'id' ) ) ;
89
+ const menuVisible = isMenuVisible ( ) ;
90
+ $focusable . attr ( 'aria-expanded' , menuVisible ? 'true' : 'false' ) ;
91
+
92
+ // if there is an active item, use it (the user is navigating between items)
93
+ // otherwise use the "selected" for combobox (for the last selected item)
94
+ let $active = $menu . find ( '> .item.active, > .item.selected' ) ;
95
+
96
+ // if there is an active item, use its id. if no active item or the dropdown is used as menu and is hidden, empty the active item
97
+ const activeId = $active . attr ( 'id' ) ;
98
+ if ( menuVisible && activeId ) {
99
+ $focusable . attr ( 'aria-activedescendant' , $active . attr ( 'id' ) ) ;
100
+ } else if ( ! isComboBox && ! menuVisible ) {
101
+ $focusable . removeAttr ( 'aria-activedescendant' ) ;
102
+ $active . removeClass ( 'active' ) . removeClass ( 'selected' ) ;
103
+ }
96
104
} ;
97
105
98
106
$dropdown . on ( 'keydown' , ( e ) => {
0 commit comments