From c17be2d67d91a66c42485f65795715dac05705c9 Mon Sep 17 00:00:00 2001 From: Anton Harniakou Date: Mon, 2 Jul 2018 13:30:46 +0300 Subject: [PATCH] fix(a11y) Make Dropdown more accessible - make it focusable - allow navigation using arrow keys - dismiss on escape --- src/components/Dropdown/Dropdown.jsx | 86 +++++++++++++++++++----- src/components/Dropdown/Dropdown.scss | 12 +++- src/components/Navigation/Navigation.jsx | 4 +- 3 files changed, 83 insertions(+), 19 deletions(-) diff --git a/src/components/Dropdown/Dropdown.jsx b/src/components/Dropdown/Dropdown.jsx index d453427c36ed..5b56b0e7e0f5 100644 --- a/src/components/Dropdown/Dropdown.jsx +++ b/src/components/Dropdown/Dropdown.jsx @@ -6,32 +6,59 @@ 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 ( -
this.dropdown = el } onMouseOver={ this._toggle.bind(this, true) } - onMouseLeave={ this._toggle.bind(this, false) }> - select language - {/* Commented out until media breakpoints are in place - { items[0].title } */} - - + onMouseLeave={ this._toggle.bind(this, false) } + > +