-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCollapsibleList.ts
39 lines (30 loc) · 996 Bytes
/
CollapsibleList.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
29
30
31
32
33
34
35
36
37
38
39
const COLLAPSE_ANIMATION_DURATION = 100;
export default class CollapsibleList {
private list: HTMLElement;
public get element() {
return this.list
}
constructor(listAttributes: DomElementInfo) {
this.list = CollapsibleList.createListElementCollapsed(listAttributes);
}
private static createListElementCollapsed(listAttributes: DomElementInfo) {
const ul = createEl("ul", listAttributes);
ul.style.maxHeight = "0";
return ul;
}
public addItems(items: HTMLElement[]) {
this.list.append(...items);
}
public isCollapsed() {
return Number.parseInt(this.list.style.maxHeight) === 0;
}
public collapse() {
this.list.style.maxHeight = `${this.list.scrollHeight}px`;
requestAnimationFrame(() => this.list.style.maxHeight = "0")
}
public unfold() {
this.list.style.maxHeight = "0";
requestAnimationFrame(() => this.list.style.maxHeight = `${this.list.scrollHeight}px`)
setTimeout(() => this.list.style.maxHeight = "initial", COLLAPSE_ANIMATION_DURATION)
}
}