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