@chr33s/solarflare 0.0.9 → 0.0.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/client.styles.ts +9 -6
- package/src/client.ts +9 -5
- package/src/stylesheets.ts +25 -2
package/package.json
CHANGED
package/src/client.styles.ts
CHANGED
|
@@ -1,4 +1,8 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {
|
|
2
|
+
safeAdoptStylesheets,
|
|
3
|
+
stylesheets,
|
|
4
|
+
supportsConstructableStylesheets,
|
|
5
|
+
} from "./stylesheets.ts";
|
|
2
6
|
|
|
3
7
|
/** Style loading state for a component. */
|
|
4
8
|
interface StyleState {
|
|
@@ -51,12 +55,11 @@ export function applyStyles(element: HTMLElement, sheets: CSSStyleSheet[]) {
|
|
|
51
55
|
const shadowRoot = element.shadowRoot;
|
|
52
56
|
|
|
53
57
|
if (shadowRoot) {
|
|
54
|
-
shadowRoot
|
|
58
|
+
safeAdoptStylesheets(shadowRoot, [...shadowRoot.adoptedStyleSheets, ...sheets]);
|
|
55
59
|
} else {
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
}
|
|
60
|
+
const toAdd = sheets.filter((s) => !document.adoptedStyleSheets.includes(s));
|
|
61
|
+
if (toAdd.length) {
|
|
62
|
+
safeAdoptStylesheets(document, [...document.adoptedStyleSheets, ...toAdd]);
|
|
60
63
|
}
|
|
61
64
|
}
|
|
62
65
|
}
|
package/src/client.ts
CHANGED
|
@@ -3,7 +3,11 @@ import { parsePath } from "./paths.ts";
|
|
|
3
3
|
import { hydrateStore, initHydrationCoordinator } from "./hydration.ts";
|
|
4
4
|
import { installHeadHoisting, createHeadContext, setHeadContext } from "./head.ts";
|
|
5
5
|
import { getRuntime } from "./runtime.ts";
|
|
6
|
-
import {
|
|
6
|
+
import {
|
|
7
|
+
safeAdoptStylesheets,
|
|
8
|
+
stylesheets,
|
|
9
|
+
supportsConstructableStylesheets,
|
|
10
|
+
} from "./stylesheets.ts";
|
|
7
11
|
import { getPreloadedStylesheet } from "./server.styles.ts";
|
|
8
12
|
|
|
9
13
|
export { initHmrEntry, reloadAllStylesheets } from "./hmr.ts";
|
|
@@ -51,15 +55,15 @@ export function registerInlineStyles(tag: string, styles: InlineStyleEntry[]) {
|
|
|
51
55
|
const sheets = stylesheets.getForConsumer(tag);
|
|
52
56
|
const shadowRoot = el?.shadowRoot;
|
|
53
57
|
if (shadowRoot) {
|
|
54
|
-
shadowRoot
|
|
58
|
+
safeAdoptStylesheets(shadowRoot, [
|
|
55
59
|
...shadowRoot.adoptedStyleSheets.filter((s) => !sheets.includes(s)),
|
|
56
60
|
...sheets,
|
|
57
|
-
];
|
|
61
|
+
]);
|
|
58
62
|
} else {
|
|
59
|
-
document
|
|
63
|
+
safeAdoptStylesheets(document, [
|
|
60
64
|
...document.adoptedStyleSheets.filter((s) => !sheets.includes(s)),
|
|
61
65
|
...sheets,
|
|
62
|
-
];
|
|
66
|
+
]);
|
|
63
67
|
}
|
|
64
68
|
}
|
|
65
69
|
|
package/src/stylesheets.ts
CHANGED
|
@@ -9,6 +9,29 @@ export const supportsConstructableStylesheets = () => {
|
|
|
9
9
|
}
|
|
10
10
|
};
|
|
11
11
|
|
|
12
|
+
/** Recreates a stylesheet in the current document context. */
|
|
13
|
+
function cloneSheet(sheet: CSSStyleSheet): CSSStyleSheet {
|
|
14
|
+
const fresh = new CSSStyleSheet();
|
|
15
|
+
fresh.replaceSync([...sheet.cssRules].map((r) => r.cssText).join(""));
|
|
16
|
+
return fresh;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/** Safely adopts stylesheets, recreating them if cross-document sharing fails. */
|
|
20
|
+
export function safeAdoptStylesheets(
|
|
21
|
+
target: Document | ShadowRoot,
|
|
22
|
+
sheets: CSSStyleSheet[],
|
|
23
|
+
): CSSStyleSheet[] {
|
|
24
|
+
try {
|
|
25
|
+
target.adoptedStyleSheets = sheets;
|
|
26
|
+
return sheets;
|
|
27
|
+
} catch {
|
|
28
|
+
// Stylesheet was created in a different document context - recreate
|
|
29
|
+
const cloned = sheets.map(cloneSheet);
|
|
30
|
+
target.adoptedStyleSheets = cloned;
|
|
31
|
+
return cloned;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
12
35
|
/** Stylesheet entry with metadata. */
|
|
13
36
|
interface StylesheetEntry {
|
|
14
37
|
sheet: CSSStyleSheet;
|
|
@@ -143,7 +166,7 @@ class StylesheetManager {
|
|
|
143
166
|
// Include global sheets
|
|
144
167
|
const globalSheets = [...this.#sheets.values()].filter((e) => e.isGlobal).map((e) => e.sheet);
|
|
145
168
|
|
|
146
|
-
shadowRoot
|
|
169
|
+
safeAdoptStylesheets(shadowRoot, [...globalSheets, ...sheets]);
|
|
147
170
|
}
|
|
148
171
|
|
|
149
172
|
/** Removes a consumer from all its stylesheets. */
|
|
@@ -171,7 +194,7 @@ class StylesheetManager {
|
|
|
171
194
|
#adoptToDocument(sheet: CSSStyleSheet) {
|
|
172
195
|
if (!this.#documentSheets.includes(sheet)) {
|
|
173
196
|
this.#documentSheets.push(sheet);
|
|
174
|
-
document
|
|
197
|
+
safeAdoptStylesheets(document, [...this.#documentSheets]);
|
|
175
198
|
}
|
|
176
199
|
}
|
|
177
200
|
|