Add keyboard interactions and focus trapping to Navigation

This commit is contained in:
2018-04-29 10:30:32 +01:00
parent 387d6a770e
commit db70a6cb06
6 changed files with 147 additions and 45 deletions

View File

@@ -12,19 +12,19 @@ export default class Navigation {
}
init() {
if (!this.state) return false;
this.container.setAttribute('aria-hidden', true);
this.container.querySelector('.Nav-list').setAttribute('role', 'menubar');
this.container.querySelectorAll('.Nav-listItem').forEach((item) => {
// Override implied listitem role
item.setAttribute('role', 'none');
});
this.container.querySelectorAll('.Nav-link').forEach((link) => {
link.setAttribute('role', 'menuitem');
});
this.attachEvents();
this.attachClickEvents();
this.attachKeyEvents();
return true;
}
attachEvents() {
attachClickEvents() {
this.container.addEventListener('click', (e) => {
if (this.container === e.target) {
this.closeNav();
}
});
this.menu.container.addEventListener('click', () => {
this.openNav();
});
@@ -33,11 +33,34 @@ export default class Navigation {
});
}
attachKeyEvents() {
const first = this.container.querySelector('.Nav-link');
const last = this.container.querySelector('.Nav-close');
this.container.addEventListener('keydown', (e) => {
if (e.keyCode === 27) {
this.closeNav();
}
});
first.addEventListener('keydown', (e) => {
if (e.shiftKey && e.keyCode === 9) {
e.preventDefault();
last.focus();
}
});
last.addEventListener('keydown', (e) => {
if (!e.shiftKey && e.keyCode === 9) {
e.preventDefault();
first.focus();
}
});
}
openNav() {
this.state = 'visible';
this.menu.hideMenu();
this.container.classList.add(STATES.open);
this.container.setAttribute('aria-hidden', false);
this.container.querySelector('.Nav-titleLink').focus();
}
closeNav() {