-
Notifications
You must be signed in to change notification settings - Fork 7
/
sparkly-text.js
157 lines (131 loc) · 4.18 KB
/
sparkly-text.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
let sheet;
let sparkleTemplate;
// https://caniuse.com/mdn-api_cssstylesheet_replacesync
const supportsConstructableStylesheets =
"replaceSync" in CSSStyleSheet.prototype;
const motionOK = window.matchMedia("(prefers-reduced-motion: no-preference)");
class SparklyText extends HTMLElement {
#numberOfSparkles = 3;
#sparkleSvg = `<svg width="1200" height="1200" viewBox="0 0 1200 1200" aria-hidden="true">
<path fill="red" d="m611.04 866.16c17.418-61.09 50.25-116.68 95.352-161.42 45.098-44.742 100.94-77.133 162.17-94.062l38.641-10.68-38.641-10.68c-61.227-16.93-117.07-49.32-162.17-94.062-45.102-44.738-77.934-100.33-95.352-161.42l-11.039-38.641-11.039 38.641c-17.418 61.09-50.25 116.68-95.352 161.42-45.098 44.742-100.94 77.133-162.17 94.062l-38.641 10.68 38.641 10.68c61.227 16.93 117.07 49.32 162.17 94.062 45.102 44.738 77.934 100.33 95.352 161.42l11.039 38.641z"/>
</svg>`;
#css = `
:host {
--_sparkle-base-size: var(--sparkly-text-size, 1em);
--_sparkle-base-animation-length: var(--sparkly-text-animation-length, 1.5s);
--_sparkle-base-color: var(--sparkly-text-color, #4ab9f8);
position: relative;
z-index: 0;
}
svg {
position: absolute;
z-index: -1;
width: var(--_sparkle-base-size);
height: var(--_sparkle-base-size);
transform-origin: center;
pointer-events: none;
}
@media (prefers-reduced-motion: no-preference) {
svg {
animation: sparkle-spin var(--_sparkle-base-animation-length) linear infinite;
}
}
svg path {
fill: var(--_sparkle-base-color);
}
@keyframes sparkle-spin {
0% {
scale: 0;
opacity: 0;
rotate: 0deg;
}
50% {
scale: 1;
opacity: 1;
}
100% {
scale: 0;
opacity: 0;
rotate: 180deg;
}
}
`;
static register() {
if ("customElements" in window) {
window.customElements.define("sparkly-text", SparklyText);
}
}
generateCss() {
if (!sheet) {
if (supportsConstructableStylesheets) {
sheet = new CSSStyleSheet();
sheet.replaceSync(this.#css);
} else {
sheet = document.createElement("style");
sheet.textContent = this.#css;
}
}
if (supportsConstructableStylesheets) {
this.shadowRoot.adoptedStyleSheets = [sheet];
} else {
this.shadowRoot.append(sheet.cloneNode(true));
}
}
connectedCallback() {
const needsSparkles = motionOK.matches || !this.shadowRoot;
if (!this.shadowRoot) {
this.attachShadow({ mode: "open" });
this.generateCss();
this.shadowRoot.append(document.createElement("slot"));
}
if (needsSparkles) {
this.#numberOfSparkles = parseInt(
this.getAttribute("number-of-sparkles") || `${this.#numberOfSparkles}`,
10
);
if (Number.isNaN(this.#numberOfSparkles)) {
throw new Error(`Invalid number-of-sparkles value`);
}
this.addSparkles();
}
motionOK.addEventListener("change", this.motionOkChange);
}
disconnectedCallback() {
motionOK.removeEventListener("change", this.motionOkChange);
}
// Declare as an arrow function to get the appropriate 'this'
motionOkChange = () => {
if (motionOK.matches) {
this.addSparkles();
}
};
addSparkles() {
for (let i = 0; i < this.#numberOfSparkles; i++) {
setTimeout(() => {
this.addSparkle(sparkle => {
sparkle.style.top = `calc(${
Math.random() * 110 - 5
}% - var(--_sparkle-base-size) / 2)`;
sparkle.style.left = `calc(${
Math.random() * 110 - 5
}% - var(--_sparkle-base-size) / 2)`;
});
}, i * 500);
}
}
addSparkle(update) {
if (!sparkleTemplate) {
const span = document.createElement("span");
span.innerHTML = this.#sparkleSvg;
sparkleTemplate = span.firstElementChild.cloneNode(true);
}
const sparkleWrapper = sparkleTemplate.cloneNode(true);
update(sparkleWrapper);
this.shadowRoot.appendChild(sparkleWrapper);
sparkleWrapper.addEventListener("animationiteration", () => {
update(sparkleWrapper);
});
}
}
SparklyText.register();
export { SparklyText };