2018-10-16 19:49:16 +02:00
|
|
|
|
|
|
|
class HeaderMobileToggle {
|
|
|
|
|
2021-04-19 22:41:13 +02:00
|
|
|
setup() {
|
|
|
|
this.elem = this.$el;
|
|
|
|
this.toggleButton = this.$refs.toggle;
|
|
|
|
this.menu = this.$refs.menu;
|
2018-10-16 19:49:16 +02:00
|
|
|
|
2021-04-19 22:41:13 +02:00
|
|
|
this.open = false;
|
2018-10-16 19:49:16 +02:00
|
|
|
this.toggleButton.addEventListener('click', this.onToggle.bind(this));
|
|
|
|
this.onWindowClick = this.onWindowClick.bind(this);
|
2021-04-19 22:41:13 +02:00
|
|
|
this.onKeyDown = this.onKeyDown.bind(this);
|
2018-10-16 19:49:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
onToggle(event) {
|
|
|
|
this.open = !this.open;
|
|
|
|
this.menu.classList.toggle('show', this.open);
|
2021-04-19 22:41:13 +02:00
|
|
|
this.toggleButton.setAttribute('aria-expanded', this.open ? 'true' : 'false');
|
2018-10-16 19:49:16 +02:00
|
|
|
if (this.open) {
|
2021-04-19 22:41:13 +02:00
|
|
|
this.elem.addEventListener('keydown', this.onKeyDown);
|
2018-10-16 19:49:16 +02:00
|
|
|
window.addEventListener('click', this.onWindowClick)
|
|
|
|
} else {
|
2021-04-19 22:41:13 +02:00
|
|
|
this.elem.removeEventListener('keydown', this.onKeyDown);
|
2018-10-16 19:49:16 +02:00
|
|
|
window.removeEventListener('click', this.onWindowClick)
|
|
|
|
}
|
|
|
|
event.stopPropagation();
|
|
|
|
}
|
|
|
|
|
2021-04-19 22:41:13 +02:00
|
|
|
onKeyDown(event) {
|
|
|
|
if (event.code === 'Escape') {
|
|
|
|
this.onToggle(event);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-16 19:49:16 +02:00
|
|
|
onWindowClick(event) {
|
|
|
|
this.onToggle(event);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-04-19 22:41:13 +02:00
|
|
|
export default HeaderMobileToggle;
|