dssoca 0.20.0 → 0.21.0

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 (133) hide show
  1. package/README.md +3 -3
  2. package/dist/components/Accordion.svelte +23 -52
  3. package/dist/components/BottomNav.svelte +18 -28
  4. package/dist/components/BoxPlot.svelte +7 -6
  5. package/dist/components/BumpChart.svelte +9 -12
  6. package/dist/components/Chart.svelte +9 -12
  7. package/dist/components/Checkbox.svelte +210 -0
  8. package/dist/components/Checkbox.svelte.d.ts +38 -0
  9. package/dist/components/FileDrop.svelte +52 -83
  10. package/dist/components/FileDrop.svelte.d.ts +6 -2
  11. package/dist/components/Heatmap.svelte +8 -8
  12. package/dist/components/Image.svelte +44 -44
  13. package/dist/components/Input.svelte +8 -3
  14. package/dist/components/LogStream.svelte +15 -25
  15. package/dist/components/Menu.svelte +69 -94
  16. package/dist/components/Menu.svelte.d.ts +6 -0
  17. package/dist/components/Modal.svelte +61 -14
  18. package/dist/components/Modal.svelte.d.ts +7 -2
  19. package/dist/components/NumberField.svelte +18 -19
  20. package/dist/components/Pagination.svelte +47 -37
  21. package/dist/components/Pagination.svelte.d.ts +6 -2
  22. package/dist/components/Popover.svelte +247 -0
  23. package/dist/components/Popover.svelte.d.ts +36 -0
  24. package/dist/components/RadioGroup.svelte +227 -0
  25. package/dist/components/RadioGroup.svelte.d.ts +42 -0
  26. package/dist/components/ScatterPlot.svelte +11 -13
  27. package/dist/components/SearchPalette.svelte +124 -120
  28. package/dist/components/SearchPalette.svelte.d.ts +6 -4
  29. package/dist/components/SegmentedControl.svelte +41 -62
  30. package/dist/components/SegmentedControl.svelte.d.ts +7 -2
  31. package/dist/components/ShortcutsHelp.svelte +48 -47
  32. package/dist/components/Sidebar.svelte +48 -60
  33. package/dist/components/Switch.svelte +20 -10
  34. package/dist/components/Switch.svelte.d.ts +6 -2
  35. package/dist/components/Table.svelte +15 -37
  36. package/dist/components/Tabs.svelte +223 -0
  37. package/dist/components/Tabs.svelte.d.ts +51 -0
  38. package/dist/components/TierList.svelte +38 -38
  39. package/dist/components/TierList.svelte.d.ts +7 -3
  40. package/dist/components/Toaster.svelte +29 -29
  41. package/dist/components/Tooltip.svelte +37 -21
  42. package/dist/components/Topbar.svelte +13 -38
  43. package/dist/core/accordion.d.ts +32 -0
  44. package/dist/core/accordion.js +53 -0
  45. package/dist/core/behaviors/focusable.d.ts +5 -0
  46. package/dist/core/behaviors/focusable.js +14 -0
  47. package/dist/core/behaviors/roving.d.ts +13 -0
  48. package/dist/core/behaviors/roving.js +29 -0
  49. package/dist/core/bottom-nav.d.ts +36 -0
  50. package/dist/core/bottom-nav.js +33 -0
  51. package/dist/core/chart-nav.d.ts +53 -0
  52. package/dist/{chart-nav.js → core/chart-nav.js} +27 -0
  53. package/dist/core/checkbox.d.ts +36 -0
  54. package/dist/core/checkbox.js +29 -0
  55. package/dist/core/file-drop.d.ts +75 -0
  56. package/dist/core/file-drop.js +101 -0
  57. package/dist/core/image.d.ts +59 -0
  58. package/dist/core/image.js +64 -0
  59. package/dist/core/input.d.ts +21 -0
  60. package/dist/core/input.js +17 -0
  61. package/dist/core/log-stream.d.ts +51 -0
  62. package/dist/core/log-stream.js +50 -0
  63. package/dist/core/menu.d.ts +70 -0
  64. package/dist/core/menu.js +84 -0
  65. package/dist/core/modal.d.ts +59 -0
  66. package/dist/core/modal.js +44 -0
  67. package/dist/core/number-field.d.ts +47 -0
  68. package/dist/core/number-field.js +64 -0
  69. package/dist/core/pagination.d.ts +59 -0
  70. package/dist/core/pagination.js +65 -0
  71. package/dist/core/popover.d.ts +68 -0
  72. package/dist/core/popover.js +55 -0
  73. package/dist/core/radio.d.ts +36 -0
  74. package/dist/core/radio.js +48 -0
  75. package/dist/core/search-palette.d.ts +124 -0
  76. package/dist/core/search-palette.js +149 -0
  77. package/dist/core/segmented.d.ts +46 -0
  78. package/dist/core/segmented.js +66 -0
  79. package/dist/core/shortcuts-help.d.ts +56 -0
  80. package/dist/core/shortcuts-help.js +50 -0
  81. package/dist/core/sidebar.d.ts +63 -0
  82. package/dist/core/sidebar.js +84 -0
  83. package/dist/core/switch.d.ts +19 -0
  84. package/dist/core/switch.js +11 -0
  85. package/dist/core/table.d.ts +40 -0
  86. package/dist/core/table.js +57 -0
  87. package/dist/core/tabs.d.ts +84 -0
  88. package/dist/core/tabs.js +90 -0
  89. package/dist/{tierlist-core.d.ts → core/tierlist.d.ts} +62 -0
  90. package/dist/{tierlist-core.js → core/tierlist.js} +34 -0
  91. package/dist/core/toast.d.ts +112 -0
  92. package/dist/{toast-core.js → core/toast.js} +48 -2
  93. package/dist/core/tooltip.d.ts +47 -0
  94. package/dist/core/tooltip.js +51 -0
  95. package/dist/core/topbar.d.ts +41 -0
  96. package/dist/core/topbar.js +52 -0
  97. package/dist/dssoca.config.d.ts +1 -1
  98. package/dist/dssoca.config.js +4 -0
  99. package/dist/index.d.ts +14 -1
  100. package/dist/index.js +9 -1
  101. package/dist/theme.css +47 -0
  102. package/dist/toast.svelte.d.ts +2 -2
  103. package/dist/toast.svelte.js +1 -1
  104. package/dist/tokens.css +47 -0
  105. package/dist/vanilla/accordion.js +28 -29
  106. package/dist/vanilla/checkbox.d.ts +8 -0
  107. package/dist/vanilla/checkbox.js +43 -0
  108. package/dist/vanilla/delegate.d.ts +2 -0
  109. package/dist/vanilla/delegate.js +9 -0
  110. package/dist/vanilla/fields.js +37 -32
  111. package/dist/vanilla/index.d.ts +4 -0
  112. package/dist/vanilla/index.js +16 -3
  113. package/dist/vanilla/menu.js +92 -86
  114. package/dist/vanilla/modal.d.ts +3 -1
  115. package/dist/vanilla/modal.js +65 -15
  116. package/dist/vanilla/popover.d.ts +10 -0
  117. package/dist/vanilla/popover.js +129 -0
  118. package/dist/vanilla/radio.d.ts +3 -0
  119. package/dist/vanilla/radio.js +54 -0
  120. package/dist/vanilla/segmented.js +49 -51
  121. package/dist/vanilla/switch.d.ts +2 -1
  122. package/dist/vanilla/switch.js +13 -8
  123. package/dist/vanilla/tabs.d.ts +2 -0
  124. package/dist/vanilla/tabs.js +87 -0
  125. package/dist/vanilla/tierlist.d.ts +1 -1
  126. package/dist/vanilla/tierlist.js +29 -25
  127. package/dist/vanilla/toast.d.ts +2 -2
  128. package/dist/vanilla/toast.js +16 -14
  129. package/dist/vanilla/tooltip.js +35 -23
  130. package/dist/vanilla.css +349 -42
  131. package/package.json +1 -1
  132. package/dist/chart-nav.d.ts +0 -13
  133. package/dist/toast-core.d.ts +0 -61
