@stacksjs/components 0.2.855 → 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 (100) hide show
  1. package/dist/ContextMenu-43g3w5tp.stx +188 -0
  2. package/dist/DataGrid-7rt7v865.stx +376 -0
  3. package/dist/GraphView-0t3t9mec.stx +320 -0
  4. package/dist/JsonTree-satc4010.stx +317 -0
  5. package/dist/PreviewFrame-erbgvzj8.stx +257 -0
  6. package/dist/SplitPane-7sqfdwqa.stx +159 -0
  7. package/dist/component-api/component-api.js +36 -0
  8. package/dist/context-menu/context-menu.js +575 -0
  9. package/dist/data-grid/data-grid.js +1244 -0
  10. package/dist/graph-view/graph-view.js +1408 -0
  11. package/dist/index.d.ts +8 -0
  12. package/dist/index.js +22 -20
  13. package/dist/json-tree/json-tree.js +1121 -0
  14. package/dist/preview-frame/preview-frame.js +356 -0
  15. package/dist/split-pane/split-pane.js +364 -0
  16. package/dist/ui/context-menu/ContextMenu.d.ts +14 -0
  17. package/dist/ui/context-menu/context-menu.d.ts +8 -0
  18. package/dist/ui/context-menu/core.d.ts +74 -0
  19. package/dist/ui/context-menu/index.d.ts +11 -0
  20. package/dist/ui/context-menu/view.d.ts +36 -0
  21. package/dist/ui/data-grid/DataGrid.d.ts +30 -0
  22. package/dist/ui/data-grid/core.d.ts +141 -0
  23. package/dist/ui/data-grid/data-grid.d.ts +9 -0
  24. package/dist/ui/data-grid/index.d.ts +20 -0
  25. package/dist/ui/data-grid/view.d.ts +56 -0
  26. package/dist/ui/graph-view/GraphView.d.ts +21 -0
  27. package/dist/ui/graph-view/core/edges.d.ts +34 -0
  28. package/dist/ui/graph-view/core/force.d.ts +10 -0
  29. package/dist/ui/graph-view/core/graph.d.ts +83 -0
  30. package/dist/ui/graph-view/core/index.d.ts +5 -0
  31. package/dist/ui/graph-view/core/layered.d.ts +24 -0
  32. package/dist/ui/graph-view/core/viewport.d.ts +34 -0
  33. package/dist/ui/graph-view/graph-view.d.ts +9 -0
  34. package/dist/ui/graph-view/index.d.ts +21 -0
  35. package/dist/ui/graph-view/view.d.ts +51 -0
  36. package/dist/ui/json-tree/JsonTree.d.ts +18 -0
  37. package/dist/ui/json-tree/core.d.ts +108 -0
  38. package/dist/ui/json-tree/index.d.ts +18 -0
  39. package/dist/ui/json-tree/json-tree.d.ts +10 -0
  40. package/dist/ui/json-tree/tinker.d.ts +84 -0
  41. package/dist/ui/json-tree/view.d.ts +59 -0
  42. package/dist/ui/preview-frame/PreviewFrame.d.ts +44 -0
  43. package/dist/ui/preview-frame/core.d.ts +81 -0
  44. package/dist/ui/preview-frame/index.d.ts +19 -0
  45. package/dist/ui/preview-frame/preview-frame.d.ts +8 -0
  46. package/dist/ui/preview-frame/view.d.ts +37 -0
  47. package/dist/ui/split-pane/SplitPane.d.ts +18 -0
  48. package/dist/ui/split-pane/core.d.ts +58 -0
  49. package/dist/ui/split-pane/index.d.ts +15 -0
  50. package/dist/ui/split-pane/split-pane.d.ts +8 -0
  51. package/dist/ui/split-pane/view.d.ts +27 -0
  52. package/dist/ui/virtual-table/index.d.ts +6 -0
  53. package/dist/utils/component-api.d.ts +4 -0
  54. package/package.json +38 -3
  55. package/src/index.ts +8 -0
  56. package/src/ui/code-editor/README.md +2 -1
  57. package/src/ui/context-menu/ContextMenu.stx +188 -0
  58. package/src/ui/context-menu/README.md +117 -0
  59. package/src/ui/context-menu/context-menu.ts +8 -0
  60. package/src/ui/context-menu/core.ts +250 -0
  61. package/src/ui/context-menu/index.ts +16 -0
  62. package/src/ui/context-menu/view.ts +540 -0
  63. package/src/ui/data-grid/DataGrid.stx +376 -0
  64. package/src/ui/data-grid/README.md +204 -0
  65. package/src/ui/data-grid/core.ts +813 -0
  66. package/src/ui/data-grid/data-grid.ts +9 -0
  67. package/src/ui/data-grid/index.ts +34 -0
  68. package/src/ui/data-grid/view.ts +806 -0
  69. package/src/ui/graph-view/GraphView.stx +320 -0
  70. package/src/ui/graph-view/README.md +210 -0
  71. package/src/ui/graph-view/core/edges.ts +180 -0
  72. package/src/ui/graph-view/core/force.ts +237 -0
  73. package/src/ui/graph-view/core/graph.ts +277 -0
  74. package/src/ui/graph-view/core/index.ts +5 -0
  75. package/src/ui/graph-view/core/layered.ts +256 -0
  76. package/src/ui/graph-view/core/viewport.ts +141 -0
  77. package/src/ui/graph-view/graph-view.ts +9 -0
  78. package/src/ui/graph-view/index.ts +35 -0
  79. package/src/ui/graph-view/view.ts +809 -0
  80. package/src/ui/json-tree/JsonTree.stx +317 -0
  81. package/src/ui/json-tree/README.md +193 -0
  82. package/src/ui/json-tree/core.ts +588 -0
  83. package/src/ui/json-tree/index.ts +28 -0
  84. package/src/ui/json-tree/json-tree.ts +10 -0
  85. package/src/ui/json-tree/tinker.ts +349 -0
  86. package/src/ui/json-tree/view.ts +665 -0
  87. package/src/ui/preview-frame/PreviewFrame.stx +257 -0
  88. package/src/ui/preview-frame/README.md +107 -0
  89. package/src/ui/preview-frame/core.ts +195 -0
  90. package/src/ui/preview-frame/index.ts +31 -0
  91. package/src/ui/preview-frame/preview-frame.ts +8 -0
  92. package/src/ui/preview-frame/view.ts +336 -0
  93. package/src/ui/split-pane/README.md +87 -0
  94. package/src/ui/split-pane/SplitPane.stx +159 -0
  95. package/src/ui/split-pane/core.ts +200 -0
  96. package/src/ui/split-pane/index.ts +24 -0
  97. package/src/ui/split-pane/split-pane.ts +8 -0
  98. package/src/ui/split-pane/view.ts +305 -0
  99. package/src/ui/virtual-table/index.ts +6 -0
  100. package/src/utils/component-api.ts +72 -0
