@stacksjs/components 0.2.855 → 0.2.858
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/ContextMenu-43g3w5tp.stx +188 -0
- package/dist/DataGrid-7rt7v865.stx +376 -0
- package/dist/GraphView-0t3t9mec.stx +320 -0
- package/dist/JsonTree-satc4010.stx +317 -0
- package/dist/PreviewFrame-erbgvzj8.stx +257 -0
- package/dist/SplitPane-7sqfdwqa.stx +159 -0
- package/dist/component-api/component-api.js +36 -0
- package/dist/context-menu/context-menu.js +575 -0
- package/dist/data-grid/data-grid.js +1244 -0
- package/dist/graph-view/graph-view.js +1408 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.js +22 -20
- package/dist/json-tree/json-tree.js +1121 -0
- package/dist/preview-frame/preview-frame.js +356 -0
- package/dist/split-pane/split-pane.js +364 -0
- package/dist/ui/context-menu/ContextMenu.d.ts +14 -0
- package/dist/ui/context-menu/context-menu.d.ts +8 -0
- package/dist/ui/context-menu/core.d.ts +74 -0
- package/dist/ui/context-menu/index.d.ts +11 -0
- package/dist/ui/context-menu/view.d.ts +36 -0
- package/dist/ui/data-grid/DataGrid.d.ts +30 -0
- package/dist/ui/data-grid/core.d.ts +141 -0
- package/dist/ui/data-grid/data-grid.d.ts +9 -0
- package/dist/ui/data-grid/index.d.ts +20 -0
- package/dist/ui/data-grid/view.d.ts +56 -0
- package/dist/ui/graph-view/GraphView.d.ts +21 -0
- package/dist/ui/graph-view/core/edges.d.ts +34 -0
- package/dist/ui/graph-view/core/force.d.ts +10 -0
- package/dist/ui/graph-view/core/graph.d.ts +83 -0
- package/dist/ui/graph-view/core/index.d.ts +5 -0
- package/dist/ui/graph-view/core/layered.d.ts +24 -0
- package/dist/ui/graph-view/core/viewport.d.ts +34 -0
- package/dist/ui/graph-view/graph-view.d.ts +9 -0
- package/dist/ui/graph-view/index.d.ts +21 -0
- package/dist/ui/graph-view/view.d.ts +51 -0
- package/dist/ui/json-tree/JsonTree.d.ts +18 -0
- package/dist/ui/json-tree/core.d.ts +108 -0
- package/dist/ui/json-tree/index.d.ts +18 -0
- package/dist/ui/json-tree/json-tree.d.ts +10 -0
- package/dist/ui/json-tree/tinker.d.ts +84 -0
- package/dist/ui/json-tree/view.d.ts +59 -0
- package/dist/ui/preview-frame/PreviewFrame.d.ts +44 -0
- package/dist/ui/preview-frame/core.d.ts +81 -0
- package/dist/ui/preview-frame/index.d.ts +19 -0
- package/dist/ui/preview-frame/preview-frame.d.ts +8 -0
- package/dist/ui/preview-frame/view.d.ts +37 -0
- package/dist/ui/split-pane/SplitPane.d.ts +18 -0
- package/dist/ui/split-pane/core.d.ts +58 -0
- package/dist/ui/split-pane/index.d.ts +15 -0
- package/dist/ui/split-pane/split-pane.d.ts +8 -0
- package/dist/ui/split-pane/view.d.ts +27 -0
- package/dist/ui/virtual-table/index.d.ts +6 -0
- package/dist/utils/component-api.d.ts +4 -0
- package/package.json +38 -3
- package/src/index.ts +8 -0
- package/src/ui/code-editor/README.md +2 -1
- package/src/ui/context-menu/ContextMenu.stx +188 -0
- package/src/ui/context-menu/README.md +117 -0
- package/src/ui/context-menu/context-menu.ts +8 -0
- package/src/ui/context-menu/core.ts +250 -0
- package/src/ui/context-menu/index.ts +16 -0
- package/src/ui/context-menu/view.ts +540 -0
- package/src/ui/data-grid/DataGrid.stx +376 -0
- package/src/ui/data-grid/README.md +204 -0
- package/src/ui/data-grid/core.ts +813 -0
- package/src/ui/data-grid/data-grid.ts +9 -0
- package/src/ui/data-grid/index.ts +34 -0
- package/src/ui/data-grid/view.ts +806 -0
- package/src/ui/graph-view/GraphView.stx +320 -0
- package/src/ui/graph-view/README.md +210 -0
- package/src/ui/graph-view/core/edges.ts +180 -0
- package/src/ui/graph-view/core/force.ts +237 -0
- package/src/ui/graph-view/core/graph.ts +277 -0
- package/src/ui/graph-view/core/index.ts +5 -0
- package/src/ui/graph-view/core/layered.ts +256 -0
- package/src/ui/graph-view/core/viewport.ts +141 -0
- package/src/ui/graph-view/graph-view.ts +9 -0
- package/src/ui/graph-view/index.ts +35 -0
- package/src/ui/graph-view/view.ts +809 -0
- package/src/ui/json-tree/JsonTree.stx +317 -0
- package/src/ui/json-tree/README.md +193 -0
- package/src/ui/json-tree/core.ts +588 -0
- package/src/ui/json-tree/index.ts +28 -0
- package/src/ui/json-tree/json-tree.ts +10 -0
- package/src/ui/json-tree/tinker.ts +349 -0
- package/src/ui/json-tree/view.ts +665 -0
- package/src/ui/preview-frame/PreviewFrame.stx +257 -0
- package/src/ui/preview-frame/README.md +107 -0
- package/src/ui/preview-frame/core.ts +195 -0
- package/src/ui/preview-frame/index.ts +31 -0
- package/src/ui/preview-frame/preview-frame.ts +8 -0
- package/src/ui/preview-frame/view.ts +336 -0
- package/src/ui/split-pane/README.md +87 -0
- package/src/ui/split-pane/SplitPane.stx +159 -0
- package/src/ui/split-pane/core.ts +200 -0
- package/src/ui/split-pane/index.ts +24 -0
- package/src/ui/split-pane/split-pane.ts +8 -0
- package/src/ui/split-pane/view.ts +305 -0
- package/src/ui/virtual-table/index.ts +6 -0
- package/src/utils/component-api.ts +72 -0
|
@@ -0,0 +1,336 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A preview in the DOM: one sandboxed frame, sized like a device and shrunk to
|
|
3
|
+
* fit the box it is shown in.
|
|
4
|
+
*
|
|
5
|
+
* The frame is made new for every load. A frame's sandbox is read when its
|
|
6
|
+
* document is created, so a frame that is reused keeps the permissions of the
|
|
7
|
+
* page before; a new one cannot. It also makes "reload" exactly what it says.
|
|
8
|
+
*/
|
|
9
|
+
import type { PreviewScheme, ResolvedViewport, ViewportName } from './core'
|
|
10
|
+
import { buildDocument, describeViewport, fitScale, normalizeScheme, resolveViewport, sandboxValue, stepViewport } from './core'
|
|
11
|
+
|
|
12
|
+
export interface PreviewFrameOptions {
|
|
13
|
+
html?: string
|
|
14
|
+
viewport?: ViewportName | string
|
|
15
|
+
/** A viewport of its own size, in CSS pixels. Both are needed. */
|
|
16
|
+
frameWidth?: number | null
|
|
17
|
+
frameHeight?: number | null
|
|
18
|
+
landscape?: boolean
|
|
19
|
+
scheme?: PreviewScheme
|
|
20
|
+
/** Let the page's scripts run, in an origin of their own. */
|
|
21
|
+
scripts?: boolean
|
|
22
|
+
forms?: boolean
|
|
23
|
+
/** The frame's accessible name, and the title of a wrapped fragment. */
|
|
24
|
+
title?: string
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export interface PreviewState {
|
|
28
|
+
viewport: ViewportName
|
|
29
|
+
width: number | null
|
|
30
|
+
height: number | null
|
|
31
|
+
landscape: boolean
|
|
32
|
+
scale: number
|
|
33
|
+
/** What was asked for. */
|
|
34
|
+
scheme: PreviewScheme
|
|
35
|
+
/** What the page sees: `auto` settled against the host page. */
|
|
36
|
+
effectiveScheme: 'light' | 'dark'
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export interface PreviewFrameCallbacks {
|
|
40
|
+
onLoad?: (state: PreviewState) => void
|
|
41
|
+
onReload?: (state: PreviewState) => void
|
|
42
|
+
onViewport?: (state: PreviewState) => void
|
|
43
|
+
onScheme?: (state: PreviewState) => void
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const STAGE_PADDING = 16
|
|
47
|
+
|
|
48
|
+
export class PreviewFrameView {
|
|
49
|
+
private html: string
|
|
50
|
+
private viewportName: string
|
|
51
|
+
private frameWidth: number | null
|
|
52
|
+
private frameHeight: number | null
|
|
53
|
+
private landscape: boolean
|
|
54
|
+
private scheme: PreviewScheme
|
|
55
|
+
private scripts: boolean
|
|
56
|
+
private forms: boolean
|
|
57
|
+
private title: string
|
|
58
|
+
private viewport: ResolvedViewport
|
|
59
|
+
private scale = 1
|
|
60
|
+
private frame: HTMLIFrameElement | null = null
|
|
61
|
+
private readonly stage: HTMLElement
|
|
62
|
+
private readonly device: HTMLElement
|
|
63
|
+
private observer: ResizeObserver | null = null
|
|
64
|
+
private pending = 0
|
|
65
|
+
private readonly disposers: Array<() => void> = []
|
|
66
|
+
|
|
67
|
+
constructor(private readonly root: HTMLElement, options: PreviewFrameOptions = {}, private readonly callbacks: PreviewFrameCallbacks = {}) {
|
|
68
|
+
this.html = options.html ?? ''
|
|
69
|
+
this.viewportName = options.viewport || 'fill'
|
|
70
|
+
this.frameWidth = options.frameWidth ?? null
|
|
71
|
+
this.frameHeight = options.frameHeight ?? null
|
|
72
|
+
this.landscape = Boolean(options.landscape)
|
|
73
|
+
this.scheme = normalizeScheme(options.scheme)
|
|
74
|
+
this.scripts = Boolean(options.scripts)
|
|
75
|
+
this.forms = Boolean(options.forms)
|
|
76
|
+
this.title = options.title || 'Preview'
|
|
77
|
+
this.viewport = this.resolve()
|
|
78
|
+
|
|
79
|
+
this.stage = this.part('stage')
|
|
80
|
+
this.device = this.part('device')
|
|
81
|
+
|
|
82
|
+
const onClick = (event: Event): void => this.onToolbarClick(event)
|
|
83
|
+
const onKey = (event: Event): void => this.onToolbarKey(event as KeyboardEvent)
|
|
84
|
+
this.root.addEventListener('click', onClick)
|
|
85
|
+
this.root.addEventListener('keydown', onKey)
|
|
86
|
+
this.disposers.push(() => this.root.removeEventListener('click', onClick), () => this.root.removeEventListener('keydown', onKey))
|
|
87
|
+
|
|
88
|
+
if (typeof ResizeObserver !== 'undefined') {
|
|
89
|
+
// On the next frame, not inside the observer: resizing the device from
|
|
90
|
+
// within the notification that the stage resized is what the browser
|
|
91
|
+
// reports as a resize loop.
|
|
92
|
+
this.observer = new ResizeObserver(() => {
|
|
93
|
+
cancelAnimationFrame(this.pending)
|
|
94
|
+
this.pending = requestAnimationFrame(() => this.layout())
|
|
95
|
+
})
|
|
96
|
+
this.observer.observe(this.stage)
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
this.load()
|
|
100
|
+
this.reflect()
|
|
101
|
+
this.root.setAttribute('data-preview-ready', '')
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
getState(): PreviewState {
|
|
105
|
+
return {
|
|
106
|
+
viewport: this.viewport.name,
|
|
107
|
+
width: this.viewport.size?.width ?? null,
|
|
108
|
+
height: this.viewport.size?.height ?? null,
|
|
109
|
+
landscape: this.viewport.landscape,
|
|
110
|
+
scale: this.scale,
|
|
111
|
+
scheme: this.scheme,
|
|
112
|
+
effectiveScheme: this.effectiveScheme(),
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
setHtml(html: string | null | undefined): void {
|
|
117
|
+
const next = html == null ? '' : String(html)
|
|
118
|
+
if (next === this.html)
|
|
119
|
+
return
|
|
120
|
+
this.html = next
|
|
121
|
+
this.load()
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
setViewport(name: string, options: { landscape?: boolean, width?: number | null, height?: number | null } = {}): void {
|
|
125
|
+
this.viewportName = name || 'fill'
|
|
126
|
+
if ('landscape' in options)
|
|
127
|
+
this.landscape = Boolean(options.landscape)
|
|
128
|
+
if ('width' in options)
|
|
129
|
+
this.frameWidth = options.width ?? null
|
|
130
|
+
if ('height' in options)
|
|
131
|
+
this.frameHeight = options.height ?? null
|
|
132
|
+
const before = JSON.stringify(this.viewport)
|
|
133
|
+
this.viewport = this.resolve()
|
|
134
|
+
this.layout()
|
|
135
|
+
this.reflect()
|
|
136
|
+
if (JSON.stringify(this.viewport) !== before)
|
|
137
|
+
this.callbacks.onViewport?.(this.getState())
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
setScheme(scheme: unknown): void {
|
|
141
|
+
const next = normalizeScheme(scheme)
|
|
142
|
+
if (next === this.scheme)
|
|
143
|
+
return
|
|
144
|
+
this.scheme = next
|
|
145
|
+
this.applyScheme()
|
|
146
|
+
this.reflect()
|
|
147
|
+
this.callbacks.onScheme?.(this.getState())
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/** Changing what the page may do needs a new frame, so this loads it again. */
|
|
151
|
+
setPermissions(options: { scripts?: boolean, forms?: boolean }): void {
|
|
152
|
+
const scripts = options.scripts ?? this.scripts
|
|
153
|
+
const forms = options.forms ?? this.forms
|
|
154
|
+
if (scripts === this.scripts && forms === this.forms)
|
|
155
|
+
return
|
|
156
|
+
this.scripts = scripts
|
|
157
|
+
this.forms = forms
|
|
158
|
+
this.load()
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/** Load the same markup again, from nothing. */
|
|
162
|
+
reload(): void {
|
|
163
|
+
this.load()
|
|
164
|
+
this.callbacks.onReload?.(this.getState())
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
destroy(): void {
|
|
168
|
+
this.observer?.disconnect()
|
|
169
|
+
cancelAnimationFrame(this.pending)
|
|
170
|
+
for (const dispose of this.disposers) dispose()
|
|
171
|
+
this.frame?.remove()
|
|
172
|
+
this.frame = null
|
|
173
|
+
this.root.removeAttribute('data-preview-ready')
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
private part(name: string): HTMLElement {
|
|
177
|
+
const found = this.root.querySelector<HTMLElement>(`[data-pf="${name}"]`)
|
|
178
|
+
if (!found)
|
|
179
|
+
throw new Error(`PreviewFrame: the template has no ${name} part`)
|
|
180
|
+
return found
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
private resolve(): ResolvedViewport {
|
|
184
|
+
return resolveViewport(this.viewportName, { width: this.frameWidth, height: this.frameHeight, landscape: this.landscape })
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
/**
|
|
188
|
+
* The scheme the page will see. `auto` follows the host page: a `dark` or
|
|
189
|
+
* `light` class on an ancestor, the way stx themes a page, and the system
|
|
190
|
+
* setting when there is neither.
|
|
191
|
+
*/
|
|
192
|
+
private effectiveScheme(): 'light' | 'dark' {
|
|
193
|
+
if (this.scheme !== 'auto')
|
|
194
|
+
return this.scheme
|
|
195
|
+
if (this.root.closest('.dark'))
|
|
196
|
+
return 'dark'
|
|
197
|
+
if (this.root.closest('.light'))
|
|
198
|
+
return 'light'
|
|
199
|
+
return typeof matchMedia === 'function' && matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light'
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
private load(): void {
|
|
203
|
+
const frame = document.createElement('iframe')
|
|
204
|
+
// Written before the frame is in the document and before it has content:
|
|
205
|
+
// the sandbox a frame is created with is the one its page lives under.
|
|
206
|
+
frame.setAttribute('sandbox', sandboxValue({ scripts: this.scripts, forms: this.forms }))
|
|
207
|
+
frame.setAttribute('referrerpolicy', 'no-referrer')
|
|
208
|
+
frame.setAttribute('title', this.title)
|
|
209
|
+
frame.setAttribute('data-pf', 'frame')
|
|
210
|
+
frame.className = 'stx-preview-frame'
|
|
211
|
+
frame.addEventListener('load', () => {
|
|
212
|
+
if (frame === this.frame)
|
|
213
|
+
this.callbacks.onLoad?.(this.getState())
|
|
214
|
+
})
|
|
215
|
+
frame.srcdoc = buildDocument(this.html, { title: this.title })
|
|
216
|
+
|
|
217
|
+
this.frame?.remove()
|
|
218
|
+
this.frame = frame
|
|
219
|
+
this.applyScheme()
|
|
220
|
+
this.device.appendChild(frame)
|
|
221
|
+
this.layout()
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
/**
|
|
225
|
+
* A frame's page takes its `prefers-color-scheme` from the `color-scheme`
|
|
226
|
+
* its frame element is drawn with. So the scheme is set here, on the host's
|
|
227
|
+
* side, and the page needs no script and no cooperation to follow it.
|
|
228
|
+
*/
|
|
229
|
+
private applyScheme(): void {
|
|
230
|
+
const scheme = this.effectiveScheme()
|
|
231
|
+
this.root.setAttribute('data-scheme', scheme)
|
|
232
|
+
if (this.frame)
|
|
233
|
+
this.frame.style.colorScheme = scheme
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
private layout(): void {
|
|
237
|
+
const frame = this.frame
|
|
238
|
+
if (!frame)
|
|
239
|
+
return
|
|
240
|
+
const size = this.viewport.size
|
|
241
|
+
this.root.setAttribute('data-viewport', this.viewport.name)
|
|
242
|
+
|
|
243
|
+
if (!size) {
|
|
244
|
+
this.scale = 1
|
|
245
|
+
this.device.style.width = ''
|
|
246
|
+
this.device.style.height = ''
|
|
247
|
+
frame.style.width = ''
|
|
248
|
+
frame.style.height = ''
|
|
249
|
+
frame.style.transform = ''
|
|
250
|
+
}
|
|
251
|
+
else {
|
|
252
|
+
const box = this.stage.getBoundingClientRect()
|
|
253
|
+
this.scale = fitScale(size, { width: box.width, height: box.height }, STAGE_PADDING)
|
|
254
|
+
// The page is laid out at its real size and drawn smaller, so its media
|
|
255
|
+
// queries see the device's width, not the box's.
|
|
256
|
+
this.device.style.width = `${size.width * this.scale}px`
|
|
257
|
+
this.device.style.height = `${size.height * this.scale}px`
|
|
258
|
+
frame.style.width = `${size.width}px`
|
|
259
|
+
frame.style.height = `${size.height}px`
|
|
260
|
+
frame.style.transform = this.scale === 1 ? '' : `scale(${this.scale})`
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
const readout = this.root.querySelector('[data-pf="size"]')
|
|
264
|
+
if (readout)
|
|
265
|
+
readout.textContent = describeViewport(this.viewport, this.scale)
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
/** Bring the toolbar in line with the state. */
|
|
269
|
+
private reflect(): void {
|
|
270
|
+
for (const button of this.root.querySelectorAll<HTMLElement>('[data-pf-viewport]')) {
|
|
271
|
+
const chosen = button.getAttribute('data-pf-viewport') === this.viewport.name
|
|
272
|
+
button.setAttribute('aria-checked', String(chosen))
|
|
273
|
+
button.tabIndex = chosen ? 0 : -1
|
|
274
|
+
}
|
|
275
|
+
// A viewport of its own size matches no button; the first one keeps the group reachable.
|
|
276
|
+
if (!this.root.querySelector('[data-pf-viewport][tabindex="0"]'))
|
|
277
|
+
this.root.querySelector<HTMLElement>('[data-pf-viewport]')?.setAttribute('tabindex', '0')
|
|
278
|
+
|
|
279
|
+
const dark = this.effectiveScheme() === 'dark'
|
|
280
|
+
const scheme = this.root.querySelector<HTMLElement>('[data-pf-action="scheme"]')
|
|
281
|
+
if (scheme) {
|
|
282
|
+
scheme.setAttribute('aria-pressed', String(dark))
|
|
283
|
+
const text = scheme.querySelector('[data-pf="scheme-text"]')
|
|
284
|
+
if (text)
|
|
285
|
+
text.textContent = dark ? 'Dark' : 'Light'
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
const rotate = this.root.querySelector<HTMLButtonElement>('[data-pf-action="rotate"]')
|
|
289
|
+
if (rotate) {
|
|
290
|
+
const size = resolveViewport(this.viewportName, { width: this.frameWidth, height: this.frameHeight }).size
|
|
291
|
+
// Only something taller than it is wide has a side to be turned on.
|
|
292
|
+
rotate.disabled = !size || this.viewport.name === 'custom' || size.height <= size.width
|
|
293
|
+
rotate.setAttribute('aria-pressed', String(this.viewport.landscape))
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
private onToolbarClick(event: Event): void {
|
|
298
|
+
const target = event.target instanceof Element ? event.target.closest<HTMLElement>('[data-pf-viewport], [data-pf-action]') : null
|
|
299
|
+
if (!target || !this.root.contains(target))
|
|
300
|
+
return
|
|
301
|
+
|
|
302
|
+
const viewport = target.getAttribute('data-pf-viewport')
|
|
303
|
+
if (viewport) {
|
|
304
|
+
this.setViewport(viewport, { width: null, height: null })
|
|
305
|
+
return
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
switch (target.getAttribute('data-pf-action')) {
|
|
309
|
+
case 'scheme':
|
|
310
|
+
this.setScheme(this.effectiveScheme() === 'dark' ? 'light' : 'dark')
|
|
311
|
+
break
|
|
312
|
+
case 'rotate':
|
|
313
|
+
this.setViewport(this.viewportName, { landscape: !this.landscape })
|
|
314
|
+
break
|
|
315
|
+
case 'reload':
|
|
316
|
+
this.reload()
|
|
317
|
+
break
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
/** The viewport buttons are one radio group: the arrows move through it. */
|
|
322
|
+
private onToolbarKey(event: KeyboardEvent): void {
|
|
323
|
+
const target = event.target instanceof Element ? event.target.closest<HTMLElement>('[data-pf-viewport]') : null
|
|
324
|
+
if (!target)
|
|
325
|
+
return
|
|
326
|
+
const direction = event.key === 'ArrowRight' || event.key === 'ArrowDown' ? 1 : event.key === 'ArrowLeft' || event.key === 'ArrowUp' ? -1 : 0
|
|
327
|
+
if (direction === 0)
|
|
328
|
+
return
|
|
329
|
+
|
|
330
|
+
event.preventDefault()
|
|
331
|
+
const current = (target.getAttribute('data-pf-viewport') || 'fill') as ViewportName
|
|
332
|
+
const next = stepViewport(current, direction)
|
|
333
|
+
this.setViewport(next, { width: null, height: null })
|
|
334
|
+
this.root.querySelector<HTMLElement>(`[data-pf-viewport="${next}"]`)?.focus()
|
|
335
|
+
}
|
|
336
|
+
}
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
# SplitPane
|
|
2
|
+
|
|
3
|
+
Two or more panes with a separator to drag between each pair. Side by side or
|
|
4
|
+
stacked, with minimum sizes, keyboard resizing, and a layout that is remembered.
|
|
5
|
+
|
|
6
|
+
```html
|
|
7
|
+
<SplitPane storageKey="workbench" minSize="200" @resize-end="saved($event)">
|
|
8
|
+
<div>editor</div>
|
|
9
|
+
<div data-min-size="320">results</div>
|
|
10
|
+
</SplitPane>
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
The children of the tag are the panes. Nest one `<SplitPane>` in a pane of
|
|
14
|
+
another, with the other direction, for a three-way layout.
|
|
15
|
+
|
|
16
|
+
## How it works
|
|
17
|
+
|
|
18
|
+
A layout is a list of fractions that add up to one. Each pane gets its fraction
|
|
19
|
+
as its `flex-grow` from a basis of nothing, so the browser divides whatever
|
|
20
|
+
length there is in those proportions and a window that changes size needs no
|
|
21
|
+
script. Pixels come in only where a person does: a drag is so many pixels, and
|
|
22
|
+
a pane must not be smaller than so many.
|
|
23
|
+
|
|
24
|
+
| File | What it is |
|
|
25
|
+
|---|---|
|
|
26
|
+
| `core.ts` | The arithmetic: shares to fractions, moving a divider, fitting a layout inside its limits. No DOM. |
|
|
27
|
+
| `view.ts` | The DOM: puts the separators in, sizes the panes, turns a drag or a key into a new layout, stores it. |
|
|
28
|
+
| `SplitPane.stx` | The template, the styles, and the props and events. |
|
|
29
|
+
|
|
30
|
+
`@stacksjs/components/split-pane` exports the first two for a host that splits
|
|
31
|
+
an element of its own.
|
|
32
|
+
|
|
33
|
+
## Props
|
|
34
|
+
|
|
35
|
+
| Prop | Default | |
|
|
36
|
+
|---|---|---|
|
|
37
|
+
| `direction` | `'horizontal'` | `horizontal` puts the panes side by side, `vertical` stacks them. |
|
|
38
|
+
| `sizes` | equal | Shares, one per pane: `[30, 70]`, `[1, 2, 1]`, or the text `30,70`. Changing it later sets the layout. |
|
|
39
|
+
| `minSize` | `48` | The least a pane can be, in pixels. |
|
|
40
|
+
| `step` | `16` | Pixels an arrow key moves a separator. Shift moves five times as far. |
|
|
41
|
+
| `storageKey` | | Keeps the layout in local storage under `stx:split-pane:<key>`. A stored layout wins over `sizes` on the way up. |
|
|
42
|
+
| `label` | `'Resize panes'` | The accessible name of a separator. |
|
|
43
|
+
| `height` | `'100%'` | A CSS length. A split fills its box, so the box needs a height. |
|
|
44
|
+
| `id`, `className` | | |
|
|
45
|
+
|
|
46
|
+
On a pane itself: `data-size` (its share, used when `sizes` is left out),
|
|
47
|
+
`data-min-size` and `data-max-size` (pixels).
|
|
48
|
+
|
|
49
|
+
## Events
|
|
50
|
+
|
|
51
|
+
| Event | Payload |
|
|
52
|
+
|---|---|
|
|
53
|
+
| `@resize` | `{ sizes, pixels, index, source }` on every change. `sizes` are fractions, `index` is the separator that moved (-1 when set from outside), `source` is `pointer`, `keyboard`, `reset` or `api`. |
|
|
54
|
+
| `@resize-end` | The same, once: at the end of a drag, after a key, after a reset. |
|
|
55
|
+
|
|
56
|
+
## Methods
|
|
57
|
+
|
|
58
|
+
From `componentApi(target)` (`@stacksjs/components/component-api`), where
|
|
59
|
+
`target` is the component's element, its id or a selector: `getSizes()`, `setSizes(sizes)`, `reset()`.
|
|
60
|
+
|
|
61
|
+
## Pointer and keys
|
|
62
|
+
|
|
63
|
+
| | |
|
|
64
|
+
|---|---|
|
|
65
|
+
| Drag a separator | Resize. The pane next to the separator shrinks first; at its minimum, the one beyond it gives. |
|
|
66
|
+
| Double click | Back to the starting layout. |
|
|
67
|
+
| Arrow keys | Move the focused separator by `step`. Left and right when side by side, up and down when stacked. |
|
|
68
|
+
| Shift+arrow | Five steps. |
|
|
69
|
+
| Home, End | As far as it goes each way. |
|
|
70
|
+
| Enter | Back to the starting layout. |
|
|
71
|
+
|
|
72
|
+
A separator is a `role="separator"` that takes focus, with `aria-valuenow`,
|
|
73
|
+
`aria-valuemin` and `aria-valuemax` as the percentage of the pane before it,
|
|
74
|
+
and `aria-controls` naming that pane.
|
|
75
|
+
|
|
76
|
+
## Theming
|
|
77
|
+
|
|
78
|
+
`--stx-split-line` (falls back to the `line` role), `--stx-split-line-active`
|
|
79
|
+
(the `accent` role), `--stx-split-line-size` (the drawn thickness, `1px`) and
|
|
80
|
+
`--stx-split-grab` (the strip that can be grabbed, `9px`).
|
|
81
|
+
|
|
82
|
+
## Limits of this version
|
|
83
|
+
|
|
84
|
+
- **Panes are read once.** Adding or removing a pane after it has mounted is
|
|
85
|
+
not picked up.
|
|
86
|
+
- **No collapse.** A pane stops at its minimum; there is no snap to closed.
|
|
87
|
+
- **Left to right.** The arrow keys are not mirrored in a right-to-left page.
|
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
<script server>
|
|
2
|
+
export const direction = $props.direction === 'vertical' ? 'vertical' : 'horizontal'
|
|
3
|
+
/*
|
|
4
|
+
* Shares, one per pane: `[30, 70]`, `[1, 2, 1]`, or the text `30,70`. Left
|
|
5
|
+
* out, each pane's own `data-size` is used, and equal shares after that.
|
|
6
|
+
*/
|
|
7
|
+
export const sizes = Array.isArray($props.sizes)
|
|
8
|
+
? $props.sizes
|
|
9
|
+
: (typeof $props.sizes === 'string' && $props.sizes.trim() !== '' ? $props.sizes : null)
|
|
10
|
+
export const minSize = $num($props.minSize, 48)
|
|
11
|
+
export const step = $num($props.step, 16)
|
|
12
|
+
export const storageKey = $props.storageKey || ''
|
|
13
|
+
export const label = $props.label || 'Resize panes'
|
|
14
|
+
/* A CSS length. A split fills the box it is given, so the box needs a height. */
|
|
15
|
+
export const height = $props.height || '100%'
|
|
16
|
+
export const className = $props.className || ''
|
|
17
|
+
export const splitId = $props.id || `split-pane-${$uid}`
|
|
18
|
+
</script>
|
|
19
|
+
|
|
20
|
+
<script client>
|
|
21
|
+
/*
|
|
22
|
+
* <SplitPane>: panes side by side, or stacked, with a separator to drag
|
|
23
|
+
* between each pair. The children of the tag are the panes.
|
|
24
|
+
*
|
|
25
|
+
* The arithmetic is in ./core and the DOM in ./view; this is the wiring.
|
|
26
|
+
*/
|
|
27
|
+
import { parseSizes } from './core'
|
|
28
|
+
import { SplitPaneView } from './view'
|
|
29
|
+
|
|
30
|
+
const emit = defineEmits(['resize', 'resize-end'])
|
|
31
|
+
|
|
32
|
+
const asDirection = value => (value === 'vertical' ? 'vertical' : 'horizontal')
|
|
33
|
+
const liveDirection = useReactiveProp('direction', {{ direction }}, { parse: asDirection })
|
|
34
|
+
const liveSizes = useReactiveProp('sizes', {{ sizes }}, { parse: parseSizes })
|
|
35
|
+
|
|
36
|
+
const rootRef = useRef('split-pane')
|
|
37
|
+
let split = null
|
|
38
|
+
|
|
39
|
+
onMount(() => {
|
|
40
|
+
const root = rootRef()
|
|
41
|
+
if (!root) return
|
|
42
|
+
|
|
43
|
+
split = new SplitPaneView(root, {
|
|
44
|
+
direction: asDirection(liveDirection()),
|
|
45
|
+
sizes: parseSizes(liveSizes()),
|
|
46
|
+
minSize: {{ minSize }},
|
|
47
|
+
step: {{ step }},
|
|
48
|
+
storageKey: {{ storageKey }},
|
|
49
|
+
label: {{ label }},
|
|
50
|
+
}, {
|
|
51
|
+
onResize(detail) { emit('resize', detail) },
|
|
52
|
+
onResizeEnd(detail) { emit('resize-end', detail) },
|
|
53
|
+
})
|
|
54
|
+
|
|
55
|
+
// The first run of each finds the layout the view was just built with, and
|
|
56
|
+
// must leave a stored layout alone; only a later change is a new instruction.
|
|
57
|
+
let sizesSeen = false
|
|
58
|
+
effect(() => {
|
|
59
|
+
const next = parseSizes(liveSizes())
|
|
60
|
+
if (sizesSeen && next) split.setSizes(next)
|
|
61
|
+
sizesSeen = true
|
|
62
|
+
})
|
|
63
|
+
effect(() => split.setDirection(asDirection(liveDirection())))
|
|
64
|
+
})
|
|
65
|
+
|
|
66
|
+
onDestroy(() => {
|
|
67
|
+
if (split) split.destroy()
|
|
68
|
+
split = null
|
|
69
|
+
})
|
|
70
|
+
|
|
71
|
+
defineExpose({
|
|
72
|
+
getSizes: () => (split ? split.getSizes() : []),
|
|
73
|
+
setSizes: (next) => { if (split) split.setSizes(parseSizes(next)) },
|
|
74
|
+
reset: () => { if (split) split.reset() },
|
|
75
|
+
})
|
|
76
|
+
</script>
|
|
77
|
+
|
|
78
|
+
<div
|
|
79
|
+
ref="split-pane"
|
|
80
|
+
id="{{ splitId }}"
|
|
81
|
+
data-stx-split-pane
|
|
82
|
+
data-direction="{{ direction }}"
|
|
83
|
+
class="stx-split {{ className }}"
|
|
84
|
+
style="height: {{ height }}"
|
|
85
|
+
>
|
|
86
|
+
<slot />
|
|
87
|
+
</div>
|
|
88
|
+
|
|
89
|
+
<style>
|
|
90
|
+
/*
|
|
91
|
+
* A separator is drawn as a hairline and grabbed by a wider strip around it,
|
|
92
|
+
* so it does not have to be thick to be easy to hit. Every colour falls back
|
|
93
|
+
* to a role token.
|
|
94
|
+
*/
|
|
95
|
+
:where([data-stx-split-pane]) {
|
|
96
|
+
--stx-split-line: var(--stx-line, #e5e5e5);
|
|
97
|
+
--stx-split-line-active: var(--stx-accent, #2563eb);
|
|
98
|
+
--stx-split-line-size: 1px;
|
|
99
|
+
--stx-split-grab: 9px;
|
|
100
|
+
display: flex;
|
|
101
|
+
flex-direction: row;
|
|
102
|
+
width: 100%;
|
|
103
|
+
min-width: 0;
|
|
104
|
+
min-height: 0;
|
|
105
|
+
}
|
|
106
|
+
:where([data-stx-split-pane][data-direction="vertical"]) { flex-direction: column; }
|
|
107
|
+
|
|
108
|
+
:where([data-stx-split-pane]) > :where([data-split-pane]) {
|
|
109
|
+
min-width: 0;
|
|
110
|
+
min-height: 0;
|
|
111
|
+
overflow: auto;
|
|
112
|
+
}
|
|
113
|
+
/* Before the view has run, the panes still share the space rather than stack. */
|
|
114
|
+
:where([data-stx-split-pane]:not([data-split-ready])) > * { flex: 1 1 0px; min-width: 0; min-height: 0; }
|
|
115
|
+
|
|
116
|
+
[data-stx-split-pane] > .stx-split-separator {
|
|
117
|
+
position: relative;
|
|
118
|
+
flex: 0 0 var(--stx-split-line-size);
|
|
119
|
+
background: var(--stx-split-line);
|
|
120
|
+
cursor: col-resize;
|
|
121
|
+
touch-action: none;
|
|
122
|
+
outline: none;
|
|
123
|
+
transition: background-color 120ms ease;
|
|
124
|
+
}
|
|
125
|
+
[data-stx-split-pane][data-direction="vertical"] > .stx-split-separator { cursor: row-resize; }
|
|
126
|
+
|
|
127
|
+
[data-stx-split-pane] > .stx-split-separator::after {
|
|
128
|
+
content: '';
|
|
129
|
+
position: absolute;
|
|
130
|
+
z-index: 1;
|
|
131
|
+
top: 0;
|
|
132
|
+
bottom: 0;
|
|
133
|
+
left: calc((var(--stx-split-grab) - var(--stx-split-line-size)) / -2);
|
|
134
|
+
width: var(--stx-split-grab);
|
|
135
|
+
}
|
|
136
|
+
[data-stx-split-pane][data-direction="vertical"] > .stx-split-separator::after {
|
|
137
|
+
left: 0;
|
|
138
|
+
right: 0;
|
|
139
|
+
top: calc((var(--stx-split-grab) - var(--stx-split-line-size)) / -2);
|
|
140
|
+
bottom: auto;
|
|
141
|
+
width: auto;
|
|
142
|
+
height: var(--stx-split-grab);
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
[data-stx-split-pane] > .stx-split-separator:is(:hover, [data-active]) { background: var(--stx-split-line-active); }
|
|
146
|
+
[data-stx-split-pane] > .stx-split-separator:focus-visible {
|
|
147
|
+
background: var(--stx-split-line-active);
|
|
148
|
+
box-shadow: 0 0 0 2px color-mix(in srgb, var(--stx-split-line-active) 40%, transparent);
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/* A drag must not select the text it passes over, or lose the pointer to a frame. */
|
|
152
|
+
[data-stx-split-pane][data-dragging] { user-select: none; -webkit-user-select: none; cursor: col-resize; }
|
|
153
|
+
[data-stx-split-pane][data-dragging][data-direction="vertical"] { cursor: row-resize; }
|
|
154
|
+
[data-stx-split-pane][data-dragging] iframe { pointer-events: none; }
|
|
155
|
+
|
|
156
|
+
@media (prefers-reduced-motion: reduce) {
|
|
157
|
+
[data-stx-split-pane] > .stx-split-separator { transition: none; }
|
|
158
|
+
}
|
|
159
|
+
</style>
|