2023-04-19 05:20:02 +08:00
|
|
|
import {Component} from './component';
|
2018-10-17 01:49:16 +08:00
|
|
|
|
2022-11-16 00:04:46 +08:00
|
|
|
export class HeaderMobileToggle extends Component {
|
2018-10-17 01:49:16 +08:00
|
|
|
|
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);
|
2023-04-19 05:20:02 +08:00
|
|
|
window.addEventListener('click', this.onWindowClick);
|
2018-10-17 01:49:16 +08:00
|
|
|
} else {
|
2021-04-20 04:41:13 +08:00
|
|
|
this.elem.removeEventListener('keydown', this.onKeyDown);
|
2023-04-19 05:20:02 +08:00
|
|
|
window.removeEventListener('click', this.onWindowClick);
|
2018-10-17 01:49:16 +08:00
|
|
|
}
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2023-04-19 05:20:02 +08:00
|
|
|
}
|