-
Notifications
You must be signed in to change notification settings - Fork 0
/
child-limit-group.ts
62 lines (52 loc) · 1.79 KB
/
child-limit-group.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import { LitElement, html } from "lit";
import { customElement, property } from "lit/decorators.js";
@customElement("child-limit-group")
export class ChildLimitGroup extends LitElement {
/**
* The maximum number of children allowed in the group.
* If 0, there is no limit.
*/
@property({ type: Number })
max = 0;
shown: number[] = [];
@property({ type: Boolean })
default_hidden = true;
slot_holder: NodeListOf<HTMLElement> | undefined;
show_child(index: number) {
if (this.shown.includes(index)) return;
this.shown.push(index);
if (this.max > 0 && this.shown.length > this.max) {
this.slot_holder![this.shown.shift()!].style.display = 'none';
}
this.slot_holder![index].style.display = '';
}
mutationObserver = new MutationObserver((mutations) => {
mutations.forEach((mutation) => {
this.slot_holder!.forEach((child, index) => {
if (child.isEqualNode(mutation.target) && child.style.display !== 'none') {
this.show_child(index);
}
});
});
});
async firstUpdated() {
await this.updateComplete;
this.slot_holder = this.childNodes as NodeListOf<HTMLElement>;
console.log(this.slot_holder);
this.slot_holder.forEach((child, index) => {
if (!child || !child.style) return;
this.show_child(index);
this.mutationObserver.observe(child, { attributes: true, attributeFilter: ['style'] });
});
}
render() {
return html`
<slot></slot>
`;
}
}
declare global {
interface HTMLElementTagNameMap {
"child-limit-group": ChildLimitGroup;
}
}