@stacksjs/components 0.2.854 → 0.2.857

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.
Files changed (139) hide show
  1. package/dist/CodeEditor-jg4w7e15.stx +497 -0
  2. package/dist/ContextMenu-43g3w5tp.stx +188 -0
  3. package/dist/DataGrid-7rt7v865.stx +376 -0
  4. package/dist/GraphView-0t3t9mec.stx +320 -0
  5. package/dist/JsonTree-satc4010.stx +317 -0
  6. package/dist/PreviewFrame-erbgvzj8.stx +257 -0
  7. package/dist/SplitPane-7sqfdwqa.stx +159 -0
  8. package/dist/code-editor/editor.js +2166 -0
  9. package/dist/component-api/component-api.js +36 -0
  10. package/dist/components/CodeBlock.d.ts +12 -5
  11. package/dist/context-menu/context-menu.js +575 -0
  12. package/dist/data-grid/data-grid.js +1244 -0
  13. package/dist/graph-view/graph-view.js +1408 -0
  14. package/dist/index.d.ts +10 -0
  15. package/dist/index.js +34 -15
  16. package/dist/json-tree/json-tree.js +1121 -0
  17. package/dist/preview-frame/preview-frame.js +356 -0
  18. package/dist/split-pane/split-pane.js +364 -0
  19. package/dist/ui/code-editor/CodeEditor.d.ts +46 -0
  20. package/dist/ui/code-editor/controller.d.ts +103 -0
  21. package/dist/ui/code-editor/core/commands.d.ts +55 -0
  22. package/dist/ui/code-editor/core/completion.d.ts +40 -0
  23. package/dist/ui/code-editor/core/editor.d.ts +69 -0
  24. package/dist/ui/code-editor/core/highlight.d.ts +61 -0
  25. package/dist/ui/code-editor/core/history.d.ts +27 -0
  26. package/dist/ui/code-editor/core/index.d.ts +13 -0
  27. package/dist/ui/code-editor/core/keymap.d.ts +38 -0
  28. package/dist/ui/code-editor/core/selection.d.ts +30 -0
  29. package/dist/ui/code-editor/core/text.d.ts +64 -0
  30. package/dist/ui/code-editor/editor.d.ts +15 -0
  31. package/dist/ui/code-editor/index.d.ts +29 -0
  32. package/dist/ui/code-editor/tokenizer.d.ts +18 -0
  33. package/dist/ui/code-editor/view.d.ts +59 -0
  34. package/dist/ui/context-menu/ContextMenu.d.ts +14 -0
  35. package/dist/ui/context-menu/context-menu.d.ts +8 -0
  36. package/dist/ui/context-menu/core.d.ts +74 -0
  37. package/dist/ui/context-menu/index.d.ts +11 -0
  38. package/dist/ui/context-menu/view.d.ts +36 -0
  39. package/dist/ui/data-grid/DataGrid.d.ts +30 -0
  40. package/dist/ui/data-grid/core.d.ts +141 -0
  41. package/dist/ui/data-grid/data-grid.d.ts +9 -0
  42. package/dist/ui/data-grid/index.d.ts +20 -0
  43. package/dist/ui/data-grid/view.d.ts +56 -0
  44. package/dist/ui/graph-view/GraphView.d.ts +21 -0
  45. package/dist/ui/graph-view/core/edges.d.ts +34 -0
  46. package/dist/ui/graph-view/core/force.d.ts +10 -0
  47. package/dist/ui/graph-view/core/graph.d.ts +83 -0
  48. package/dist/ui/graph-view/core/index.d.ts +5 -0
  49. package/dist/ui/graph-view/core/layered.d.ts +24 -0
  50. package/dist/ui/graph-view/core/viewport.d.ts +34 -0
  51. package/dist/ui/graph-view/graph-view.d.ts +9 -0
  52. package/dist/ui/graph-view/index.d.ts +21 -0
  53. package/dist/ui/graph-view/view.d.ts +51 -0
  54. package/dist/ui/json-tree/JsonTree.d.ts +18 -0
  55. package/dist/ui/json-tree/core.d.ts +108 -0
  56. package/dist/ui/json-tree/index.d.ts +18 -0
  57. package/dist/ui/json-tree/json-tree.d.ts +10 -0
  58. package/dist/ui/json-tree/tinker.d.ts +84 -0
  59. package/dist/ui/json-tree/view.d.ts +59 -0
  60. package/dist/ui/preview-frame/PreviewFrame.d.ts +44 -0
  61. package/dist/ui/preview-frame/core.d.ts +81 -0
  62. package/dist/ui/preview-frame/index.d.ts +19 -0
  63. package/dist/ui/preview-frame/preview-frame.d.ts +8 -0
  64. package/dist/ui/preview-frame/view.d.ts +37 -0
  65. package/dist/ui/split-pane/SplitPane.d.ts +18 -0
  66. package/dist/ui/split-pane/core.d.ts +58 -0
  67. package/dist/ui/split-pane/index.d.ts +15 -0
  68. package/dist/ui/split-pane/split-pane.d.ts +8 -0
  69. package/dist/ui/split-pane/view.d.ts +27 -0
  70. package/dist/ui/virtual-table/index.d.ts +6 -0
  71. package/dist/utils/component-api.d.ts +4 -0
  72. package/dist/utils/highlighter.d.ts +19 -1
  73. package/dist/utils/highlighter.js +24 -7
  74. package/dist/utils/syntax-theme.d.ts +44 -0
  75. package/package.json +44 -4
  76. package/src/components/CodeBlock.stx +195 -17
  77. package/src/index.ts +10 -0
  78. package/src/ui/code-editor/CodeEditor.stx +497 -0
  79. package/src/ui/code-editor/README.md +135 -0
  80. package/src/ui/code-editor/controller.ts +517 -0
  81. package/src/ui/code-editor/core/commands.ts +381 -0
  82. package/src/ui/code-editor/core/completion.ts +212 -0
  83. package/src/ui/code-editor/core/editor.ts +319 -0
  84. package/src/ui/code-editor/core/highlight.ts +203 -0
  85. package/src/ui/code-editor/core/history.ts +190 -0
  86. package/src/ui/code-editor/core/index.ts +14 -0
  87. package/src/ui/code-editor/core/keymap.ts +125 -0
  88. package/src/ui/code-editor/core/selection.ts +104 -0
  89. package/src/ui/code-editor/core/text.ts +252 -0
  90. package/src/ui/code-editor/editor.ts +16 -0
  91. package/src/ui/code-editor/index.ts +46 -0
  92. package/src/ui/code-editor/tokenizer.ts +74 -0
  93. package/src/ui/code-editor/view.ts +1066 -0
  94. package/src/ui/context-menu/ContextMenu.stx +188 -0
  95. package/src/ui/context-menu/README.md +117 -0
  96. package/src/ui/context-menu/context-menu.ts +8 -0
  97. package/src/ui/context-menu/core.ts +250 -0
  98. package/src/ui/context-menu/index.ts +16 -0
  99. package/src/ui/context-menu/view.ts +540 -0
  100. package/src/ui/data-grid/DataGrid.stx +376 -0
  101. package/src/ui/data-grid/README.md +204 -0
  102. package/src/ui/data-grid/core.ts +813 -0
  103. package/src/ui/data-grid/data-grid.ts +9 -0
  104. package/src/ui/data-grid/index.ts +34 -0
  105. package/src/ui/data-grid/view.ts +806 -0
  106. package/src/ui/graph-view/GraphView.stx +320 -0
  107. package/src/ui/graph-view/README.md +210 -0
  108. package/src/ui/graph-view/core/edges.ts +180 -0
  109. package/src/ui/graph-view/core/force.ts +237 -0
  110. package/src/ui/graph-view/core/graph.ts +277 -0
  111. package/src/ui/graph-view/core/index.ts +5 -0
  112. package/src/ui/graph-view/core/layered.ts +256 -0
  113. package/src/ui/graph-view/core/viewport.ts +141 -0
  114. package/src/ui/graph-view/graph-view.ts +9 -0
  115. package/src/ui/graph-view/index.ts +35 -0
  116. package/src/ui/graph-view/view.ts +809 -0
  117. package/src/ui/json-tree/JsonTree.stx +317 -0
  118. package/src/ui/json-tree/README.md +193 -0
  119. package/src/ui/json-tree/core.ts +588 -0
  120. package/src/ui/json-tree/index.ts +28 -0
  121. package/src/ui/json-tree/json-tree.ts +10 -0
  122. package/src/ui/json-tree/tinker.ts +349 -0
  123. package/src/ui/json-tree/view.ts +665 -0
  124. package/src/ui/preview-frame/PreviewFrame.stx +257 -0
  125. package/src/ui/preview-frame/README.md +107 -0
  126. package/src/ui/preview-frame/core.ts +195 -0
  127. package/src/ui/preview-frame/index.ts +31 -0
  128. package/src/ui/preview-frame/preview-frame.ts +8 -0
  129. package/src/ui/preview-frame/view.ts +336 -0
  130. package/src/ui/split-pane/README.md +87 -0
  131. package/src/ui/split-pane/SplitPane.stx +159 -0
  132. package/src/ui/split-pane/core.ts +200 -0
  133. package/src/ui/split-pane/index.ts +24 -0
  134. package/src/ui/split-pane/split-pane.ts +8 -0
  135. package/src/ui/split-pane/view.ts +305 -0
  136. package/src/ui/virtual-table/index.ts +6 -0
  137. package/src/utils/component-api.ts +72 -0
  138. package/src/utils/highlighter.ts +121 -218
  139. package/src/utils/syntax-theme.ts +144 -0
