-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathsparkly-text.js
executable file
·205 lines (172 loc) · 5.6 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
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.rainbow path {
animation: rainbow-colors calc(var(--_sparkle-base-animation-length) * 2) linear infinite;
}
}
svg path {
fill: var(--_sparkle-base-color);
}
@keyframes rainbow-colors {
0%, 100% { fill: #ff0000; }
14% { fill: #ff8000; }
28% { fill: #ffff00; }
42% { fill: #00ff00; }
56% { fill: #0000ff; }
70% { fill: #4b0082; }
84% { fill: #8f00ff; }
}
@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.cleanupSparkles();
this.addSparkles();
}
motionOK.addEventListener("change", this.motionOkChange);
window.addEventListener("popstate", this.handleNavigation);
window.addEventListener("pageshow", this.handlePageShow);
}
disconnectedCallback() {
motionOK.removeEventListener("change", this.motionOkChange);
window.removeEventListener("popstate", this.handleNavigation);
window.removeEventListener("pageshow", this.handlePageShow);
this.cleanupSparkles();
}
handleNavigation = () => {
if (motionOK.matches) {
this.cleanupSparkles();
this.addSparkles();
}
};
handlePageShow = (event) => {
// If the page is being loaded from the bfcache
if (event.persisted && motionOK.matches) {
this.cleanupSparkles();
this.addSparkles();
}
};
cleanupSparkles() {
// Remove all existing sparkle SVGs
const sparkles = this.shadowRoot.querySelectorAll('svg');
sparkles.forEach(sparkle => sparkle.remove());
}
// 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);
// Add rainbow class if --sparkly-text-color is set to 'rainbow'
const styles = getComputedStyle(this);
if (styles.getPropertyValue('--sparkly-text-color').trim() === 'rainbow') {
sparkleWrapper.classList.add('rainbow');
}
update(sparkleWrapper);
this.shadowRoot.appendChild(sparkleWrapper);
sparkleWrapper.addEventListener("animationiteration", () => {
update(sparkleWrapper);
});
}
}
SparklyText.register();
export { SparklyText };