forked from sandeepravi/collage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdetails-disclosure.js
50 lines (40 loc) · 1.55 KB
/
details-disclosure.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
class DetailsDisclosure extends HTMLElement {
constructor() {
super();
this.mainDetailsToggle = this.querySelector('details');
this.content = this.mainDetailsToggle.querySelector('summary').nextElementSibling;
this.mainDetailsToggle.addEventListener('focusout', this.onFocusOut.bind(this));
this.mainDetailsToggle.addEventListener('toggle', this.onToggle.bind(this));
}
onFocusOut() {
setTimeout(() => {
if (!this.contains(document.activeElement)) this.close();
})
}
onToggle() {
if (!this.animations) this.animations = this.content.getAnimations();
if (this.mainDetailsToggle.hasAttribute('open')) {
this.animations.forEach(animation => animation.play());
} else {
this.animations.forEach(animation => animation.cancel());
}
}
close() {
this.mainDetailsToggle.removeAttribute('open');
this.mainDetailsToggle.querySelector('summary').setAttribute('aria-expanded', false);
}
}
customElements.define('details-disclosure', DetailsDisclosure);
class HeaderMenu extends DetailsDisclosure {
constructor() {
super();
this.header = document.querySelector('.header-wrapper');
}
onToggle() {
if (!this.header) return;
this.header.preventHide = this.mainDetailsToggle.open;
if (document.documentElement.style.getPropertyValue('--header-bottom-position-desktop') !== '') return;
document.documentElement.style.setProperty('--header-bottom-position-desktop', `${Math.floor(this.header.getBoundingClientRect().bottom)}px`);
}
}
customElements.define('header-menu', HeaderMenu);