@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,317 @@
1
+ <script server>
2
+ /*
3
+ * Either prop can arrive here as an object (a value known on the server) or
4
+ * not at all (a bound one, which the client script reads live).
5
+ */
6
+ export const value = $props.value && typeof $props.value === 'object' ? $props.value : null
7
+ export const tinker = $props.tinker && typeof $props.tinker === 'object' ? $props.tinker : null
8
+ export const depth = $num($props.depth, 1)
9
+ export const rowHeight = $num($props.rowHeight) || 22
10
+ export const contextMenu = $bool($props.contextMenu)
11
+ /* A CSS length. `100%` fills a sized parent, which is what a pane wants. */
12
+ export const height = $props.height || '20rem'
13
+ export const label = $props.label || 'Value'
14
+ export const className = $props.className || ''
15
+ export const treeId = $props.id || `json-tree-${$uid}`
16
+ </script>
17
+
18
+ <script client>
19
+ /*
20
+ * <JsonTree>: an expandable inspector for a described value.
21
+ *
22
+ * This script is the wiring: props in, events out. Which rows are visible is
23
+ * in ./core, how a tinker result becomes nodes is in ./tinker, and how the
24
+ * rows on screen are drawn is in ./view.
25
+ */
26
+ import { fromTinkerValue } from './tinker'
27
+ import { JsonTreeView } from './view'
28
+
29
+ const emit = defineEmits(['select', 'toggle', 'copy', 'open', 'load-more', 'context'])
30
+
31
+ function asBoolean(value) {
32
+ if (value == null) return false
33
+ if (typeof value === 'string') return !['false', '0', 'off', 'no'].includes(value.trim().toLowerCase())
34
+ return Boolean(value)
35
+ }
36
+ // A described value is an object or a list of them. As an attribute it is JSON.
37
+ function asDescribed(value) {
38
+ if (value && typeof value === 'object') return value
39
+ if (typeof value === 'string' && (value.charAt(0) === '{' || value.charAt(0) === '[')) {
40
+ try { return JSON.parse(value) }
41
+ catch (e) { return null }
42
+ }
43
+ return null
44
+ }
45
+ function asDepth(value) {
46
+ const number = Number(value)
47
+ return value == null || value === '' || Number.isNaN(number) ? 1 : number
48
+ }
49
+
50
+ const liveValue = useReactiveProp('value', {{ value }}, { parse: asDescribed })
51
+ const liveTinker = useReactiveProp('tinker', {{ tinker }}, { parse: asDescribed })
52
+ const liveDepth = useReactiveProp('depth', {{ depth }}, { parse: asDepth })
53
+ const liveContextMenu = useReactiveProp('contextMenu', {{ contextMenu }}, { parse: asBoolean })
54
+ // A function can only arrive through a bound attribute, and is read as it is.
55
+ const liveOpenable = useReactiveProp('openable', null, { parse: v => v })
56
+
57
+ // The nodes to show: the described value, or the tinker result adapted.
58
+ function described() {
59
+ const nodes = asDescribed(liveValue())
60
+ if (nodes) return nodes
61
+ const result = asDescribed(liveTinker())
62
+ return result ? fromTinkerValue(result) : null
63
+ }
64
+ function asFunction(value) { return typeof value === 'function' ? value : null }
65
+
66
+ const rootRef = useRef('json-tree')
67
+ let tree = null
68
+
69
+ onMount(() => {
70
+ const root = rootRef()
71
+ if (!root) return
72
+
73
+ tree = new JsonTreeView(root, {
74
+ value: described(),
75
+ depth: asDepth(liveDepth()),
76
+ rowHeight: {{ rowHeight }},
77
+ openable: asFunction(liveOpenable()),
78
+ contextMenu: asBoolean(liveContextMenu()),
79
+ }, {
80
+ onSelect(detail) { emit('select', detail) },
81
+ onToggle(detail) { emit('toggle', detail) },
82
+ onCopy(detail) { emit('copy', detail) },
83
+ onOpen(detail) { emit('open', detail) },
84
+ onLoadMore(detail) { emit('load-more', detail) },
85
+ onContext(detail) { emit('context', detail) },
86
+ })
87
+
88
+ // The first run of each finds what the view was just built with. Setting it
89
+ // again would throw away nothing yet, but would build the rows twice.
90
+ let valueSeen = false
91
+ effect(() => {
92
+ const next = described()
93
+ if (valueSeen) tree.setValue(next)
94
+ valueSeen = true
95
+ })
96
+ let depthSeen = false
97
+ effect(() => {
98
+ const next = asDepth(liveDepth())
99
+ if (depthSeen) tree.setDepth(next)
100
+ depthSeen = true
101
+ })
102
+ effect(() => tree.setOpenable(asFunction(liveOpenable())))
103
+ effect(() => tree.setContextMenu(asBoolean(liveContextMenu())))
104
+ })
105
+
106
+ onDestroy(() => {
107
+ if (tree) tree.destroy()
108
+ tree = null
109
+ })
110
+
111
+ defineExpose({
112
+ expandAll: (id) => { if (tree) tree.expandAll(id) },
113
+ collapseAll: () => { if (tree) tree.collapseAll() },
114
+ expand: id => (tree ? tree.expand(id) : false),
115
+ collapse: id => (tree ? tree.collapse(id) : false),
116
+ reveal: id => (tree ? tree.reveal(id) : false),
117
+ appendChildren: (id, nodes, more) => (tree ? tree.appendChildren(id, nodes, more) : false),
118
+ setValue: (node) => { if (tree) tree.setValue(node) },
119
+ setTinker: (result) => { if (tree) tree.setValue(result ? fromTinkerValue(result) : null) },
120
+ getSelected: () => (tree ? tree.getSelected() : null),
121
+ select: id => (tree ? tree.select(id) : false),
122
+ focus: () => { if (tree) tree.focus() },
123
+ tree: () => tree,
124
+ })
125
+ </script>
126
+
127
+ <div
128
+ ref="json-tree"
129
+ id="{{ treeId }}"
130
+ data-stx-json-tree
131
+ class="stx-jt {{ className }}"
132
+ style="height: {{ height }}"
133
+ >
134
+ {{-- The tree is the thing with focus. Its rows are drawn by the view, and
135
+ the two wrappers between them carry no meaning of their own. --}}
136
+ <div data-jt="scroller" role="tree" tabindex="0" aria-label="{{ label }}" class="stx-jt-scroller">
137
+ <div data-jt="content" role="none" class="stx-jt-content">
138
+ <div data-jt="rows" role="none" class="stx-jt-rows"></div>
139
+ </div>
140
+ </div>
141
+ <div data-jt="status" role="status" aria-live="polite" class="stx-jt-status"></div>
142
+ </div>
143
+
144
+ <style>
145
+ /*
146
+ * Everything the tree is drawn with is a custom property. The surface colours
147
+ * fall back to the library's role tokens and the value colours to the syntax
148
+ * tokens <CodeBlock> and <CodeEditor> use, so a value reads like the code that
149
+ * produced it. Each has a literal behind it, because the stylesheet that
150
+ * defines the syntax tokens ships with those components and may not be here.
151
+ */
152
+ :where([data-stx-json-tree]) {
153
+ --stx-tree-font: ui-monospace, SFMono-Regular, 'SF Mono', Menlo, Consolas, 'Liberation Mono', monospace;
154
+ --stx-tree-font-size: 12.5px;
155
+ --stx-tree-row-height: 22px;
156
+ --stx-tree-indent: 14px;
157
+ --stx-tree-bg: var(--stx-panel, #ffffff);
158
+ --stx-tree-fg: var(--stx-fg, #171717);
159
+ --stx-tree-muted: var(--stx-fg-soft, #737373);
160
+ --stx-tree-hover: color-mix(in srgb, var(--stx-fg, #171717) 6%, transparent);
161
+ --stx-tree-active: color-mix(in srgb, var(--stx-accent, #2563eb) 16%, transparent);
162
+ --stx-tree-focus: var(--stx-accent, #2563eb);
163
+ --stx-tree-line: var(--stx-line, #e5e5e5);
164
+ --stx-tree-key: var(--stx-syntax-attribute, #0550ae);
165
+ --stx-tree-string: var(--stx-syntax-string, #0a3069);
166
+ --stx-tree-number: var(--stx-syntax-numeric, #0550ae);
167
+ --stx-tree-keyword: var(--stx-syntax-keyword, #cf222e);
168
+ --stx-tree-type: var(--stx-syntax-type, #953800);
169
+ --stx-tree-function: var(--stx-syntax-function, #8250df);
170
+ --stx-tree-comment: var(--stx-syntax-comment, #6e7781);
171
+ --stx-tree-error: var(--stx-danger, #dc2626);
172
+ position: relative;
173
+ display: flex;
174
+ flex-direction: column;
175
+ min-width: 0;
176
+ background: var(--stx-tree-bg);
177
+ color: var(--stx-tree-fg);
178
+ }
179
+ /* Dark follows the `.dark` class, as the role tokens do: a surface from one
180
+ and text from the other would not be readable. */
181
+ :where(.dark) :where([data-stx-json-tree]) {
182
+ --stx-tree-bg: var(--stx-panel, #262626);
183
+ --stx-tree-fg: var(--stx-fg, #f5f5f5);
184
+ --stx-tree-muted: var(--stx-fg-soft, #a3a3a3);
185
+ --stx-tree-hover: color-mix(in srgb, var(--stx-fg, #f5f5f5) 8%, transparent);
186
+ --stx-tree-line: var(--stx-line, #404040);
187
+ --stx-tree-key: var(--stx-syntax-attribute, #79c0ff);
188
+ --stx-tree-string: var(--stx-syntax-string, #a5d6ff);
189
+ --stx-tree-number: var(--stx-syntax-numeric, #79c0ff);
190
+ --stx-tree-keyword: var(--stx-syntax-keyword, #ff7b72);
191
+ --stx-tree-type: var(--stx-syntax-type, #ffa657);
192
+ --stx-tree-function: var(--stx-syntax-function, #d2a8ff);
193
+ --stx-tree-comment: var(--stx-syntax-comment, #8b949e);
194
+ --stx-tree-error: var(--stx-danger, #f87171);
195
+ }
196
+
197
+ [data-stx-json-tree] .stx-jt-scroller {
198
+ position: relative;
199
+ flex: 1 1 auto;
200
+ min-height: 0;
201
+ overflow: auto;
202
+ outline: none;
203
+ font-family: var(--stx-tree-font);
204
+ font-size: var(--stx-tree-font-size);
205
+ line-height: var(--stx-tree-row-height);
206
+ font-variant-ligatures: none;
207
+ }
208
+ [data-stx-json-tree] .stx-jt-scroller:focus-visible {
209
+ box-shadow: inset 0 0 0 2px color-mix(in srgb, var(--stx-tree-focus) 55%, transparent);
210
+ }
211
+ [data-stx-json-tree] .stx-jt-content { position: relative; min-width: 100%; }
212
+ [data-stx-json-tree] .stx-jt-rows { position: absolute; top: 0; left: 0; right: 0; }
213
+
214
+ /* Every row is the same height and is placed by a transform, so which rows
215
+ are on screen is arithmetic and moving one costs no layout. */
216
+ [data-stx-json-tree] .stx-jt-row {
217
+ position: absolute;
218
+ top: 0;
219
+ left: 0;
220
+ right: 0;
221
+ box-sizing: border-box;
222
+ display: flex;
223
+ align-items: center;
224
+ height: var(--stx-tree-row-height);
225
+ padding-left: calc(6px + var(--stx-tree-indent) * var(--stx-tree-depth, 0));
226
+ padding-right: 6px;
227
+ white-space: nowrap;
228
+ cursor: default;
229
+ contain: layout style;
230
+ }
231
+ [data-stx-json-tree] .stx-jt-row:hover { background: var(--stx-tree-hover); }
232
+ [data-stx-json-tree] .stx-jt-row[data-active] { background: var(--stx-tree-active); }
233
+ [data-stx-json-tree] .stx-jt-scroller:focus-visible .stx-jt-row[data-active] {
234
+ box-shadow: inset 2px 0 0 var(--stx-tree-focus);
235
+ }
236
+
237
+ [data-stx-json-tree] .stx-jt-twisty {
238
+ flex: 0 0 var(--stx-tree-indent);
239
+ color: var(--stx-tree-muted);
240
+ text-align: center;
241
+ user-select: none;
242
+ -webkit-user-select: none;
243
+ }
244
+ [data-stx-json-tree] .stx-jt-label { flex: 0 0 auto; color: var(--stx-tree-key); }
245
+ [data-stx-json-tree] .stx-jt-separator { flex: 0 0 auto; color: var(--stx-tree-muted); white-space: pre; }
246
+ [data-stx-json-tree] .stx-jt-preview {
247
+ flex: 1 1 auto;
248
+ min-width: 0;
249
+ overflow: hidden;
250
+ text-overflow: ellipsis;
251
+ color: var(--stx-tree-muted);
252
+ }
253
+
254
+ [data-stx-json-tree] .stx-jt-preview[data-kind="string"],
255
+ [data-stx-json-tree] .stx-jt-preview[data-kind="regexp"],
256
+ [data-stx-json-tree] .stx-jt-preview[data-kind="url"],
257
+ [data-stx-json-tree] .stx-jt-preview[data-kind="date"] { color: var(--stx-tree-string); }
258
+ [data-stx-json-tree] .stx-jt-preview[data-kind="number"],
259
+ [data-stx-json-tree] .stx-jt-preview[data-kind="bigint"],
260
+ [data-stx-json-tree] .stx-jt-preview[data-kind="bytes"] { color: var(--stx-tree-number); }
261
+ [data-stx-json-tree] .stx-jt-preview[data-kind="boolean"],
262
+ [data-stx-json-tree] .stx-jt-preview[data-kind="null"],
263
+ [data-stx-json-tree] .stx-jt-preview[data-kind="undefined"] { color: var(--stx-tree-keyword); }
264
+ [data-stx-json-tree] .stx-jt-preview[data-kind="function"],
265
+ [data-stx-json-tree] .stx-jt-preview[data-kind="symbol"],
266
+ [data-stx-json-tree] .stx-jt-preview[data-kind="promise"] { color: var(--stx-tree-function); }
267
+ [data-stx-json-tree] .stx-jt-preview[data-kind="model"],
268
+ [data-stx-json-tree] .stx-jt-preview[data-kind="html"],
269
+ [data-stx-json-tree] .stx-jt-preview[data-kind="relation"] { color: var(--stx-tree-type); }
270
+ [data-stx-json-tree] .stx-jt-preview[data-kind="error"] { color: var(--stx-tree-error); }
271
+ [data-stx-json-tree] .stx-jt-preview[data-kind="ref"],
272
+ [data-stx-json-tree] .stx-jt-preview[data-kind="cut"],
273
+ [data-stx-json-tree] .stx-jt-preview[data-kind="more"],
274
+ [data-stx-json-tree] .stx-jt-preview[data-kind="hidden"] { color: var(--stx-tree-comment); font-style: italic; }
275
+ [data-stx-json-tree] .stx-jt-row[data-kind="ref"],
276
+ [data-stx-json-tree] .stx-jt-row[data-kind="more"] { cursor: pointer; }
277
+
278
+ /* The row's buttons: there for a pointer over the row, and for the row the
279
+ cursor is on. The keyboard reaches the same things through the tree's keys. */
280
+ [data-stx-json-tree] .stx-jt-actions {
281
+ flex: 0 0 auto;
282
+ display: none;
283
+ gap: 4px;
284
+ margin-left: 8px;
285
+ }
286
+ [data-stx-json-tree] .stx-jt-row:hover .stx-jt-actions,
287
+ [data-stx-json-tree] .stx-jt-row[data-active] .stx-jt-actions { display: inline-flex; }
288
+ [data-stx-json-tree] .stx-jt-action {
289
+ appearance: none;
290
+ box-sizing: border-box;
291
+ height: calc(var(--stx-tree-row-height) - 4px);
292
+ margin: 0;
293
+ padding: 0 6px;
294
+ border: 1px solid var(--stx-tree-line);
295
+ border-radius: 4px;
296
+ background: var(--stx-tree-bg);
297
+ color: var(--stx-tree-muted);
298
+ font: inherit;
299
+ font-size: 11px;
300
+ line-height: 1;
301
+ cursor: pointer;
302
+ }
303
+ [data-stx-json-tree] .stx-jt-action:hover { color: var(--stx-tree-fg); border-color: var(--stx-tree-focus); }
304
+ [data-stx-json-tree] .stx-jt-action[data-jt-action="open"] { color: var(--stx-tree-focus); }
305
+
306
+ [data-stx-json-tree] .stx-jt-status {
307
+ position: absolute;
308
+ width: 1px;
309
+ height: 1px;
310
+ margin: -1px;
311
+ padding: 0;
312
+ overflow: hidden;
313
+ clip: rect(0, 0, 0, 0);
314
+ white-space: nowrap;
315
+ border: 0;
316
+ }
317
+ </style>
@@ -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>