dssoca 0.20.0 → 0.22.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 +404 -0
  23. package/dist/components/Popover.svelte.d.ts +55 -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 +41 -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 +111 -0
  72. package/dist/core/popover.js +86 -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 +199 -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 +32 -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 +364 -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
package/README.md CHANGED
@@ -84,9 +84,9 @@ wires the interactive parts (modal, menu, accordion, tooltip, switch, toasts, ic
84
84
  **after** `theme.css`; it needs CSS `@scope` (Chrome/Edge 118+, Safari 17.4+, Firefox 2026+).
85
85
 
86
86
  ```html
87
- <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/dssoca@0.20/dist/theme.css" />
88
- <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/dssoca@0.20/dist/vanilla.css" />
89
- <script type="module" src="https://cdn.jsdelivr.net/npm/dssoca@0.20/dist/vanilla/index.js"></script>
87
+ <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/dssoca@0.21/dist/theme.css" />
88
+ <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/dssoca@0.21/dist/vanilla.css" />
89
+ <script type="module" src="https://cdn.jsdelivr.net/npm/dssoca@0.21/dist/vanilla/index.js"></script>
90
90
 
91
91
  <button class="ss-btn primary" type="button" data-ss-modal="#confirm">deploy</button>
92
92
  ```
@@ -19,6 +19,13 @@
19
19
  import { untrack, type Snippet } from 'svelte'
20
20
  import { resolveComponentSize, type Size } from '../config.js'
21
21
  import Icon from './Icon.svelte'
22
+ import {
23
+ accordionHeaderAttrs,
24
+ accordionHeaderKey,
25
+ accordionOpenIds,
26
+ accordionToggle,
27
+ accordionValue,
28
+ } from '../core/accordion.js'
22
29
 
23
30
  interface Props {
24
31
  /** The collapsible sections, top to bottom. */
@@ -80,76 +87,41 @@
80
87
  }: Props = $props()
81
88
 
82
89
  // --- open-state model ----------------------------------------------------
83
- // Normalise whatever `value`/`defaultValue` shape we were handed into a Set.
84
- function toSet(v: AccordionValue): Set<string> {
85
- if (v == null) return new Set()
86
- return new Set(Array.isArray(v) ? v : [v])
87
- }
90
+ // The decisions (what a press opens or closes, where a key moves focus) live in
91
+ // `core/accordion.ts`, shared with vanilla/accordion.ts (DS-0197).
88
92
 
89
93
  // Uncontrolled fallback store, seeded once from defaultValue. `untrack`
90
94
  // marks the read as an intentional one-time snapshot (not a live binding),
91
95
  // silencing the "referenced locally" reactivity warning.
92
- let internal = $state<Set<string>>(untrack(() => toSet(defaultValue)))
96
+ let internal = $state<string[]>(untrack(() => accordionOpenIds(defaultValue)))
93
97
 
94
98
  // Controlled when the consumer passes `value` (incl. bound); else internal.
95
99
  const controlled = $derived(value !== undefined)
96
- const open = $derived(controlled ? toSet(value) : internal)
97
-
98
- const isOpen = (item: AccordionItem): boolean => open.has(item.id)
100
+ const open = $derived(controlled ? accordionOpenIds(value) : internal)
99
101
 
100
- /** Project a Set back to the public value shape for `value`/`onChange`. */
101
- function project(next: Set<string>): AccordionValue {
102
- if (multiple) return [...next]
103
- return next.size ? [...next][0] : undefined
104
- }
102
+ const isOpen = (item: AccordionItem): boolean => open.includes(item.id)
105
103
 
106
104
  function toggle(item: AccordionItem) {
107
- if (item.disabled) return
108
- const next = new Set(open)
109
- if (next.has(item.id)) {
110
- next.delete(item.id)
111
- } else {
112
- if (!multiple) next.clear() // single-open: opening one closes others
113
- next.add(item.id)
114
- }
105
+ const next = accordionToggle({ open, multiple }, item.id, item.disabled)
106
+ if (!next) return
107
+ const projected = accordionValue(next, multiple)
115
108
  if (controlled) {
116
- value = project(next) // keep the bound value in sync
109
+ value = projected // keep the bound value in sync
117
110
  } else {
118
111
  internal = next
119
112
  }
120
- onChange?.(project(next))
113
+ onChange?.(projected)
121
114
  }
122
115
 
123
116
  // --- roving keyboard nav across headers (WAI-ARIA Accordion pattern) -----
124
117
  let headerEls = $state<Array<HTMLButtonElement | null>>([])
125
118
 
126
- function focusHeader(index: number) {
127
- const n = items.length
128
- if (n === 0) return
129
- const i = ((index % n) + n) % n // wrap
130
- headerEls[i]?.focus()
131
- }
132
-
133
119
  function onHeaderKeydown(e: KeyboardEvent, index: number) {
134
- switch (e.key) {
135
- case 'ArrowDown':
136
- e.preventDefault()
137
- focusHeader(index + 1)
138
- break
139
- case 'ArrowUp':
140
- e.preventDefault()
141
- focusHeader(index - 1)
142
- break
143
- case 'Home':
144
- e.preventDefault()
145
- focusHeader(0)
146
- break
147
- case 'End':
148
- e.preventDefault()
149
- focusHeader(items.length - 1)
150
- break
151
- // Enter / Space activate the native <button>; no extra handling needed.
152
- }
120
+ // Enter / Space activate the native <button>; the core lets them through.
121
+ const result = accordionHeaderKey(items.length, index, e.key)
122
+ if (!result) return
123
+ if (result.preventDefault) e.preventDefault()
124
+ headerEls[result.focus]?.focus()
153
125
  }
154
126
 
155
127
  const headerId = (item: AccordionItem) => `${idBase}-h-${item.id}`
@@ -172,9 +144,8 @@
172
144
  type="button"
173
145
  class="head"
174
146
  id={headerId(item)}
175
- aria-expanded={expanded}
176
147
  aria-controls={panelId(item)}
177
- aria-disabled={item.disabled || undefined}
148
+ {...accordionHeaderAttrs(expanded, item.disabled)}
178
149
  onclick={() => toggle(item)}
179
150
  onkeydown={(e) => onHeaderKeydown(e, i)}
180
151
  >
@@ -21,6 +21,12 @@
21
21
  <script lang="ts">
22
22
  import Icon from './Icon.svelte'
23
23
  import { resolveComponentSize, type Size } from '../config.js'
24
+ import {
25
+ bottomNavHasBadge,
26
+ bottomNavPress,
27
+ bottomNavTabAttrs,
28
+ bottomNavTabElement,
29
+ } from '../core/bottom-nav.js'
24
30
 
25
31
  interface Props {
26
32
  /** Tabs to render, left → right, in equal-width columns. */
@@ -44,26 +50,12 @@
44
50
 
45
51
  let { items = DEFAULT_ITEMS, active, onSelect, ariaLabel = 'Primary', size }: Props = $props()
46
52
 
47
- const isActive = (item: BottomNavItem): boolean => item.id === active
48
-
49
- const hasBadge = (item: BottomNavItem): boolean => item.badge != null && item.badge !== ''
50
-
51
- /** Fold the count badge into the accessible name so it isn't lost to AT. */
52
- function accessibleName(item: BottomNavItem): string {
53
- return hasBadge(item) ? `${item.label}, ${item.badge}` : item.label
54
- }
55
-
53
+ // Behaviour lives in `core/bottom-nav.ts`: which element a tab is, what a press does and
54
+ // the state attributes are decided there; this file renders the result.
56
55
  function activate(item: BottomNavItem, e: MouseEvent) {
57
- // Disabled tabs are inert (DS-0078): no navigation, no onSelect. The
58
- // <button> branch is natively disabled; this also guards the <a> branch.
59
- if (item.disabled) {
60
- e.preventDefault()
61
- return
62
- }
63
- // Real links navigate natively; intercept only the SPA (no-href) path so a
64
- // plain <button> never submits/scrolls. onSelect fires either way.
65
- if (!item.href) e.preventDefault()
66
- onSelect?.(item.id)
56
+ const press = bottomNavPress(item)
57
+ if (press.preventDefault) e.preventDefault()
58
+ if (press.select) onSelect?.(item.id)
67
59
  }
68
60
 
69
61
  const sizeAttr = $derived(resolveComponentSize('BottomNav', size))
@@ -72,19 +64,19 @@
72
64
  <nav class="ss-bottom-nav" aria-label={ariaLabel} data-size-variant={sizeAttr}>
73
65
  <ul class="list">
74
66
  {#each items as item (item.id)}
75
- {@const current = isActive(item)}
67
+ {@const attrs = bottomNavTabAttrs(item, active)}
68
+ {@const current = attrs['aria-current'] === 'page'}
76
69
  <li class="row">
77
- {#if item.href && !item.disabled}
70
+ {#if bottomNavTabElement(item) === 'a'}
78
71
  <a
79
72
  class="tab {current ? 'active' : ''}"
80
73
  href={item.href}
81
- aria-current={current ? 'page' : undefined}
82
- aria-label={hasBadge(item) ? accessibleName(item) : undefined}
74
+ {...attrs}
83
75
  onclick={(e) => activate(item, e)}
84
76
  >
85
77
  <span class="glyph">
86
78
  <Icon name={item.icon} px={20} />
87
- {#if hasBadge(item)}
79
+ {#if bottomNavHasBadge(item)}
88
80
  <span class="badge" aria-hidden="true">{item.badge}</span>
89
81
  {/if}
90
82
  </span>
@@ -99,14 +91,12 @@
99
91
  class="tab {current ? 'active' : ''}"
100
92
  class:disabled={item.disabled}
101
93
  disabled={item.disabled || undefined}
102
- aria-disabled={item.disabled ? 'true' : undefined}
103
- aria-current={current ? 'page' : undefined}
104
- aria-label={hasBadge(item) ? accessibleName(item) : undefined}
94
+ {...attrs}
105
95
  onclick={(e) => activate(item, e)}
106
96
  >
107
97
  <span class="glyph">
108
98
  <Icon name={item.icon} px={20} />
109
- {#if hasBadge(item)}
99
+ {#if bottomNavHasBadge(item)}
110
100
  <span class="badge" aria-hidden="true">{item.badge}</span>
111
101
  {/if}
112
102
  </span>
@@ -14,7 +14,7 @@
14
14
  import { scaleBand, scaleLinear } from 'd3-scale'
15
15
  import { extent, quantile } from 'd3-array'
16
16
  import { resolveComponentSize, type Size } from '../config.js'
17
- import { chartNavStep } from '../chart-nav.js'
17
+ import { chartMarkKey, chartTabStop, chartWindowKey } from '../core/chart-nav.js'
18
18
 
19
19
  interface Props {
20
20
  /** One box per group. Each group has a `label`, `values`, and optional `color`. */
@@ -190,12 +190,13 @@
190
190
 
191
191
  // Roving tabindex (DS-0161): the chart is ONE tab stop; arrow keys move between boxes.
192
192
  let focusIdx = $state(0)
193
- const tabIdx = $derived(Math.min(focusIdx, Math.max(0, boxes.length - 1)))
193
+ const tabIdx = $derived(chartTabStop(focusIdx, boxes.length))
194
194
  const boxEls: (SVGRectElement | null)[] = $state([])
195
195
  function onBoxKeydown(e: KeyboardEvent, i: number) {
196
- const next = chartNavStep(e.key, i, boxes.length)
197
- if (next === null) return
198
- e.preventDefault()
196
+ const result = chartMarkKey({ index: i, count: boxes.length }, e.key)
197
+ if (!result || result.action.type !== 'focus') return
198
+ if (result.preventDefault) e.preventDefault()
199
+ const next = result.action.index
199
200
  focusIdx = next
200
201
  boxEls[next]?.focus()
201
202
  }
@@ -205,7 +206,7 @@
205
206
  }
206
207
  // Escape dismisses the tooltip whether it came from hover or focus (WCAG 1.4.13).
207
208
  function onWindowKeydown(e: KeyboardEvent) {
208
- if (e.key === 'Escape' && active) hide()
209
+ if (chartWindowKey(active !== null, e.key)) hide()
209
210
  }
210
211
  </script>
211
212
 
@@ -15,7 +15,7 @@
15
15
  import { line as shapeLine } from 'd3-shape'
16
16
  import { max } from 'd3-array'
17
17
  import { resolveComponentSize, type Size } from '../config.js'
18
- import { chartNavStep } from '../chart-nav.js'
18
+ import { chartMarkKey, chartTabStop, chartWindowKey } from '../core/chart-nav.js'
19
19
 
20
20
  interface Props {
21
21
  /** One series per competitor; each carries a `label`, `ranks` and optional `color`. */
@@ -159,26 +159,23 @@
159
159
  $effect(() => {
160
160
  if (!active) return
161
161
  const onKey = (e: KeyboardEvent) => {
162
- if (e.key === 'Escape') hide()
162
+ if (chartWindowKey(true, e.key)) hide()
163
163
  }
164
164
  window.addEventListener('keydown', onKey)
165
165
  return () => window.removeEventListener('keydown', onKey)
166
166
  })
167
167
 
168
- // Roving tabindex: the whole chart is one tab stop (DS-0161 / chart-nav.ts).
168
+ // Roving tabindex: the whole chart is one tab stop (DS-0161 / core/chart-nav.ts).
169
169
  let focusIndex = $state(0)
170
- const tabStop = $derived(Math.min(focusIndex, Math.max(0, nodeCount - 1)))
170
+ const tabStop = $derived(chartTabStop(focusIndex, nodeCount))
171
171
  let nodesEl = $state<SVGGElement | null>(null)
172
172
 
173
173
  function onNodeKeydown(e: KeyboardEvent, i: number) {
174
- if (e.key === 'Escape') {
175
- hide()
176
- return
177
- }
178
- // SVG geometry does not mirror in RTL, so Left/Right stay physical.
179
- const next = chartNavStep(e.key, i, nodeCount)
180
- if (next === null) return
181
- e.preventDefault()
174
+ const result = chartMarkKey({ index: i, count: nodeCount, escape: 'dismiss' }, e.key)
175
+ if (!result) return
176
+ if (result.preventDefault) e.preventDefault()
177
+ if (result.action.type === 'dismiss') return hide()
178
+ const next = result.action.index
182
179
  focusIndex = next
183
180
  nodesEl?.querySelector<SVGElement>(`[data-index="${next}"]`)?.focus()
184
181
  }
@@ -23,7 +23,7 @@
23
23
  import { line as shapeLine, area as shapeArea } from 'd3-shape'
24
24
  import { extent, max } from 'd3-array'
25
25
  import { resolveComponentSize, type Size } from '../config.js'
26
- import { chartNavStep } from '../chart-nav.js'
26
+ import { chartMarkKey, chartTabStop, chartWindowKey } from '../core/chart-nav.js'
27
27
 
28
28
  type XType = 'linear' | 'time' | 'band'
29
29
 
@@ -284,27 +284,24 @@
284
284
  // Bars flattened in render order (category, then series) for the roving tabindex.
285
285
  const flatBars = $derived(barGroups.flatMap((g) => g.bars))
286
286
 
287
- // Roving tabindex: the chart is ONE tab stop; arrow keys move between marks (chart-nav.ts).
287
+ // Roving tabindex: the chart is ONE tab stop; arrow keys move between marks (core/chart-nav.ts).
288
288
  const markCount = $derived(variant === 'bar' ? flatBars.length : pointMarkers.length)
289
289
  let focusIndex = $state(0)
290
- const tabStop = $derived(Math.min(focusIndex, Math.max(0, markCount - 1)))
290
+ const tabStop = $derived(chartTabStop(focusIndex, markCount))
291
291
  let marksEl = $state<SVGGElement | null>(null)
292
292
 
293
293
  function onMarkKeydown(e: KeyboardEvent, i: number) {
294
- if (e.key === 'Escape') {
295
- hide()
296
- return
297
- }
298
- // SVG geometry does not mirror in RTL, so Left/Right stay physical.
299
- const next = chartNavStep(e.key, i, markCount)
300
- if (next === null) return
301
- e.preventDefault()
294
+ const result = chartMarkKey({ index: i, count: markCount, escape: 'dismiss' }, e.key)
295
+ if (!result) return
296
+ if (result.preventDefault) e.preventDefault()
297
+ if (result.action.type === 'dismiss') return hide()
298
+ const next = result.action.index
302
299
  focusIndex = next
303
300
  marksEl?.querySelector<SVGElement>(`[data-index="${next}"]`)?.focus()
304
301
  }
305
302
  // Escape also dismisses a hover-opened tooltip (WCAG 1.4.13).
306
303
  function onWindowKeydown(e: KeyboardEvent) {
307
- if (e.key === 'Escape' && active) hide()
304
+ if (chartWindowKey(active !== null, e.key)) hide()
308
305
  }
309
306
 
310
307
  // Auto SR summary.
@@ -0,0 +1,210 @@
1
+ <script lang="ts">
2
+ // ─────────────────────────────────────────────────────────────────────────
3
+ // Checkbox — DS-0197. A native `<input type="checkbox">` restyled as a square
4
+ // box (house rule: zero radius), so Space, label presses, form submission and
5
+ // the checked / unchecked / mixed semantics all come from the platform. The
6
+ // input itself IS the box (`appearance: none`), which is why the base focus
7
+ // ring lands on it; the check glyph and the indeterminate dash sit on top.
8
+ // ─────────────────────────────────────────────────────────────────────────
9
+ import Icon from './Icon.svelte'
10
+ import { resolveComponentSize, type Size } from '../config.js'
11
+ import { checkboxInputAttrs, checkboxToggle } from '../core/checkbox.js'
12
+
13
+ interface Props {
14
+ /** Checked state (bindable). */
15
+ checked?: boolean
16
+ /**
17
+ * Mixed state (bindable): shown as a dash and announced as "mixed". A user
18
+ * toggle always clears it and lands on checked.
19
+ */
20
+ indeterminate?: boolean
21
+ /** Visible label next to the box; becomes the accessible name. */
22
+ label?: string
23
+ /**
24
+ * Visually hide the label while keeping it as the accessible name — for
25
+ * dense rows (table row selection) where the visible context sits elsewhere.
26
+ */
27
+ labelHidden?: boolean
28
+ /** Id for the underlying input. */
29
+ id?: string
30
+ /** Form field name: the checkbox is submitted with its form when checked. */
31
+ name?: string
32
+ /** Submitted value when checked (the browser default is `on`). */
33
+ value?: string
34
+ /** Disable the checkbox: inert + non-toggling, not submitted. */
35
+ disabled?: boolean
36
+ /** Token size (sm|md|lg); inherits the global size when unset. */
37
+ size?: Size
38
+ /** Id of an element that further describes the checkbox (→ aria-describedby). */
39
+ describedby?: string
40
+ /**
41
+ * Fired after `checked` changes, with the new value. There is no
42
+ * `details.reason`: the native input reports a press on the box, a press on
43
+ * the label and Space as the same event.
44
+ */
45
+ onchange?: (checked: boolean) => void
46
+ }
47
+
48
+ let {
49
+ checked = $bindable(false),
50
+ indeterminate = $bindable(false),
51
+ label,
52
+ labelHidden = false,
53
+ id,
54
+ name,
55
+ value,
56
+ disabled = false,
57
+ size,
58
+ describedby,
59
+ onchange,
60
+ }: Props = $props()
61
+
62
+ const uid = $props.id()
63
+ const inputId = $derived(id ?? uid)
64
+
65
+ let input = $state<HTMLInputElement>()
66
+
67
+ // `indeterminate` exists only as a DOM property (no HTML attribute).
68
+ $effect(() => {
69
+ if (input) input.indeterminate = indeterminate
70
+ })
71
+
72
+ // The browser has already toggled the input; the decision is still the core's
73
+ // (shared with vanilla/checkbox.ts), so its result is written back.
74
+ function onInputChange() {
75
+ if (!input) return
76
+ const next = checkboxToggle({ checked, indeterminate, disabled })
77
+ if (next === null) {
78
+ input.checked = checked
79
+ input.indeterminate = indeterminate
80
+ return
81
+ }
82
+ checked = next.checked
83
+ indeterminate = next.indeterminate
84
+ input.checked = next.checked
85
+ onchange?.(next.checked)
86
+ }
87
+
88
+ const sizeAttr = $derived(resolveComponentSize('Checkbox', size))
89
+ </script>
90
+
91
+ <label
92
+ class="ss-checkbox"
93
+ class:has-label={label != null && !labelHidden}
94
+ data-size-variant={sizeAttr}
95
+ >
96
+ <span class="control">
97
+ <input
98
+ bind:this={input}
99
+ id={inputId}
100
+ class="input"
101
+ type="checkbox"
102
+ {name}
103
+ {value}
104
+ {checked}
105
+ {disabled}
106
+ aria-describedby={describedby}
107
+ {...checkboxInputAttrs(indeterminate)}
108
+ onchange={onInputChange}
109
+ />
110
+ <!-- Both marks are always in the DOM; the input's own state picks one in CSS. -->
111
+ <span class="glyph" aria-hidden="true">
112
+ <span class="check"><Icon name="check" strokeWidth={3} /></span>
113
+ <span class="dash"></span>
114
+ </span>
115
+ </span>
116
+ {#if label != null}
117
+ <span class="label" class:sr-only={labelHidden}>{label}</span>
118
+ {/if}
119
+ </label>
120
+
121
+ <style>.ss-checkbox {
122
+ position: relative;
123
+ display: inline-flex;
124
+ align-items: center;
125
+ min-inline-size: var(--ss-s-6);
126
+ min-block-size: var(--ss-s-6);
127
+ cursor: pointer;
128
+ }
129
+ .ss-checkbox.has-label {
130
+ gap: var(--ss-checkbox-gap, var(--ss-gap-sm));
131
+ }
132
+ .ss-checkbox .control {
133
+ position: relative;
134
+ flex: 0 0 auto;
135
+ display: inline-grid;
136
+ inline-size: var(--ss-checkbox-size, calc(var(--ss-control-font) + 4px));
137
+ block-size: var(--ss-checkbox-size, calc(var(--ss-control-font) + 4px));
138
+ }
139
+ .ss-checkbox .input {
140
+ grid-area: 1/1;
141
+ appearance: none;
142
+ box-sizing: border-box;
143
+ inline-size: 100%;
144
+ block-size: 100%;
145
+ margin: 0;
146
+ border-radius: 0;
147
+ border: 1px solid var(--ss-fg-faint);
148
+ background: var(--ss-bg);
149
+ cursor: inherit;
150
+ transition: background var(--ss-dur-fast) var(--ss-ease), border-color var(--ss-dur-fast) var(--ss-ease);
151
+ }
152
+ .ss-checkbox .input:not(:disabled):hover {
153
+ border-color: var(--ss-fg);
154
+ }
155
+ .ss-checkbox .input:checked, .ss-checkbox .input:indeterminate {
156
+ background: var(--ss-primary);
157
+ border-color: var(--ss-primary);
158
+ }
159
+ .ss-checkbox .glyph {
160
+ grid-area: 1/1;
161
+ display: grid;
162
+ place-items: center;
163
+ pointer-events: none;
164
+ color: var(--ss-fg-on-primary);
165
+ --ss-icon: calc(var(--ss-checkbox-size, calc(var(--ss-control-font) + 4px)) - 6px);
166
+ }
167
+ .ss-checkbox .check,
168
+ .ss-checkbox .dash {
169
+ display: none;
170
+ }
171
+ .ss-checkbox .dash {
172
+ inline-size: calc(var(--ss-checkbox-size, calc(var(--ss-control-font) + 4px)) - 8px);
173
+ block-size: 2px;
174
+ background: currentColor;
175
+ }
176
+ .ss-checkbox .input:checked:not(:indeterminate) + .glyph .check {
177
+ display: grid;
178
+ }
179
+ .ss-checkbox .input:indeterminate + .glyph .dash {
180
+ display: block;
181
+ }
182
+ .ss-checkbox .label {
183
+ font: 500 var(--ss-control-font)/1.3 var(--ss-font-body);
184
+ color: var(--ss-fg);
185
+ user-select: none;
186
+ }
187
+ .ss-checkbox .label.sr-only {
188
+ position: absolute;
189
+ width: 1px;
190
+ height: 1px;
191
+ padding: 0;
192
+ margin: -1px;
193
+ overflow: hidden;
194
+ clip: rect(0, 0, 0, 0);
195
+ white-space: nowrap;
196
+ border: 0;
197
+ }
198
+ .ss-checkbox:has(.input:disabled) {
199
+ cursor: not-allowed;
200
+ }
201
+ .ss-checkbox:has(.input:disabled) .control,
202
+ .ss-checkbox:has(.input:disabled) .label {
203
+ opacity: 0.45;
204
+ }
205
+
206
+ @media (prefers-reduced-motion: reduce) {
207
+ .ss-checkbox .input {
208
+ transition: none;
209
+ }
210
+ }</style>
@@ -0,0 +1,38 @@
1
+ import { type Size } from '../config.js';
2
+ interface Props {
3
+ /** Checked state (bindable). */
4
+ checked?: boolean;
5
+ /**
6
+ * Mixed state (bindable): shown as a dash and announced as "mixed". A user
7
+ * toggle always clears it and lands on checked.
8
+ */
9
+ indeterminate?: boolean;
10
+ /** Visible label next to the box; becomes the accessible name. */
11
+ label?: string;
12
+ /**
13
+ * Visually hide the label while keeping it as the accessible name — for
14
+ * dense rows (table row selection) where the visible context sits elsewhere.
15
+ */
16
+ labelHidden?: boolean;
17
+ /** Id for the underlying input. */
18
+ id?: string;
19
+ /** Form field name: the checkbox is submitted with its form when checked. */
20
+ name?: string;
21
+ /** Submitted value when checked (the browser default is `on`). */
22
+ value?: string;
23
+ /** Disable the checkbox: inert + non-toggling, not submitted. */
24
+ disabled?: boolean;
25
+ /** Token size (sm|md|lg); inherits the global size when unset. */
26
+ size?: Size;
27
+ /** Id of an element that further describes the checkbox (→ aria-describedby). */
28
+ describedby?: string;
29
+ /**
30
+ * Fired after `checked` changes, with the new value. There is no
31
+ * `details.reason`: the native input reports a press on the box, a press on
32
+ * the label and Space as the same event.
33
+ */
34
+ onchange?: (checked: boolean) => void;
35
+ }
36
+ declare const Checkbox: import("svelte").Component<Props, {}, "checked" | "indeterminate">;
37
+ type Checkbox = ReturnType<typeof Checkbox>;
38
+ export default Checkbox;