@@ -0,0 +1,257 @@
1
+ <script server>
2
+ export const html = typeof $props.html === 'string' ? $props.html : ''
3
+ export const viewport = ['phone', 'tablet', 'desktop'].includes($props.viewport) ? $props.viewport : 'fill'
4
+ export const frameWidth = $num($props.frameWidth, 0)
5
+ export const frameHeight = $num($props.frameHeight, 0)
6
+ export const landscape = $bool($props.landscape)
7
+ export const scheme = ['light', 'dark'].includes($props.scheme) ? $props.scheme : 'auto'
8
+ /*
9
+ * Off unless asked for. A page's scripts never share the host's origin either
10
+ * way; this decides whether they run at all.
11
+ */
12
+ export const scripts = $bool($props.scripts)
13
+ export const forms = $bool($props.forms)
14
+ export const toolbar = $bool($props.toolbar, true)
15
+ export const title = $props.title || 'Preview'
16
+ /* A CSS length for the whole component, toolbar included. */
17
+ export const height = $props.height || '24rem'
18
+ export const className = $props.className || ''
19
+ export const previewId = $props.id || `preview-frame-${$uid}`
20
+ export const viewports = [
21
+ { name: 'fill', label: 'Fill' },
22
+ { name: 'phone', label: 'Phone' },
23
+ { name: 'tablet', label: 'Tablet' },
24
+ { name: 'desktop', label: 'Desktop' },
25
+ ]
26
+ </script>
27
+
28
+ <script client>
29
+ /*
30
+ * <PreviewFrame>: an HTML string shown as a page, in a frame that cannot
31
+ * reach the page around it.
32
+ *
33
+ * What goes into the frame and what it is allowed is in ./core; the frame
34
+ * itself, its size and its toolbar are in ./view. This is the wiring.
35
+ */
36
+ import { PreviewFrameView } from './view'
37
+
38
+ const emit = defineEmits(['load', 'reload', 'viewport', 'scheme'])
39
+
40
+ function asBoolean(value) {
41
+ if (value == null) return false
42
+ if (typeof value === 'string') return !['false', '0', 'off', 'no'].includes(value.trim().toLowerCase())
43
+ return Boolean(value)
44
+ }
45
+ function asText(value) { return value == null ? '' : String(value) }
46
+ function asSize(value) {
47
+ const size = Number(value)
48
+ return Number.isFinite(size) && size > 0 ? size : null
49
+ }
50
+
51
+ const liveHtml = useReactiveProp('html', {{ html }}, { parse: asText })
52
+ const liveViewport = useReactiveProp('viewport', {{ viewport }}, { parse: asText })
53
+ const liveScheme = useReactiveProp('scheme', {{ scheme }}, { parse: asText })
54
+ const liveLandscape = useReactiveProp('landscape', {{ landscape }}, { parse: asBoolean })
55
+ const liveScripts = useReactiveProp('scripts', {{ scripts }}, { parse: asBoolean })
56
+ const liveFrameWidth = useReactiveProp('frameWidth', {{ frameWidth }}, { parse: asSize })
57
+ const liveFrameHeight = useReactiveProp('frameHeight', {{ frameHeight }}, { parse: asSize })
58
+
59
+ const rootRef = useRef('preview-frame')
60
+ let preview = null
61
+
62
+ onMount(() => {
63
+ const root = rootRef()
64
+ if (!root) return
65
+
66
+ preview = new PreviewFrameView(root, {
67
+ html: asText(liveHtml()),
68
+ viewport: asText(liveViewport()),
69
+ frameWidth: asSize(liveFrameWidth()),
70
+ frameHeight: asSize(liveFrameHeight()),
71
+ landscape: asBoolean(liveLandscape()),
72
+ scheme: asText(liveScheme()),
73
+ scripts: asBoolean(liveScripts()),
74
+ forms: {{ forms }},
75
+ title: {{ title }},
76
+ }, {
77
+ onLoad(detail) { emit('load', detail) },
78
+ onReload(detail) { emit('reload', detail) },
79
+ onViewport(detail) { emit('viewport', detail) },
80
+ onScheme(detail) { emit('scheme', detail) },
81
+ })
82
+
83
+ effect(() => preview.setHtml(asText(liveHtml())))
84
+ effect(() => preview.setScheme(asText(liveScheme())))
85
+ effect(() => preview.setPermissions({ scripts: asBoolean(liveScripts()) }))
86
+ // What the toolbar chose stays chosen until the parent says something new.
87
+ let viewportSeen = false
88
+ effect(() => {
89
+ const name = asText(liveViewport())
90
+ const options = { landscape: asBoolean(liveLandscape()), width: asSize(liveFrameWidth()), height: asSize(liveFrameHeight()) }
91
+ if (viewportSeen) preview.setViewport(name, options)
92
+ viewportSeen = true
93
+ })
94
+ })
95
+
96
+ onDestroy(() => {
97
+ if (preview) preview.destroy()
98
+ preview = null
99
+ })
100
+
101
+ defineExpose({
102
+ reload: () => { if (preview) preview.reload() },
103
+ setHtml: (next) => { if (preview) preview.setHtml(asText(next)) },
104
+ setViewport: (name, options) => { if (preview) preview.setViewport(asText(name), options || {}) },
105
+ setScheme: (next) => { if (preview) preview.setScheme(asText(next)) },
106
+ getState: () => (preview ? preview.getState() : null),
107
+ })
108
+ </script>
109
+
110
+ <div
111
+ ref="preview-frame"
112
+ id="{{ previewId }}"
113
+ data-stx-preview-frame
114
+ data-viewport="{{ viewport }}"
115
+ class="stx-preview {{ className }}"
116
+ style="height: {{ height }}"
117
+ >
118
+ @if(toolbar)
119
+ <div data-pf="toolbar" role="toolbar" aria-label="{{ title }} controls" class="stx-preview-toolbar">
120
+ <div role="radiogroup" aria-label="Viewport" class="stx-preview-group">
121
+ @foreach(option in viewports)
122
+ <button
123
+ type="button"
124
+ role="radio"
125
+ data-pf-viewport="{{ option.name }}"
126
+ aria-checked="{{ option.name === viewport ? 'true' : 'false' }}"
127
+ tabindex="{{ option.name === viewport ? '0' : '-1' }}"
128
+ class="stx-preview-button"
129
+ >{{ option.label }}</button>
130
+ @endforeach
131
+ </div>
132
+ <button type="button" data-pf-action="rotate" aria-pressed="false" class="stx-preview-button">Rotate</button>
133
+ {{-- Not a live region: it changes on every drag of a pane, and reading
134
+ each size out would bury everything else. --}}
135
+ <span data-pf="size" class="stx-preview-size"></span>
136
+ <span class="stx-preview-spacer"></span>
137
+ <button type="button" data-pf-action="scheme" aria-pressed="false" aria-label="Dark colour scheme" class="stx-preview-button">
138
+ <span data-pf="scheme-text">Light</span>
139
+ </button>
140
+ <button type="button" data-pf-action="reload" class="stx-preview-button">Reload</button>
141
+ </div>
142
+ @endif
143
+
144
+ <div data-pf="stage" class="stx-preview-stage">
145
+ <div data-pf="device" class="stx-preview-device"></div>
146
+ </div>
147
+ </div>
148
+
149
+ <style>
150
+ /*
151
+ * The stage is the neutral ground a device sits on. The frame's own
152
+ * background is the page's business: the frame is drawn in the scheme the
153
+ * page was given, so an unstyled page is white or near-black as it would be
154
+ * in a browser.
155
+ */
156
+ :where([data-stx-preview-frame]) {
157
+ --stx-preview-bg: var(--stx-panel, #ffffff);
158
+ --stx-preview-fg: var(--stx-fg, #171717);
159
+ --stx-preview-fg-soft: var(--stx-fg-soft, #737373);
160
+ --stx-preview-stage: var(--stx-surface-sunken, #e5e5e5);
161
+ --stx-preview-line: var(--stx-line, #e5e5e5);
162
+ --stx-preview-active: var(--stx-accent, #2563eb);
163
+ --stx-preview-font-size: 12px;
164
+ --stx-preview-radius: 8px;
165
+ display: flex;
166
+ flex-direction: column;
167
+ box-sizing: border-box;
168
+ overflow: hidden;
169
+ border: 1px solid var(--stx-preview-line);
170
+ border-radius: var(--stx-preview-radius);
171
+ background: var(--stx-preview-bg);
172
+ color: var(--stx-preview-fg);
173
+ }
174
+
175
+ /*
176
+ * The layout is written out here rather than left to utility classes: a
177
+ * device that is the wrong size is a wrong preview, so it must not depend on
178
+ * what the app's stylesheet happens to generate.
179
+ */
180
+ [data-stx-preview-frame] :is(.stx-preview-toolbar) {
181
+ display: flex;
182
+ flex: none;
183
+ align-items: center;
184
+ gap: 8px;
185
+ padding: 4px 8px;
186
+ border-bottom: 1px solid var(--stx-preview-line);
187
+ font-size: var(--stx-preview-font-size);
188
+ line-height: 1.5;
189
+ /* One line always: a toolbar that wraps changes the size of the stage under it. */
190
+ white-space: nowrap;
191
+ overflow: hidden;
192
+ }
193
+ [data-stx-preview-frame] :is(.stx-preview-spacer) { flex: 1 1 0; }
194
+ [data-stx-preview-frame] :is(.stx-preview-group) {
195
+ display: flex;
196
+ flex: none;
197
+ border: 1px solid var(--stx-preview-line);
198
+ border-radius: 6px;
199
+ overflow: hidden;
200
+ }
201
+ [data-stx-preview-frame] :is(.stx-preview-button) {
202
+ padding: 2px 8px;
203
+ flex: none;
204
+ border: 0;
205
+ border-radius: 5px;
206
+ background: transparent;
207
+ color: inherit;
208
+ font: inherit;
209
+ cursor: pointer;
210
+ }
211
+ [data-stx-preview-frame] :is(.stx-preview-group) > :is(.stx-preview-button) { border-radius: 0; }
212
+ [data-stx-preview-frame] :is(.stx-preview-button):hover { background: color-mix(in srgb, var(--stx-preview-fg) 8%, transparent); }
213
+ [data-stx-preview-frame] :is(.stx-preview-button):is([aria-checked="true"], [aria-pressed="true"]) {
214
+ background: color-mix(in srgb, var(--stx-preview-active) 16%, transparent);
215
+ color: var(--stx-preview-active);
216
+ }
217
+ [data-stx-preview-frame] :is(.stx-preview-button):focus-visible {
218
+ outline: 2px solid var(--stx-preview-active);
219
+ outline-offset: -2px;
220
+ }
221
+ [data-stx-preview-frame] :is(.stx-preview-button):disabled { opacity: 0.45; cursor: default; background: transparent; }
222
+ [data-stx-preview-frame] :is(.stx-preview-size) { overflow: hidden; text-overflow: ellipsis; color: var(--stx-preview-fg-soft); font-variant-numeric: tabular-nums; }
223
+
224
+ [data-stx-preview-frame] :is(.stx-preview-stage) {
225
+ position: relative;
226
+ flex: 1 1 0;
227
+ min-height: 0;
228
+ overflow: hidden;
229
+ display: flex;
230
+ align-items: center;
231
+ justify-content: center;
232
+ background: var(--stx-preview-stage);
233
+ }
234
+ [data-stx-preview-frame] :is(.stx-preview-device) {
235
+ position: relative;
236
+ flex: none;
237
+ overflow: hidden;
238
+ border-radius: 6px;
239
+ box-shadow: 0 0 0 1px var(--stx-preview-line), 0 8px 24px rgb(0 0 0 / 0.12);
240
+ }
241
+ [data-stx-preview-frame] :is(.stx-preview-frame) {
242
+ display: block;
243
+ border: 0;
244
+ transform-origin: 0 0;
245
+ /* Never see-through, so the stage does not show under a page with no background. */
246
+ background: Canvas;
247
+ }
248
+
249
+ /* Filling: no device, the page has the whole stage. */
250
+ [data-stx-preview-frame][data-viewport="fill"] :is(.stx-preview-device) {
251
+ width: 100%;
252
+ height: 100%;
253
+ border-radius: 0;
254
+ box-shadow: none;
255
+ }
256
+ [data-stx-preview-frame][data-viewport="fill"] :is(.stx-preview-frame) { width: 100%; height: 100%; }
257
+ </style>
@@ -0,0 +1,107 @@
1
+ # PreviewFrame
2
+
3
+ An HTML string shown as a page: a mail, a rendered view, any markup. In a
4
+ sandboxed frame that cannot reach the page around it, at the size of a phone, a
5
+ tablet or a desktop, in light or dark.
6
+
7
+ ```html
8
+ <script client>
9
+ const mail = state('<h1>Welcome</h1><p>Thanks for signing up.</p>')
10
+ </script>
11
+
12
+ <PreviewFrame :html="mail()" viewport="phone" scheme="dark" title="Welcome mail" />
13
+ ```
14
+
15
+ ## How it works
16
+
17
+ The markup goes into an `<iframe srcdoc>` with a `sandbox` attribute.
18
+
19
+ - **Isolation.** The sandbox is empty by default: no scripts, no forms, and an
20
+ origin of its own. `scripts` adds `allow-scripts` and nothing else, so a
21
+ page's scripts run but still have a unique origin: no access to the host's
22
+ DOM, storage or cookies, and none of their own. `allow-same-origin`, top
23
+ navigation and popups are never granted. Links are sent to a new window,
24
+ which the sandbox refuses, so a click cannot navigate the preview away.
25
+ - **Colour scheme.** A frame's page takes its `prefers-color-scheme` from the
26
+ `color-scheme` its frame element is drawn with. The scheme is set there, on
27
+ the host's side, so the page follows it with no script and no cooperation,
28
+ and a change does not reload it.
29
+ - **Viewport.** The frame is laid out at the device's real size and drawn
30
+ smaller to fit, so the page's own media queries see 390 pixels for a phone
31
+ whatever the box is. It is never enlarged.
32
+ - **Reload.** The frame is made new for every load. A frame's sandbox is read
33
+ when its document is created, so a new frame is the only way to be sure of
34
+ the permissions, and it makes reload start from nothing.
35
+
36
+ A fragment (`<h1>Hi</h1>`) is wrapped in a page that declares both colour
37
+ schemes, so bare markup goes dark on a dark preview. A whole document is left
38
+ as written: a mail with no dark styles is shown light, as a mail client would.
39
+
40
+ | File | What it is |
41
+ |---|---|
42
+ | `core.ts` | The document that goes into the frame, the sandbox value, the viewport presets and the arithmetic of fitting one. No DOM. |
43
+ | `view.ts` | The frame itself, its size, its scheme, and the toolbar. |
44
+ | `PreviewFrame.stx` | The template, the styles, and the props and events. |
45
+
46
+ `@stacksjs/components/preview-frame` exports the first two.
47
+
48
+ ## Props
49
+
50
+ | Prop | Default | |
51
+ |---|---|---|
52
+ | `html` | `''` | The markup: a whole document or a fragment. |
53
+ | `viewport` | `'fill'` | `fill`, `phone` (390 × 844), `tablet` (820 × 1180) or `desktop` (1280 × 800). |
54
+ | `frameWidth`, `frameHeight` | | A viewport of its own size, in CSS pixels. Both are needed, and they win over `viewport`. |
55
+ | `landscape` | `false` | Turn a phone or tablet on its side. |
56
+ | `scheme` | `'auto'` | `light`, `dark`, or `auto` to follow the host page (a `dark` or `light` class on an ancestor, then the system). |
57
+ | `scripts` | `false` | Let the page's own scripts run. |
58
+ | `forms` | `false` | Let a form in the page submit. |
59
+ | `toolbar` | `true` | Show the viewport, rotate, scheme and reload controls. |
60
+ | `title` | `'Preview'` | The frame's accessible name. |
61
+ | `height` | `'24rem'` | A CSS length for the whole component, toolbar included. |
62
+ | `id`, `className` | | |
63
+
64
+ ## Events
65
+
66
+ Every payload is the state: `{ viewport, width, height, landscape, scale, scheme, effectiveScheme }`.
67
+
68
+ | Event | |
69
+ |---|---|
70
+ | `@load` | The page has loaded. |
71
+ | `@reload` | A reload was asked for, from the toolbar or `reload()`. |
72
+ | `@viewport` | The viewport changed. |
73
+ | `@scheme` | The colour scheme changed. |
74
+
75
+ ## Methods
76
+
77
+ From `componentApi(target)` (`@stacksjs/components/component-api`), where
78
+ `target` is the component's element, its id or a selector: `reload()`, `setHtml(html)`,
79
+ `setViewport(name, { landscape, width, height })`, `setScheme(scheme)`,
80
+ `getState()`.
81
+
82
+ ## Keys
83
+
84
+ The viewport buttons are one radio group: Tab reaches the chosen one and the
85
+ arrow keys move through them. Rotate, the scheme toggle and Reload are buttons;
86
+ the toggle reports `aria-pressed` for dark.
87
+
88
+ ## Theming
89
+
90
+ `--stx-preview-bg` (the `panel` role), `--stx-preview-fg` (`fg`),
91
+ `--stx-preview-fg-soft` (`fg-soft`), `--stx-preview-stage` (`surface-sunken`,
92
+ the ground a device sits on), `--stx-preview-line` (`line`),
93
+ `--stx-preview-active` (`accent`), `--stx-preview-radius` and
94
+ `--stx-preview-font-size`.
95
+
96
+ ## Limits of this version
97
+
98
+ - **The page can load from the network.** The sandbox isolates the page from
99
+ the host; it does not stop an `<img>` or a stylesheet being fetched. Markup
100
+ from an untrusted source can therefore still tell its sender it was opened.
101
+ - **Nothing comes back from the page.** There is no channel for its height,
102
+ its title or its errors. With `scripts` on, a page can `postMessage` its
103
+ parent and the host can listen for it.
104
+ - **No device chrome, pixel ratio or touch emulation.** A viewport is a size.
105
+ - **Inherits what the engine does.** Passing `color-scheme` through to a frame
106
+ is standard and was checked in WebKit; an engine without it shows the page
107
+ in the system scheme.
@@ -0,0 +1,195 @@
1
+ /**
2
+ * What a preview is made of, before there is a frame. No DOM.
3
+ *
4
+ * The document that goes into the frame, the permissions the frame is given,
5
+ * and the arithmetic of showing a phone-sized page in a box that is not
6
+ * phone-sized.
7
+ */
8
+
9
+ export type PreviewScheme = 'light' | 'dark' | 'auto'
10
+ export type ViewportName = 'fill' | 'phone' | 'tablet' | 'desktop' | 'custom'
11
+
12
+ export interface ViewportSize {
13
+ width: number
14
+ height: number
15
+ }
16
+
17
+ export interface ViewportPreset extends ViewportSize {
18
+ name: Exclude<ViewportName, 'fill' | 'custom'>
19
+ label: string
20
+ }
21
+
22
+ /** CSS pixels, portrait for the two that are held in a hand. */
23
+ export const VIEWPORT_PRESETS: readonly ViewportPreset[] = [
24
+ { name: 'phone', label: 'Phone', width: 390, height: 844 },
25
+ { name: 'tablet', label: 'Tablet', width: 820, height: 1180 },
26
+ { name: 'desktop', label: 'Desktop', width: 1280, height: 800 },
27
+ ]
28
+
29
+ export interface ResolvedViewport {
30
+ name: ViewportName
31
+ /** Null for `fill`: the page gets whatever the box is. */
32
+ size: ViewportSize | null
33
+ landscape: boolean
34
+ }
35
+
36
+ function positive(value: unknown): number | null {
37
+ const number = typeof value === 'string' ? Number.parseFloat(value) : value
38
+ return typeof number === 'number' && Number.isFinite(number) && number > 0 ? Math.round(number) : null
39
+ }
40
+
41
+ /**
42
+ * The viewport a name stands for.
43
+ *
44
+ * A width and a height given outright win over a name. `landscape` turns a
45
+ * preset on its side; a desktop is already on its side and stays as it is.
46
+ * A name nobody knows fills the box, which is the least surprising thing to do
47
+ * with it.
48
+ */
49
+ export function resolveViewport(name: unknown, options: { width?: unknown, height?: unknown, landscape?: boolean } = {}): ResolvedViewport {
50
+ const width = positive(options.width)
51
+ const height = positive(options.height)
52
+ const landscape = Boolean(options.landscape)
53
+ if (width && height)
54
+ return { name: 'custom', size: { width, height }, landscape: false }
55
+
56
+ const preset = VIEWPORT_PRESETS.find(entry => entry.name === name)
57
+ if (!preset)
58
+ return { name: 'fill', size: null, landscape: false }
59
+
60
+ const turn = landscape && preset.height > preset.width
61
+ return {
62
+ name: preset.name,
63
+ size: turn ? { width: preset.height, height: preset.width } : { width: preset.width, height: preset.height },
64
+ landscape: turn,
65
+ }
66
+ }
67
+
68
+ /**
69
+ * How much to shrink a viewport so it fits a box, never enlarging it: a page
70
+ * drawn larger than life would misreport what it looks like.
71
+ */
72
+ export function fitScale(viewport: ViewportSize, available: ViewportSize, padding = 0): number {
73
+ const width = available.width - padding * 2
74
+ const height = available.height - padding * 2
75
+ if (width <= 0 || height <= 0 || viewport.width <= 0 || viewport.height <= 0)
76
+ return 1
77
+ return Math.min(1, width / viewport.width, height / viewport.height)
78
+ }
79
+
80
+ /** `390 × 844`, with the scale when the page is shown smaller than life. */
81
+ export function describeViewport(viewport: ResolvedViewport, scale = 1): string {
82
+ if (!viewport.size)
83
+ return 'Fills the frame'
84
+ const size = `${viewport.size.width} × ${viewport.size.height}`
85
+ return scale < 0.995 ? `${size} at ${Math.round(scale * 100)}%` : size
86
+ }
87
+
88
+ export function normalizeScheme(value: unknown): PreviewScheme {
89
+ return value === 'light' || value === 'dark' ? value : 'auto'
90
+ }
91
+
92
+ export interface SandboxOptions {
93
+ /** Let the page's own scripts run. They run in an origin of their own and cannot reach the host. */
94
+ scripts?: boolean
95
+ /** Let a form in the page submit. */
96
+ forms?: boolean
97
+ }
98
+
99
+ /**
100
+ * Tokens that would give the page a way back to the host, and are never
101
+ * granted whatever is asked for. `allow-same-origin` puts the page in the
102
+ * host's origin, where its scripts can read the host's storage and DOM; the
103
+ * navigation ones let it replace the host page; popups get a window the
104
+ * sandbox does not cover.
105
+ */
106
+ export const FORBIDDEN_SANDBOX_TOKENS: readonly string[] = [
107
+ 'allow-same-origin',
108
+ 'allow-top-navigation',
109
+ 'allow-top-navigation-by-user-activation',
110
+ 'allow-top-navigation-to-custom-protocols',
111
+ 'allow-popups',
112
+ 'allow-popups-to-escape-sandbox',
113
+ 'allow-modals',
114
+ 'allow-downloads',
115
+ 'allow-pointer-lock',
116
+ 'allow-presentation',
117
+ 'allow-storage-access-by-user-activation',
118
+ ]
119
+
120
+ /**
121
+ * The frame's `sandbox` value. Empty is the strictest there is: no scripts, no
122
+ * forms, an origin of its own. An empty string still has to be written as the
123
+ * attribute, since a frame with no `sandbox` attribute is not sandboxed at all.
124
+ */
125
+ export function sandboxValue(options: SandboxOptions = {}): string {
126
+ const tokens: string[] = []
127
+ if (options.scripts)
128
+ tokens.push('allow-scripts')
129
+ if (options.forms)
130
+ tokens.push('allow-forms')
131
+ return tokens.join(' ')
132
+ }
133
+
134
+ /** Whether a sandbox value keeps the page away from the host. */
135
+ export function isIsolatingSandbox(value: string): boolean {
136
+ const tokens = value.toLowerCase().split(/\s+/).filter(Boolean)
137
+ return !tokens.some(token => FORBIDDEN_SANDBOX_TOKENS.includes(token))
138
+ }
139
+
140
+ const HAS_DOCUMENT = /<!doctype\s|<html[\s>]/i
141
+ const HEAD_OPEN = /<head(?:\s[^>]*)?>/i
142
+ const HTML_OPEN = /<html(?:\s[^>]*)?>/i
143
+ const HAS_BASE = /<base[\s>]/i
144
+ const HAS_COLOR_SCHEME = /<meta[^>]+name\s*=\s*["']?color-scheme/i
145
+
146
+ export interface DocumentOptions {
147
+ /** The title of a document built around a fragment. */
148
+ title?: string
149
+ }
150
+
151
+ function escapeHtml(text: string): string {
152
+ return text.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;')
153
+ }
154
+
155
+ /**
156
+ * The document the frame is given.
157
+ *
158
+ * A fragment (`<h1>Hi</h1>`) is wrapped in a page that follows the colour
159
+ * scheme, so bare markup is dark on a dark preview the way it would be in a
160
+ * dark page. A whole document is left as its author wrote it: a mail that
161
+ * declares no dark styles is shown light, which is what a mail client does
162
+ * with it.
163
+ *
164
+ * Either way links open "in a new window", which the sandbox refuses, so a
165
+ * click cannot navigate the preview away from what is being previewed.
166
+ */
167
+ export function buildDocument(html: string | null | undefined, options: DocumentOptions = {}): string {
168
+ const source = html == null ? '' : String(html)
169
+ const base = '<base target="_blank">'
170
+
171
+ if (!HAS_DOCUMENT.test(source)) {
172
+ const title = options.title ? `<title>${escapeHtml(options.title)}</title>` : ''
173
+ return `<!doctype html><html><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1"><meta name="color-scheme" content="light dark">${base}${title}</head><body>${source}</body></html>`
174
+ }
175
+
176
+ if (HAS_BASE.test(source))
177
+ return source
178
+ if (HEAD_OPEN.test(source))
179
+ return source.replace(HEAD_OPEN, match => match + base)
180
+ if (HTML_OPEN.test(source))
181
+ return source.replace(HTML_OPEN, match => `${match}<head>${base}</head>`)
182
+ return base + source
183
+ }
184
+
185
+ /** Whether a document says which colour schemes it supports. */
186
+ export function declaresColorScheme(html: string): boolean {
187
+ return HAS_COLOR_SCHEME.test(html)
188
+ }
189
+
190
+ /** The preset after `name` in the toolbar's order, for the arrow keys. `fill` comes first. */
191
+ export function stepViewport(name: ViewportName, direction: 1 | -1): ViewportName {
192
+ const order: ViewportName[] = ['fill', ...VIEWPORT_PRESETS.map(preset => preset.name)]
193
+ const index = order.indexOf(name)
194
+ return order[((index < 0 ? 0 : index) + direction + order.length) % order.length]
195
+ }
@@ -0,0 +1,31 @@
1
+ import type { PreviewScheme, ViewportName } from './core'
2
+
3
+ export * from './core'
4
+ export { default as PreviewFrame } from './PreviewFrame.stx'
5
+ export * from './view'
6
+
7
+ export interface PreviewFrameProps {
8
+ /** The markup to show: a whole document or a fragment. */
9
+ html?: string
10
+ /** `fill` (default), `phone`, `tablet` or `desktop`. */
11
+ viewport?: Exclude<ViewportName, 'custom'>
12
+ /** A viewport of its own size, in CSS pixels. Both are needed, and they win over `viewport`. */
13
+ frameWidth?: number
14
+ frameHeight?: number
15
+ /** Turn a phone or tablet on its side. */
16
+ landscape?: boolean
17
+ /** What the page's `prefers-color-scheme` reports. `auto` (default) follows the host page. */
18
+ scheme?: PreviewScheme
19
+ /** Let the page's own scripts run. They get an origin of their own and cannot reach the host. Default off. */
20
+ scripts?: boolean
21
+ /** Let a form in the page submit. Default off. */
22
+ forms?: boolean
23
+ /** Show the viewport, scheme and reload controls. Default on. */
24
+ toolbar?: boolean
25
+ /** The frame's accessible name. Default `Preview`. */
26
+ title?: string
27
+ /** A CSS length for the whole component. Default `24rem`. */
28
+ height?: string
29
+ id?: string
30
+ className?: string
31
+ }
@@ -0,0 +1,8 @@
1
+ /**
2
+ * The preview frame without its stx template.
3
+ *
4
+ * `@stacksjs/components/preview-frame`. The document and sandbox a preview is
5
+ * built from (`./core`, no DOM), and the view that puts one in a frame.
6
+ */
7
+ export * from './core'
8
+ export * from './view'