Skip to content

fix(a11y) Make Dropdown more accessible #2325

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

Closed
wants to merge 1 commit into from
Closed
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
86 changes: 71 additions & 15 deletions src/components/Dropdown/Dropdown.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,43 +6,99 @@ export default class Dropdown extends React.Component {
active: false
};

componentDidMount() {
document.addEventListener('keyup', (e) => {
if (e.key === "Escape" && this.state.active) {
this.setState({ active: false}, () => {
this.dropdownButton.focus();
});
}
});

document.addEventListener('focus', e => {
if (!this.dropdown.contains(e.target)) {
this.setState({ active: false });
}
}, true);
}

render() {
let { className = '', items = [] } = this.props;
let activeMod = this.state.active ? "dropdown__list--active" : "";

return (
<div
tabIndex="0"
<nav
className={ `dropdown ${className}` }
ref={ el => this.dropdown = el }
onMouseOver={ this._toggle.bind(this, true) }
onMouseLeave={ this._toggle.bind(this, false) }>
<img
className="dropdown__language"
alt="select language"
src={ LanguageIcon } />
{/* Commented out until media breakpoints are in place
<span>{ items[0].title }</span> */}
<i className="dropdown__arrow" />

onMouseLeave={ this._toggle.bind(this, false) }
>
<button
ref={ el => this.dropdownButton = el }
aria-haspopup="true"
aria-expanded={ String(this.state.active) }
aria-label="Select language"
onClick={ this._handleClick.bind(this) }
>
<img
className="dropdown__language"
alt="select language"
src={ LanguageIcon } />
{/* Commented out until media breakpoints are in place
<span>{ items[0].title }</span> */}
<i aria-hidden="true" className="dropdown__arrow" />
</button>
<div className={ `dropdown__list ${activeMod}` }>
<ul>
{
items.map(item => {
items.map((item, i) => {
return (
<li key={ item.title }>
<a href={ item.url }>
<span>{ item.title }</span>
<a
onKeyDown={this._handleArrowKeys.bind(this, i, items.length - 1)}
ref={ node => this.links ? this.links.push(node) : this.links = [node] }
href={ item.url }>
<span lang={ item.lang }>{ item.title }</span>
</a>
</li>
);
})
}
</ul>
</div>
</div>
</nav>
);
}

_handleArrowKeys(currentIndex, lastIndex, e) {
if (["ArrowDown", "ArrowUp"].includes(e.key)) {
e.preventDefault();
}

let newIndex = currentIndex;
if (e.key === "ArrowDown") {
newIndex++;
if (newIndex > lastIndex) {
newIndex = 0;
}
}

if (e.key === "ArrowUp") {
newIndex--;
if (newIndex < 0) {
newIndex = lastIndex;
}
}

this.links[newIndex].focus();
}

_handleClick(e) {
this.setState({active: true}, () => {
this.links[0].focus();
});
}

/**
* Toggle visibility of dropdown items
*
Expand Down
12 changes: 10 additions & 2 deletions src/components/Dropdown/Dropdown.scss
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,16 @@

.dropdown {
position: relative;
color: #fff;
cursor: pointer;

button {
cursor: pointer;
color: #fff;
border: none;
background-color: transparent;
margin: 0;
padding: 0;
font-size: inherit;
}
}

.dropdown__language {
Expand Down
4 changes: 2 additions & 2 deletions src/components/Navigation/Navigation.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ export default class Navigation extends React.Component {
<Dropdown
className="navigation__languages"
items={[
{ title: 'English', url: 'https://webpack.js.org/' },
{ title: '中文', url: 'https://webpack.docschina.org/' }
{ lang: 'en', title: 'English', url: 'https://webpack.js.org/' },
{ lang: 'zh', title: '中文', url: 'https://webpack.docschina.org/' }
]} />
</Container>

Expand Down