|
| 1 | +let sheet; |
| 2 | + |
1 | 3 | class SparklyText extends HTMLElement {
|
2 | 4 | #numberOfSparkles = 3;
|
3 | 5 | #sparkleSvg = `<svg width="1200" height="1200" viewBox="0 0 1200 1200" aria-hidden="true">
|
4 | 6 | <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"/>
|
5 | 7 | </svg>`;
|
6 | 8 |
|
7 | 9 | generateCss() {
|
8 |
| - const css = ` |
9 |
| - :host { |
10 |
| - --_sparkle-base-size: var(--sparkly-text-size, 1em); |
11 |
| - --_sparkle-base-animation-length: var(--sparkly-text-animation-length, 1.5s); |
12 |
| - --_sparkle-base-color: var(--sparkly-text-color, #4ab9f8); |
13 |
| -
|
14 |
| - position: relative; |
15 |
| - z-index: 0; |
16 |
| - } |
17 |
| -
|
18 |
| - .sparkle-wrapper { |
19 |
| - position: absolute; |
20 |
| - z-index: -1; |
21 |
| - width: var(--_sparkle-base-size); |
22 |
| - height: var(--_sparkle-base-size); |
23 |
| - transform-origin: center; |
24 |
| - } |
25 |
| -
|
26 |
| - @media (prefers-reduced-motion: no-preference) { |
27 |
| - .sparkle-wrapper { |
28 |
| - animation: sparkle-spin var(--_sparkle-base-animation-length) linear 1; |
29 |
| - } |
30 |
| - } |
31 |
| -
|
32 |
| - svg { |
33 |
| - width: var(--_sparkle-base-size) !important; |
34 |
| - height: var(--_sparkle-base-size) !important; |
35 |
| - display: block; |
36 |
| - position: absolute; |
37 |
| - pointer-events: none; |
38 |
| - } |
39 |
| -
|
40 |
| - svg path { |
41 |
| - fill: var(--_sparkle-base-color); |
42 |
| - } |
43 |
| -
|
44 |
| - @keyframes sparkle-spin { |
45 |
| - 0% { |
46 |
| - scale: 0; |
47 |
| - opacity: 0; |
48 |
| - rotate: 0deg; |
49 |
| - } |
50 |
| -
|
51 |
| - 50% { |
52 |
| - scale: 1; |
53 |
| - opacity: 1; |
54 |
| - } |
55 |
| -
|
56 |
| - 100% { |
57 |
| - scale: 0; |
58 |
| - opacity: 0; |
59 |
| - rotate: 180deg; |
60 |
| - } |
61 |
| - } |
| 10 | + if (!sheet) { |
| 11 | + const css = ` |
| 12 | + :host { |
| 13 | + --_sparkle-base-size: var(--sparkly-text-size, 1em); |
| 14 | + --_sparkle-base-animation-length: var(--sparkly-text-animation-length, 1.5s); |
| 15 | + --_sparkle-base-color: var(--sparkly-text-color, #4ab9f8); |
| 16 | +
|
| 17 | + position: relative; |
| 18 | + z-index: 0; |
| 19 | + } |
| 20 | +
|
| 21 | + .sparkle-wrapper { |
| 22 | + position: absolute; |
| 23 | + z-index: -1; |
| 24 | + width: var(--_sparkle-base-size); |
| 25 | + height: var(--_sparkle-base-size); |
| 26 | + transform-origin: center; |
| 27 | + } |
| 28 | +
|
| 29 | + @media (prefers-reduced-motion: no-preference) { |
| 30 | + .sparkle-wrapper { |
| 31 | + animation: sparkle-spin var(--_sparkle-base-animation-length) linear 1; |
| 32 | + } |
| 33 | + } |
| 34 | +
|
| 35 | + svg { |
| 36 | + width: var(--_sparkle-base-size) !important; |
| 37 | + height: var(--_sparkle-base-size) !important; |
| 38 | + display: block; |
| 39 | + position: absolute; |
| 40 | + pointer-events: none; |
| 41 | + } |
| 42 | +
|
| 43 | + svg path { |
| 44 | + fill: var(--_sparkle-base-color); |
| 45 | + } |
| 46 | +
|
| 47 | + @keyframes sparkle-spin { |
| 48 | + 0% { |
| 49 | + scale: 0; |
| 50 | + opacity: 0; |
| 51 | + rotate: 0deg; |
| 52 | + } |
| 53 | +
|
| 54 | + 50% { |
| 55 | + scale: 1; |
| 56 | + opacity: 1; |
| 57 | + } |
| 58 | +
|
| 59 | + 100% { |
| 60 | + scale: 0; |
| 61 | + opacity: 0; |
| 62 | + rotate: 180deg; |
| 63 | + } |
| 64 | + } |
62 | 65 | `;
|
63 |
| - let sheet = new CSSStyleSheet(); |
64 |
| - sheet.replaceSync(css); |
| 66 | + sheet = new CSSStyleSheet(); |
| 67 | + sheet.replaceSync(css); |
| 68 | + } |
65 | 69 | this.shadowRoot.adoptedStyleSheets = [sheet];
|
66 | 70 | }
|
67 | 71 |
|
|
0 commit comments