@stacksjs/components 0.2.867 → 0.2.868

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 (109) hide show
  1. package/dist/AppearancePicker-61r20jpp.stx +351 -0
  2. package/dist/{Button-sebyqeva.stx → Button-pg8mzsq3.stx} +249 -0
  3. package/dist/Checkbox-52mhrzkz.stx +417 -0
  4. package/dist/ColorSwatches-ecfxphjy.stx +287 -0
  5. package/dist/{NumberInput-axcfj9y8.stx → NumberInput-e2afxyh3.stx} +303 -0
  6. package/dist/{PasswordInput-8m3109tp.stx → PasswordInput-n2ven456.stx} +315 -0
  7. package/dist/RadioGroup-ps9kn22f.stx +329 -0
  8. package/dist/RadioGroupOption-6hyy1bvb.stx +246 -0
  9. package/dist/{SegmentedControl-fr69q4gb.stx → SegmentedControl-7q0t8c9s.stx} +170 -1
  10. package/dist/{Select-1cnqbcmg.stx → Select-ygzchedx.stx} +266 -0
  11. package/dist/SettingsGroup-dkae46k5.stx +200 -0
  12. package/dist/SettingsPane-jbd1gbdt.stx +50 -0
  13. package/dist/SettingsPaneHeader-f0cvkqs7.stx +188 -0
  14. package/dist/SettingsRow-4cxbsf1q.stx +424 -0
  15. package/dist/SettingsSidebarGroup-p2ygv5yy.stx +108 -0
  16. package/dist/SettingsSidebarItem-x8fsn3w5.stx +228 -0
  17. package/dist/SettingsWindow-mm490ymt.stx +537 -0
  18. package/dist/Slider-j59dgw1c.stx +377 -0
  19. package/dist/Switch-ga9ap8n8.stx +374 -0
  20. package/dist/{TextInput-tsvfwxmg.stx → TextInput-tyqzz3jy.stx} +325 -0
  21. package/dist/index.d.ts +6 -0
  22. package/dist/index.js +35 -24
  23. package/dist/settings/settings.js +757 -0
  24. package/dist/ui/appearance-picker/AppearancePicker.d.ts +12 -0
  25. package/dist/ui/appearance-picker/index.d.ts +16 -0
  26. package/dist/ui/button/Button.d.ts +24 -1
  27. package/dist/ui/button/index.d.ts +1 -0
  28. package/dist/ui/checkbox/Checkbox.d.ts +9 -1
  29. package/dist/ui/checkbox/index.d.ts +1 -0
  30. package/dist/ui/color-swatches/ColorSwatches.d.ts +26 -0
  31. package/dist/ui/color-swatches/index.d.ts +18 -0
  32. package/dist/ui/input/NumberInput.d.ts +11 -1
  33. package/dist/ui/input/PasswordInput.d.ts +9 -1
  34. package/dist/ui/input/TextInput.d.ts +12 -1
  35. package/dist/ui/input/index.d.ts +7 -3
  36. package/dist/ui/radio-group/RadioGroup.d.ts +14 -2
  37. package/dist/ui/radio-group/RadioGroupOption.d.ts +6 -1
  38. package/dist/ui/radio-group/index.d.ts +2 -0
  39. package/dist/ui/segmented-control/SegmentedControl.d.ts +9 -1
  40. package/dist/ui/segmented-control/index.d.ts +1 -0
  41. package/dist/ui/select/Select.d.ts +10 -1
  42. package/dist/ui/select/index.d.ts +1 -0
  43. package/dist/ui/settings/SettingsGroup.d.ts +30 -0
  44. package/dist/ui/settings/SettingsPane.d.ts +19 -0
  45. package/dist/ui/settings/SettingsPaneHeader.d.ts +20 -0
  46. package/dist/ui/settings/SettingsRow.d.ts +53 -0
  47. package/dist/ui/settings/SettingsSidebarGroup.d.ts +11 -0
  48. package/dist/ui/settings/SettingsSidebarItem.d.ts +22 -0
  49. package/dist/ui/settings/SettingsWindow.d.ts +22 -0
  50. package/dist/ui/settings/core.d.ts +146 -0
  51. package/dist/ui/settings/index.d.ts +73 -0
  52. package/dist/ui/settings/settings.d.ts +8 -0
  53. package/dist/ui/settings/view.d.ts +36 -0
  54. package/dist/ui/slider/Slider.d.ts +24 -0
  55. package/dist/ui/slider/core.d.ts +33 -0
  56. package/dist/ui/slider/index.d.ts +20 -0
  57. package/dist/ui/switch/Switch.d.ts +10 -1
  58. package/dist/ui/switch/index.d.ts +2 -1
  59. package/dist/utils/mac-theme.d.ts +24 -0
  60. package/package.json +8 -3
  61. package/src/index.ts +6 -0
  62. package/src/ui/appearance-picker/AppearancePicker.stx +351 -0
  63. package/src/ui/appearance-picker/README.md +61 -0
  64. package/src/ui/appearance-picker/index.ts +22 -0
  65. package/src/ui/button/Button.stx +249 -0
  66. package/src/ui/button/README.md +21 -0
  67. package/src/ui/button/index.ts +8 -0
  68. package/src/ui/checkbox/Checkbox.stx +275 -0
  69. package/src/ui/checkbox/index.ts +2 -0
  70. package/src/ui/color-swatches/ColorSwatches.stx +287 -0
  71. package/src/ui/color-swatches/README.md +66 -0
  72. package/src/ui/color-swatches/index.ts +25 -0
  73. package/src/ui/input/NumberInput.stx +303 -0
  74. package/src/ui/input/PasswordInput.stx +315 -0
  75. package/src/ui/input/TextInput.stx +325 -0
  76. package/src/ui/input/index.ts +9 -3
  77. package/src/ui/radio-group/README.md +17 -0
  78. package/src/ui/radio-group/RadioGroup.stx +184 -6
  79. package/src/ui/radio-group/RadioGroupOption.stx +178 -2
  80. package/src/ui/radio-group/index.ts +4 -0
  81. package/src/ui/segmented-control/SegmentedControl.stx +170 -1
  82. package/src/ui/segmented-control/index.ts +2 -0
  83. package/src/ui/select/README.md +12 -0
  84. package/src/ui/select/Select.stx +266 -0
  85. package/src/ui/select/index.ts +2 -0
  86. package/src/ui/settings/README.md +221 -0
  87. package/src/ui/settings/SettingsGroup.stx +200 -0
  88. package/src/ui/settings/SettingsPane.stx +50 -0
  89. package/src/ui/settings/SettingsPaneHeader.stx +188 -0
  90. package/src/ui/settings/SettingsRow.stx +424 -0
  91. package/src/ui/settings/SettingsSidebarGroup.stx +108 -0
  92. package/src/ui/settings/SettingsSidebarItem.stx +228 -0
  93. package/src/ui/settings/SettingsWindow.stx +537 -0
  94. package/src/ui/settings/core.ts +359 -0
  95. package/src/ui/settings/index.ts +111 -0
  96. package/src/ui/settings/settings.ts +8 -0
  97. package/src/ui/settings/view.ts +740 -0
  98. package/src/ui/slider/README.md +91 -0
  99. package/src/ui/slider/Slider.stx +377 -0
  100. package/src/ui/slider/core.ts +120 -0
  101. package/src/ui/slider/index.ts +34 -0
  102. package/src/ui/switch/README.md +11 -0
  103. package/src/ui/switch/Switch.stx +217 -0
  104. package/src/ui/switch/index.ts +4 -1
  105. package/src/utils/mac-theme.ts +95 -0
  106. package/dist/Checkbox-80myyzzm.stx +0 -142
  107. package/dist/RadioGroup-smqcqgds.stx +0 -151
  108. package/dist/RadioGroupOption-twcw1b2a.stx +0 -70
  109. package/dist/Switch-s8yf2rvm.stx +0 -157
