@stacksjs/components 0.2.864 → 0.2.867
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/dist/index.js +18 -18
- package/dist/preview-frame/preview-frame.js +58 -2
- package/dist/ui/preview-frame/core.d.ts +22 -0
- package/dist/ui/preview-frame/view.d.ts +9 -0
- package/package.json +3 -3
- package/src/ui/preview-frame/README.md +16 -14
- package/src/ui/preview-frame/core.ts +50 -0
- package/src/ui/preview-frame/index.ts +4 -4
- package/src/ui/preview-frame/view.ts +61 -4
|
@@ -70,10 +70,29 @@ var HEAD_OPEN = /<head(?:\s[^>]*)?>/i;
|
|
|
70
70
|
var HTML_OPEN = /<html(?:\s[^>]*)?>/i;
|
|
71
71
|
var HAS_BASE = /<base[\s>]/i;
|
|
72
72
|
var HAS_COLOR_SCHEME = /<meta[^>]+name\s*=\s*["']?color-scheme/i;
|
|
73
|
+
var SCHEME_FEATURE = /\(\s*prefers-color-scheme\s*:\s*(light|dark)\s*\)/gi;
|
|
74
|
+
var STYLE_OR_SCRIPT = /(<(style|script)\b[^>]*>)([\s\S]*?)(<\/\2\s*>)/gi;
|
|
75
|
+
var MEDIA_ATTRIBUTE = /(\smedia\s*=\s*)("[^"]*"|'[^']*')/gi;
|
|
76
|
+
var ALWAYS = "(min-width: 0px)";
|
|
77
|
+
var NEVER = "(max-width: 0px)";
|
|
78
|
+
function forceColorScheme(html, scheme) {
|
|
79
|
+
const settle = (text) => text.replace(SCHEME_FEATURE, (_, asked) => asked.toLowerCase() === scheme ? ALWAYS : NEVER);
|
|
80
|
+
const rewritten = html.replace(STYLE_OR_SCRIPT, (_, open, _tag, body, close) => open + settle(body) + close).replace(MEDIA_ATTRIBUTE, (_, name, value) => name + settle(value));
|
|
81
|
+
const rule = `<style data-stx-preview-scheme>:root{color-scheme:${scheme} !important}</style>`;
|
|
82
|
+
if (HEAD_OPEN.test(rewritten))
|
|
83
|
+
return rewritten.replace(HEAD_OPEN, (match) => match + rule);
|
|
84
|
+
if (HTML_OPEN.test(rewritten))
|
|
85
|
+
return rewritten.replace(HTML_OPEN, (match) => `${match}<head>${rule}</head>`);
|
|
86
|
+
return rule + rewritten;
|
|
87
|
+
}
|
|
73
88
|
function escapeHtml(text) {
|
|
74
89
|
return text.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">");
|
|
75
90
|
}
|
|
76
91
|
function buildDocument(html, options = {}) {
|
|
92
|
+
const built = wrapDocument(html, options);
|
|
93
|
+
return options.forceScheme ? forceColorScheme(built, options.forceScheme) : built;
|
|
94
|
+
}
|
|
95
|
+
function wrapDocument(html, options) {
|
|
77
96
|
const source = html == null ? "" : String(html);
|
|
78
97
|
const base = '<base target="_blank">';
|
|
79
98
|
if (!HAS_DOCUMENT.test(source)) {
|
|
@@ -98,6 +117,29 @@ function stepViewport(name, direction) {
|
|
|
98
117
|
}
|
|
99
118
|
// src/ui/preview-frame/view.ts
|
|
100
119
|
var STAGE_PADDING = 16;
|
|
120
|
+
var PREFERS_DARK = "(prefers-color-scheme: dark)";
|
|
121
|
+
var framesFollow;
|
|
122
|
+
function framePassesColorScheme() {
|
|
123
|
+
if (framesFollow !== undefined)
|
|
124
|
+
return framesFollow;
|
|
125
|
+
try {
|
|
126
|
+
if (typeof matchMedia !== "function" || !document.body)
|
|
127
|
+
return true;
|
|
128
|
+
const systemDark = matchMedia(PREFERS_DARK).matches;
|
|
129
|
+
const probe = document.createElement("iframe");
|
|
130
|
+
probe.setAttribute("aria-hidden", "true");
|
|
131
|
+
probe.tabIndex = -1;
|
|
132
|
+
probe.style.cssText = `position:absolute;width:1px;height:1px;border:0;opacity:0;pointer-events:none;color-scheme:${systemDark ? "light" : "dark"}`;
|
|
133
|
+
document.body.appendChild(probe);
|
|
134
|
+
probe.offsetWidth;
|
|
135
|
+
const seen = probe.contentWindow?.matchMedia?.(PREFERS_DARK).matches;
|
|
136
|
+
probe.remove();
|
|
137
|
+
framesFollow = typeof seen === "boolean" ? seen !== systemDark : true;
|
|
138
|
+
} catch {
|
|
139
|
+
framesFollow = true;
|
|
140
|
+
}
|
|
141
|
+
return framesFollow;
|
|
142
|
+
}
|
|
101
143
|
|
|
102
144
|
class PreviewFrameView {
|
|
103
145
|
root;
|
|
@@ -114,6 +156,7 @@ class PreviewFrameView {
|
|
|
114
156
|
viewport;
|
|
115
157
|
scale = 1;
|
|
116
158
|
frame = null;
|
|
159
|
+
written;
|
|
117
160
|
stage;
|
|
118
161
|
device;
|
|
119
162
|
observer = null;
|
|
@@ -188,7 +231,10 @@ class PreviewFrameView {
|
|
|
188
231
|
if (next === this.scheme)
|
|
189
232
|
return;
|
|
190
233
|
this.scheme = next;
|
|
191
|
-
this.
|
|
234
|
+
if (this.schemeToWrite() !== this.written)
|
|
235
|
+
this.load();
|
|
236
|
+
else
|
|
237
|
+
this.applyScheme();
|
|
192
238
|
this.reflect();
|
|
193
239
|
this.callbacks.onScheme?.(this.getState());
|
|
194
240
|
}
|
|
@@ -232,6 +278,13 @@ class PreviewFrameView {
|
|
|
232
278
|
return "light";
|
|
233
279
|
return typeof matchMedia === "function" && matchMedia("(prefers-color-scheme: dark)").matches ? "dark" : "light";
|
|
234
280
|
}
|
|
281
|
+
schemeToWrite() {
|
|
282
|
+
if (framePassesColorScheme())
|
|
283
|
+
return;
|
|
284
|
+
if (this.scheme === "auto" && !this.root.closest(".dark, .light"))
|
|
285
|
+
return;
|
|
286
|
+
return this.effectiveScheme();
|
|
287
|
+
}
|
|
235
288
|
load() {
|
|
236
289
|
const frame = document.createElement("iframe");
|
|
237
290
|
frame.setAttribute("sandbox", sandboxValue({ scripts: this.scripts, forms: this.forms }));
|
|
@@ -243,7 +296,8 @@ class PreviewFrameView {
|
|
|
243
296
|
if (frame === this.frame)
|
|
244
297
|
this.callbacks.onLoad?.(this.getState());
|
|
245
298
|
});
|
|
246
|
-
|
|
299
|
+
this.written = this.schemeToWrite();
|
|
300
|
+
frame.srcdoc = buildDocument(this.html, { title: this.title, forceScheme: this.written });
|
|
247
301
|
this.frame?.remove();
|
|
248
302
|
this.frame = frame;
|
|
249
303
|
this.applyScheme();
|
|
@@ -348,6 +402,8 @@ export {
|
|
|
348
402
|
declaresColorScheme,
|
|
349
403
|
describeViewport,
|
|
350
404
|
fitScale,
|
|
405
|
+
forceColorScheme,
|
|
406
|
+
framePassesColorScheme,
|
|
351
407
|
isIsolatingSandbox,
|
|
352
408
|
normalizeScheme,
|
|
353
409
|
resolveViewport,
|
|
@@ -23,6 +23,27 @@ export declare function normalizeScheme(value: unknown): PreviewScheme;
|
|
|
23
23
|
export declare function sandboxValue(options?: SandboxOptions): string;
|
|
24
24
|
/** Whether a sandbox value keeps the page away from the host. */
|
|
25
25
|
export declare function isIsolatingSandbox(value: string): boolean;
|
|
26
|
+
/**
|
|
27
|
+
* A document that answers for one colour scheme whatever the system prefers.
|
|
28
|
+
*
|
|
29
|
+
* Chromium hands a frame's `color-scheme` to its page; WebKit does not, so in
|
|
30
|
+
* Safari and in a Craft window the page keeps the system's scheme and a
|
|
31
|
+
* preview asked for `light` on a dark Mac was drawn dark (stacksjs/stx#2186).
|
|
32
|
+
* Nothing set on the frame from outside reaches a sandboxed page there, so the
|
|
33
|
+
* scheme is written into the page instead:
|
|
34
|
+
*
|
|
35
|
+
* - every `(prefers-color-scheme: …)` in a style sheet, a `media` attribute or
|
|
36
|
+
* a script is replaced by a condition that is always true or always false,
|
|
37
|
+
* so the page's own rules, and a `matchMedia` it calls, answer for the
|
|
38
|
+
* scheme asked for;
|
|
39
|
+
* - the root is given that `color-scheme`, so the canvas, form controls,
|
|
40
|
+
* system colours and `light-dark()` follow it too.
|
|
41
|
+
*
|
|
42
|
+
* Text is left alone: a page that shows the words "prefers-color-scheme" to
|
|
43
|
+
* its reader keeps them. A style sheet the page loads from elsewhere is not
|
|
44
|
+
* rewritten, since only the markup is in hand.
|
|
45
|
+
*/
|
|
46
|
+
export declare function forceColorScheme(html: string, scheme: 'light' | 'dark'): string;
|
|
26
47
|
/**
|
|
27
48
|
* The document the frame is given.
|
|
28
49
|
*
|
|
@@ -69,6 +90,7 @@ export declare interface SandboxOptions {
|
|
|
69
90
|
}
|
|
70
91
|
export declare interface DocumentOptions {
|
|
71
92
|
title?: string
|
|
93
|
+
forceScheme?: 'light' | 'dark'
|
|
72
94
|
}
|
|
73
95
|
/**
|
|
74
96
|
* What a preview is made of, before there is a frame. No DOM.
|
|
@@ -1,4 +1,13 @@
|
|
|
1
1
|
import type { PreviewScheme, ViewportName } from './core';
|
|
2
|
+
/**
|
|
3
|
+
* Whether this engine hands a frame's `color-scheme` to the page inside it.
|
|
4
|
+
*
|
|
5
|
+
* Asked of the engine once, with a frame of this page's own origin drawn in
|
|
6
|
+
* the scheme the system does not prefer: its page either reports that scheme
|
|
7
|
+
* (Chromium) or the system's (WebKit). An environment with no layout to ask,
|
|
8
|
+
* a DOM emulator, is taken at its word and left alone.
|
|
9
|
+
*/
|
|
10
|
+
export declare function framePassesColorScheme(): boolean;
|
|
2
11
|
export declare interface PreviewFrameOptions {
|
|
3
12
|
html?: string
|
|
4
13
|
viewport?: ViewportName | string
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@stacksjs/components",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.2.
|
|
4
|
+
"version": "0.2.867",
|
|
5
5
|
"description": "A modern component library built with STX, featuring headwind utility classes and TypeScript syntax highlighting.",
|
|
6
6
|
"author": "Chris Breuer <chris@stacksjs.org>",
|
|
7
7
|
"license": "MIT",
|
|
@@ -124,8 +124,8 @@
|
|
|
124
124
|
"dependencies": {
|
|
125
125
|
"@iconify-json/f7": "^1.2.2",
|
|
126
126
|
"@stacksjs/ts-css": "^0.3.11",
|
|
127
|
-
"@stacksjs/stx": "0.2.
|
|
128
|
-
"bun-plugin-stx": "0.2.
|
|
127
|
+
"@stacksjs/stx": "0.2.867",
|
|
128
|
+
"bun-plugin-stx": "0.2.867",
|
|
129
129
|
"ts-syntax-highlighter": "^0.2.20",
|
|
130
130
|
"ts-video-player": "^0.1.11"
|
|
131
131
|
},
|
|
@@ -23,10 +23,12 @@ The markup goes into an `<iframe srcdoc>` with a `sandbox` attribute.
|
|
|
23
23
|
navigation and popups are never granted. Links are sent to a new window,
|
|
24
24
|
which the sandbox refuses, so a click cannot navigate the preview away.
|
|
25
25
|
- **Colour scheme.** The scheme is set on the frame element, as its
|
|
26
|
-
`color-scheme`, on the host's side.
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
26
|
+
`color-scheme`, on the host's side. Chromium hands a frame's `color-scheme`
|
|
27
|
+
to the page inside as its `prefers-color-scheme`, so there the page follows
|
|
28
|
+
with no script and no cooperation, and a change does not reload it. WebKit
|
|
29
|
+
(Safari, and a Craft window, which is a WKWebView) does not, so there the
|
|
30
|
+
scheme is written into the document instead: see Limits. Which of the two
|
|
31
|
+
an engine does is asked of it once, with a frame of the host's own.
|
|
30
32
|
- **Viewport.** The frame is laid out at the device's real size and drawn
|
|
31
33
|
smaller to fit, so the page's own media queries see 390 pixels for a phone
|
|
32
34
|
whatever the box is. It is never enlarged.
|
|
@@ -103,13 +105,13 @@ the ground a device sits on), `--stx-preview-line` (`line`),
|
|
|
103
105
|
its title or its errors. With `scripts` on, a page can `postMessage` its
|
|
104
106
|
parent and the host can listen for it.
|
|
105
107
|
- **No device chrome, pixel ratio or touch emulation.** A viewport is a size.
|
|
106
|
-
- **
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
108
|
+
- **In WebKit the scheme is written into the page, with what that costs.**
|
|
109
|
+
Every `(prefers-color-scheme: …)` in the markup's style sheets, `media`
|
|
110
|
+
attributes and scripts is settled to the scheme asked for, and the root is
|
|
111
|
+
given that `color-scheme`. So: a change of scheme loads the page again
|
|
112
|
+
(its scroll position and anything typed into it are lost, and `@load` fires
|
|
113
|
+
again); a style sheet the page fetches from elsewhere is not rewritten and
|
|
114
|
+
still follows the system; and a script sees a `matchMedia` that never
|
|
115
|
+
changes. A preview left on `auto` under a host with no `dark` or `light`
|
|
116
|
+
class is not touched: its page follows the system by itself. Chromium needs
|
|
117
|
+
none of this and gets none of it (stacksjs/stx#2186).
|
|
@@ -146,6 +146,51 @@ const HAS_COLOR_SCHEME = /<meta[^>]+name\s*=\s*["']?color-scheme/i
|
|
|
146
146
|
export interface DocumentOptions {
|
|
147
147
|
/** The title of a document built around a fragment. */
|
|
148
148
|
title?: string
|
|
149
|
+
/**
|
|
150
|
+
* Write this scheme into the document, for an engine that does not hand a
|
|
151
|
+
* frame's `color-scheme` to the page inside it. See `forceColorScheme`.
|
|
152
|
+
*/
|
|
153
|
+
forceScheme?: 'light' | 'dark'
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
const SCHEME_FEATURE = /\(\s*prefers-color-scheme\s*:\s*(light|dark)\s*\)/gi
|
|
157
|
+
const STYLE_OR_SCRIPT = /(<(style|script)\b[^>]*>)([\s\S]*?)(<\/\2\s*>)/gi
|
|
158
|
+
const MEDIA_ATTRIBUTE = /(\smedia\s*=\s*)("[^"]*"|'[^']*')/gi
|
|
159
|
+
/** Always true, and always false in any frame wide enough to show a page. */
|
|
160
|
+
const ALWAYS = '(min-width: 0px)'
|
|
161
|
+
const NEVER = '(max-width: 0px)'
|
|
162
|
+
|
|
163
|
+
/**
|
|
164
|
+
* A document that answers for one colour scheme whatever the system prefers.
|
|
165
|
+
*
|
|
166
|
+
* Chromium hands a frame's `color-scheme` to its page; WebKit does not, so in
|
|
167
|
+
* Safari and in a Craft window the page keeps the system's scheme and a
|
|
168
|
+
* preview asked for `light` on a dark Mac was drawn dark (stacksjs/stx#2186).
|
|
169
|
+
* Nothing set on the frame from outside reaches a sandboxed page there, so the
|
|
170
|
+
* scheme is written into the page instead:
|
|
171
|
+
*
|
|
172
|
+
* - every `(prefers-color-scheme: …)` in a style sheet, a `media` attribute or
|
|
173
|
+
* a script is replaced by a condition that is always true or always false,
|
|
174
|
+
* so the page's own rules, and a `matchMedia` it calls, answer for the
|
|
175
|
+
* scheme asked for;
|
|
176
|
+
* - the root is given that `color-scheme`, so the canvas, form controls,
|
|
177
|
+
* system colours and `light-dark()` follow it too.
|
|
178
|
+
*
|
|
179
|
+
* Text is left alone: a page that shows the words "prefers-color-scheme" to
|
|
180
|
+
* its reader keeps them. A style sheet the page loads from elsewhere is not
|
|
181
|
+
* rewritten, since only the markup is in hand.
|
|
182
|
+
*/
|
|
183
|
+
export function forceColorScheme(html: string, scheme: 'light' | 'dark'): string {
|
|
184
|
+
const settle = (text: string): string => text.replace(SCHEME_FEATURE, (_, asked: string) => asked.toLowerCase() === scheme ? ALWAYS : NEVER)
|
|
185
|
+
const rewritten = html
|
|
186
|
+
.replace(STYLE_OR_SCRIPT, (_, open: string, _tag: string, body: string, close: string) => open + settle(body) + close)
|
|
187
|
+
.replace(MEDIA_ATTRIBUTE, (_, name: string, value: string) => name + settle(value))
|
|
188
|
+
const rule = `<style data-stx-preview-scheme>:root{color-scheme:${scheme} !important}</style>`
|
|
189
|
+
if (HEAD_OPEN.test(rewritten))
|
|
190
|
+
return rewritten.replace(HEAD_OPEN, match => match + rule)
|
|
191
|
+
if (HTML_OPEN.test(rewritten))
|
|
192
|
+
return rewritten.replace(HTML_OPEN, match => `${match}<head>${rule}</head>`)
|
|
193
|
+
return rule + rewritten
|
|
149
194
|
}
|
|
150
195
|
|
|
151
196
|
function escapeHtml(text: string): string {
|
|
@@ -165,6 +210,11 @@ function escapeHtml(text: string): string {
|
|
|
165
210
|
* click cannot navigate the preview away from what is being previewed.
|
|
166
211
|
*/
|
|
167
212
|
export function buildDocument(html: string | null | undefined, options: DocumentOptions = {}): string {
|
|
213
|
+
const built = wrapDocument(html, options)
|
|
214
|
+
return options.forceScheme ? forceColorScheme(built, options.forceScheme) : built
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
function wrapDocument(html: string | null | undefined, options: DocumentOptions): string {
|
|
168
218
|
const source = html == null ? '' : String(html)
|
|
169
219
|
const base = '<base target="_blank">'
|
|
170
220
|
|
|
@@ -15,10 +15,10 @@ export interface PreviewFrameProps {
|
|
|
15
15
|
/** Turn a phone or tablet on its side. */
|
|
16
16
|
landscape?: boolean
|
|
17
17
|
/**
|
|
18
|
-
*
|
|
19
|
-
*
|
|
20
|
-
*
|
|
21
|
-
*
|
|
18
|
+
* What the page's `prefers-color-scheme` reports. `auto` (default) follows
|
|
19
|
+
* the host page. Carried by the frame where the engine allows it and
|
|
20
|
+
* written into the page where it does not (WebKit), where a change loads
|
|
21
|
+
* the page again.
|
|
22
22
|
*/
|
|
23
23
|
scheme?: PreviewScheme
|
|
24
24
|
/** Let the page's own scripts run. They get an origin of their own and cannot reach the host. Default off. */
|
|
@@ -45,6 +45,41 @@ export interface PreviewFrameCallbacks {
|
|
|
45
45
|
|
|
46
46
|
const STAGE_PADDING = 16
|
|
47
47
|
|
|
48
|
+
const PREFERS_DARK = '(prefers-color-scheme: dark)'
|
|
49
|
+
let framesFollow: boolean | undefined
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Whether this engine hands a frame's `color-scheme` to the page inside it.
|
|
53
|
+
*
|
|
54
|
+
* Asked of the engine once, with a frame of this page's own origin drawn in
|
|
55
|
+
* the scheme the system does not prefer: its page either reports that scheme
|
|
56
|
+
* (Chromium) or the system's (WebKit). An environment with no layout to ask,
|
|
57
|
+
* a DOM emulator, is taken at its word and left alone.
|
|
58
|
+
*/
|
|
59
|
+
export function framePassesColorScheme(): boolean {
|
|
60
|
+
if (framesFollow !== undefined)
|
|
61
|
+
return framesFollow
|
|
62
|
+
try {
|
|
63
|
+
if (typeof matchMedia !== 'function' || !document.body)
|
|
64
|
+
return true
|
|
65
|
+
const systemDark = matchMedia(PREFERS_DARK).matches
|
|
66
|
+
const probe = document.createElement('iframe')
|
|
67
|
+
probe.setAttribute('aria-hidden', 'true')
|
|
68
|
+
probe.tabIndex = -1
|
|
69
|
+
probe.style.cssText = `position:absolute;width:1px;height:1px;border:0;opacity:0;pointer-events:none;color-scheme:${systemDark ? 'light' : 'dark'}`
|
|
70
|
+
document.body.appendChild(probe)
|
|
71
|
+
// Read after a layout, which is when the frame's style is settled.
|
|
72
|
+
void probe.offsetWidth
|
|
73
|
+
const seen = probe.contentWindow?.matchMedia?.(PREFERS_DARK).matches
|
|
74
|
+
probe.remove()
|
|
75
|
+
framesFollow = typeof seen === 'boolean' ? seen !== systemDark : true
|
|
76
|
+
}
|
|
77
|
+
catch {
|
|
78
|
+
framesFollow = true
|
|
79
|
+
}
|
|
80
|
+
return framesFollow
|
|
81
|
+
}
|
|
82
|
+
|
|
48
83
|
export class PreviewFrameView {
|
|
49
84
|
private html: string
|
|
50
85
|
private viewportName: string
|
|
@@ -58,6 +93,8 @@ export class PreviewFrameView {
|
|
|
58
93
|
private viewport: ResolvedViewport
|
|
59
94
|
private scale = 1
|
|
60
95
|
private frame: HTMLIFrameElement | null = null
|
|
96
|
+
/** The scheme written into the document on show, where the engine needs it written. */
|
|
97
|
+
private written: 'light' | 'dark' | undefined
|
|
61
98
|
private readonly stage: HTMLElement
|
|
62
99
|
private readonly device: HTMLElement
|
|
63
100
|
private observer: ResizeObserver | null = null
|
|
@@ -142,7 +179,12 @@ export class PreviewFrameView {
|
|
|
142
179
|
if (next === this.scheme)
|
|
143
180
|
return
|
|
144
181
|
this.scheme = next
|
|
145
|
-
|
|
182
|
+
// Where the scheme is written into the page, a different one is a
|
|
183
|
+
// different page: it is loaded again. Elsewhere the page is told in place.
|
|
184
|
+
if (this.schemeToWrite() !== this.written)
|
|
185
|
+
this.load()
|
|
186
|
+
else
|
|
187
|
+
this.applyScheme()
|
|
146
188
|
this.reflect()
|
|
147
189
|
this.callbacks.onScheme?.(this.getState())
|
|
148
190
|
}
|
|
@@ -199,6 +241,19 @@ export class PreviewFrameView {
|
|
|
199
241
|
return typeof matchMedia === 'function' && matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light'
|
|
200
242
|
}
|
|
201
243
|
|
|
244
|
+
/**
|
|
245
|
+
* The scheme the document has to carry itself, or nothing when the engine
|
|
246
|
+
* carries it or the page already has it: a preview left on `auto` under a
|
|
247
|
+
* host with no scheme of its own follows the system, as its page does anyway.
|
|
248
|
+
*/
|
|
249
|
+
private schemeToWrite(): 'light' | 'dark' | undefined {
|
|
250
|
+
if (framePassesColorScheme())
|
|
251
|
+
return undefined
|
|
252
|
+
if (this.scheme === 'auto' && !this.root.closest('.dark, .light'))
|
|
253
|
+
return undefined
|
|
254
|
+
return this.effectiveScheme()
|
|
255
|
+
}
|
|
256
|
+
|
|
202
257
|
private load(): void {
|
|
203
258
|
const frame = document.createElement('iframe')
|
|
204
259
|
// Written before the frame is in the document and before it has content:
|
|
@@ -212,7 +267,8 @@ export class PreviewFrameView {
|
|
|
212
267
|
if (frame === this.frame)
|
|
213
268
|
this.callbacks.onLoad?.(this.getState())
|
|
214
269
|
})
|
|
215
|
-
|
|
270
|
+
this.written = this.schemeToWrite()
|
|
271
|
+
frame.srcdoc = buildDocument(this.html, { title: this.title, forceScheme: this.written })
|
|
216
272
|
|
|
217
273
|
this.frame?.remove()
|
|
218
274
|
this.frame = frame
|
|
@@ -225,8 +281,9 @@ export class PreviewFrameView {
|
|
|
225
281
|
* The scheme is set here, on the host's side, as the frame element's
|
|
226
282
|
* `color-scheme`. Chromium hands that to the page as its
|
|
227
283
|
* `prefers-color-scheme`, so the page follows with no script and no
|
|
228
|
-
* cooperation. WebKit does not hand it over
|
|
229
|
-
*
|
|
284
|
+
* cooperation. WebKit does not hand it over, so there the scheme is also
|
|
285
|
+
* written into the document when it is loaded (`schemeToWrite`,
|
|
286
|
+
* stacksjs/stx#2186).
|
|
230
287
|
*/
|
|
231
288
|
private applyScheme(): void {
|
|
232
289
|
const scheme = this.effectiveScheme()
|