Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[a11y] Update dropdown component for keyboard usage #261

Merged
merged 15 commits into from
Jul 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ exports[`<MobileDropdown /> UI snapshots renders correctly 1`] = `

.emotion-0:focus {
outline: none;
box-shadow: 0 1px 3px 0 rgba(51,46,84,0.15);
-webkit-transition: 200ms ease-in-out;
transition: 200ms ease-in-out;
}

.emotion-0::-moz-focus-inner {
Expand Down
112 changes: 0 additions & 112 deletions src/shared-components/dropdown/desktopDropdown.js

This file was deleted.

154 changes: 154 additions & 0 deletions src/shared-components/dropdown/desktopDropdown.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
import React from 'react';
import PropTypes from 'prop-types';
import { css } from '@emotion/core';
import useResetFocus from 'src/utils/accessibility/useResetFocus';

import OffClickWrapper from '../offClickWrapper';
import ChevronIcon from '../../svgs/icons/chevron-icon.svg';
import {
DropdownContainer,
dropdownInputStyle,
IconContainer,
DropdownOptionsContainer,
DropdownOption,
} from './style';

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

File CHANGELOG:

  1. Convert to TS
  2. Add useResetFocus hook to move focus back to the button when an option is selected
  3. Update roles, tabIndex, and aria-labels for the DropdownOptionsContainer ul and DropdownOption li.
  4. Add a keydown handler to allow selecting an option using the enter key.

import { OptionType } from './index';

type DesktopDropdownProps = {
value?: string;
options: OptionType[];
currentOption: OptionType;
textAlign: 'left' | 'center';
closeDropdown: () => void;
onOptionClick: (event: any) => void;
onSelectClick: () => void;
isOpen: boolean;
optionsContainerMaxHeight: string;
};

const DesktopDropdown = ({
value,
options,
textAlign,
currentOption,
closeDropdown,
onOptionClick,
onSelectClick,
isOpen,
optionsContainerMaxHeight,
}: DesktopDropdownProps) => {
const { initialFocus, resetFocus } = useResetFocus<HTMLDivElement>();

const handleKeyDown = (event: React.KeyboardEvent) => {
// This key handler allows users to open the dropdown options via the keyboard
if (event.key === 'Enter') {
onSelectClick();
}
};

const handleOptionKeydown = (event: React.KeyboardEvent) => {
// This allows users to select an option via the enter key
if (event.key === 'Enter') {
onOptionClick(event);
resetFocus();
}
};

return (
<OffClickWrapper
onOffClick={closeDropdown}
css={css`
width: 100%;
`}
>
<DropdownContainer textAlign={textAlign}>
<div
id="select-input-box"
onClick={onSelectClick}
onKeyDown={handleKeyDown}
tabIndex={0}
aria-label="Open dropdown option"
aria-haspopup="listbox"
role="button"
ref={initialFocus}
>
<div css={dropdownInputStyle({ textAlign })}>
{currentOption && currentOption.label}
</div>
<IconContainer>
<ChevronIcon width={10} height={10} rotate={isOpen ? 90 : 0} />
</IconContainer>
</div>

<DropdownOptionsContainer
isOpen={isOpen}
optionsContainerMaxHeight={optionsContainerMaxHeight}
role="listbox"
aria-activedescendant={value}
tabIndex={isOpen ? 0 : -1}
aria-labelledby="select-input-box"
>
{options.map(option => {
const {
value: optionValue, disabled, label, ...rest
} = option;

return (
<DropdownOption
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you play around at all with trying to achieve the (kind of) opposite of useResetFocus, but rather than resetting focus back to another element, we actually set the focus ahead to the first descendent? (Kind of like the menu button when using keyboard navigation here: https://reach.tech/menu-button/#reach-skip-nav).

I also have not dug into the internals of all this as much as you have, so I'm not use if that kind of functionality is consistent with "best practices" or we'd be potentially introducing hacky behavior that we ought to achieve with other code.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a good idea - I'm going to write up a separate ticket for this and will investigate further. Looking at the reach-ui implementation, it looks like they generally send focus to the first list element, but it may make sense to focus on the selected element if there is one.

key={optionValue}
value={optionValue}
selected={value === optionValue}
disabled={!!disabled}
onClick={onOptionClick}
onKeyDown={handleOptionKeydown}
role="option"
aria-selected={value === optionValue}
tabIndex={isOpen && !disabled ? 0 : -1}
// eslint-disable-next-line react/jsx-props-no-spreading
{...rest}
>
{label}
</DropdownOption>
);
})}
</DropdownOptionsContainer>
</DropdownContainer>
</OffClickWrapper>
);
};

DesktopDropdown.defaultProps = {
value: undefined,
options: [{ value: null, label: '' }],
currentOption: { value: null, label: '' },
textAlign: 'left',
isOpen: false,
};

DesktopDropdown.propTypes = {
// eslint-disable-next-line react/forbid-prop-types
value: PropTypes.any,
options: PropTypes.arrayOf(
PropTypes.shape({
// eslint-disable-next-line react/forbid-prop-types
value: PropTypes.any,
label: PropTypes.string,
disabled: PropTypes.bool,
}),
),
textAlign: PropTypes.oneOf(['left', 'center']),
currentOption: PropTypes.shape({
// eslint-disable-next-line react/forbid-prop-types
value: PropTypes.any,
label: PropTypes.string,
disabled: PropTypes.bool,
}),
closeDropdown: PropTypes.func.isRequired,
onOptionClick: PropTypes.func.isRequired,
onSelectClick: PropTypes.func.isRequired,
isOpen: PropTypes.bool,
optionsContainerMaxHeight: PropTypes.string.isRequired,
};

export default DesktopDropdown;
101 changes: 0 additions & 101 deletions src/shared-components/dropdown/index.js

This file was deleted.

Loading