@@ -0,0 +1,740 @@
1
+ /**
2
+ * A Settings window in the DOM.
3
+ *
4
+ * The shell, the sidebar items, the panes and the rows are separate
5
+ * components that never import each other; they meet in attributes. This
6
+ * reads those attributes off the page each time it needs them (so a pane or a
7
+ * row added later is found), asks ./core what to do, and writes the answer
8
+ * back: which pane is shown, which item is selected, what a search lists.
9
+ */
10
+ import type { SettingsHistory, SettingsItemInfo, SettingsPaneInfo, SettingsRowInfo, SettingsSearchHit, SettingsTypeAhead } from './core'
11
+ import { emptySettingsHistory, findSettingsPane, resolveSettingsPane, searchSettings, SETTINGS_HIGHLIGHT_MS, settingsCanGoBack, settingsCanGoForward, settingsHistoryBack, settingsHistoryCurrent, settingsHistoryForward, settingsHistoryVisit, settingsParentPane, settingsResultsAnnouncement, settingsRovingIndex, settingsSidebarPane, settingsStorageName, settingsTypeAheadAdd, settingsTypeAheadMatch } from './core'
12
+
13
+ export interface SettingsWindowOptions {
14
+ /** The pane to open on. A stored pane wins over it; the first pane is the fallback. */
15
+ pane?: string
16
+ /** Remembers the open pane in local storage under `stx:settings:<key>`. */
17
+ storageKey?: string
18
+ /**
19
+ * Whether the shortcuts (find, back, forward) are heard anywhere in the
20
+ * document, as in a window that is all Settings, or only inside the shell.
21
+ */
22
+ globalKeys?: boolean
23
+ }
24
+
25
+ export interface SettingsPaneDetail {
26
+ id: string
27
+ title: string
28
+ /** The pane above a sub-pane, or empty. */
29
+ parent: string
30
+ }
31
+
32
+ export interface SettingsSearchDetail {
33
+ query: string
34
+ /** The sidebar items left showing. */
35
+ panes: string[]
36
+ /** The settings listed under them, best first. */
37
+ settings: Array<{ id: string, label: string, pane: string }>
38
+ }
39
+
40
+ export interface SettingsNavigateDetail {
41
+ /** The pane the row opened. */
42
+ id: string
43
+ /** The pane the row is in. */
44
+ from: string
45
+ /** The row's `data-setting`, or empty. */
46
+ setting: string
47
+ }
48
+
49
+ export interface SettingsWindowCallbacks {
50
+ onPane?: (detail: SettingsPaneDetail) => void
51
+ onSearch?: (detail: SettingsSearchDetail) => void
52
+ onNavigate?: (detail: SettingsNavigateDetail) => void
53
+ }
54
+
55
+ interface InertElement extends HTMLElement {
56
+ __stx_inert_content?: boolean
57
+ }
58
+
59
+ const ROOT = '[data-stx-settings]'
60
+ const ITEM = '[data-settings-item]'
61
+ const PANE = '[data-settings-pane]'
62
+ const ROW = '[data-settings-row]'
63
+ const RESULT = '[data-settings-result]'
64
+ /** Things inside a navigation row that take a press for themselves. */
65
+ const CONTROLS = 'button, a[href], input, select, textarea, label, [role="switch"], [role="checkbox"], [role="radio"], [role="slider"], [role="button"], [contenteditable]'
66
+
67
+ function domId(text: string): string {
68
+ return text.replace(/[^\w-]+/g, '-')
69
+ }
70
+
71
+ export class SettingsWindowView {
72
+ private history: SettingsHistory = emptySettingsHistory()
73
+ private current = ''
74
+ private query = ''
75
+ private storageKey: string
76
+ private typed: SettingsTypeAhead | null = null
77
+ private hits: Array<{ hit: SettingsSearchHit, row: HTMLElement }> = []
78
+ private highlighted: HTMLElement | null = null
79
+ private highlightTimer: ReturnType<typeof setTimeout> | null = null
80
+ private readonly disposers: Array<() => void> = []
81
+
82
+ private readonly list: HTMLElement | null
83
+ private readonly account: HTMLElement | null
84
+ private readonly input: HTMLInputElement | null
85
+ private readonly field: HTMLElement | null
86
+ private readonly results: InertElement | null
87
+ private readonly empty: HTMLElement | null
88
+ private readonly live: InertElement | null
89
+ private readonly backButton: HTMLButtonElement | null
90
+ private readonly forwardButton: HTMLButtonElement | null
91
+ private readonly title: InertElement | null
92
+ private readonly body: HTMLElement | null
93
+
94
+ constructor(private readonly root: HTMLElement, options: SettingsWindowOptions = {}, private readonly callbacks: SettingsWindowCallbacks = {}) {
95
+ this.storageKey = options.storageKey || ''
96
+ if (!root.id)
97
+ root.id = `settings-${Math.random().toString(36).slice(2, 8)}`
98
+
99
+ this.list = this.own('[data-settings-list]')
100
+ this.account = this.own('[data-settings-account]')
101
+ this.input = this.own<HTMLInputElement>('[data-settings-search-input]')
102
+ this.field = this.own('[data-settings-search]')
103
+ this.results = this.own('[data-settings-results]')
104
+ this.empty = this.own('[data-settings-empty]')
105
+ this.live = this.own('[data-settings-live]')
106
+ this.backButton = this.own<HTMLButtonElement>('[data-settings-back]')
107
+ this.forwardButton = this.own<HTMLButtonElement>('[data-settings-forward]')
108
+ this.title = this.own('[data-settings-title]')
109
+ this.body = this.own('[data-settings-body]')
110
+ for (const filled of [this.results, this.live, this.title]) {
111
+ if (filled)
112
+ filled.__stx_inert_content = true
113
+ }
114
+
115
+ this.listen(root, 'click', event => this.onClick(event))
116
+ this.listen(options.globalKeys ? document : root, 'keydown', event => this.onKeyDown(event as KeyboardEvent))
117
+ if (this.input)
118
+ this.listen(this.input, 'input', () => this.search(this.input!.value))
119
+ if (this.body)
120
+ this.listen(this.body, 'scroll', () => this.syncScrolled(), { passive: true })
121
+ // A window that is not in front goes quiet. The shell cannot see AppKit's
122
+ // key window, but it is told when the page gains and loses focus.
123
+ this.listen(window, 'focus', () => root.setAttribute('data-stx-window-active', 'true'))
124
+ this.listen(window, 'blur', () => root.setAttribute('data-stx-window-active', 'false'))
125
+
126
+ this.describe()
127
+ const panes = this.panes()
128
+ const shown = this.paneElements().find(pane => !pane.hidden)?.getAttribute('data-settings-pane')
129
+ this.open(resolveSettingsPane(panes, this.stored(), options.pane, shown))
130
+ root.setAttribute('data-settings-ready', '')
131
+ }
132
+
133
+ /** The id of the open pane. */
134
+ getPane(): string {
135
+ return this.current
136
+ }
137
+
138
+ /** Open a pane, as a click on its sidebar item would. False when there is no such pane. */
139
+ showPane(id: string): boolean {
140
+ if (!findSettingsPane(this.panes(), id))
141
+ return false
142
+ this.open(id)
143
+ return true
144
+ }
145
+
146
+ /**
147
+ * Open the pane holding `[data-setting="id"]`, bring the row into view and
148
+ * highlight it. False when no row has that id.
149
+ */
150
+ revealSetting(id: string): boolean {
151
+ if (!id)
152
+ return false
153
+ const row = this.rowElements().find(element => element.getAttribute('data-setting') === id)
154
+ const pane = row ? this.paneOf(row) : ''
155
+ if (!row || !pane)
156
+ return false
157
+ this.open(pane)
158
+ this.reveal(row)
159
+ return true
160
+ }
161
+
162
+ back(): void {
163
+ this.travel(settingsHistoryBack(this.history, this.panes()))
164
+ }
165
+
166
+ forward(): void {
167
+ this.travel(settingsHistoryForward(this.history))
168
+ }
169
+
170
+ focusSearch(): void {
171
+ if (!this.input)
172
+ return
173
+ this.input.focus()
174
+ this.input.select()
175
+ }
176
+
177
+ /** Search for `query`, as typing it into the field would. */
178
+ search(query: string): void {
179
+ this.query = query
180
+ if (this.input && this.input.value !== query)
181
+ this.input.value = query
182
+ this.applySearch(true)
183
+ }
184
+
185
+ destroy(): void {
186
+ for (const dispose of this.disposers) dispose()
187
+ this.disposers.length = 0
188
+ this.clearHighlight()
189
+ this.root.removeAttribute('data-settings-ready')
190
+ }
191
+
192
+ // -------------------------------------------------------------------------
193
+ // Reading the page
194
+ // -------------------------------------------------------------------------
195
+
196
+ /** The first match that belongs to this shell and not to one nested in a pane. */
197
+ private own<T extends HTMLElement = HTMLElement>(selector: string): T | null {
198
+ return this.all<T>(selector)[0] ?? null
199
+ }
200
+
201
+ private all<T extends HTMLElement = HTMLElement>(selector: string, within: ParentNode = this.root): T[] {
202
+ return Array.from(within.querySelectorAll<T>(selector)).filter(element => element.closest(ROOT) === this.root)
203
+ }
204
+
205
+ private listen(target: EventTarget, type: string, handler: (event: Event) => void, options?: AddEventListenerOptions): void {
206
+ target.addEventListener(type, handler, options)
207
+ this.disposers.push(() => target.removeEventListener(type, handler, options))
208
+ }
209
+
210
+ private paneElements(): HTMLElement[] {
211
+ return this.all(PANE)
212
+ }
213
+
214
+ private paneElement(id: string): HTMLElement | null {
215
+ return this.paneElements().find(pane => pane.getAttribute('data-settings-pane') === id) ?? null
216
+ }
217
+
218
+ private panes(): SettingsPaneInfo[] {
219
+ return this.paneElements().map(pane => ({
220
+ id: pane.getAttribute('data-settings-pane') || '',
221
+ title: pane.getAttribute('data-title') || pane.getAttribute('data-settings-pane') || '',
222
+ keywords: pane.getAttribute('data-keywords') || '',
223
+ parent: pane.getAttribute('data-parent') || '',
224
+ })).filter(pane => pane.id !== '')
225
+ }
226
+
227
+ private itemElements(): HTMLElement[] {
228
+ return this.list ? this.all(ITEM, this.list) : []
229
+ }
230
+
231
+ private visibleItems(): HTMLElement[] {
232
+ return this.itemElements().filter(item => !item.hidden && !(item as HTMLButtonElement).disabled)
233
+ }
234
+
235
+ private labelOf(item: HTMLElement): string {
236
+ return (item.querySelector('.stx-settings-item-label')?.textContent || item.getAttribute('aria-label') || item.textContent || '').trim()
237
+ }
238
+
239
+ private items(): SettingsItemInfo[] {
240
+ return this.itemElements().map(item => ({
241
+ id: item.getAttribute('data-settings-item') || '',
242
+ label: this.labelOf(item),
243
+ keywords: item.getAttribute('data-keywords') || '',
244
+ }))
245
+ }
246
+
247
+ private rowElements(): HTMLElement[] {
248
+ return this.all(ROW).filter(row => this.paneOf(row) !== '')
249
+ }
250
+
251
+ private paneOf(element: Element): string {
252
+ return element.closest(PANE)?.getAttribute('data-settings-pane') || ''
253
+ }
254
+
255
+ private stored(): string {
256
+ if (!this.storageKey)
257
+ return ''
258
+ try {
259
+ return localStorage.getItem(settingsStorageName(this.storageKey)) || ''
260
+ }
261
+ catch {
262
+ // Storage can be switched off, and a window that forgets still opens.
263
+ return ''
264
+ }
265
+ }
266
+
267
+ private store(): void {
268
+ if (!this.storageKey || !this.current)
269
+ return
270
+ try {
271
+ localStorage.setItem(settingsStorageName(this.storageKey), this.current)
272
+ }
273
+ catch {}
274
+ }
275
+
276
+ // -------------------------------------------------------------------------
277
+ // Panes and history
278
+ // -------------------------------------------------------------------------
279
+
280
+ /**
281
+ * Give the tabs and their panels the names and the links their roles
282
+ * promise. Run before every pane change, so anything added since is covered.
283
+ */
284
+ private describe(): void {
285
+ const prefix = this.root.id
286
+ const items = new Map<string, HTMLElement>()
287
+ for (const item of this.itemElements()) {
288
+ const id = item.getAttribute('data-settings-item') || ''
289
+ if (!item.id)
290
+ item.id = `${prefix}-item-${domId(id)}`
291
+ item.setAttribute('role', 'tab')
292
+ items.set(id, item)
293
+ // A tab list owns its tabs directly: whatever wraps one for layout says
294
+ // so, or the list reads as holding groups of nothing.
295
+ for (let wrapper = item.parentElement; wrapper && wrapper !== this.list; wrapper = wrapper.parentElement) {
296
+ if (!wrapper.hasAttribute('role'))
297
+ wrapper.setAttribute('role', 'presentation')
298
+ }
299
+ }
300
+
301
+ for (const pane of this.paneElements()) {
302
+ const id = pane.getAttribute('data-settings-pane') || ''
303
+ if (!pane.id)
304
+ pane.id = `${prefix}-pane-${domId(id)}`
305
+ const item = items.get(id)
306
+ if (item) {
307
+ item.setAttribute('aria-controls', pane.id)
308
+ pane.setAttribute('role', 'tabpanel')
309
+ pane.setAttribute('aria-labelledby', item.id)
310
+ pane.removeAttribute('aria-label')
311
+ }
312
+ else if (this.title?.id) {
313
+ // A sub-pane has no tab. Its name is the title the toolbar shows.
314
+ pane.setAttribute('role', 'region')
315
+ if (!pane.hasAttribute('aria-label'))
316
+ pane.setAttribute('aria-label', pane.getAttribute('data-title') || id)
317
+ }
318
+ }
319
+ }
320
+
321
+ private open(id: string): void {
322
+ if (!id)
323
+ return
324
+ this.history = settingsHistoryVisit(this.history, id)
325
+ this.show()
326
+ }
327
+
328
+ private travel(next: SettingsHistory): void {
329
+ if (next === this.history)
330
+ return
331
+ this.history = next
332
+ this.show()
333
+ }
334
+
335
+ /** Draw the pane the history points at. */
336
+ private show(): void {
337
+ this.describe()
338
+ const panes = this.panes()
339
+ const id = settingsHistoryCurrent(this.history)
340
+ const info = findSettingsPane(panes, id)
341
+ const target = this.paneElement(id)
342
+ if (!info || !target)
343
+ return
344
+
345
+ const active = document.activeElement
346
+ const focusLeaves = active instanceof HTMLElement && this.paneElements().some(pane => pane !== target && pane.contains(active))
347
+
348
+ for (const pane of this.paneElements()) {
349
+ pane.hidden = pane !== target
350
+ pane.toggleAttribute('data-active', pane === target)
351
+ }
352
+
353
+ const selected = settingsSidebarPane(panes, id)
354
+ for (const item of this.itemElements()) {
355
+ const on = item.getAttribute('data-settings-item') === selected
356
+ item.setAttribute('aria-selected', on ? 'true' : 'false')
357
+ item.toggleAttribute('data-active', on)
358
+ }
359
+ this.syncTabStops()
360
+
361
+ if (this.title)
362
+ this.title.textContent = info.title
363
+ this.root.setAttribute('data-pane', id)
364
+ this.root.toggleAttribute('data-subpane', settingsParentPane(panes, id) !== '')
365
+ this.syncHistoryButtons(panes)
366
+
367
+ const changed = id !== this.current
368
+ this.current = id
369
+ if (changed) {
370
+ this.clearHighlight()
371
+ // A different screen, not more of the one that was there.
372
+ if (this.body)
373
+ this.body.scrollTop = 0
374
+ this.syncScrolled()
375
+ // Focus that sat in the pane that left would otherwise fall to the page.
376
+ if (focusLeaves) {
377
+ if (!target.hasAttribute('tabindex'))
378
+ target.setAttribute('tabindex', '-1')
379
+ target.focus({ preventScroll: true })
380
+ }
381
+ this.store()
382
+ this.callbacks.onPane?.({ id, title: info.title, parent: settingsParentPane(panes, id) })
383
+ }
384
+ }
385
+
386
+ /** One stop in the tab order: the selected item, or the first showing when it is filtered out. */
387
+ private syncTabStops(): void {
388
+ const visible = this.visibleItems()
389
+ const stop = visible.find(item => item.getAttribute('aria-selected') === 'true') ?? visible[0]
390
+ for (const item of this.itemElements())
391
+ item.setAttribute('tabindex', item === stop ? '0' : '-1')
392
+ }
393
+
394
+ private syncHistoryButtons(panes: SettingsPaneInfo[]): void {
395
+ const set = (button: HTMLButtonElement | null, enabled: boolean, other: HTMLButtonElement | null): void => {
396
+ if (!button)
397
+ return
398
+ const focused = document.activeElement === button
399
+ button.disabled = !enabled
400
+ // A disabled button drops the focus it had; its neighbour keeps it in the toolbar.
401
+ if (focused && !enabled && other && !other.disabled)
402
+ other.focus()
403
+ }
404
+ const back = settingsCanGoBack(this.history, panes)
405
+ const forward = settingsCanGoForward(this.history)
406
+ // Enable before disabling, so there is somewhere for focus to go.
407
+ if (back) {
408
+ set(this.backButton, true, null)
409
+ set(this.forwardButton, forward, this.backButton)
410
+ }
411
+ else {
412
+ set(this.forwardButton, forward, null)
413
+ set(this.backButton, false, this.forwardButton)
414
+ }
415
+ }
416
+
417
+ private syncScrolled(): void {
418
+ this.root.toggleAttribute('data-scrolled', Boolean(this.body && this.body.scrollTop > 0))
419
+ }
420
+
421
+ // -------------------------------------------------------------------------
422
+ // Search
423
+ // -------------------------------------------------------------------------
424
+
425
+ private applySearch(report: boolean): void {
426
+ const rows = this.rowElements()
427
+ const index: SettingsRowInfo[] = rows.map(row => ({
428
+ id: row.getAttribute('data-setting') || '',
429
+ label: (row.getAttribute('data-label') || '').trim(),
430
+ keywords: row.getAttribute('data-keywords') || '',
431
+ pane: this.paneOf(row),
432
+ }))
433
+ const found = searchSettings(this.query, { panes: this.panes(), items: this.items(), rows: index })
434
+ const searching = found.query !== ''
435
+
436
+ this.root.toggleAttribute('data-searching', searching)
437
+ this.field?.toggleAttribute('data-filled', this.query !== '')
438
+
439
+ for (const item of this.itemElements())
440
+ item.hidden = searching && !found.panes.includes(item.getAttribute('data-settings-item') || '')
441
+ if (this.list) {
442
+ // A group whose every item is hidden would leave its gap behind.
443
+ for (const group of this.all('[data-settings-group]', this.list))
444
+ group.hidden = searching && this.all(ITEM, group).every(item => item.hidden)
445
+ this.list.hidden = searching && found.panes.length === 0
446
+ }
447
+ // The account is not a search result.
448
+ if (this.account)
449
+ this.account.hidden = searching
450
+
451
+ this.hits = found.settings.map(hit => ({ hit, row: rows[hit.row] }))
452
+ this.drawResults()
453
+
454
+ const total = found.panes.length + found.settings.length
455
+ if (this.empty)
456
+ this.empty.hidden = !searching || total > 0
457
+ if (this.live)
458
+ this.live.textContent = searching ? settingsResultsAnnouncement(total) : ''
459
+ this.syncTabStops()
460
+
461
+ if (report) {
462
+ this.callbacks.onSearch?.({
463
+ query: this.query.trim(),
464
+ panes: found.panes,
465
+ settings: found.settings.map(hit => ({ id: hit.id, label: hit.label, pane: hit.pane })),
466
+ })
467
+ }
468
+ }
469
+
470
+ /** The settings a search found, listed under the sidebar items: the label, and the pane it is in beneath. */
471
+ private drawResults(): void {
472
+ const box = this.results
473
+ if (!box)
474
+ return
475
+ box.replaceChildren()
476
+ box.hidden = this.hits.length === 0
477
+ this.hits.forEach(({ hit }, position) => {
478
+ const button = document.createElement('button')
479
+ button.type = 'button'
480
+ button.className = 'stx-settings-result'
481
+ button.setAttribute('data-settings-result', String(position))
482
+ button.setAttribute('tabindex', '-1')
483
+ const label = document.createElement('span')
484
+ label.className = 'stx-settings-result-label'
485
+ label.textContent = hit.label
486
+ const pane = document.createElement('span')
487
+ pane.className = 'stx-settings-result-pane'
488
+ pane.textContent = hit.paneTitle
489
+ button.append(label, pane)
490
+ box.append(button)
491
+ })
492
+ }
493
+
494
+ private chooseResult(position: number): void {
495
+ const found = this.hits[position]
496
+ if (!found || !found.row.isConnected)
497
+ return
498
+ this.open(found.hit.pane)
499
+ this.reveal(found.row)
500
+ }
501
+
502
+ private clearSearch(): void {
503
+ if (this.query === '' && (!this.input || this.input.value === ''))
504
+ return
505
+ this.search('')
506
+ }
507
+
508
+ // -------------------------------------------------------------------------
509
+ // Finding a row
510
+ // -------------------------------------------------------------------------
511
+
512
+ private reveal(row: HTMLElement): void {
513
+ if (this.body) {
514
+ const frame = this.body.getBoundingClientRect()
515
+ const box = row.getBoundingClientRect()
516
+ // Centred in the scroller, by its own scroll position alone: scrolling
517
+ // the row into view would move the page around a sheet as well.
518
+ this.body.scrollTop += (box.top - frame.top) - (frame.height - box.height) / 2
519
+ this.syncScrolled()
520
+ }
521
+ this.clearHighlight()
522
+ row.setAttribute('data-highlight', '')
523
+ this.highlighted = row
524
+ this.highlightTimer = setTimeout(() => this.clearHighlight(), SETTINGS_HIGHLIGHT_MS)
525
+ }
526
+
527
+ private clearHighlight(): void {
528
+ if (this.highlightTimer)
529
+ clearTimeout(this.highlightTimer)
530
+ this.highlightTimer = null
531
+ this.highlighted?.removeAttribute('data-highlight')
532
+ this.highlighted = null
533
+ }
534
+
535
+ // -------------------------------------------------------------------------
536
+ // Pointer and keys
537
+ // -------------------------------------------------------------------------
538
+
539
+ private inside(target: EventTarget | null): target is Element {
540
+ return target instanceof Element && target.closest(ROOT) === this.root
541
+ }
542
+
543
+ private navigationRow(target: Element): HTMLElement | null {
544
+ const row = target.closest<HTMLElement>('[data-navigate]')
545
+ if (!row || row.closest(ROOT) !== this.root || row.getAttribute('aria-disabled') === 'true')
546
+ return null
547
+ // A switch or a button inside the row is pressed for itself.
548
+ const control = target.closest(CONTROLS)
549
+ if (control && control !== row && row.contains(control))
550
+ return null
551
+ return row
552
+ }
553
+
554
+ private navigate(row: HTMLElement): void {
555
+ const id = row.getAttribute('data-navigate') || ''
556
+ if (!findSettingsPane(this.panes(), id))
557
+ return
558
+ const from = this.current
559
+ this.open(id)
560
+ this.callbacks.onNavigate?.({ id, from, setting: row.getAttribute('data-setting') || '' })
561
+ }
562
+
563
+ private onClick(event: Event): void {
564
+ const target = event.target
565
+ if (!this.inside(target))
566
+ return
567
+
568
+ const item = target.closest<HTMLElement>(ITEM)
569
+ if (item && this.list?.contains(item)) {
570
+ this.open(item.getAttribute('data-settings-item') || '')
571
+ return
572
+ }
573
+ const result = target.closest<HTMLElement>(RESULT)
574
+ if (result) {
575
+ this.chooseResult(Number(result.getAttribute('data-settings-result')))
576
+ return
577
+ }
578
+ if (target.closest('[data-settings-back]')) {
579
+ this.back()
580
+ return
581
+ }
582
+ if (target.closest('[data-settings-forward]')) {
583
+ this.forward()
584
+ return
585
+ }
586
+ if (target.closest('[data-settings-search-clear]')) {
587
+ this.clearSearch()
588
+ this.input?.focus()
589
+ return
590
+ }
591
+ const row = this.navigationRow(target)
592
+ if (row)
593
+ this.navigate(row)
594
+ }
595
+
596
+ private onKeyDown(event: KeyboardEvent): void {
597
+ if (event.defaultPrevented)
598
+ return
599
+ const command = (event.metaKey || event.ctrlKey) && !event.altKey
600
+
601
+ if (command && !event.shiftKey) {
602
+ const key = event.key.toLowerCase()
603
+ if (key === 'f' && this.input) {
604
+ event.preventDefault()
605
+ this.focusSearch()
606
+ return
607
+ }
608
+ if (key === '[') {
609
+ event.preventDefault()
610
+ this.back()
611
+ return
612
+ }
613
+ if (key === ']') {
614
+ event.preventDefault()
615
+ this.forward()
616
+ return
617
+ }
618
+ }
619
+
620
+ const target = event.target
621
+ if (!this.inside(target))
622
+ return
623
+
624
+ // Escape with nothing typed is the window's to close; with a search it
625
+ // only ends the search, and focus goes back to the list.
626
+ if (event.key === 'Escape' && this.query !== '') {
627
+ event.preventDefault()
628
+ this.clearSearch()
629
+ this.visibleItems().find(item => item.getAttribute('tabindex') === '0')?.focus()
630
+ return
631
+ }
632
+
633
+ if (target === this.input) {
634
+ this.onSearchKey(event)
635
+ return
636
+ }
637
+
638
+ const result = target.closest<HTMLElement>(RESULT)
639
+ if (result) {
640
+ this.onResultKey(event, result)
641
+ return
642
+ }
643
+
644
+ const item = target.closest<HTMLElement>(ITEM)
645
+ if (item && this.list?.contains(item)) {
646
+ this.onItemKey(event, item)
647
+ return
648
+ }
649
+
650
+ if ((event.key === 'Enter' || event.key === ' ') && !command && !event.altKey) {
651
+ const row = target.closest<HTMLElement>('[data-navigate]')
652
+ // Only the row itself: a key on a control inside it belongs to the control.
653
+ if (row && row === target && this.navigationRow(target)) {
654
+ event.preventDefault()
655
+ this.navigate(row)
656
+ }
657
+ }
658
+ }
659
+
660
+ private resultButtons(): HTMLElement[] {
661
+ return this.results ? Array.from(this.results.querySelectorAll<HTMLElement>(RESULT)) : []
662
+ }
663
+
664
+ private onSearchKey(event: KeyboardEvent): void {
665
+ if (event.key === 'ArrowDown') {
666
+ const first = this.visibleItems()[0] ?? this.resultButtons()[0]
667
+ if (first) {
668
+ event.preventDefault()
669
+ first.focus()
670
+ }
671
+ }
672
+ else if (event.key === 'Enter' && this.query.trim() !== '') {
673
+ event.preventDefault()
674
+ const first = this.visibleItems()[0]
675
+ if (first)
676
+ this.open(first.getAttribute('data-settings-item') || '')
677
+ else
678
+ this.chooseResult(0)
679
+ }
680
+ }
681
+
682
+ private onResultKey(event: KeyboardEvent, result: HTMLElement): void {
683
+ const buttons = this.resultButtons()
684
+ const index = buttons.indexOf(result)
685
+ if (event.key === 'ArrowUp' && index === 0) {
686
+ const items = this.visibleItems()
687
+ const last = items[items.length - 1] ?? this.input
688
+ if (last) {
689
+ event.preventDefault()
690
+ last.focus()
691
+ }
692
+ return
693
+ }
694
+ const next = settingsRovingIndex(event.key, index, buttons.length)
695
+ if (next === -1)
696
+ return
697
+ event.preventDefault()
698
+ buttons[next].focus()
699
+ }
700
+
701
+ private onItemKey(event: KeyboardEvent, item: HTMLElement): void {
702
+ if (event.metaKey || event.ctrlKey || event.altKey)
703
+ return
704
+ const items = this.visibleItems()
705
+ const index = items.indexOf(item)
706
+
707
+ // Past the last item a search's settings begin.
708
+ if (event.key === 'ArrowDown' && index === items.length - 1) {
709
+ const first = this.resultButtons()[0]
710
+ if (first) {
711
+ event.preventDefault()
712
+ first.focus()
713
+ return
714
+ }
715
+ }
716
+
717
+ let next = settingsRovingIndex(event.key, index, items.length)
718
+ if (next === -1 && event.key.length === 1 && (event.key !== ' ' || this.typed !== null)) {
719
+ const typed = settingsTypeAheadAdd(this.typed, event.key, Date.now())
720
+ // A space only ever continues a word; alone it presses the item.
721
+ if (event.key === ' ' && typed.buffer.trim() === '') {
722
+ this.typed = null
723
+ return
724
+ }
725
+ this.typed = typed
726
+ next = settingsTypeAheadMatch(items.map(entry => this.labelOf(entry)), typed.buffer, index)
727
+ if (next === -1) {
728
+ event.preventDefault()
729
+ return
730
+ }
731
+ }
732
+ if (next === -1)
733
+ return
734
+
735
+ event.preventDefault()
736
+ // Moving selects, as it does in System Settings.
737
+ items[next].focus()
738
+ this.open(items[next].getAttribute('data-settings-item') || '')
739
+ }
740
+ }