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 8 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.

151 changes: 151 additions & 0 deletions src/shared-components/dropdown/desktopDropdown.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
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>();

return (
<OffClickWrapper
onOffClick={closeDropdown}
css={css`
width: 100%;
`}
>
<DropdownContainer textAlign={textAlign}>
<div
id="select-input-box"
onClick={onSelectClick}
onKeyDown={(event: React.KeyboardEvent) => {
ipc103 marked this conversation as resolved.
Show resolved Hide resolved
if (event.key === 'Enter') {
onSelectClick();
}
}}
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={event => {
if (event.key === 'Enter') {
onOptionClick(event);
resetFocus();
}
}}
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',
closeDropdown: () => undefined,
onOptionClick: () => undefined,
onSelectClick: () => undefined,
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,
ipc103 marked this conversation as resolved.
Show resolved Hide resolved
onOptionClick: PropTypes.func,
onSelectClick: PropTypes.func,
isOpen: PropTypes.bool,
optionsContainerMaxHeight: PropTypes.string.isRequired,
};

export default DesktopDropdown;
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,20 @@ import MobileDropdown from './mobileDropdown';
import DesktopDropdown from './desktopDropdown';
import allowNullPropType from '../../utils/allowNullPropType';

export type OptionType = {
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 file is just TS conversion changes.

Copy link
Contributor

@michaeljaltamirano michaeljaltamirano Jul 20, 2020

Choose a reason for hiding this comment

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

@ipc103 I think we'll also want to convert this to a Function component. We're typing optionsContainerMaxHeight as optional in children components, but it's provided via defaultProps. If we convert this to a Function component we can rely on ES6 defaults to inform TypeScript that it is optional but, downstream, being required is not type inconsistent.

Fortunately the only non-render methods aren't lifecycle methods so it should be pretty straightforward to convert.

value: string;
label: string;
disabled?: boolean;
};

type DropdownProps = {
value?: string;
onChange: (option: OptionType) => void;
options: OptionType[];
optionsContainerMaxHeight?: string;
textAlign: 'left' | 'center';
ipc103 marked this conversation as resolved.
Show resolved Hide resolved
};

const defaultProps = {
textAlign: 'left',
onChange: () => undefined,
Expand All @@ -28,7 +42,11 @@ const propTypes = {
optionsContainerMaxHeight: PropTypes.string,
};

class Dropdown extends React.Component {
class Dropdown extends React.Component<DropdownProps> {
static defaultProps = defaultProps;

static propTypes = propTypes;

state = { isOpen: false };

onSelectClick = () => {
Expand All @@ -37,10 +55,10 @@ class Dropdown extends React.Component {
this.setState({ isOpen: !isOpen });
};

onSelectChange = event => {
onSelectChange = (event: React.ChangeEvent<HTMLSelectElement>) => {
const { onChange } = this.props;

const { value, selectedOptions } = event.target;
const {target} = event;
Copy link
Contributor

Choose a reason for hiding this comment

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

Did this not auto-format to be { target }? I've noticed this before in this repo... wonder if something is up

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It did not - and apparently I've gotten very dependent on our linting to fix my bad whitespace habits 😅

const { value, selectedOptions } = target;

if (selectedOptions && selectedOptions.length) {
const { label } = selectedOptions[0];
Expand All @@ -50,15 +68,15 @@ class Dropdown extends React.Component {
this.closeDropdown();
};

onOptionClick = event => {
onOptionClick = (event: React.MouseEvent<HTMLLIElement>) => {
const { onChange } = this.props;

if (event.target.hasAttribute('disabled')) {
const target = event.target as HTMLSelectElement;
ipc103 marked this conversation as resolved.
Show resolved Hide resolved
if (target.hasAttribute('disabled')) {
return;
}

const value = event.target.getAttribute('value');
const label = event.target.innerText;
const value = target.getAttribute('value') as string;
const label = target.innerText;
onChange({ value, label });
this.closeDropdown();
};
Expand Down Expand Up @@ -95,7 +113,4 @@ class Dropdown extends React.Component {
}
}

Dropdown.defaultProps = defaultProps;
Dropdown.propTypes = propTypes;

export default Dropdown;
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,21 @@ import PropTypes from 'prop-types';
import ChevronIcon from '../../svgs/icons/chevron-icon.svg';
import { DropdownContainer, dropdownInputStyle, IconContainer } from './style';

import { OptionType } from './index';

type MobileDropdownProps = {
ipc103 marked this conversation as resolved.
Show resolved Hide resolved
value: string | null;
ipc103 marked this conversation as resolved.
Show resolved Hide resolved
options: OptionType[];
textAlign: 'left' | 'center';
onSelectChange: (event: any) => void;
ipc103 marked this conversation as resolved.
Show resolved Hide resolved
};

const MobileDropdown = ({
textAlign, value, onSelectChange, options,
}) => (
textAlign,
value,
onSelectChange,
options,
}: MobileDropdownProps) => (
<DropdownContainer textAlign={textAlign}>
<select
css={dropdownInputStyle({ textAlign })}
Expand Down Expand Up @@ -39,7 +51,7 @@ MobileDropdown.defaultProps = {
value: null,
options: [{ value: null, label: '' }],
textAlign: 'left',
onSelectChange() {},
onSelectChange: () => undefined,
};

MobileDropdown.propTypes = {
Expand All @@ -51,7 +63,7 @@ MobileDropdown.propTypes = {
value: PropTypes.any,
label: PropTypes.string,
disabled: PropTypes.bool,
})
}),
),
textAlign: PropTypes.oneOf(['left', 'center']),
onSelectChange: PropTypes.func,
Expand Down
Loading