dssoca 0.19.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 (155) hide show
  1. package/README.md +3 -3
  2. package/dist/components/Accordion.svelte +30 -54
  3. package/dist/components/Badge.svelte +12 -7
  4. package/dist/components/Badge.svelte.d.ts +2 -1
  5. package/dist/components/BottomNav.svelte +18 -28
  6. package/dist/components/BoxPlot.svelte +34 -7
  7. package/dist/components/BumpChart.svelte +65 -26
  8. package/dist/components/Button.svelte +23 -1
  9. package/dist/components/Button.svelte.d.ts +5 -1
  10. package/dist/components/Card.svelte +32 -14
  11. package/dist/components/Card.svelte.d.ts +4 -1
  12. package/dist/components/Chart.svelte +166 -32
  13. package/dist/components/Chart.svelte.d.ts +13 -1
  14. package/dist/components/Checkbox.svelte +210 -0
  15. package/dist/components/Checkbox.svelte.d.ts +38 -0
  16. package/dist/components/DateField.svelte +1 -1
  17. package/dist/components/FileDrop.svelte +113 -77
  18. package/dist/components/FileDrop.svelte.d.ts +12 -2
  19. package/dist/components/Heatmap.svelte +128 -50
  20. package/dist/components/Heatmap.svelte.d.ts +2 -1
  21. package/dist/components/Icon.svelte +12 -3
  22. package/dist/components/Icon.svelte.d.ts +4 -0
  23. package/dist/components/Image.svelte +44 -44
  24. package/dist/components/Input.svelte +11 -6
  25. package/dist/components/Link.svelte +25 -14
  26. package/dist/components/LogStream.svelte +48 -33
  27. package/dist/components/Menu.svelte +81 -102
  28. package/dist/components/Menu.svelte.d.ts +8 -2
  29. package/dist/components/MetricTile.svelte +11 -1
  30. package/dist/components/MetricTile.svelte.d.ts +6 -0
  31. package/dist/components/Modal.svelte +108 -21
  32. package/dist/components/Modal.svelte.d.ts +18 -5
  33. package/dist/components/NumberField.svelte +44 -22
  34. package/dist/components/Pagination.svelte +54 -41
  35. package/dist/components/Pagination.svelte.d.ts +6 -2
  36. package/dist/components/Popover.svelte +247 -0
  37. package/dist/components/Popover.svelte.d.ts +36 -0
  38. package/dist/components/RadioGroup.svelte +227 -0
  39. package/dist/components/RadioGroup.svelte.d.ts +42 -0
  40. package/dist/components/ScatterPlot.svelte +42 -7
  41. package/dist/components/SearchPalette.svelte +216 -136
  42. package/dist/components/SearchPalette.svelte.d.ts +11 -4
  43. package/dist/components/SegmentedControl.svelte +47 -59
  44. package/dist/components/SegmentedControl.svelte.d.ts +7 -2
  45. package/dist/components/Select.svelte +3 -4
  46. package/dist/components/ServiceCard.svelte +105 -68
  47. package/dist/components/ServiceCard.svelte.d.ts +8 -3
  48. package/dist/components/ShortcutsHelp.svelte +49 -48
  49. package/dist/components/Sidebar.svelte +88 -117
  50. package/dist/components/Switch.svelte +24 -18
  51. package/dist/components/Switch.svelte.d.ts +6 -2
  52. package/dist/components/Table.svelte +29 -44
  53. package/dist/components/Table.svelte.d.ts +6 -2
  54. package/dist/components/Tabs.svelte +223 -0
  55. package/dist/components/Tabs.svelte.d.ts +51 -0
  56. package/dist/components/Textarea.svelte +1 -1
  57. package/dist/components/TierList.svelte +45 -49
  58. package/dist/components/TierList.svelte.d.ts +7 -3
  59. package/dist/components/Toaster.svelte +154 -32
  60. package/dist/components/Tooltip.svelte +122 -18
  61. package/dist/components/Tooltip.svelte.d.ts +1 -1
  62. package/dist/components/Topbar.svelte +29 -49
  63. package/dist/core/accordion.d.ts +32 -0
  64. package/dist/core/accordion.js +53 -0
  65. package/dist/core/behaviors/focusable.d.ts +5 -0
  66. package/dist/core/behaviors/focusable.js +14 -0
  67. package/dist/core/behaviors/roving.d.ts +13 -0
  68. package/dist/core/behaviors/roving.js +29 -0
  69. package/dist/core/bottom-nav.d.ts +36 -0
  70. package/dist/core/bottom-nav.js +33 -0
  71. package/dist/core/chart-nav.d.ts +53 -0
  72. package/dist/core/chart-nav.js +62 -0
  73. package/dist/core/checkbox.d.ts +36 -0
  74. package/dist/core/checkbox.js +29 -0
  75. package/dist/core/file-drop.d.ts +75 -0
  76. package/dist/core/file-drop.js +101 -0
  77. package/dist/core/image.d.ts +59 -0
  78. package/dist/core/image.js +64 -0
  79. package/dist/core/input.d.ts +21 -0
  80. package/dist/core/input.js +17 -0
  81. package/dist/core/log-stream.d.ts +51 -0
  82. package/dist/core/log-stream.js +50 -0
  83. package/dist/core/menu.d.ts +70 -0
  84. package/dist/core/menu.js +84 -0
  85. package/dist/core/modal.d.ts +59 -0
  86. package/dist/core/modal.js +44 -0
  87. package/dist/core/number-field.d.ts +47 -0
  88. package/dist/core/number-field.js +64 -0
  89. package/dist/core/pagination.d.ts +59 -0
  90. package/dist/core/pagination.js +65 -0
  91. package/dist/core/popover.d.ts +68 -0
  92. package/dist/core/popover.js +55 -0
  93. package/dist/core/radio.d.ts +36 -0
  94. package/dist/core/radio.js +48 -0
  95. package/dist/core/search-palette.d.ts +124 -0
  96. package/dist/core/search-palette.js +149 -0
  97. package/dist/core/segmented.d.ts +46 -0
  98. package/dist/core/segmented.js +66 -0
  99. package/dist/core/shortcuts-help.d.ts +56 -0
  100. package/dist/core/shortcuts-help.js +50 -0
  101. package/dist/core/sidebar.d.ts +63 -0
  102. package/dist/core/sidebar.js +84 -0
  103. package/dist/core/switch.d.ts +19 -0
  104. package/dist/core/switch.js +11 -0
  105. package/dist/core/table.d.ts +40 -0
  106. package/dist/core/table.js +57 -0
  107. package/dist/core/tabs.d.ts +84 -0
  108. package/dist/core/tabs.js +90 -0
  109. package/dist/{tierlist-core.d.ts → core/tierlist.d.ts} +77 -5
  110. package/dist/{tierlist-core.js → core/tierlist.js} +64 -7
  111. package/dist/core/toast.d.ts +112 -0
  112. package/dist/{toast-core.js → core/toast.js} +48 -2
  113. package/dist/core/tooltip.d.ts +47 -0
  114. package/dist/core/tooltip.js +51 -0
  115. package/dist/core/topbar.d.ts +41 -0
  116. package/dist/core/topbar.js +52 -0
  117. package/dist/dssoca.config.d.ts +1 -1
  118. package/dist/dssoca.config.js +4 -0
  119. package/dist/icons.d.ts +19 -2
  120. package/dist/icons.js +42 -3
  121. package/dist/index.d.ts +15 -2
  122. package/dist/index.js +10 -2
  123. package/dist/theme.css +47 -0
  124. package/dist/toast.svelte.d.ts +2 -2
  125. package/dist/toast.svelte.js +1 -1
  126. package/dist/tokens.css +47 -0
  127. package/dist/vanilla/accordion.js +35 -32
  128. package/dist/vanilla/checkbox.d.ts +8 -0
  129. package/dist/vanilla/checkbox.js +43 -0
  130. package/dist/vanilla/delegate.d.ts +2 -0
  131. package/dist/vanilla/delegate.js +9 -0
  132. package/dist/vanilla/fields.js +46 -28
  133. package/dist/vanilla/index.d.ts +5 -1
  134. package/dist/vanilla/index.js +17 -4
  135. package/dist/vanilla/menu.js +101 -84
  136. package/dist/vanilla/modal.d.ts +7 -1
  137. package/dist/vanilla/modal.js +120 -15
  138. package/dist/vanilla/popover.d.ts +10 -0
  139. package/dist/vanilla/popover.js +129 -0
  140. package/dist/vanilla/radio.d.ts +3 -0
  141. package/dist/vanilla/radio.js +54 -0
  142. package/dist/vanilla/segmented.js +50 -43
  143. package/dist/vanilla/switch.d.ts +2 -1
  144. package/dist/vanilla/switch.js +13 -8
  145. package/dist/vanilla/tabs.d.ts +2 -0
  146. package/dist/vanilla/tabs.js +87 -0
  147. package/dist/vanilla/tierlist.d.ts +1 -1
  148. package/dist/vanilla/tierlist.js +34 -34
  149. package/dist/vanilla/toast.d.ts +13 -5
  150. package/dist/vanilla/toast.js +132 -16
  151. package/dist/vanilla/tooltip.d.ts +5 -1
  152. package/dist/vanilla/tooltip.js +78 -19
  153. package/dist/vanilla.css +675 -160
  154. package/package.json +1 -1
  155. package/dist/toast-core.d.ts +0 -61
