2023-04-18 17:20:02 -04:00
|
|
|
import {Component} from './component';
|
2018-10-16 13:49:16 -04:00
|
|
|
|
2022-11-15 11:04:46 -05:00
|
|
|
export class HeaderMobileToggle extends Component {
|
2018-10-16 13:49:16 -04:00
|
|
|
|
2021-04-19 16:41:13 -04:00
|
|
|
setup() {
|
|
|
|
this.elem = this.$el;
|
|
|
|
this.toggleButton = this.$refs.toggle;
|
|
|
|
this.menu = this.$refs.menu;
|
2018-10-16 13:49:16 -04:00
|
|
|
|
2021-04-19 16:41:13 -04:00
|
|
|
this.open = false;
|
2018-10-16 13:49:16 -04:00
|
|
|
this.toggleButton.addEventListener('click', this.onToggle.bind(this));
|
|
|
|
this.onWindowClick = this.onWindowClick.bind(this);
|
2021-04-19 16:41:13 -04:00
|
|
|
this.onKeyDown = this.onKeyDown.bind(this);
|
2018-10-16 13:49:16 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
onToggle(event) {
|
|
|
|
this.open = !this.open;
|
|
|
|
this.menu.classList.toggle('show', this.open);
|
2021-04-19 16:41:13 -04:00
|
|
|
this.toggleButton.setAttribute('aria-expanded', this.open ? 'true' : 'false');
|
2018-10-16 13:49:16 -04:00
|
|
|
if (this.open) {
|
2021-04-19 16:41:13 -04:00
|
|
|
this.elem.addEventListener('keydown', this.onKeyDown);
|
2023-04-18 17:20:02 -04:00
|
|
|
window.addEventListener('click', this.onWindowClick);
|
2018-10-16 13:49:16 -04:00
|
|
|
} else {
|
2021-04-19 16:41:13 -04:00
|
|
|
this.elem.removeEventListener('keydown', this.onKeyDown);
|
2023-04-18 17:20:02 -04:00
|
|
|
window.removeEventListener('click', this.onWindowClick);
|
2018-10-16 13:49:16 -04:00
|
|
|
}
|
|
|
|
event.stopPropagation();
|
|
|
|
}
|
|
|
|
|
2021-04-19 16:41:13 -04:00
|
|
|
onKeyDown(event) {
|
|
|
|
if (event.code === 'Escape') {
|
|
|
|
this.onToggle(event);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-16 13:49:16 -04:00
|
|
|
onWindowClick(event) {
|
|
|
|
this.onToggle(event);
|
|
|
|
}
|
|
|
|
|
2023-04-18 17:20:02 -04:00
|
|
|
}
|