@@ -38,6 +38,26 @@
38
38
  import { resolveComponentSize, type Size } from '../config.js'
39
39
  import { shortcuts } from '../shortcuts.svelte.js'
40
40
  import { lockScroll, unlockScroll } from '../vanilla/modal.js'
41
+ import {
42
+ searchBackdropPress,
43
+ searchClosePress,
44
+ searchFilter,
45
+ searchFirstActive,
46
+ searchFlat,
47
+ searchGroups,
48
+ searchHover,
49
+ searchInputAttrs,
50
+ searchInputKey,
51
+ searchItemPress,
52
+ searchOptionAttrs,
53
+ searchShortcut,
54
+ searchStatus,
55
+ searchValidActive,
56
+ type SearchPaletteAction,
57
+ type SearchPaletteOpenDetails,
58
+ type SearchPaletteReason,
59
+ type SearchPaletteState,
60
+ } from '../core/search-palette.js'
41
61
 
42
62
  interface Props {
43
63
  /** Whether the palette is shown. Bindable — sync it from caller state. */
@@ -72,10 +92,11 @@
72
92
  size?: Size
73
93
  /** Called with the chosen item on Enter/click. Return false to keep the palette open. */
74
94
  onselect?: (item: T) => void | boolean
75
- /** Called after the palette opens. */
76
- onopen?: () => void
77
- /** Called after the palette closes (Esc, backdrop, selection). */
78
- onclose?: () => void
95
+ /** Called after the palette opens, with why (`details.reason`: shortcut · prop). */
96
+ onopen?: (details: SearchPaletteOpenDetails) => void
97
+ /** Called after the palette closes, with why (`details.reason`: escape · backdrop ·
98
+ * close-button · select · shortcut · prop). */
99
+ onclose?: (details: SearchPaletteOpenDetails) => void
79
100
  /** Replaces the default option row body (label + hint). */
80
101
  item?: Snippet<[T, { active: boolean }]>
81
102
  /** Replaces the default no-results state; receives the current query. */
@@ -115,50 +136,32 @@
115
136
  const listId = `${uid}-listbox`
116
137
  const optionId = (index: number) => `${uid}-option-${index}`
117
138
 
118
- // Case- and diacritic-insensitive fold ("São" matches "sao").
119
- const fold = (s: string) =>
120
- s
121
- .normalize('NFD')
122
- .replace(/[\u0300-\u036f]/g, '')
123
- .toLowerCase()
124
-
125
- const filtered = $derived.by(() => {
126
- if (!filter) return items
127
- const q = fold(query.trim())
128
- if (!q) return items
129
- return items.filter((it) => fold([it.label, ...(it.keywords ?? [])].join(' ')).includes(q))
130
- })
139
+ // Behaviour lives in `core/search-palette.ts`: filtering, grouping, the active-option
140
+ // movement, what a key does and why the palette opens/closes are decided there; this
141
+ // file holds the state and applies the result.
142
+ const filtered = $derived(filter ? searchFilter(items, query) : items)
131
143
 
132
144
  // Buckets in first-appearance order, ungrouped (label null) always first;
133
145
  // options carry a flat index so keyboard order equals DOM order.
134
- const groups = $derived.by(() => {
135
- const buckets: { label: string | null; bucket: T[] }[] = []
136
- for (const it of filtered) {
137
- const label = it.group ?? null
138
- let b = buckets.find((entry) => entry.label === label)
139
- if (!b) {
140
- b = { label, bucket: [] }
141
- buckets.push(b)
142
- }
143
- b.bucket.push(it)
144
- }
145
- buckets.sort((a, b) => Number(b.label === null) - Number(a.label === null))
146
- let index = 0
147
- return buckets.map(({ label, bucket }) => ({
148
- label,
149
- entries: bucket.map((entry) => ({ item: entry, index: index++ })),
150
- }))
151
- })
152
- const flat = $derived(groups.flatMap((g) => g.entries.map((e) => e.item)))
153
-
154
- function firstSelectable(): number {
155
- const i = flat.findIndex((it) => !it.disabled)
156
- return i === -1 ? 0 : i
146
+ const groups = $derived(searchGroups(filtered))
147
+ const flat = $derived(searchFlat(groups))
148
+
149
+ const palette = $derived<SearchPaletteState>({ open, active, items: flat })
150
+
151
+ // Why the next open/close happens. Set when the palette changes `open` itself (or the
152
+ // native dialog reports Esc through `cancel`); left null when the consumer flips the
153
+ // prop, which reports as 'prop'.
154
+ let pending: SearchPaletteReason | null = null
155
+ const takeReason = (): SearchPaletteReason => {
156
+ const reason = pending ?? 'prop'
157
+ pending = null
158
+ return reason
157
159
  }
158
160
 
159
161
  // Keep the active index valid as the list changes (typing, items swapped).
160
162
  $effect(() => {
161
- if (active >= flat.length || flat[active]?.disabled) active = firstSelectable()
163
+ const valid = searchValidActive(palette)
164
+ if (valid !== active) active = valid
162
165
  })
163
166
 
164
167
  // Mirror `open` onto the native dialog (same guard dance as Modal), then
@@ -169,7 +172,7 @@
169
172
  if (open && !el.open) {
170
173
  el.showModal()
171
174
  input?.focus()
172
- onopen?.()
175
+ onopen?.({ reason: takeReason() })
173
176
  } else if (!open && el.open) {
174
177
  el.close()
175
178
  }
@@ -186,7 +189,36 @@
186
189
 
187
190
  // Polite live text for the always-rendered status node: silent until the
188
191
  // user types, then the result count (or the no-results text).
189
- const status = $derived(query.trim() ? (flat.length ? resultsText(flat.length) : emptyText) : '')
192
+ const status = $derived(searchStatus(query, flat.length, resultsText, emptyText))
193
+
194
+ function run(action: SearchPaletteAction | null) {
195
+ if (!action) return
196
+ switch (action.type) {
197
+ case 'open':
198
+ pending = action.reason
199
+ open = true
200
+ break
201
+ case 'close':
202
+ pending = action.reason
203
+ open = false
204
+ break
205
+ case 'move':
206
+ active = action.index
207
+ void tick().then(() => optionAt(active)?.scrollIntoView({ block: 'nearest' }))
208
+ break
209
+ case 'activate':
210
+ // href options: click the real anchor so the router/native semantics
211
+ // apply — its click handler runs choose() for us.
212
+ if (action.via === 'anchor') optionAt(action.index)?.click()
213
+ else choose(action.index)
214
+ break
215
+ }
216
+ }
217
+
218
+ // Esc on the native dialog fires `cancel` right before `close`.
219
+ function onDialogCancel() {
220
+ pending = 'escape'
221
+ }
190
222
 
191
223
  // Native `close` (Esc, .close()) → sync state, optionally reset, notify.
192
224
  function onDialogClose() {
@@ -195,13 +227,26 @@
195
227
  query = ''
196
228
  active = 0
197
229
  }
198
- onclose?.()
230
+ onclose?.({ reason: takeReason() })
199
231
  }
200
232
 
201
- // A pointerdown landing on the <dialog> itself (not the inner panel) is a
202
- // backdrop hit (Modal's detection).
233
+ // A press that starts on the <dialog> itself (not the inner panel) is a
234
+ // backdrop hit (Modal's detection). Remembering where the press began avoids
235
+ // closing on a drag that started inside the panel and was released on the
236
+ // backdrop.
237
+ //
238
+ // The dialog closes on the `click`, not on the pointerdown: closing hands
239
+ // focus back to the opener, and if that happened on pointerdown the same
240
+ // gesture's mousedown would then land on the page and blur it, leaving focus
241
+ // on <body>.
242
+ let pressedBackdrop = false
203
243
  function onPointerDown(e: PointerEvent) {
204
- if (e.target === dialog) open = false
244
+ pressedBackdrop = e.target === dialog
245
+ }
246
+ function onClick(e: MouseEvent) {
247
+ const hit = pressedBackdrop && e.target === dialog
248
+ pressedBackdrop = false
249
+ run(searchBackdropPress(palette, hit))
205
250
  }
206
251
 
207
252
  // DS-0139: the toggle chord goes through the shortcut registry (id
@@ -218,71 +263,35 @@
218
263
  label: 'Open search',
219
264
  keys: shortcut,
220
265
  allowInInputs: true, // chorded — must fire while typing (incl. the palette's own input)
221
- onPress: () => {
222
- open = !open
223
- },
266
+ onPress: () => run(searchShortcut(open)),
224
267
  }