@@ -1,8 +1,17 @@
1
1
  <script lang="ts">
2
2
  import { fly } from 'svelte/transition'
3
3
  import { prefersReducedMotion } from 'svelte/motion'
4
+ import { untrack } from 'svelte'
4
5
  import { toasts, type Toast, type ToastKind } from '../toast.svelte.js'
5
6
  import { resolveComponentSize, type Size } from '../config.js'
7
+ import {
8
+ toastActionDismisses,
9
+ toastFocusAfterDismiss,
10
+ toastKey,
11
+ toastPressStartsSwipe,
12
+ toastShouldResume,
13
+ toastSwipeDismisses,
14
+ } from '../core/toast.js'
6
15
 
7
16
  type Position =
8
17
  | 'top-right'
@@ -20,6 +29,7 @@
20
29
  }
21
30
 
22
31
  let { size, position = 'top-right' }: Props = $props()
32
+ const uid = $props.id()
23
33
 
24
34
  const glyph: Record<ToastKind, string> = { success: '✓', error: '✕', info: 'i', loading: '' }
25
35
  // Honor prefers-reduced-motion: collapse the fly transition to an instant swap.
@@ -43,7 +53,16 @@
43
53
  })
44
54
  const flyDuration = $derived(reduce ? 0 : tokenDuration)
