Skip to content

Commit c7dda87

Browse files
committed
clarify the "item active" logic, use "removeAttr" instead of "attr(..., '')"
1 parent 4b3b66c commit c7dda87

File tree

1 file changed

+15
-7
lines changed

1 file changed

+15
-7
lines changed

web_src/js/features/aria.js

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -86,13 +86,21 @@ function attachOneDropdownAria($dropdown) {
8686

8787
// update aria attributes according to current active/selected item
8888
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+
}
96104
};
97105

98106
$dropdown.on('keydown', (e) => {

0 commit comments

Comments
 (0)