-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrestart-animations-element.js
61 lines (50 loc) · 1.44 KB
/
restart-animations-element.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
51
52
53
54
55
56
57
58
59
60
61
const restartAnimationsElementStyleSheet = new CSSStyleSheet();
await restartAnimationsElementStyleSheet.replace(`
:host { display: inline-block; }
`);
export class RestartAnimationsElement extends HTMLElement {
get #buttonSlot() {
return this.shadowRoot.querySelector("slot[name=button]");
}
get #buttonElement() {
const [slottedButton] = this.#buttonSlot.assignedElements();
return slottedButton ?? this.shadowRoot.querySelector("button[part=button]");
}
get #forElement() {
return this.htmlFor ? this.ownerDocument.getElementById(this.htmlFor) : this.ownerDocument;
}
get htmlFor() {
return this.getAttribute("for");
}
set htmlFor(id) {
this.setAttribute("id", id);
}
connectedCallback() {
if (!this.shadowRoot) {
this.attachShadow({ mode: "open" });
this.shadowRoot.adoptedStyleSheets = [restartAnimationsElementStyleSheet];
this.shadowRoot.innerHTML = `
<slot name="button">
<button type="button" part="button">
<slot>Restart Animations</slot>
</button>
</slot>
`;
}
this.#buttonElement.addEventListener("click", this);
}
handleEvent(event) {
if (event.type === "click") {
for (const animation of this.#forElement.getAnimations({ subtree: true })) {
animation.cancel();
animation.play();
}
}
}
static define(tagName = "restart-animations") {
if (!window.customElements.get(tagName)) {
window[this.name] = this;
window.customElements.define(tagName, this);
}
}
}