-
Notifications
You must be signed in to change notification settings - Fork 1
/
accordion.ts
28 lines (24 loc) · 925 Bytes
/
accordion.ts
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
import DisclosureElement from '../disclosure/disclosure';
export default class AccordionElement extends HTMLElement {
public connectedCallback(): void {
this.addEventListener('toggle', this.onToggle, { capture: true });
}
public disconnectedCallback(): void {
this.removeEventListener('toggle', this.onToggle, { capture: true });
}
private onToggle = (e: Event) => {
const target = e.target as DisclosureElement | HTMLDetailsElement;
if (!target.open) return;
this.querySelectorAll<DisclosureElement | HTMLDetailsElement>(
':scope > :is(ui-disclosure, details)'
).forEach((el) => {
el.toggleAttribute('open', el === target);
if (this.required) {
el.toggleAttribute('disabled', el === target);
}
});
};
get required() {
return this.hasAttribute('required');
}
}