Skip to content

Commit

Permalink
fix(dropdown): improve accessibility keyboard navigation of dropdown
Browse files Browse the repository at this point in the history
Prior behavior: tabbing would focus the dropdown and open it, subsequent tabs would focus on each
item of the list. Instead, the desired behavior is one tab opens the list, only the up and down keys
can be used to navigate the list, then the next tab would close list. For this refactor I set the
tabIndex to -1 to disable focus on the list items. I also removed the tabIndex from the Container
and adjusted the keyHandler toassign focus from the button instead of the container. This was
because there was always an extra tab needed to close and leave the dropdown because both the
container and button were being traveresed. Since the butten needs the focus--disabling wasn't an
option.

#210
  • Loading branch information
willwinman committed Jul 12, 2021
1 parent 7082524 commit d18fc68
Showing 1 changed file with 3 additions and 4 deletions.
7 changes: 3 additions & 4 deletions src/components/Dropdown/Dropdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -375,8 +375,8 @@ const Dropdown = ({
}
break;
case 'ArrowDown':
if (focusedElement && focusedElement.id === `${name}-container`) {
const optionsContainer = focusedElement.children[1];
if (focusedElement && focusedElement.id === `${name}-button-value`) {
const optionsContainer = focusedElement.nextElementSibling;
if (optionsContainer) {
const toFocus = optionsContainer.children[0] as HTMLElement | undefined;
if (toFocus) {
Expand Down Expand Up @@ -447,7 +447,6 @@ const Dropdown = ({
setIsOpen(true);
}}
ref={mergeRefs([containerRef, containerInternalRef])}
tabIndex={tabIndex}
{...containerProps}
>
<Button
Expand Down Expand Up @@ -522,7 +521,7 @@ const Dropdown = ({
key={`${name}-option-${option.id}`}
onBlur={handleBlur}
onClick={() => handleSelect(option.id)}
tabIndex={tabIndex}
tabIndex={-1}
color={defaultedColor}
variant={optionsVariant}
multi={multi}
Expand Down

0 comments on commit d18fc68

Please sign in to comment.