225
268
  const el = dialog
226
269
  return open && el ? shortcuts.add({ ...options, scope: 'focus' }, el) : shortcuts.add(options)
227
270
  })
228
271
 
229
- // Step to the next non-disabled option, wrapping; delta 0 re-validates.
230
- function move(delta: number, from = active) {
231
- const len = flat.length
232
- if (!len) return
233
- let i = from
234
- for (let step = 0; step < len; step++) {
235
- i = (i + delta + len) % len
236
- if (!flat[i].disabled) {
237
- active = i
238
- break
239
- }
240
- }
241
- void tick().then(() =>
242
- list?.querySelector(`#${CSS.escape(optionId(active))}`)?.scrollIntoView({ block: 'nearest' }),
243
- )
244
- }
245
-
246
272
  function onInputKeydown(e: KeyboardEvent) {
247
- if (e.isComposing) return
248
- if (e.key === 'ArrowDown') {
249
- e.preventDefault()
250
- move(1)
251
- } else if (e.key === 'ArrowUp') {
252
- e.preventDefault()
253
- move(-1)
254
- } else if (e.key === 'PageUp') {
255
- // Home/End stay native (caret editing in the textbox, per APG combobox);
256
- // PageUp/PageDown jump to the first/last option instead.
257
- e.preventDefault()
258
- move(1, -1)
259
- } else if (e.key === 'PageDown') {
260
- e.preventDefault()
261
- move(-1, flat.length)
262
- } else if (e.key === 'Enter') {
263
- e.preventDefault()
264
- const it = flat[active]
265
- if (!it || it.disabled) return
266
- // href options: click the real anchor so the router/native semantics
267
- // apply — its click handler runs choose() for us.
268
- if (it.href) anchorAt(active)?.click()
269
- else choose(it)
270
- } else if (e.key === 'Tab') {
271
- // Focus stays on the input while the palette is open.
272
- e.preventDefault()
273
- }
273
+ const result = searchInputKey(palette, e.key, e.isComposing)
274
+ if (!result) return
275
+ if (result.preventDefault) e.preventDefault()
276
+ run(result.action)
274
277
  }
