@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,540 @@
1
+ /**
2
+ * A context menu in the DOM.
3
+ *
4
+ * One layer holds every open menu: the one that was asked for and any
5
+ * submenus opened from it. Where the browser has a top layer (the popover
6
+ * API) the layer goes there, so a menu is never clipped by an overflow or
7
+ * mispositioned by a transformed ancestor. Focus is real focus, on the item,
8
+ * which is what a screen reader follows; it goes back to where it came from
9
+ * when the menu closes.
10
+ */
11
+ import type { MenuBox, MenuItem, MenuPoint, ResolvedItem } from './core'
12
+ import { ariaShortcut, firstIndex, formatShortcut, hasSubmenu, isActionable, lastIndex, placeAtBox, placeAtPoint, placeSubmenu, resolveItems, stepIndex, typeAheadIndex } from './core'
13
+
14
+ export type MenuItemsSource = MenuItem[] | ((target: Element | null) => MenuItem[] | null | undefined)
15
+
16
+ export interface ContextMenuOptions {
17
+ items?: MenuItemsSource | null
18
+ /** The accessible name of the menu. */
19
+ label?: string
20
+ /** Off: a right click gets the browser's own menu. */
21
+ disabled?: boolean
22
+ /** Written as Mac shortcuts (`⌘C`) or not (`Ctrl+C`). Left out, the platform decides. */
23
+ apple?: boolean
24
+ }
25
+
26
+ export interface MenuSelectDetail {
27
+ item: MenuItem
28
+ id: string
29
+ /** Indexes from the top menu down to the item, counting the separators that are drawn. */
30
+ path: number[]
31
+ /** The element the menu was opened on. */
32
+ target: Element | null
33
+ }
34
+
35
+ export interface MenuOpenDetail {
36
+ x: number
37
+ y: number
38
+ target: Element | null
39
+ keyboard: boolean
40
+ }
41
+
42
+ export type MenuCloseReason = 'select' | 'escape' | 'outside' | 'blur' | 'tab' | 'api'
43
+
44
+ export interface ContextMenuCallbacks {
45
+ onSelect?: (detail: MenuSelectDetail) => void
46
+ onOpen?: (detail: MenuOpenDetail) => void
47
+ onClose?: (detail: { reason: MenuCloseReason }) => void
48
+ }
49
+
50
+ interface OpenMenu {
51
+ element: HTMLElement
52
+ items: ResolvedItem[]
53
+ /** The item in the menu above that this one opened from. */
54
+ opener: HTMLElement | null
55
+ }
56
+
57
+ const SUBMENU_DELAY = 160
58
+ const TYPE_AHEAD_RESET = 600
59
+
60
+ function detectApple(): boolean {
61
+ const platform = typeof navigator === 'undefined' ? '' : (navigator.platform || navigator.userAgent || '')
62
+ return /mac|iphone|ipad/i.test(platform)
63
+ }
64
+
65
+ let menuCounter = 0
66
+
67
+ export class ContextMenuView {
68
+ private items: MenuItemsSource | null
69
+ private label: string
70
+ private disabled: boolean
71
+ private readonly apple: boolean
72
+ private readonly layer: HTMLElement
73
+ private readonly uid = `stx-menu-${++menuCounter}`
74
+ private stack: OpenMenu[] = []
75
+ private target: Element | null = null
76
+ private returnFocus: HTMLElement | null = null
77
+ private hoverTimer: ReturnType<typeof setTimeout> | null = null
78
+ private typed = ''
79
+ private typedAt = 0
80
+ private readonly disposers: Array<() => void> = []
81
+
82
+ constructor(private readonly root: HTMLElement, options: ContextMenuOptions = {}, private readonly callbacks: ContextMenuCallbacks = {}) {
83
+ this.items = options.items ?? null
84
+ this.label = options.label || 'Context menu'
85
+ this.disabled = Boolean(options.disabled)
86
+ this.apple = options.apple ?? detectApple()
87
+
88
+ this.layer = document.createElement('div')
89
+ this.layer.className = 'stx-menu-layer'
90
+ this.layer.setAttribute('data-menu-layer', '')
91
+ // What the view writes here is the host's data, and never a template.
92
+ ;(this.layer as HTMLElement & { __stx_inert_content?: boolean }).__stx_inert_content = true
93
+ if ('popover' in HTMLElement.prototype)
94
+ this.layer.setAttribute('popover', 'manual')
95
+ else
96
+ this.layer.hidden = true
97
+ this.root.appendChild(this.layer)
98
+
99
+ this.listen(this.root, 'contextmenu', event => this.onContextMenu(event as MouseEvent))
100
+ this.listen(this.root, 'keydown', event => this.onHostKeyDown(event as KeyboardEvent))
101
+ this.listen(this.layer, 'keydown', event => this.onMenuKeyDown(event as KeyboardEvent))
102
+ this.listen(this.layer, 'click', event => this.onMenuClick(event as MouseEvent))
103
+ this.listen(this.layer, 'pointermove', event => this.onMenuPointerMove(event as PointerEvent))
104
+ this.listen(this.layer, 'contextmenu', event => event.preventDefault())
105
+ this.root.setAttribute('data-menu-ready', '')
106
+ }
107
+
108
+ get isOpen(): boolean {
109
+ return this.stack.length > 0
110
+ }
111
+
112
+ setItems(items: MenuItemsSource | null | undefined): void {
113
+ this.items = items ?? null
114
+ }
115
+
116
+ setDisabled(disabled: boolean): void {
117
+ this.disabled = disabled
118
+ if (disabled)
119
+ this.close('api')
120
+ }
121
+
122
+ setLabel(label: string): void {
123
+ this.label = label || 'Context menu'
124
+ }
125
+
126
+ /**
127
+ * Open the menu at a point in the viewport, or anchored to an element.
128
+ * `items` replaces the menu's own for this opening. False when there was
129
+ * nothing to show.
130
+ */
131
+ open(at: MenuPoint | Element, items?: MenuItem[] | null, options: { keyboard?: boolean, target?: Element | null } = {}): boolean {
132
+ this.close('api', false)
133
+
134
+ const anchor = at instanceof Element ? at : null
135
+ const target = options.target ?? anchor ?? null
136
+ const source = items ?? (typeof this.items === 'function' ? this.items(target) : this.items)
137
+ const resolved = resolveItems(source)
138
+ if (resolved.length === 0)
139
+ return false
140
+
141
+ this.target = target
142
+ const active = document.activeElement
143
+ this.returnFocus = active instanceof HTMLElement && active !== document.body ? active : null
144
+
145
+ this.showLayer()
146
+ const menu = this.buildMenu(resolved, null)
147
+ const size = menu.element.getBoundingClientRect()
148
+ const viewport = { width: window.innerWidth, height: window.innerHeight }
149
+ const place = anchor
150
+ ? placeAtBox(boxOf(anchor), size, viewport)
151
+ : placeAtPoint(at as MenuPoint, size, viewport)
152
+ this.position(menu.element, place)
153
+
154
+ this.listenWhileOpen()
155
+ // By keyboard the first item is where the next arrow key would be anyway.
156
+ // By pointer nothing is chosen until the pointer or a key says so.
157
+ if (options.keyboard || anchor)
158
+ this.focusItem(menu, firstIndex(menu.items))
159
+ else
160
+ menu.element.focus({ preventScroll: true })
161
+
162
+ this.callbacks.onOpen?.({ x: place.x, y: place.y, target, keyboard: Boolean(options.keyboard) })
163
+ return true
164
+ }
165
+
166
+ close(reason: MenuCloseReason = 'api', restoreFocus = true): void {
167
+ if (!this.isOpen)
168
+ return
169
+ this.clearHover()
170
+ for (const menu of this.stack) menu.element.remove()
171
+ this.stack = []
172
+ this.hideLayer()
173
+ this.stopListeningWhileOpen()
174
+
175
+ const back = this.returnFocus
176
+ this.returnFocus = null
177
+ this.target = null
178
+ // A click somewhere else has already put focus where the person wants it.
179
+ if (restoreFocus && reason !== 'outside' && reason !== 'blur' && back && back.isConnected)
180
+ back.focus({ preventScroll: true })
181
+ this.callbacks.onClose?.({ reason })
182
+ }
183
+
184
+ destroy(): void {
185
+ this.close('api', false)
186
+ for (const dispose of this.disposers) dispose()
187
+ this.layer.remove()
188
+ this.root.removeAttribute('data-menu-ready')
189
+ }
190
+
191
+ private listen(target: EventTarget, type: string, handler: (event: Event) => void, options?: AddEventListenerOptions): () => void {
192
+ target.addEventListener(type, handler, options)
193
+ const dispose = (): void => target.removeEventListener(type, handler, options)
194
+ this.disposers.push(dispose)
195
+ return dispose
196
+ }
197
+
198
+ private whileOpen: Array<() => void> = []
199
+
200
+ private listenWhileOpen(): void {
201
+ const add = (target: EventTarget, type: string, handler: (event: Event) => void, options?: AddEventListenerOptions): void => {
202
+ target.addEventListener(type, handler, options)
203
+ this.whileOpen.push(() => target.removeEventListener(type, handler, options))
204
+ }
205
+ const outside = (event: Event): void => {
206
+ if (!(event.target instanceof Node) || !this.layer.contains(event.target))
207
+ this.close('outside')
208
+ }
209
+ // In the capture phase, so a page that stops the event still closes the menu.
210
+ add(document, 'pointerdown', outside, { capture: true })
211
+ add(document, 'scroll', outside, { capture: true, passive: true })
212
+ add(window, 'resize', () => this.close('blur'))
213
+ add(window, 'blur', () => this.close('blur'))
214
+ }
215
+
216
+ private stopListeningWhileOpen(): void {
217
+ for (const dispose of this.whileOpen) dispose()
218
+ this.whileOpen = []
219
+ }
220
+
221
+ private showLayer(): void {
222
+ if (this.layer.hasAttribute('popover')) {
223
+ try {
224
+ this.layer.showPopover()
225
+ }
226
+ catch {}
227
+ }
228
+ else {
229
+ this.layer.hidden = false
230
+ }
231
+ }
232
+
233
+ private hideLayer(): void {
234
+ if (this.layer.hasAttribute('popover')) {
235
+ try {
236
+ this.layer.hidePopover()
237
+ }
238
+ catch {}
239
+ }
240
+ else {
241
+ this.layer.hidden = true
242
+ }
243
+ }
244
+
245
+ private position(element: HTMLElement, at: MenuPoint): void {
246
+ element.style.left = `${Math.round(at.x)}px`
247
+ element.style.top = `${Math.round(at.y)}px`
248
+ }
249
+
250
+ private buildMenu(items: ResolvedItem[], opener: HTMLElement | null): OpenMenu {
251
+ const element = document.createElement('div')
252
+ element.className = 'stx-menu'
253
+ element.setAttribute('role', 'menu')
254
+ element.setAttribute('tabindex', '-1')
255
+ element.setAttribute('data-menu-depth', String(this.stack.length))
256
+ if (opener)
257
+ element.setAttribute('aria-labelledby', opener.id)
258
+ else
259
+ element.setAttribute('aria-label', this.label)
260
+
261
+ items.forEach((item, index) => {
262
+ if (item.separator) {
263
+ const line = document.createElement('div')
264
+ line.className = 'stx-menu-separator'
265
+ line.setAttribute('role', 'separator')
266
+ element.appendChild(line)
267
+ return
268
+ }
269
+
270
+ const row = document.createElement('div')
271
+ row.className = 'stx-menu-item'
272
+ row.id = `${this.uid}-${item.path.join('-')}`
273
+ row.setAttribute('role', typeof item.checked === 'boolean' ? 'menuitemcheckbox' : 'menuitem')
274
+ row.setAttribute('tabindex', '-1')
275
+ row.setAttribute('data-menu-index', String(index))
276
+ row.setAttribute('data-menu-id', item.id)
277
+ if (typeof item.checked === 'boolean')
278
+ row.setAttribute('aria-checked', String(item.checked))
279
+ if (item.disabled)
280
+ row.setAttribute('aria-disabled', 'true')
281
+ if (item.danger)
282
+ row.setAttribute('data-danger', '')
283
+
284
+ const check = document.createElement('span')
285
+ check.className = 'stx-menu-check'
286
+ check.setAttribute('aria-hidden', 'true')
287
+ check.textContent = item.checked === true ? '✓' : ''
288
+ row.appendChild(check)
289
+
290
+ const label = document.createElement('span')
291
+ label.className = 'stx-menu-label'
292
+ label.textContent = item.label || ''
293
+ row.appendChild(label)
294
+
295
+ if (hasSubmenu(item)) {
296
+ row.setAttribute('aria-haspopup', 'menu')
297
+ row.setAttribute('aria-expanded', 'false')
298
+ const arrow = document.createElement('span')
299
+ arrow.className = 'stx-menu-arrow'
300
+ arrow.setAttribute('aria-hidden', 'true')
301
+ arrow.textContent = '›'
302
+ row.appendChild(arrow)
303
+ }
304
+ else if (item.shortcut) {
305
+ row.setAttribute('aria-keyshortcuts', ariaShortcut(item.shortcut, this.apple))
306
+ const shortcut = document.createElement('span')
307
+ shortcut.className = 'stx-menu-shortcut'
308
+ // The attribute above says it to a screen reader in words; the symbols would be read as noise.
309
+ shortcut.setAttribute('aria-hidden', 'true')
310
+ shortcut.textContent = formatShortcut(item.shortcut, this.apple)
311
+ row.appendChild(shortcut)
312
+ }
313
+ element.appendChild(row)
314
+ })
315
+
316
+ this.layer.appendChild(element)
317
+ const menu: OpenMenu = { element, items, opener }
318
+ this.stack.push(menu)
319
+ return menu
320
+ }
321
+
322
+ private menuOf(node: EventTarget | null): OpenMenu | null {
323
+ if (!(node instanceof Element))
324
+ return null
325
+ const element = node.closest('.stx-menu')
326
+ return this.stack.find(menu => menu.element === element) ?? null
327
+ }
328
+
329
+ private rowAt(menu: OpenMenu, index: number): HTMLElement | null {
330
+ return index < 0 ? null : menu.element.querySelector<HTMLElement>(`:scope > [data-menu-index="${index}"]`)
331
+ }
332
+
333
+ private activeIndex(menu: OpenMenu): number {
334
+ const active = document.activeElement
335
+ if (!(active instanceof HTMLElement) || active.parentElement !== menu.element)
336
+ return -1
337
+ const index = Number(active.getAttribute('data-menu-index'))
338
+ return Number.isInteger(index) ? index : -1
339
+ }
340
+
341
+ private focusItem(menu: OpenMenu, index: number): void {
342
+ const row = this.rowAt(menu, index)
343
+ if (row)
344
+ row.focus({ preventScroll: false })
345
+ else
346
+ menu.element.focus({ preventScroll: true })
347
+ }
348
+
349
+ /** Close every menu below `menu` in the stack. */
350
+ private closeBelow(menu: OpenMenu): void {
351
+ const depth = this.stack.indexOf(menu)
352
+ for (const closing of this.stack.splice(depth + 1)) {
353
+ closing.opener?.setAttribute('aria-expanded', 'false')
354
+ closing.element.remove()
355
+ }
356
+ }
357
+
358
+ private openSubmenu(menu: OpenMenu, index: number, focusFirst: boolean): void {
359
+ const item = menu.items[index]
360
+ const row = this.rowAt(menu, index)
361
+ if (!row || !hasSubmenu(item) || item.disabled)
362
+ return
363
+
364
+ const below = this.stack[this.stack.indexOf(menu) + 1]
365
+ if (below && below.opener === row) {
366
+ if (focusFirst)
367
+ this.focusItem(below, firstIndex(below.items))
368
+ return
369
+ }
370
+
371
+ this.closeBelow(menu)
372
+ row.setAttribute('aria-expanded', 'true')
373
+ const submenu = this.buildMenu(item.children!, row)
374
+ const place = placeSubmenu(boxOf(row), submenu.element.getBoundingClientRect(), { width: window.innerWidth, height: window.innerHeight })
375
+ submenu.element.setAttribute('data-side', place.side)
376
+ this.position(submenu.element, place)
377
+ if (focusFirst)
378
+ this.focusItem(submenu, firstIndex(submenu.items))
379
+ }
380
+
381
+ private choose(menu: OpenMenu, index: number): void {
382
+ const item = menu.items[index]
383
+ if (!isActionable(item))
384
+ return
385
+ if (hasSubmenu(item)) {
386
+ this.openSubmenu(menu, index, true)
387
+ return
388
+ }
389
+
390
+ const { children: _children, path, id, ...plain } = item
391
+ const detail: MenuSelectDetail = { item: { ...plain, id }, id, path: [...path], target: this.target }
392
+ this.close('select')
393
+ this.callbacks.onSelect?.(detail)
394
+ }
395
+
396
+ private clearHover(): void {
397
+ if (this.hoverTimer)
398
+ clearTimeout(this.hoverTimer)
399
+ this.hoverTimer = null
400
+ }
401
+
402
+ private onContextMenu(event: MouseEvent): void {
403
+ if (this.disabled || this.layer.contains(event.target as Node))
404
+ return
405
+ const target = event.target instanceof Element ? event.target : null
406
+ // A menu opened with the keyboard arrives as a contextmenu event too, at
407
+ // the element's position rather than the pointer's.
408
+ const keyboard = event.button !== 2 && event.detail === 0 && event.clientX === 0 && event.clientY === 0
409
+ const opened = keyboard && target
410
+ ? this.open(target, null, { keyboard: true, target })
411
+ : this.open({ x: event.clientX, y: event.clientY }, null, { target })
412
+ if (opened)
413
+ event.preventDefault()
414
+ }
415
+
416
+ /** Shift+F10 and the menu key, for an engine that does not turn them into a contextmenu event. */
417
+ private onHostKeyDown(event: KeyboardEvent): void {
418
+ if (this.disabled || this.isOpen || this.layer.contains(event.target as Node))
419
+ return
420
+ if (event.key !== 'ContextMenu' && !(event.key === 'F10' && event.shiftKey))
421
+ return
422
+ const target = event.target instanceof Element ? event.target : null
423
+ if (target && this.open(target, null, { keyboard: true, target }))
424
+ event.preventDefault()
425
+ }
426
+
427
+ private onMenuClick(event: MouseEvent): void {
428
+ const menu = this.menuOf(event.target)
429
+ const row = event.target instanceof Element ? event.target.closest<HTMLElement>('.stx-menu-item') : null
430
+ if (!menu || !row)
431
+ return
432
+ this.choose(menu, Number(row.getAttribute('data-menu-index')))
433
+ }
434
+
435
+ private onMenuPointerMove(event: PointerEvent): void {
436
+ const menu = this.menuOf(event.target)
437
+ const row = event.target instanceof Element ? event.target.closest<HTMLElement>('.stx-menu-item') : null
438
+ if (!menu || !row || document.activeElement === row)
439
+ return
440
+
441
+ const index = Number(row.getAttribute('data-menu-index'))
442
+ row.focus({ preventScroll: true })
443
+ this.clearHover()
444
+ // A pause before a submenu opens or closes, so crossing a row on the way
445
+ // to somewhere else does not flash menus open and shut.
446
+ this.hoverTimer = setTimeout(() => {
447
+ if (document.activeElement !== row)
448
+ return
449
+ if (hasSubmenu(menu.items[index]))
450
+ this.openSubmenu(menu, index, false)
451
+ else
452
+ this.closeBelow(menu)
453
+ }, SUBMENU_DELAY)
454
+ }
455
+
456
+ private onMenuKeyDown(event: KeyboardEvent): void {
457
+ const menu = this.menuOf(event.target) ?? this.stack[this.stack.length - 1]
458
+ if (!menu)
459
+ return
460
+ const index = this.activeIndex(menu)
461
+ const depth = this.stack.indexOf(menu)
462
+ let handled = true
463
+
464
+ switch (event.key) {
465
+ case 'ArrowDown':
466
+ this.focusItem(menu, stepIndex(menu.items, index, 1))
467
+ break
468
+ case 'ArrowUp':
469
+ this.focusItem(menu, stepIndex(menu.items, index, -1))
470
+ break
471
+ case 'Home':
472
+ case 'PageUp':
473
+ this.focusItem(menu, firstIndex(menu.items))
474
+ break
475
+ case 'End':
476
+ case 'PageDown':
477
+ this.focusItem(menu, lastIndex(menu.items))
478
+ break
479
+ case 'ArrowRight':
480
+ if (hasSubmenu(menu.items[index]))
481
+ this.openSubmenu(menu, index, true)
482
+ break
483
+ case 'ArrowLeft':
484
+ if (depth > 0)
485
+ this.closeSubmenu(menu)
486
+ break
487
+ case 'Escape':
488
+ if (depth > 0)
489
+ this.closeSubmenu(menu)
490
+ else
491
+ this.close('escape')
492
+ break
493
+ case 'Enter':
494
+ case ' ':
495
+ if (index >= 0)
496
+ this.choose(menu, index)
497
+ break
498
+ case 'Tab':
499
+ // Tab leaves a menu. Focus goes back to where it was, and the browser
500
+ // moves on from there.
501
+ this.close('tab')
502
+ handled = false
503
+ break
504
+ default:
505
+ handled = this.typeAhead(menu, index, event)
506
+ }
507
+
508
+ if (handled) {
509
+ event.preventDefault()
510
+ // A dialog under the menu must not also answer this Escape.
511
+ event.stopPropagation()
512
+ }
513
+ }
514
+
515
+ private closeSubmenu(menu: OpenMenu): void {
516
+ const opener = menu.opener
517
+ const parent = this.stack[this.stack.indexOf(menu) - 1]
518
+ if (parent)
519
+ this.closeBelow(parent)
520
+ opener?.focus({ preventScroll: true })
521
+ }
522
+
523
+ private typeAhead(menu: OpenMenu, index: number, event: KeyboardEvent): boolean {
524
+ if (event.key.length !== 1 || event.metaKey || event.ctrlKey || event.altKey)
525
+ return false
526
+ const now = Date.now()
527
+ this.typed = now - this.typedAt > TYPE_AHEAD_RESET ? event.key : this.typed + event.key
528
+ this.typedAt = now
529
+
530
+ const found = typeAheadIndex(menu.items, index, this.typed)
531
+ if (found >= 0)
532
+ this.focusItem(menu, found)
533
+ return true
534
+ }
535
+ }
536
+
537
+ function boxOf(element: Element): MenuBox {
538
+ const rect = element.getBoundingClientRect()
539
+ return { left: rect.left, top: rect.top, right: rect.right, bottom: rect.bottom }
540
+ }