@@ -0,0 +1,376 @@
1
+ <script server>
2
+ /*
3
+ * Rows written into the page as a plain prop. A bound one (`:rows="rows()"`)
4
+ * is read live by the client script; this is what it starts from.
5
+ */
6
+ export const rows = Array.isArray($props.rows) ? $props.rows : []
7
+ export const columns = Array.isArray($props.columns) ? $props.columns : []
8
+ export const rowHeight = $num($props.rowHeight) || 28
9
+ export const headerHeight = $num($props.headerHeight) || 32
10
+ /* A CSS length. `100%` fills a sized parent, which is what a pane wants. */
11
+ export const height = $props.height || '24rem'
12
+ export const filterable = $bool($props.filterable, true)
13
+ export const exportable = $bool($props.exportable, true)
14
+ export const selectable = $bool($props.selectable, true)
15
+ export const contextMenu = $bool($props.contextMenu)
16
+ export const filter = typeof $props.filter === 'string' ? $props.filter : ''
17
+ export const label = $props.label || 'Data'
18
+ export const fileName = $props.fileName || 'data'
19
+ export const className = $props.className || ''
20
+ /*
21
+ * Ids unique to this instance: the grid, its filter and its status line all
22
+ * have to be findable when two grids share a page.
23
+ */
24
+ export const gridId = $props.id || `data-grid-${$uid}`
25
+ export const filterId = `${gridId}-filter`
26
+ export const statusId = `${gridId}-status`
27
+ export const hasToolbar = filterable || exportable
28
+ </script>
29
+
30
+ <script client>
31
+ /*
32
+ * <DataGrid>: rows and columns, sorted, filtered, selected and exported.
33
+ *
34
+ * This script is the wiring: props in, events out. What a sort or a filter
35
+ * does is in ./core, and what is drawn is in ./view.
36
+ */
37
+ import { DataGridView } from './view'
38
+
39
+ const emit = defineEmits(['sort', 'filter', 'select', 'cell', 'activate', 'copy', 'export', 'context'])
40
+
41
+ function asList(value) {
42
+ if (Array.isArray(value)) return value
43
+ if (typeof value === 'string' && value.charAt(0) === '[') {
44
+ try { return JSON.parse(value) }
45
+ catch (e) { return [] }
46
+ }
47
+ return []
48
+ }
49
+ function asText(value) { return value == null ? '' : String(value) }
50
+
51
+ const liveRows = useReactiveProp('rows', {{ rows }}, { parse: asList })
52
+ const liveColumns = useReactiveProp('columns', {{ columns }}, { parse: asList })
53
+ const liveFilter = useReactiveProp('filter', {{ filter }}, { parse: asText })
54
+ const liveFileName = useReactiveProp('fileName', {{ fileName }}, { parse: asText })
55
+
56
+ const rootRef = useRef('data-grid')
57
+ let grid = null
58
+
59
+ onMount(() => {
60
+ const root = rootRef()
61
+ if (!root) return
62
+
63
+ const firstColumns = asList(liveColumns())
64
+ grid = new DataGridView(root, {
65
+ rows: asList(liveRows()),
66
+ columns: firstColumns.length ? firstColumns : null,
67
+ rowHeight: {{ rowHeight }},
68
+ headerHeight: {{ headerHeight }},
69
+ selectable: {{ selectable }},
70
+ contextMenu: {{ contextMenu }},
71
+ filter: asText(liveFilter()),
72
+ fileName: asText(liveFileName()),
73
+ }, {
74
+ onSort(sort) { emit('sort', sort) },
75
+ onFilter(detail) { emit('filter', detail) },
76
+ onSelect(detail) { emit('select', detail) },
77
+ onCell(detail) { emit('cell', detail) },
78
+ onActivate(detail) { emit('activate', detail) },
79
+ onCopy(detail) { emit('copy', detail) },
80
+ onExport(detail) { emit('export', detail) },
81
+ onContext(detail) { emit('context', detail) },
82
+ })
83
+
84
+ // The first run of each finds what the view was just built with. Only a
85
+ // later change is news, and rows handed over with setRows() must not be
86
+ // replaced by a prop that has not changed.
87
+ let rowsSeen = false
88
+ effect(() => {
89
+ const next = asList(liveRows())
90
+ if (rowsSeen) grid.setRows(next)
91
+ rowsSeen = true
92
+ })
93
+ let columnsSeen = false
94
+ effect(() => {
95
+ const next = asList(liveColumns())
96
+ if (columnsSeen) grid.setColumns(next.length ? next : null)
97
+ columnsSeen = true
98
+ })
99
+ let filterSeen = false
100
+ effect(() => {
101
+ const next = asText(liveFilter())
102
+ if (filterSeen) grid.setFilter(next)
103
+ filterSeen = true
104
+ })
105
+ effect(() => grid.setFileName(asText(liveFileName())))
106
+ })
107
+
108
+ onDestroy(() => {
109
+ if (grid) grid.destroy()
110
+ grid = null
111
+ })
112
+
113
+ defineExpose({
114
+ setRows: (next) => { if (grid) grid.setRows(asList(next)) },
115
+ setColumns: (next) => { if (grid) grid.setColumns(Array.isArray(next) && next.length ? next : null) },
116
+ getView: () => (grid ? grid.getView() : []),
117
+ getSelection: () => (grid ? grid.getSelection() : { rows: [], indices: [] }),
118
+ clearSelection: () => { if (grid) grid.clearSelection() },
119
+ setFilter: (text) => { if (grid) grid.setFilter(asText(text)) },
120
+ setSort: (sort) => { if (grid) grid.setSort(asList(sort)) },
121
+ exportCsv: () => (grid ? grid.exportCsv() : ''),
122
+ exportJson: () => (grid ? grid.exportJson() : '[]'),
123
+ download: format => (grid ? grid.download(format === 'json' ? 'json' : 'csv') : ''),
124
+ scrollToRow: (index) => { if (grid) grid.scrollToRow(index) },
125
+ focus: () => { if (grid) grid.focus() },
126
+ grid: () => grid,
127
+ })
128
+ </script>
129
+
130
+ <div
131
+ ref="data-grid"
132
+ data-stx-data-grid
133
+ class="stx-dg {{ className }}"
134
+ style="height: {{ height }}"
135
+ @if(selectable)data-selectable@endif
136
+ >
137
+ @if(hasToolbar)
138
+ <div data-dg="toolbar" class="stx-dg-toolbar">
139
+ @if(filterable)
140
+ <label for="{{ filterId }}" class="sr-only">Filter {{ label }}</label>
141
+ <input
142
+ data-dg="filter"
143
+ id="{{ filterId }}"
144
+ type="search"
145
+ class="stx-dg-filter"
146
+ placeholder="Filter rows"
147
+ autocomplete="off"
148
+ spellcheck="false"
149
+ aria-controls="{{ gridId }}"
150
+ value="{{ filter }}"
151
+ >
152
+ @endif
153
+ <span data-dg="status" id="{{ statusId }}" role="status" aria-live="polite" class="stx-dg-status"></span>
154
+ @if(exportable)
155
+ <button type="button" data-dg="export-csv" class="stx-dg-button" aria-label="Export {{ label }} as CSV">CSV</button>
156
+ <button type="button" data-dg="export-json" class="stx-dg-button" aria-label="Export {{ label }} as JSON">JSON</button>
157
+ @endif
158
+ </div>
159
+ @else
160
+ <span data-dg="status" id="{{ statusId }}" role="status" aria-live="polite" class="sr-only"></span>
161
+ @endif
162
+
163
+ {{-- The one scroll container, and the grid itself: focus stays here and the
164
+ active cell is named, since the cells under it come and go. --}}
165
+ <div
166
+ data-dg="scroller"
167
+ id="{{ gridId }}"
168
+ role="grid"
169
+ tabindex="0"
170
+ aria-label="{{ label }}"
171
+ aria-describedby="{{ statusId }}"
172
+ @if(selectable)aria-multiselectable="true"@endif
173
+ class="stx-dg-scroller"
174
+ >
175
+ <div data-dg="content" role="presentation" class="stx-dg-content">
176
+ <div data-dg="header" role="row" aria-rowindex="1" class="stx-dg-header"></div>
177
+ <div data-dg="body" role="rowgroup" class="stx-dg-body"></div>
178
+ </div>
179
+ </div>
180
+ </div>
181
+
182
+ <style>
183
+ /*
184
+ * Everything the grid is drawn with is a custom property, each falling back to
185
+ * one of the library's role tokens, so an app that has themed its roles has a
186
+ * matching grid, in light and in dark.
187
+ *
188
+ * The row and header heights are set by the view, in pixels: the arithmetic
189
+ * that decides which rows exist depends on every row being exactly that tall.
190
+ */
191
+ :where([data-stx-data-grid]) {
192
+ --stx-grid-font: inherit;
193
+ --stx-grid-mono: ui-monospace, SFMono-Regular, 'SF Mono', Menlo, Consolas, 'Liberation Mono', monospace;
194
+ --stx-grid-font-size: 13px;
195
+ --stx-grid-row-height: 28px;
196
+ --stx-grid-header-height: 32px;
197
+ --stx-grid-cell-padding: 12px;
198
+ --stx-grid-bg: var(--stx-panel, #ffffff);
199
+ --stx-grid-fg: var(--stx-fg, #171717);
200
+ --stx-grid-header-bg: var(--stx-surface, #fafafa);
201
+ --stx-grid-header-fg: var(--stx-fg-muted, #525252);
202
+ --stx-grid-line: var(--stx-line, #e5e5e5);
203
+ --stx-grid-empty-fg: var(--stx-fg-subtle, #a3a3a3);
204
+ --stx-grid-hover: color-mix(in srgb, var(--stx-fg, #171717) 4%, transparent);
205
+ --stx-grid-selected: color-mix(in srgb, var(--stx-accent, #2563eb) 14%, transparent);
206
+ --stx-grid-accent: var(--stx-accent, #2563eb);
207
+ --stx-grid-number: var(--stx-grid-fg);
208
+ --stx-grid-boolean: var(--stx-info, #0891b2);
209
+ display: flex;
210
+ flex-direction: column;
211
+ min-width: 0;
212
+ min-height: 0;
213
+ overflow: hidden;
214
+ border: 1px solid var(--stx-grid-line);
215
+ border-radius: var(--stx-radius-panel, 8px);
216
+ background: var(--stx-grid-bg);
217
+ color: var(--stx-grid-fg);
218
+ font-family: var(--stx-grid-font);
219
+ font-size: var(--stx-grid-font-size);
220
+ }
221
+
222
+ [data-stx-data-grid] .stx-dg-toolbar {
223
+ display: flex;
224
+ flex: 0 0 auto;
225
+ align-items: center;
226
+ gap: 8px;
227
+ padding: 6px 8px;
228
+ border-bottom: 1px solid var(--stx-grid-line);
229
+ background: var(--stx-grid-header-bg);
230
+ }
231
+ [data-stx-data-grid] .stx-dg-filter {
232
+ flex: 0 1 16rem;
233
+ min-width: 6rem;
234
+ height: 26px;
235
+ padding: 0 8px;
236
+ border: 1px solid var(--stx-grid-line);
237
+ border-radius: var(--stx-radius-control, 6px);
238
+ background: var(--stx-field, var(--stx-grid-bg));
239
+ color: inherit;
240
+ font: inherit;
241
+ }
242
+ [data-stx-data-grid] .stx-dg-filter:focus-visible,
243
+ [data-stx-data-grid] .stx-dg-button:focus-visible {
244
+ outline: 2px solid var(--stx-grid-accent);
245
+ outline-offset: 1px;
246
+ }
247
+ [data-stx-data-grid] .stx-dg-status {
248
+ flex: 1 1 auto;
249
+ min-width: 0;
250
+ overflow: hidden;
251
+ color: var(--stx-grid-header-fg);
252
+ font-variant-numeric: tabular-nums;
253
+ text-overflow: ellipsis;
254
+ white-space: nowrap;
255
+ }
256
+ [data-stx-data-grid] .stx-dg-button {
257
+ flex: 0 0 auto;
258
+ height: 26px;
259
+ padding: 0 10px;
260
+ border: 1px solid var(--stx-grid-line);
261
+ border-radius: var(--stx-radius-control, 6px);
262
+ background: var(--stx-grid-bg);
263
+ color: inherit;
264
+ font: inherit;
265
+ font-size: 12px;
266
+ cursor: pointer;
267
+ }
268
+ [data-stx-data-grid] .stx-dg-button:hover { background: var(--stx-grid-hover); }
269
+
270
+ [data-stx-data-grid] .stx-dg-scroller {
271
+ position: relative;
272
+ flex: 1 1 auto;
273
+ min-height: 0;
274
+ overflow: auto;
275
+ outline: none;
276
+ /* The scroller is its own layer, so a scroll repaints nothing outside it. */
277
+ contain: strict;
278
+ }
279
+ [data-stx-data-grid] .stx-dg-scroller:focus-visible { box-shadow: inset 0 0 0 2px color-mix(in srgb, var(--stx-grid-accent) 55%, transparent); }
280
+ [data-stx-data-grid] .stx-dg-content { position: relative; min-width: 100%; }
281
+
282
+ [data-stx-data-grid] .stx-dg-header {
283
+ position: sticky;
284
+ top: 0;
285
+ z-index: 2;
286
+ min-width: 100%;
287
+ height: var(--stx-grid-header-height);
288
+ border-bottom: 1px solid var(--stx-grid-line);
289
+ background: var(--stx-grid-header-bg);
290
+ color: var(--stx-grid-header-fg);
291
+ box-sizing: border-box;
292
+ }
293
+ [data-stx-data-grid] .stx-dg-head {
294
+ position: absolute;
295
+ top: 0;
296
+ display: flex;
297
+ align-items: center;
298
+ gap: 4px;
299
+ height: 100%;
300
+ padding: 0 var(--stx-grid-cell-padding);
301
+ box-sizing: border-box;
302
+ border-right: 1px solid var(--stx-grid-line);
303
+ font-weight: 600;
304
+ white-space: nowrap;
305
+ user-select: none;
306
+ -webkit-user-select: none;
307
+ }
308
+ [data-stx-data-grid] .stx-dg-head[data-sortable] { cursor: pointer; }
309
+ [data-stx-data-grid] .stx-dg-head[data-align="right"] { flex-direction: row-reverse; }
310
+ [data-stx-data-grid] .stx-dg-head[data-align="center"] { justify-content: center; }
311
+ [data-stx-data-grid] .stx-dg-head-label { min-width: 0; overflow: hidden; text-overflow: ellipsis; }
312
+ [data-stx-data-grid] .stx-dg-sort { flex: 0 0 auto; color: var(--stx-grid-accent); font-size: 9px; font-variant-numeric: tabular-nums; }
313
+ [data-stx-data-grid] .stx-dg-resize {
314
+ position: absolute;
315
+ top: 0;
316
+ right: -4px;
317
+ z-index: 1;
318
+ width: 9px;
319
+ height: 100%;
320
+ cursor: col-resize;
321
+ touch-action: none;
322
+ }
323
+ [data-stx-data-grid] .stx-dg-resize:hover,
324
+ [data-stx-data-grid][data-resizing] .stx-dg-resize:hover { background: linear-gradient(to right, transparent 3px, var(--stx-grid-accent) 3px, var(--stx-grid-accent) 5px, transparent 5px); }
325
+ [data-stx-data-grid][data-resizing] { cursor: col-resize; user-select: none; -webkit-user-select: none; }
326
+
327
+ [data-stx-data-grid] .stx-dg-body { position: relative; min-width: 100%; }
328
+ [data-stx-data-grid] .stx-dg-row {
329
+ position: absolute;
330
+ top: 0;
331
+ left: 0;
332
+ right: 0;
333
+ height: var(--stx-grid-row-height);
334
+ border-bottom: 1px solid var(--stx-grid-line);
335
+ box-sizing: border-box;
336
+ contain: layout style;
337
+ }
338
+ [data-stx-data-grid] .stx-dg-row:hover { background: var(--stx-grid-hover); }
339
+ [data-stx-data-grid] .stx-dg-row[aria-selected="true"] { background: var(--stx-grid-selected); }
340
+
341
+ [data-stx-data-grid] .stx-dg-cell {
342
+ position: absolute;
343
+ top: 0;
344
+ height: 100%;
345
+ padding: 0 var(--stx-grid-cell-padding);
346
+ box-sizing: border-box;
347
+ overflow: hidden;
348
+ line-height: calc(var(--stx-grid-row-height) - 1px);
349
+ text-overflow: ellipsis;
350
+ white-space: pre;
351
+ cursor: default;
352
+ }
353
+ [data-stx-data-grid] .stx-dg-cell[data-align="right"] { text-align: right; }
354
+ [data-stx-data-grid] .stx-dg-cell[data-align="center"] { text-align: center; }
355
+ /* Figures in one column line up digit under digit. */
356
+ [data-stx-data-grid] .stx-dg-cell[data-type="number"] { color: var(--stx-grid-number); font-variant-numeric: tabular-nums; }
357
+ [data-stx-data-grid] .stx-dg-cell[data-type="date"] { font-variant-numeric: tabular-nums; }
358
+ [data-stx-data-grid] .stx-dg-cell[data-type="boolean"] { color: var(--stx-grid-boolean); }
359
+ [data-stx-data-grid] .stx-dg-cell[data-type="json"] { font-family: var(--stx-grid-mono); font-size: 12px; }
360
+ [data-stx-data-grid] .stx-dg-cell[data-empty] { color: var(--stx-grid-empty-fg); font-style: italic; }
361
+
362
+ /* The active cell is only marked while the grid has the keyboard. */
363
+ [data-stx-data-grid] .stx-dg-scroller:focus :is(.stx-dg-cell, .stx-dg-head)[data-active] {
364
+ outline: 2px solid var(--stx-grid-accent);
365
+ outline-offset: -2px;
366
+ }
367
+
368
+ [data-stx-data-grid][data-empty] .stx-dg-body::after {
369
+ content: 'No rows';
370
+ position: sticky;
371
+ left: 0;
372
+ display: block;
373
+ padding: 16px var(--stx-grid-cell-padding);
374
+ color: var(--stx-grid-empty-fg);
375
+ }
376
+ </style>
@@ -0,0 +1,204 @@
1
+ # DataGrid
2
+
3
+ A grid for rows of data: sortable and resizable columns, a text filter, row
4
+ selection, cell navigation from the keyboard, copy as JSON, and CSV and JSON
5
+ export of what is on show. Rows and columns are both virtualized, so a
6
+ hundred thousand rows cost what forty do. No third-party grid underneath.
7
+
8
+ ```html
9
+ <script client>
10
+ const users = state([
11
+ { id: 1, name: 'Ada', age: 36, joined: '2021-03-04', admin: true },
12
+ { id: 2, name: 'Grace', age: 45, joined: '2019-11-20', admin: false },
13
+ ])
14
+
15
+ function open(detail) {
16
+ // detail.row is the row object, detail.rowIndex its place in the view.
17
+ }
18
+ </script>
19
+
20
+ <DataGrid :rows="users()" label="Users" height="24rem" @activate="open($event)" />
21
+ ```
22
+
23
+ A component's event hands its payload to the handler as `$event`, so `open`
24
+ above receives the detail object itself.
25
+
26
+ Columns are worked out from the rows when none are given: every key of the
27
+ first 200 rows, in the order first seen, each with a type inferred from its
28
+ values. A row can be an object or an array.
29
+
30
+ ## How it works
31
+
32
+ One scroll container holds a header that sticks to its top and a body as tall
33
+ as every row together. Only the rows and columns that cross the viewport are
34
+ elements. A row that scrolls out is handed to the row that scrolls in, so
35
+ scrolling makes no elements and drops none.
36
+
37
+ The rows are never copied or reordered. The view is a `Uint32Array` of source
38
+ indices: the rows that pass the filter, in sorted order. Anything slow to work
39
+ out per row is worked out once and kept: the lower-case text a filter searches,
40
+ and one number per cell to sort by (text is ranked once with `Intl.Collator`,
41
+ so a sort never calls it per comparison).
42
+
43
+ Three layers, each usable without the ones above it:
44
+
45
+ | File | What it is |
46
+ |---|---|
47
+ | `core.ts` | The model: column and type inference, sort, filter, selection, column geometry, cell movement, CSV and JSON. No DOM and no stx. |
48
+ | `view.ts` | The DOM: which rows and columns exist, the recycling, the header, the keyboard, the pointer, the clipboard and the download. |
49
+ | `DataGrid.stx` | The template, the styles, and the props and events. |
50
+
51
+ `@stacksjs/components/data-grid` exports the first two for a host that mounts a
52
+ grid itself.
53
+
54
+ ## Props
55
+
56
+ | Prop | Default | |
57
+ |---|---|---|
58
+ | `rows` | `[]` | Objects or arrays. Bind it with `:rows`. For a very large list, use `setRows()` instead (see Limits). |
59
+ | `columns` | inferred | `{ key, label?, type?, width?, minWidth?, maxWidth?, align?, sortable?, accessor? }`. `type` is `string`, `number`, `date`, `boolean` or `json`. |
60
+ | `rowHeight` | `28` | Pixels. |
61
+ | `headerHeight` | `32` | Pixels. |
62
+ | `height` | `'24rem'` | Any CSS length. `100%` fills a sized parent. |
63
+ | `filterable` | `true` | Show the filter field. |
64
+ | `exportable` | `true` | Show the CSV and JSON buttons. |
65
+ | `selectable` | `true` | Rows can be selected. |
66
+ | `filter` | `''` | The filter text. |
67
+ | `label` | `'Data'` | The grid's accessible name. Also names the filter field and the export buttons. |
68
+ | `fileName` | `'data'` | The name of an exported file, without its extension. |
69
+ | `contextMenu` | `false` | Keep the browser's own menu away on a right click, for a host that shows one from `@context`. |
70
+ | `id`, `className` | | |
71
+
72
+ ### Column types
73
+
74
+ A type decides how a column sorts and how it is drawn. Left out, it comes from
75
+ the first 200 rows, ignoring empty cells:
76
+
77
+ | Type | When | Sorted | Drawn |
78
+ |---|---|---|---|
79
+ | `number` | Every value is a number or reads as one (`'12.5'`). One `n/a` makes the column text. | Numerically | Right-aligned, in tabular figures, digit for digit |
80
+ | `date` | Every value is a `Date` or an ISO date string | By time | As given; a `Date` as ISO |
81
+ | `boolean` | Every value is `true` or `false` | `false` first | `true`, `false` |
82
+ | `json` | Every value is an object or an array | By its text | Compact JSON, cut at 160 characters |
83
+ | `string` | Anything else | Without regard to case, digits as numbers (`Item 9` before `Item 10`) | As it is |
84
+
85
+ `null` and `undefined` are shown as such, dimmed, sort last in both
86
+ directions, and are never matched by a filter.
87
+
88
+ ## Events
89
+
90
+ | Event | Payload |
91
+ |---|---|
92
+ | `@sort` | `[{ key, direction }]`, in the order the columns are sorted by. Empty when unsorted. |
93
+ | `@filter` | `{ text, count }`. `count` is how many rows are left. |
94
+ | `@select` | `{ rows, indices }`. `indices` are positions in the rows you passed, not in the view. |
95
+ | `@cell` | `{ row, rowIndex, column, value }`. The active cell moved. `rowIndex` is -1 on the header. |
96
+ | `@activate` | `{ row, rowIndex }`. Enter on a cell, or a double click. |
97
+ | `@copy` | `{ what: 'cell' \| 'row', text }` |
98
+ | `@export` | `{ format: 'csv' \| 'json', text, rows }` |
99
+ | `@context` | `{ row, rowIndex, column, value, x, y }`. A right click. `x` and `y` are viewport coordinates, for placing a menu. |
100
+
101
+ ## Methods
102
+
103
+ From `componentApi(target)` (`@stacksjs/components/component-api`), where
104
+ `target` is the component's element, its id or a selector: `setRows(rows)`, `setColumns(columns)`,
105
+ `getView()` (the rows on show, in order), `getSelection()`, `clearSelection()`,
106
+ `setFilter(text)`, `setSort([{ key, direction }])`, `exportCsv()`,
107
+ `exportJson()` (both return the text without saving it), `download(format)`,
108
+ `scrollToRow(viewIndex)`, `focus()`, and `grid()` for the view itself.
109
+
110
+ ## Keys
111
+
112
+ The grid is one tab stop. Inside it:
113
+
114
+ | Key | |
115
+ |---|---|
116
+ | Arrow keys | Move the active cell. Up from the first row reaches the header. |
117
+ | Home, End | First and last column of the row. |
118
+ | Cmd/Ctrl+Home, Cmd/Ctrl+End | First and last cell of the grid. |
119
+ | Page Up, Page Down | A screen of rows. |
120
+ | Space | Select or deselect the row. |
121
+ | Shift+Up, Shift+Down | Carry the selection along. |
122
+ | Cmd/Ctrl+A | Select every row on show. |
123
+ | Escape | Clear the selection. |
124
+ | Enter | Activate the row. |
125
+ | Cmd/Ctrl+C | Copy the cell as JSON. |
126
+ | Shift+Cmd/Ctrl+C | Copy the row as JSON. |
127
+ | Enter or Space, on a header | Sort by that column. With Shift, add it to the sort. |
128
+ | Alt+Left, Alt+Right, on a header | Narrow or widen that column. |
129
+
130
+ With the pointer: click a header to sort (ascending, descending, none), Shift
131
+ and click to sort by a further column, drag a header's right edge to resize,
132
+ double click the edge to fit the column. Click a row to select it, Cmd/Ctrl and
133
+ click to add or remove one, Shift and click for a range.
134
+
135
+ ## Accessibility
136
+
137
+ The scroll container is the `grid`, with `aria-rowcount` and `aria-colcount`
138
+ for the whole of the data, not for what happens to be drawn. Rows carry
139
+ `aria-rowindex` and `aria-selected`, cells `aria-colindex`, headers
140
+ `aria-sort`. Focus stays on the grid and the active cell is named with
141
+ `aria-activedescendant`, because the element under a cell is recycled as the
142
+ grid scrolls. The row count, and the count after a filter, are announced from a
143
+ polite live region.
144
+
145
+ ## Theming
146
+
147
+ Every colour is a CSS custom property that falls back to one of the library's
148
+ role tokens, so an app that has themed its roles has a matching grid in light
149
+ and in dark.
150
+
151
+ - Surfaces and text: `--stx-grid-bg`, `--stx-grid-fg`, `--stx-grid-header-bg`,
152
+ `--stx-grid-header-fg`, `--stx-grid-line`, `--stx-grid-empty-fg`.
153
+ - State: `--stx-grid-hover`, `--stx-grid-selected`, `--stx-grid-accent` (the
154
+ active cell, the sort mark, the resize edge).
155
+ - By type: `--stx-grid-number`, `--stx-grid-boolean`.
156
+ - The type: `--stx-grid-font`, `--stx-grid-mono`, `--stx-grid-font-size`,
157
+ `--stx-grid-cell-padding`.
158
+
159
+ `--stx-grid-row-height` and `--stx-grid-header-height` are set by the grid from
160
+ its props. Change the props, not the variables: the arithmetic that decides
161
+ which rows exist depends on every row being exactly that tall.
162
+
163
+ ## Measured
164
+
165
+ 100,000 rows of 12 columns, headless WebKit on an Apple silicon Mac, timed
166
+ around the synchronous work with a forced layout inside the measurement
167
+ (`test/data-grid/data-grid-browser.test.ts` prints these on every run). The
168
+ clock in WebKit ticks in whole milliseconds.
169
+
170
+ | | |
171
+ |---|---|
172
+ | Elements in the grid | 236 (19 rows of 9 cells) |
173
+ | `setRows` | 6 ms |
174
+ | A vertical scroll step that replaces every row | 2 ms median, 3 ms p95 |
175
+ | A horizontal scroll step | under 1 ms median, 1 ms p95 |
176
+ | Sort by a number column | 24 ms |
177
+ | Sort by a text column of 100,000 distinct values | 170 ms the first time, 17 ms after |
178
+ | Sort by a text column of 10 distinct values | 16 ms |
179
+ | First filter (builds the search text) | 90 ms |
180
+ | A filter after that | 5 ms |
181
+ | A column resize step | 1 ms |
182
+ | CSV of all 100,000 rows | 110 ms, 14.5 MB |
183
+
184
+ ## Limits of this version
185
+
186
+ - **A bound `:rows` is also written to the DOM.** stx reflects a bound value
187
+ onto the component's element as an attribute, as JSON. The grid reads the
188
+ real array and never parses that text, but for 100,000 rows it is 24.5 MB of
189
+ attribute and about 15 ms to make, on every change. Hand a large list to
190
+ `setRows()` instead, which does neither.
191
+ - **About 600,000 rows at the default row height.** The body is one element as
192
+ tall as every row, and WebKit stops laying an element out at about 17 million
193
+ pixels. A taller list needs the scroll position scaled, which this version
194
+ does not do.
195
+ - **Rows are one height and cells one line.** Long text is cut with an
196
+ ellipsis. Copy the cell, or widen the column, to read it all.
197
+ - **A column is fitted by counting characters,** not by measuring text, so a
198
+ proportional font fits approximately.
199
+ - **Selection is by row.** There is no range of cells to select.
200
+ - **No pinned columns, no column reordering, no editing.**
201
+ - **Export saves through a link with a `download` attribute.** That works in a
202
+ browser. Whether a given native web view honours it is up to its host;
203
+ `exportCsv()` and `exportJson()` return the text for a host that saves the
204
+ file itself.