275
278
 
276
- function anchorAt(index: number): HTMLAnchorElement | null {
277
- return list?.querySelector(`#${CSS.escape(optionId(index))}`) as HTMLAnchorElement | null
279
+ function optionAt(index: number): HTMLElement | null {
280
+ return list?.querySelector<HTMLElement>(`#${CSS.escape(optionId(index))}`) ?? null
278
281
  }
279
282
 
280
283
  // Shared select path for clicks and Enter; navigation (when any) has
281
284
  // already been dispatched on the anchor itself.
282
- function choose(it: T) {
283
- if (it.disabled) return
284
- const keep = onselect?.(it) === false
285
- if (!keep) open = false
285
+ function choose(index: number) {
286
+ const result = searchItemPress(palette, index)
287
+ if (!result) return
288
+ const keep = onselect?.(flat[index]) === false
289
+ if (!keep) run(result.close)
290
+ }
291
+
292
+ function hover(index: number) {
293
+ const next = searchHover(palette, index)
294
+ if (next !== null) active = next
286
295
  }
287
296
 
288
297
  // Focus must never leave the input (aria-activedescendant pattern):
@@ -295,7 +304,7 @@
295
304
  // the derived `flat` reflects the new query regardless of handler order.
296
305
  async function onQueryInput() {
297
306
  await tick()
298
- active = firstSelectable()
307
+ active = searchFirstActive(palette)
299
308
  }
300
309
  </script>
301
310
 
@@ -304,8 +313,10 @@
304
313
  class="ss-search-palette"
305
314
  data-size-variant={resolveComponentSize('SearchPalette', size)}
306
315
  aria-label={ariaLabel}
316
+ oncancel={onDialogCancel}
307
317
  onclose={onDialogClose}
308
318
  onpointerdown={onPointerDown}
319
+ onclick={onClick}
309
320
  >
310
321
  <div class="panel">
311
322
  <div class="bar">
@@ -319,17 +330,17 @@
319
330
  {placeholder}
320
331
  spellcheck="false"
321
332
  autocomplete="off"
322
- role="combobox"
323
333
  aria-label={ariaLabel}
324
- aria-autocomplete="list"
325
- aria-expanded={flat.length > 0}
326
334
  aria-controls={listId}
327
- aria-activedescendant={flat.length ? optionId(active) : undefined}
335
+ {...searchInputAttrs(flat.length, optionId(active))}
328
336
  />
329
337
  <!-- Touch exit for the full-screen small-viewport layout (no backdrop to
330
338
  tap there); hidden on wider screens, where Esc/backdrop close it. -->
331
- <button type="button" class="close" aria-label={closeLabel} onclick={() => (open = false)}
332
- >×</button
339
+ <button
340
+ type="button"
341
+ class="close"
342
+ aria-label={closeLabel}
343
+ onclick={() => run(searchClosePress(palette))}>×</button
333
344
  >
334
345
  </div>
335
346
 
@@ -359,15 +370,13 @@
359
370
  {#if it.href && !it.disabled}
360
371
  <a
361
372
  id={optionId(index)}
362
- role="option"
363
- aria-selected={index === active}
364
373
  class="row"
365
374
  class:active={index === active}
366
375
  href={it.href}
367
- tabindex="-1"
376
+ {...searchOptionAttrs({ index, active })}
368
377
  onpointerdown={onRowPointerDown}
369
- onmousemove={() => (active = index)}
370
- onclick={() => choose(it)}
378
+ onmousemove={() => hover(index)}
379
+ onclick={() => choose(index)}
371
380
  >
372
381
  {@render body(it, index)}
373
382
  </a>
@@ -375,18 +384,13 @@
375
384
  <button
376
385
  id={optionId(index)}
377
386
  type="button"
378
- role="option"
379
- aria-selected={index === active}
380
- aria-disabled={it.disabled || undefined}
381
387
  class="row"
382
388
  class:active={index === active}
383
389
  class:disabled={it.disabled}
384
- tabindex="-1"
390
+ {...searchOptionAttrs({ index, active, disabled: it.disabled })}
385
391
  onpointerdown={onRowPointerDown}
386
- onmousemove={() => {
387
- if (!it.disabled) active = index
388
- }}
389
- onclick={() => choose(it)}
392
+ onmousemove={() => hover(index)}
393
+ onclick={() => choose(index)}
390
394
  >
391
395
  {@render body(it, index)}
392
396
  </button>
@@ -16,6 +16,7 @@ export interface SearchPaletteItem {
16
16
  }
17
17
  import { type Snippet } from 'svelte';
18
18
  import { type Size } from '../config.js';
19
+ import { type SearchPaletteOpenDetails } from '../core/search-palette.js';
19
20
  declare function $$render<T extends SearchPaletteItem>(): {
20
21
  props: {
21
22
  /** Whether the palette is shown. Bindable — sync it from caller state. */
@@ -50,10 +51,11 @@ declare function $$render<T extends SearchPaletteItem>(): {
50
51
  size?: Size;
51
52
  /** Called with the chosen item on Enter/click. Return false to keep the palette open. */
52
53
  onselect?: (item: T) => void | boolean;
53
- /** Called after the palette opens. */
54
- onopen?: () => void;
55
- /** Called after the palette closes (Esc, backdrop, selection). */
56
- onclose?: () => void;
54
+ /** Called after the palette opens, with why (`details.reason`: shortcut · prop). */
55
+ onopen?: (details: SearchPaletteOpenDetails) => void;
56
+ /** Called after the palette closes, with why (`details.reason`: escape · backdrop ·
57
+ * close-button · select · shortcut · prop). */
58
+ onclose?: (details: SearchPaletteOpenDetails) => void;
57
59
  /** Replaces the default option row body (label + hint). */
58
60
  item?: Snippet<[T, {
59
61
  active: boolean;
@@ -21,14 +21,28 @@
21
21
  // ─────────────────────────────────────────────────────────────────────────
22
22
  import Icon from './Icon.svelte'
23
23
  import { resolveComponentSize, type Size } from '../config.js'
24
+ import {
25
+ segmentedGroupAttrs,
26
+ segmentedItemAttrs,
27
+ segmentedKey,
28
+ segmentedPress,
29
+ segmentedTabStop,
30
+ type SegmentedAction,
31
+ type SegmentedChangeDetails,
32
+ type SegmentedState,
33
+ } from '../core/segmented.js'
24
34
 
25
35
  interface Props {
26
36
  /** Mutually-exclusive options: { value, label, icon?, disabled? }. */
27
37
  options?: SegmentOption[]
28
38
  /** Selected option value (bindable). */
29
39
  value?: string
30
- /** Fired when the selection changes (after `value` updates). */
31
- onChange?: (value: string) => void
40
+ /**
41
+ * Fired when the selection changes (after `value` updates). `details.reason`
42
+ * says what moved it: `'press'` (a segment was clicked) or `'key'` (arrows /
43
+ * Home / End).
44
+ */
45
+ onChange?: (value: string, details: SegmentedChangeDetails) => void
32
46
  /** Stretch segments to equal widths and fill the container. */
33
47
  fullWidth?: boolean
34
48
  /**
@@ -68,71 +82,39 @@
68
82
  options.map((o, i) => (isOptionDisabled(o) ? -1 : i)).filter((i) => i >= 0),
69
83
  )
70
84
 
85
+ // The group's state as `core/segmented.ts` sees it. Every decision (what a press
86
+ // or a key selects, which segment owns the tab stop) is made there, shared with
87
+ // vanilla/segmented.ts (DS-0197).
88
+ const group = $derived<SegmentedState>({
89
+ selected: options.findIndex((o) => o.value === value),
90
+ enabled: enabledIndexes,
91
+ })
92
+
71
93
  /** Index that owns the single tab stop (roving tabindex). The selected option
72
94
  * when it's enabled; otherwise the first enabled option; -1 when none. */
73
- const tabbableIndex = $derived.by(() => {
74
- const sel = options.findIndex((o) => o.value === value && !isOptionDisabled(o))
75
- if (sel >= 0) return sel
76
- return enabledIndexes[0] ?? -1
77
- })
95
+ const tabbableIndex = $derived(segmentedTabStop(group))
78
96
 
79
- function select(index: number) {
80
- const opt = options[index]
81
- if (!opt || isOptionDisabled(opt)) return
82
- if (opt.value !== value) {
97
+ /** Apply a core action: move the selection (and focus, for the keyboard). */
98
+ function run(action: SegmentedAction | null) {
99
+ if (!action) return
100
+ const opt = options[action.index]
101
+ if (action.changed && opt) {
83
102
  value = opt.value
84
- onChange?.(opt.value)
103
+ onChange?.(opt.value, { reason: action.reason })
85
104
  }
86
- }
87
-
88
- function focusIndex(index: number) {
89
- segments[index]?.focus()
90
- }
91
-
92
- /** Next/previous enabled option index, wrapping around the row. */
93
- function step(from: number, dir: 1 | -1): number {
94
- if (enabledIndexes.length === 0) return from
95
- const pos = enabledIndexes.indexOf(from)
96
- // `from` might be disabled (shouldn't be focusable) — fall back to an edge.
97
- const base = pos === -1 ? (dir === 1 ? -1 : 0) : pos
98
- const next = (base + dir + enabledIndexes.length) % enabledIndexes.length
99
- return enabledIndexes[next]
105
+ if (action.focus) segments[action.index]?.focus()
100
106
  }
101
107
 
102
108
  function onKeydown(e: KeyboardEvent, index: number) {
103
- let target: number
104
109
  // RTL: the row is laid out right-to-left, so ArrowLeft points at the NEXT
105
110
  // segment. Read the computed direction at keydown time (it inherits from
106
- // any ancestor `dir`). Up/Down stay logical (prev/next).
111
+ // any ancestor `dir`) and hand it to the core, which stays DOM-free.
107
112
  const rtl = getComputedStyle(e.currentTarget as Element).direction === 'rtl'
108
- const right: 1 | -1 = rtl ? -1 : 1
109
- switch (e.key) {
110
- case 'ArrowRight':
111
- target = step(index, right)
112
- break
113
- case 'ArrowLeft':
114
- target = step(index, right === 1 ? -1 : 1)
115
- break
116
- case 'ArrowDown':
117
- target = step(index, 1)
118
- break
119
- case 'ArrowUp':
120
- target = step(index, -1)
121
- break
122
- case 'Home':
123
- target = enabledIndexes[0] ?? -1
124
- break
125
- case 'End':
126
- target = enabledIndexes[enabledIndexes.length - 1] ?? -1
127
- break
128
- default:
129
- return
130
- }
131
- if (target < 0) return
132
- e.preventDefault()
113
+ const result = segmentedKey(group, index, e.key, rtl)
114
+ if (!result) return
115
+ if (result.preventDefault) e.preventDefault()
133
116
  // radiogroup semantics: moving focus also moves the selection.
134
- select(target)
135
- focusIndex(target)
117
+ run(result.action)
136
118
  }
137
119
 
138
120
  const sizeAttr = $derived(resolveComponentSize('SegmentedControl', size))
@@ -141,23 +123,20 @@
141
123
  <div
142
124
  class="ss-segmented"
143
125
  class:full={fullWidth}
144
- role="radiogroup"
145
126
  aria-label={label}
146
- aria-disabled={disabled ? 'true' : undefined}
147
127
  data-size-variant={sizeAttr}
128
+ {...segmentedGroupAttrs(disabled)}
148
129
  >
149
130
  {#each options as opt, i (opt.value)}
150
- {@const selected = opt.value === value}
131
+ {@const selected = i === group.selected}
151
132
  <button
152
133
  bind:this={segments[i]}
153
134
  type="button"
154
135
  class="segment"
155
136
  class:selected
156
- role="radio"
157
- aria-checked={selected}
158
- tabindex={i === tabbableIndex ? 0 : -1}
137
+ {...segmentedItemAttrs(i, group.selected, tabbableIndex)}
159
138
  disabled={isOptionDisabled(opt)}
160
- onclick={() => select(i)}
139
+ onclick={() => run(segmentedPress(group, i))}
161
140
  onkeydown={(e) => onKeydown(e, i)}
162
141
  >
163
142
  {#if opt.icon}<Icon name={opt.icon} class="ic" />{/if}
@@ -8,13 +8,18 @@ export interface SegmentOption {
8
8
  disabled?: boolean;
9
9
  }
10
10
  import { type Size } from '../config.js';
11
+ import { type SegmentedChangeDetails } from '../core/segmented.js';
11
12
  interface Props {
12
13
  /** Mutually-exclusive options: { value, label, icon?, disabled? }. */
13
14
  options?: SegmentOption[];
14
15
  /** Selected option value (bindable). */
15
16
  value?: string;
16
- /** Fired when the selection changes (after `value` updates). */
17
- onChange?: (value: string) => void;
17
+ /**
18
+ * Fired when the selection changes (after `value` updates). `details.reason`
19
+ * says what moved it: `'press'` (a segment was clicked) or `'key'` (arrows /
20
+ * Home / End).
21
+ */
22
+ onChange?: (value: string, details: SegmentedChangeDetails) => void;
18
23
  /** Stretch segments to equal widths and fill the container. */
19
24
  fullWidth?: boolean;
20
25
  /**