45
55
  // Bottom anchors slide in from below; centered ones come straight down/up.
46
- const flyX = $derived(position.endsWith('center') ? 0 : position.endsWith('left') ? -16 : 16)
56
+ // `left`/`right` positions are logical (inline start/end), so in RTL the
57
+ // horizontal slide mirrors with them.
58
+ const rtl = $derived(
59
+ !!rootEl &&
60
+ typeof getComputedStyle === 'function' &&
61
+ getComputedStyle(rootEl).direction === 'rtl',
62
+ )
63
+ const flyX = $derived(
64
+ position.endsWith('center') ? 0 : (position.endsWith('left') ? -16 : 16) * (rtl ? -1 : 1),
65
+ )
47
66
  const flyY = $derived(position.endsWith('center') ? (position.startsWith('top') ? -16 : 16) : 0)
48
67
 
49
68
  // ── swipe-to-dismiss ──────────────────────────────────────────────
@@ -52,12 +71,9 @@
52
71
  let dragStartX = 0
53
72
  let dragPointer: number | null = null
54
73
 
55
- const SWIPE_THRESHOLD = 64
56
-
57
74
  function onPointerDown(e: PointerEvent, t: Toast) {
58
- if (reduce || e.button !== 0) return
59
- // Ignore drags that start on the controls (buttons own their clicks).
60
- if ((e.target as HTMLElement).closest('button')) return
75
+ const onControl = (e.target as HTMLElement).closest('button') !== null
76
+ if (!toastPressStartsSwipe({ button: e.button, onControl, reducedMotion: reduce })) return
61
77
  dragId = t.id
62
78
  dragDx = 0
63
79
  dragStartX = e.clientX
@@ -73,27 +89,110 @@
73
89
 
74
90
  function endDrag(e: PointerEvent, t: Toast) {
75
91
  if (dragId !== t.id || e.pointerId !== dragPointer) return
76
- const passed = Math.abs(dragDx) >= SWIPE_THRESHOLD
92
+ const passed = toastSwipeDismisses(dragDx)
77
93
  dragId = null
78
94
  dragPointer = null
79
95
  if (passed) toasts.dismiss(t.id)
80
96
  else {
81
97
  dragDx = 0
82
- toasts.resume(t.id)
98
+ resumeIfFree(t, holdsFocus(e.currentTarget as HTMLElement))
83
99
  }
84
100
  }
85
101
 
86
102
  function runAction(t: Toast) {
87
- const keep = t.action?.onClick() === false
88
- if (!keep) toasts.dismiss(t.id)
103
+ if (toastActionDismisses(t.action?.onClick())) toasts.dismiss(t.id)
89
104
  }
90
105
 
91
106
  function onKeydown(e: KeyboardEvent, t: Toast) {
92
- if (e.key === 'Escape') {
93
- e.stopPropagation()
94
- toasts.dismiss(t.id)
107
+ const result = toastKey(e.key)
108
+ if (!result) return
109
+ if (result.stopPropagation) e.stopPropagation()
110
+ toasts.dismiss(t.id)
111
+ }
112
+
113
+ // ── pause / resume ────────────────────────────────────────────────
114
+ // Hover OR keyboard focus holds the timer; it only runs again once the
115
+ // pointer has left AND focus is outside the toast (a pointerleave must not
116
+ // restart the clock under a keyboard user, and vice versa).
117
+ let hoverId: number | null = null
118
+
119
+ const holdsFocus = (el: HTMLElement) => el.contains(document.activeElement)
120
+
121
+ function resumeIfFree(t: Toast, focused: boolean) {
122
+ const hold = { hovered: hoverId === t.id, focused, dragging: dragId === t.id }
123
+ if (toastShouldResume(hold)) toasts.resume(t.id)
124
+ }
125
+
126
+ function onPointerEnter(t: Toast) {
127
+ hoverId = t.id
128
+ toasts.pause(t.id)
129
+ }
130
+
131
+ function onPointerLeave(e: PointerEvent, t: Toast) {
132
+ if (hoverId === t.id) hoverId = null
133
+ resumeIfFree(t, holdsFocus(e.currentTarget as HTMLElement))
134
+ }
135
+
136
+ function onFocusOut(e: FocusEvent, t: Toast) {
137
+ const el = e.currentTarget as HTMLElement
138
+ // Focus is only leaving when it lands outside the toast.
139
+ resumeIfFree(t, el.contains(e.relatedTarget as Node | null))
140
+ }
141
+
142
+ // ── focus management ──────────────────────────────────────────────
143
+ // Dismissing the toast that holds focus would drop focus to <body>. Before
144
+ // the node goes, move focus to a neighbouring toast, or back to where focus
145
+ // came from before it entered the stack.
146
+ // Plain (non-reactive) bookkeeping: read only inside the pre-effect below.
147
+ // eslint-disable-next-line svelte/prefer-svelte-reactivity
148
+ const toastEls = new Map<number, HTMLElement>()
149
+ let order: number[] = []
150
+ let returnFocus: HTMLElement | null = null
151
+
152
+ function track(id: number) {
153
+ return (el: HTMLElement) => {
154
+ toastEls.set(id, el)
155
+ return () => {
156
+ if (toastEls.get(id) === el) toastEls.delete(id)
157
+ }
158
+ }
159
+ }
160
+
161
+ function onRootFocusIn(e: FocusEvent) {
162
+ const from = e.relatedTarget as HTMLElement | null
163
+ if (from && rootEl && !rootEl.contains(from)) returnFocus = from
164
+ }
165
+
166
+ $effect.pre(() => {
167
+ const ids = toasts.items.map((t) => t.id)
168
+ untrack(() => {
169
+ const alive = new Set(ids)
170
+ const active = typeof document === 'undefined' ? null : document.activeElement
171
+ if (active && rootEl?.contains(active)) {
172
+ const holder = order.find((id) => !alive.has(id) && toastEls.get(id)?.contains(active))
173
+ if (holder !== undefined) moveFocusFrom(holder, alive)
174
+ }
175
+ order = ids
176
+ })
177
+ })
178
+
179
+ function moveFocusFrom(id: number, alive: Set<number>) {
180
+ const next = toastFocusAfterDismiss(order, id, (n) => alive.has(n) && toastEls.has(n))
181
+ if (next !== null) {
182
+ toastEls.get(next)!.focus()
183
+ } else if (returnFocus?.isConnected) {
184
+ returnFocus.focus()
185
+ returnFocus = null
95
186
  }
96
187
  }
188
+
189
+ // Screen-reader announcements go through two live regions that are always
190
+ // rendered (a node inserted already carrying role=status/alert is often
191
+ // skipped by NVDA/JAWS). Each visible toast mirrors its message into the
192
+ // polite (status) or assertive (alert, errors) region; the visual toasts
193
+ // themselves carry no live semantics, so nothing is announced twice.
194
+ const politeItems = $derived(toasts.items.filter((t) => t.kind !== 'error'))
195
+ const assertiveItems = $derived(toasts.items.filter((t) => t.kind === 'error'))
97
196
  </script>
98
197
 
99
198
  <div
@@ -103,21 +202,31 @@
103
202
  data-position={position}
104
203
  data-size-variant={resolveComponentSize('Toaster', size)}
105
204
  bind:this={rootEl}
205
+ onfocusin={onRootFocusIn}
106
206
  >
207
+ <div class="live" role="status" aria-live="polite">
208
+ {#each politeItems as t (t.id)}<p>{t.message}</p>{/each}
209
+ </div>
210
+ <div class="live" role="alert" aria-live="assertive">
211
+ {#each assertiveItems as t (t.id)}<p>{t.message}</p>{/each}
212
+ </div>
107
213
  {#each toasts.items as t (t.id)}
214
+ <!-- The pointer/key handlers are progressive extras (pause, swipe, Escape); the buttons inside are the real controls. -->
215
+ <!-- svelte-ignore a11y_no_noninteractive_element_interactions -->
108
216
  <div
109
217
  class="ss-toast {t.kind}"
110
218
  class:dragging={dragId === t.id}
111
- role={t.kind === 'error' ? 'alert' : 'status'}
112
- aria-live={t.kind === 'error' ? 'assertive' : 'polite'}
219
+ role="group"
220
+ aria-labelledby="{uid}-msg-{t.id}"
113
221
  tabindex="-1"
222
+ {@attach track(t.id)}
114
223
  style:transform={dragId === t.id ? `translateX(${dragDx}px)` : undefined}
115
224
  style:opacity={dragId === t.id ? Math.max(0.3, 1 - Math.abs(dragDx) / 200) : undefined}
116
225
  transition:fly={{ x: flyX, y: flyY, duration: flyDuration }}
117
- onpointerenter={() => toasts.pause(t.id)}
118
- onpointerleave={() => dragId !== t.id && toasts.resume(t.id)}
226
+ onpointerenter={() => onPointerEnter(t)}
227
+ onpointerleave={(e) => onPointerLeave(e, t)}
119
228
  onfocusin={() => toasts.pause(t.id)}
120
- onfocusout={() => toasts.resume(t.id)}
229
+ onfocusout={(e) => onFocusOut(e, t)}
121
230
  onpointerdown={(e) => onPointerDown(e, t)}
122
231
  onpointermove={(e) => onPointerMove(e, t)}
123
232
  onpointerup={(e) => endDrag(e, t)}
@@ -129,7 +238,7 @@
129
238
  {:else}
130
239
  <span class="ic" aria-hidden="true">{glyph[t.kind]}</span>
131
240
  {/if}
132
- <span class="msg">{t.message}</span>
241
+ <span class="msg" id="{uid}-msg-{t.id}">{t.message}</span>
133
242
  {#if t.action}
134
243
  <button class="action" type="button" onclick={() => runAction(t)}>
135
244
  {t.action.label}
@@ -148,14 +257,13 @@
148
257
  display: flex;
149
258
  flex-direction: column;
150
259
  gap: var(--ss-s-2);
151
- max-width: min(360px, 100vw - var(--ss-s-8));
260
+ max-inline-size: min(360px, 100vw - var(--ss-s-8));
152
261
  pointer-events: none;
153
262
  top: var(--ss-s-4);
154
- right: var(--ss-s-4);
263
+ inset-inline-end: var(--ss-s-4);
155
264
  }
156
265
  .ss-toaster[data-position=top-left] {
157
- right: auto;
158
- left: var(--ss-s-4);
266
+ inset-inline: var(--ss-s-4) auto;
159
267
  }
160
268
  .ss-toaster[data-position=bottom-right] {
161
269
  top: auto;
@@ -164,24 +272,34 @@
164
272
  }
165
273
  .ss-toaster[data-position=bottom-left] {
166
274
  top: auto;
167
- right: auto;
168
275
  bottom: var(--ss-s-4);
169
- left: var(--ss-s-4);
276
+ inset-inline: var(--ss-s-4) auto;
170
277
  flex-direction: column-reverse;
171
278
  }
172
279
  .ss-toaster[data-position=top-center] {
173
- right: auto;
280
+ inset-inline: auto;
174
281
  left: 50%;
175
282
  transform: translateX(-50%);
176
283
  }
177
284
  .ss-toaster[data-position=bottom-center] {
178
285
  top: auto;
179
- right: auto;
180
286
  bottom: var(--ss-s-4);
287
+ inset-inline: auto;
181
288
  left: 50%;
182
289
  transform: translateX(-50%);
183
290
  flex-direction: column-reverse;
184
291
  }
292
+ .ss-toaster .live {
293
+ position: absolute;
294
+ inline-size: 1px;
295
+ block-size: 1px;
296
+ padding: 0;
297
+ margin: -1px;
298
+ overflow: hidden;
299
+ clip: rect(0 0 0 0);
300
+ white-space: nowrap;
301
+ border: 0;
302
+ }
185
303
 
186
304
  .ss-toast {
187
305
  pointer-events: auto;
@@ -191,7 +309,7 @@
191
309
  padding: var(--ss-s-3) var(--ss-s-4);
192
310
  background: var(--ss-bg-elev);
193
311
  border: 1px solid var(--ss-line);
194
- border-left: 3px solid var(--ss-fg-faint);
312
+ border-inline-start: 3px solid var(--ss-fg-faint);
195
313
  box-shadow: var(--ss-shadow-pop);
196
314
  font-family: var(--ss-font-mono);
197
315
  font-size: var(--ss-ui-md);
@@ -207,16 +325,16 @@
207
325
  transition: transform var(--ss-dur-fast) var(--ss-ease);
208
326
  }
209
327
  .ss-toast.success {
210
- border-left-color: var(--ss-primary);
328
+ border-inline-start-color: var(--ss-primary);
211
329
  }
212
330
  .ss-toast.error {
213
- border-left-color: var(--ss-red);
331
+ border-inline-start-color: var(--ss-red);
214
332
  }
215
333
  .ss-toast.info {
216
- border-left-color: var(--ss-cyan);
334
+ border-inline-start-color: var(--ss-cyan);
217
335
  }
218
336
  .ss-toast.loading {
219
- border-left-color: var(--ss-fg-faint);
337
+ border-inline-start-color: var(--ss-fg-faint);
220
338
  }
221
339
  .ss-toast .ic {
222
340
  display: inline-flex;
@@ -280,6 +398,10 @@
280
398
  font-size: var(--ss-size-h3);
281
399
  line-height: 1;
282
400
  padding: 0;
401
+ display: inline-grid;
402
+ place-items: center;
403
+ min-inline-size: var(--ss-s-6);
404
+ min-block-size: var(--ss-s-6);
283
405
  }
284
406
  .ss-toast .x:hover {
285
407
  color: var(--ss-fg);
@@ -1,6 +1,15 @@
1
1
  <script lang="ts">
2
2
  import type { Snippet } from 'svelte'
3
3
  import { resolveComponentSize, type Size } from '../config.js'
4
+ import {
5
+ tooltipDescribedBy,
6
+ tooltipFocusIn,
7
+ tooltipFocusOut,
8
+ tooltipKey,
9
+ tooltipPointerEnter,
10
+ tooltipPointerLeave,
11
+ type TooltipAction,
12
+ } from '../core/tooltip.js'
4
13
 
5
14
  /** Side the tooltip attaches to relative to the trigger. */
6
15
  type Placement = 'top' | 'bottom' | 'left' | 'right'
@@ -10,7 +19,7 @@
10
19
  * Tooltip content (the accessible description of the trigger): a plain string, or a
11
20
  * snippet for a small rendered template. Snippets must stay **phrasing content**
12
21
  * (`code`, `strong`, `kbd`, `br`, `Icon`, …) and must not contain interactive
13
- * elements — the tip is `pointer-events: none` and hidden while closed, per the
22
+ * elements — the tip is never focusable and is hidden while closed, per the
14
23
  * WAI-ARIA tooltip pattern.
15
24
  */
16
25
  text: string | Snippet
@@ -37,6 +46,7 @@
37
46
  const tipId = $props.id()
38
47
 
39
48
  let open = $state(false)
49
+ let rootEl = $state<HTMLElement | undefined>()
40
50
  let triggerEl = $state<HTMLElement | undefined>()
41
51
  let tipEl = $state<HTMLElement | undefined>()
42
52
 
@@ -47,19 +57,76 @@
47
57
  /** Cross-axis nudge in px (x for top/bottom, y for left/right). */
48
58
  let shift = $state(0)
49
59
 
50
- function show() {
51
- open = true
60
+ /** Focusable elements inside the trigger snippet, in the order a hover falls back on. */
61
+ const FOCUSABLE =
62
+ 'a[href], button, input, select, textarea, summary, [tabindex]:not([tabindex="-1"]), [contenteditable="true"]'
63
+
64
+ /**
65
+ * The element that carries `aria-describedby` while open: the focused trigger on
66
+ * focusin, else the first focusable element in the trigger (a hover), else the
67
+ * trigger's first element. The wrapper `<span>` is never focused, so a description on
68
+ * it would never be read (DS-0163).
69
+ */
70
+ let describedEl: HTMLElement | null = null
71
+
72
+ function describedTarget(from?: EventTarget | null): HTMLElement | null {
73
+ if (!triggerEl) return null
74
+ if (from instanceof HTMLElement && from !== triggerEl && triggerEl.contains(from)) return from
75
+ return (
76
+ triggerEl.querySelector<HTMLElement>(FOCUSABLE) ??
77
+ (triggerEl.firstElementChild as HTMLElement | null) ??
78
+ triggerEl
79
+ )
52
80
  }
53
- function hide() {
54
- open = false
81
+
82
+ /** Add/remove `tipId` as one token, so a trigger's own description (e.g. a hint) is kept. */
83
+ function describe(el: HTMLElement | null) {
84
+ if (describedEl === el) return
85
+ undescribe()
86
+ if (!el) return
87
+ setDescribedBy(el, true)
88
+ describedEl = el
89
+ }
90
+ function undescribe() {
91
+ const el = describedEl
92
+ describedEl = null
93
+ if (!el) return
94
+ setDescribedBy(el, false)
95
+ }
96
+ function setDescribedBy(el: HTMLElement, described: boolean) {
97
+ const value = tooltipDescribedBy(el.getAttribute('aria-describedby'), tipId, described)
98
+ if (value === undefined) el.removeAttribute('aria-describedby')
99
+ else el.setAttribute('aria-describedby', value)
55
100
  }
56
- function onKeydown(e: KeyboardEvent) {
57
- // Escape dismisses the tooltip (WAI-ARIA tooltip pattern).
58
- if (e.key === 'Escape' && open) {
101
+
102
+ // Which event shows or hides the tip is decided in core/tooltip.ts; this applies the action.
103
+ function run(action: TooltipAction | null, e?: Event) {
104
+ if (!action) return
105
+ if (action.type === 'show') {
106
+ open = true
107
+ describe(describedTarget(e?.target))
108
+ } else {
59
109
  open = false
110
+ undescribe()
60
111
  }
61
112
  }
62
113
 
114
+ /** The other end of a focus move is still inside the tooltip. */
115
+ const within = (node: EventTarget | null) =>
116
+ node instanceof Node && Boolean(rootEl?.contains(node))
117
+
118
+ // Escape dismisses the tooltip wherever focus is (WCAG 1.4.13, WAI-ARIA tooltip
119
+ // pattern): a document-level listener while open, so a hover-opened tip closes too.
120
+ $effect(() => {
121
+ if (!open) return
122
+ const onKeydown = (e: KeyboardEvent) => run(tooltipKey({ open }, e.key))
123
+ document.addEventListener('keydown', onKeydown)
124
+ return () => document.removeEventListener('keydown', onKeydown)
125
+ })
126
+
127
+ // Unmounting while open must not leave a dangling id on the trigger.
128
+ $effect(() => () => undescribe())
129
+
63
130
  const sizeAttr = $derived(resolveComponentSize('Tooltip', size))
64
131
 
65
132
  // Snippets are functions, so a runtime typeof check narrows the union.
@@ -173,9 +240,12 @@
173
240
  <!--
174
241
  Inline-block wrapper around the trigger snippet. Hover (mouseenter/leave) and
175
242
  focus (focusin/out — bubbling, so a focusable child trigger counts) toggle the
176
- tooltip; Escape dismisses it. While open the wrapper carries aria-describedby
177
- pointing at the tooltip; the tip itself is kept in the DOM but hidden (and
178
- given `hidden`) when closed so it is never announced.
243
+ tooltip; Escape (a document-level listener while open) dismisses it. While open
244
+ the focusable trigger inside the snippet (not this wrapper) carries
245
+ aria-describedby pointing at the tooltip; the tip itself is kept in the DOM but
246
+ hidden (and given `hidden`) when closed so it is never announced. While open the
247
+ tip is hoverable (it is a child of the wrapper, and a pseudo-element bridges the
248
+ offset gap), so moving the pointer onto it does not close it (WCAG 1.4.13).
179
249
 
180
250
  `text` is either a string or a snippet (DS-0144); a snippet renders inside the
181
251
  same tip element, so the accessible description stays whatever the tip contains.
@@ -186,17 +256,16 @@
186
256
  -->
187
257
  <!-- svelte-ignore a11y_no_static_element_interactions -- the wrapper is a passive
188
258
  container; the real interactive element is the trigger snippet inside it, and the
189
- hover/focus/keydown handlers only react to that child's bubbling events. -->
259
+ hover/focus handlers only react to that child's bubbling events. -->
190
260
  <span
191
261
  class="ss-tooltip"
192
262
  data-size-variant={sizeAttr}
193
263
  data-placement={resolved}
194
- aria-describedby={open ? tipId : undefined}
195
- onmouseenter={show}
196
- onmouseleave={hide}
197
- onfocusin={show}
198
- onfocusout={hide}
199
- onkeydown={onKeydown}
264
+ bind:this={rootEl}
265
+ onmouseenter={(e) => run(tooltipPointerEnter({ open }), e)}
266
+ onmouseleave={() => run(tooltipPointerLeave({ open }))}
267
+ onfocusin={(e) => run(tooltipFocusIn({ open }), e)}
268
+ onfocusout={(e) => run(tooltipFocusOut({ open }, { within: within(e.relatedTarget) }))}
200
269
  >
201
270
  <span class="trigger" bind:this={triggerEl}>{@render children()}</span>
202
271
  <span
@@ -241,9 +310,14 @@
241
310
  transform: scale(0.98);
242
311
  transition: opacity var(--ss-dur) var(--ss-ease), transform var(--ss-dur) var(--ss-ease), visibility 0s linear var(--ss-dur);
243
312
  }
313
+ .tip::before {
314
+ content: "";
315
+ position: absolute;
316
+ }
244
317
  .tip.open {
245
318
  opacity: 1;
246
319
  visibility: visible;
320
+ pointer-events: auto;
247
321
  transform: scale(1);
248
322
  transition: opacity var(--ss-dur) var(--ss-ease), transform var(--ss-dur) var(--ss-ease), visibility 0s linear 0s;
249
323
  }
@@ -292,6 +366,36 @@
292
366
  transform: translateY(calc(-50% + var(--shift))) scale(1);
293
367
  }
294
368
 
369
+ .ss-tooltip[data-placement=top] .tip::before,
370
+ .ss-tooltip[data-placement=bottom] .tip::before {
371
+ left: 0;
372
+ right: 0;
373
+ height: var(--ss-tooltip-offset, 6px);
374
+ }
375
+
376
+ .ss-tooltip[data-placement=top] .tip::before {
377
+ top: 100%;
378
+ }
379
+
380
+ .ss-tooltip[data-placement=bottom] .tip::before {
381
+ bottom: 100%;
382
+ }
383
+
384
+ .ss-tooltip[data-placement=left] .tip::before,
385
+ .ss-tooltip[data-placement=right] .tip::before {
386
+ top: 0;
387
+ bottom: 0;
388
+ width: var(--ss-tooltip-offset, 6px);
389
+ }
390
+
391
+ .ss-tooltip[data-placement=left] .tip::before {
392
+ left: 100%;
393
+ }
394
+
395
+ .ss-tooltip[data-placement=right] .tip::before {
396
+ right: 100%;
397
+ }
398
+
295
399
  @media (prefers-reduced-motion: reduce) {
296
400
  .tip,
297
401
  .tip.open {
@@ -7,7 +7,7 @@ interface Props {
7
7
  * Tooltip content (the accessible description of the trigger): a plain string, or a
8
8
  * snippet for a small rendered template. Snippets must stay **phrasing content**
9
9
  * (`code`, `strong`, `kbd`, `br`, `Icon`, …) and must not contain interactive
10
- * elements — the tip is `pointer-events: none` and hidden while closed, per the
10
+ * elements — the tip is never focusable and is hidden while closed, per the
11
11
  * WAI-ARIA tooltip pattern.
12
12
  */
13
13
  text: string | Snippet;