Skip to content

Commit e5785ef

Browse files
committed
Support Safari < 16.4
where constructible stylesheets aren't supported.
1 parent dc4b731 commit e5785ef

1 file changed

Lines changed: 17 additions & 5 deletions

File tree

‎sparkly-text.js‎

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
let sheet;
22
let sparkleTemplate;
33

4+
// https://caniuse.com/mdn-api_cssstylesheet_replacesync
5+
const supportsConstructibleStylesheets = ("replaceSync" in CSSStyleSheet.prototype);
6+
47
class SparklyText extends HTMLElement {
58
#numberOfSparkles = 3;
69
#sparkleSvg = `<svg width="1200" height="1200" viewBox="0 0 1200 1200" aria-hidden="true">
@@ -64,15 +67,24 @@ class SparklyText extends HTMLElement {
6467
}
6568
}
6669
`;
67-
sheet = new CSSStyleSheet();
68-
sheet.replaceSync(css);
70+
if (supportsConstructibleStylesheets) {
71+
sheet = new CSSStyleSheet();
72+
sheet.replaceSync(css);
73+
} else {
74+
sheet = document.createElement("style");
75+
sheet.textContent = css;
76+
}
77+
}
78+
79+
if (supportsConstructibleStylesheets) {
80+
this.shadowRoot.adoptedStyleSheets = [sheet];
81+
} else {
82+
this.shadowRoot.append(sheet.cloneNode(true));
6983
}
70-
this.shadowRoot.adoptedStyleSheets = [sheet];
7184
}
7285

7386
connectedCallback() {
74-
// https://caniuse.com/mdn-api_cssstylesheet_replacesync
75-
if (this.shadowRoot || !("replaceSync" in CSSStyleSheet.prototype)) {
87+
if (this.shadowRoot) {
7688
return;
7789
}
7890

0 commit comments

Comments
 (0)