@human-kit/ui 1.0.0-beta.5 → 1.0.0-beta.7

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 (255) hide show
  1. package/README.md +19 -17
  2. package/dist/accordion/README.md +3 -3
  3. package/dist/accordion/types.d.ts +37 -23
  4. package/dist/autocomplete/README.md +6 -7
  5. package/dist/autocomplete/empty/autocomplete-empty.svelte +1 -1
  6. package/dist/autocomplete/empty/autocomplete-empty.svelte.d.ts +1 -1
  7. package/dist/autocomplete/input/autocomplete-input.svelte +3 -3
  8. package/dist/autocomplete/input/autocomplete-input.svelte.d.ts +3 -3
  9. package/dist/autocomplete/item/autocomplete-item.svelte +10 -0
  10. package/dist/autocomplete/item-indicator/autocomplete-item-indicator.svelte +2 -2
  11. package/dist/autocomplete/item-indicator/autocomplete-item-indicator.svelte.d.ts +2 -2
  12. package/dist/autocomplete/list/autocomplete-list.svelte +2 -2
  13. package/dist/autocomplete/list/autocomplete-list.svelte.d.ts +2 -2
  14. package/dist/autocomplete/root/autocomplete.svelte +7 -7
  15. package/dist/autocomplete/root/autocomplete.svelte.d.ts +7 -7
  16. package/dist/autocomplete/status/autocomplete-status.svelte +1 -1
  17. package/dist/autocomplete/status/autocomplete-status.svelte.d.ts +1 -1
  18. package/dist/button/README.md +1 -1
  19. package/dist/button/root/button-root.svelte +24 -10
  20. package/dist/button/root/button-root.svelte.d.ts +15 -10
  21. package/dist/calendar/README.md +3 -3
  22. package/dist/calendar/body-cell/calendar-body-cell.svelte +7 -0
  23. package/dist/calendar/grid-header/calendar-grid-header.svelte +3 -4
  24. package/dist/calendar/grid-header/calendar-grid-header.svelte.d.ts +3 -4
  25. package/dist/calendar/root/calendar-root.svelte +8 -2
  26. package/dist/calendar/root/calendar-root.svelte.d.ts +8 -2
  27. package/dist/calendar/trigger-next/README.md +1 -1
  28. package/dist/calendar/trigger-previous/README.md +1 -1
  29. package/dist/checkbox/README.md +1 -1
  30. package/dist/checkbox/indicator/README.md +1 -1
  31. package/dist/checkbox/indicator/checkbox-indicator.svelte +1 -0
  32. package/dist/checkbox/indicator/checkbox-indicator.svelte.d.ts +1 -0
  33. package/dist/checkbox/root/README.md +2 -2
  34. package/dist/checkbox/root/checkbox-root.svelte +118 -40
  35. package/dist/checkbox/root/checkbox-root.svelte.d.ts +17 -10
  36. package/dist/checkbox-group/README.md +95 -0
  37. package/dist/checkbox-group/index.d.ts +15 -0
  38. package/dist/checkbox-group/index.js +6 -0
  39. package/dist/checkbox-group/index.parts.d.ts +4 -0
  40. package/dist/checkbox-group/index.parts.js +7 -0
  41. package/dist/checkbox-group/label/checkbox-group-label.svelte +36 -0
  42. package/dist/checkbox-group/label/checkbox-group-label.svelte.d.ts +4 -0
  43. package/dist/checkbox-group/root/checkbox-group-root.svelte +118 -0
  44. package/dist/checkbox-group/root/checkbox-group-root.svelte.d.ts +4 -0
  45. package/dist/checkbox-group/root/context.svelte.d.ts +52 -0
  46. package/dist/checkbox-group/root/context.svelte.js +251 -0
  47. package/dist/checkbox-group/types.d.ts +57 -0
  48. package/dist/checkbox-group/types.js +1 -0
  49. package/dist/clock/README.md +3 -3
  50. package/dist/clock/axis/README.md +1 -1
  51. package/dist/clock/root/README.md +1 -1
  52. package/dist/clock/wheel-column/clock-wheel-column.svelte +1 -1
  53. package/dist/clock/wheel-column/clock-wheel-column.svelte.d.ts +1 -1
  54. package/dist/collapsible/README.md +3 -3
  55. package/dist/collapsible/types.d.ts +10 -7
  56. package/dist/combobox/README.md +2 -2
  57. package/dist/combobox/button/README.md +6 -6
  58. package/dist/combobox/input/README.md +1 -1
  59. package/dist/combobox/input/combobox-input.svelte +3 -3
  60. package/dist/combobox/input/combobox-input.svelte.d.ts +3 -3
  61. package/dist/combobox/item/combobox-listboxitem.svelte +4 -2
  62. package/dist/combobox/item/combobox-listboxitem.svelte.d.ts +4 -2
  63. package/dist/combobox/item-indicator/README.md +1 -1
  64. package/dist/combobox/item-indicator/combobox-item-indicator.svelte +2 -2
  65. package/dist/combobox/item-indicator/combobox-item-indicator.svelte.d.ts +2 -2
  66. package/dist/combobox/list/combobox-listbox.svelte +11 -8
  67. package/dist/combobox/list/combobox-listbox.svelte.d.ts +11 -8
  68. package/dist/combobox/popover/README.md +1 -1
  69. package/dist/combobox/root/README.md +24 -24
  70. package/dist/combobox/root/combobox.svelte +26 -17
  71. package/dist/combobox/root/combobox.svelte.d.ts +26 -17
  72. package/dist/combobox/status/README.md +1 -1
  73. package/dist/combobox/status/combobox-status.svelte +1 -1
  74. package/dist/combobox/status/combobox-status.svelte.d.ts +1 -1
  75. package/dist/combobox/tag/README.md +1 -1
  76. package/dist/combobox/tag/combobox-tag.svelte +1 -1
  77. package/dist/combobox/tag/combobox-tag.svelte.d.ts +1 -1
  78. package/dist/combobox/tag-remove/README.md +1 -1
  79. package/dist/combobox/tag-remove/combobox-tag-remove.svelte +4 -5
  80. package/dist/combobox/tag-remove/combobox-tag-remove.svelte.d.ts +4 -5
  81. package/dist/combobox/tags/README.md +1 -1
  82. package/dist/combobox/tags/combobox-tags.svelte +11 -11
  83. package/dist/combobox/tags/combobox-tags.svelte.d.ts +11 -11
  84. package/dist/combobox/trigger/README.md +7 -7
  85. package/dist/datepicker/README.md +4 -4
  86. package/dist/datepicker/input/date-picker-input.svelte +3 -4
  87. package/dist/datepicker/input/date-picker-input.svelte.d.ts +3 -4
  88. package/dist/datepicker/root/date-picker-root.svelte +1 -1
  89. package/dist/datepicker/trigger/README.md +1 -1
  90. package/dist/daterangepicker/README.md +3 -3
  91. package/dist/daterangepicker/input/date-range-picker-input.svelte +3 -4
  92. package/dist/daterangepicker/input/date-range-picker-input.svelte.d.ts +3 -4
  93. package/dist/dialog/README.md +1 -1
  94. package/dist/dialog/close/dialog-close.svelte +3 -3
  95. package/dist/dialog/close/dialog-close.svelte.d.ts +3 -3
  96. package/dist/dialog/content/dialog-content.svelte +4 -4
  97. package/dist/dialog/content/dialog-content.svelte.d.ts +4 -4
  98. package/dist/dialog/description/dialog-description.svelte +3 -3
  99. package/dist/dialog/description/dialog-description.svelte.d.ts +3 -3
  100. package/dist/dialog/overlay/dialog-overlay.svelte +1 -1
  101. package/dist/dialog/overlay/dialog-overlay.svelte.d.ts +1 -1
  102. package/dist/dialog/portal/dialog-portal.svelte +1 -1
  103. package/dist/dialog/portal/dialog-portal.svelte.d.ts +1 -1
  104. package/dist/dialog/root/README.md +1 -1
  105. package/dist/dialog/root/dialog-root.svelte +36 -11
  106. package/dist/dialog/root/dialog-root.svelte.d.ts +11 -9
  107. package/dist/dialog/title/dialog-title.svelte +3 -3
  108. package/dist/dialog/title/dialog-title.svelte.d.ts +3 -3
  109. package/dist/dialog/trigger/README.md +1 -1
  110. package/dist/drawer/README.md +3 -3
  111. package/dist/drawer/body/drawer-body.svelte +2 -2
  112. package/dist/drawer/body/drawer-body.svelte.d.ts +2 -2
  113. package/dist/drawer/close/drawer-close.svelte +3 -3
  114. package/dist/drawer/close/drawer-close.svelte.d.ts +3 -3
  115. package/dist/drawer/content/drawer-content.svelte +12 -9
  116. package/dist/drawer/content/drawer-content.svelte.d.ts +12 -9
  117. package/dist/drawer/description/drawer-description.svelte +3 -3
  118. package/dist/drawer/description/drawer-description.svelte.d.ts +3 -3
  119. package/dist/drawer/indent/drawer-indent-background.svelte +1 -1
  120. package/dist/drawer/indent/drawer-indent-background.svelte.d.ts +1 -1
  121. package/dist/drawer/indent/drawer-indent.svelte +2 -2
  122. package/dist/drawer/indent/drawer-indent.svelte.d.ts +2 -2
  123. package/dist/drawer/overlay/drawer-overlay.svelte +1 -1
  124. package/dist/drawer/overlay/drawer-overlay.svelte.d.ts +1 -1
  125. package/dist/drawer/portal/drawer-portal.svelte +1 -1
  126. package/dist/drawer/portal/drawer-portal.svelte.d.ts +1 -1
  127. package/dist/drawer/root/context.d.ts +1 -1
  128. package/dist/drawer/root/drawer-root.svelte +25 -23
  129. package/dist/drawer/root/drawer-root.svelte.d.ts +25 -23
  130. package/dist/drawer/swipe-area/drawer-swipe-area.svelte +3 -3
  131. package/dist/drawer/swipe-area/drawer-swipe-area.svelte.d.ts +3 -3
  132. package/dist/drawer/title/drawer-title.svelte +3 -3
  133. package/dist/drawer/title/drawer-title.svelte.d.ts +3 -3
  134. package/dist/drawer/trigger/drawer-trigger.svelte +6 -6
  135. package/dist/drawer/trigger/drawer-trigger.svelte.d.ts +6 -6
  136. package/dist/drawer/viewport/drawer-viewport.svelte +2 -2
  137. package/dist/drawer/viewport/drawer-viewport.svelte.d.ts +2 -2
  138. package/dist/drawer/virtual-keyboard/drawer-virtual-keyboard-provider.svelte +1 -1
  139. package/dist/drawer/virtual-keyboard/drawer-virtual-keyboard-provider.svelte.d.ts +1 -1
  140. package/dist/dropzone/README.md +4 -6
  141. package/dist/index.d.ts +4 -0
  142. package/dist/index.js +4 -0
  143. package/dist/input/input.svelte +6 -6
  144. package/dist/input/input.svelte.d.ts +6 -6
  145. package/dist/listbox/README.md +3 -10
  146. package/dist/listbox/item/listbox-item.svelte +24 -15
  147. package/dist/listbox/item/listbox-item.svelte.d.ts +24 -15
  148. package/dist/listbox/root/README.md +1 -1
  149. package/dist/listbox/root/context.d.ts +1 -1
  150. package/dist/listbox/root/listbox.svelte +50 -38
  151. package/dist/listbox/root/listbox.svelte.d.ts +50 -38
  152. package/dist/locale-provider/README.md +1 -6
  153. package/dist/menu/README.md +6 -15
  154. package/dist/menu/content/menu-content.svelte +14 -10
  155. package/dist/menu/content/menu-content.svelte.d.ts +14 -10
  156. package/dist/menu/context-trigger/menu-context-trigger.svelte +8 -9
  157. package/dist/menu/context-trigger/menu-context-trigger.svelte.d.ts +8 -9
  158. package/dist/menu/group/menu-group-label.svelte +1 -1
  159. package/dist/menu/group/menu-group-label.svelte.d.ts +1 -1
  160. package/dist/menu/item/menu-item.svelte +11 -8
  161. package/dist/menu/item/menu-item.svelte.d.ts +11 -8
  162. package/dist/menu/overlay/menu-overlay.svelte +1 -1
  163. package/dist/menu/overlay/menu-overlay.svelte.d.ts +1 -1
  164. package/dist/menu/root/context.d.ts +6 -3
  165. package/dist/menu/root/menu-root.svelte +14 -8
  166. package/dist/menu/root/menu-root.svelte.d.ts +14 -8
  167. package/dist/menu/root/menu-state.svelte.d.ts +1 -1
  168. package/dist/menu/separator/menu-separator.svelte +1 -1
  169. package/dist/menu/separator/menu-separator.svelte.d.ts +1 -1
  170. package/dist/menu/submenu/menu-submenu-root.svelte +10 -7
  171. package/dist/menu/submenu/menu-submenu-root.svelte.d.ts +10 -7
  172. package/dist/numberfield/root/README.md +1 -1
  173. package/dist/numberfield/scrub-area-cursor/README.md +1 -1
  174. package/dist/popover/README.md +1 -1
  175. package/dist/popover/content/popover-content.svelte +19 -14
  176. package/dist/popover/content/popover-content.svelte.d.ts +19 -14
  177. package/dist/popover/overlay/popover-overlay.svelte +1 -1
  178. package/dist/popover/overlay/popover-overlay.svelte.d.ts +1 -1
  179. package/dist/popover/root/README.md +1 -1
  180. package/dist/popover/root/popover-root.svelte +13 -10
  181. package/dist/popover/root/popover-root.svelte.d.ts +13 -10
  182. package/dist/popover/trigger/README.md +1 -1
  183. package/dist/primitives/floating.d.ts +3 -3
  184. package/dist/primitives/focus-visible.svelte.d.ts +14 -0
  185. package/dist/primitives/focus-visible.svelte.js +19 -0
  186. package/dist/primitives/index.d.ts +1 -0
  187. package/dist/primitives/index.js +1 -0
  188. package/dist/primitives/input-modality.d.ts +7 -0
  189. package/dist/primitives/input-modality.js +32 -5
  190. package/dist/primitives/scroll-lock.js +37 -8
  191. package/dist/radio-group/README.md +103 -0
  192. package/dist/radio-group/index.d.ts +18 -0
  193. package/dist/radio-group/index.js +9 -0
  194. package/dist/radio-group/index.parts.d.ts +4 -0
  195. package/dist/radio-group/index.parts.js +4 -0
  196. package/dist/radio-group/indicator/radio-group-indicator.svelte +43 -0
  197. package/dist/radio-group/indicator/radio-group-indicator.svelte.d.ts +12 -0
  198. package/dist/radio-group/item/context.d.ts +18 -0
  199. package/dist/radio-group/item/context.js +15 -0
  200. package/dist/radio-group/item/radio-group-item.svelte +377 -0
  201. package/dist/radio-group/item/radio-group-item.svelte.d.ts +35 -0
  202. package/dist/radio-group/label/radio-group-label.svelte +37 -0
  203. package/dist/radio-group/label/radio-group-label.svelte.d.ts +4 -0
  204. package/dist/radio-group/root/context.svelte.d.ts +57 -0
  205. package/dist/radio-group/root/context.svelte.js +284 -0
  206. package/dist/radio-group/root/radio-group-root.svelte +193 -0
  207. package/dist/radio-group/root/radio-group-root.svelte.d.ts +4 -0
  208. package/dist/radio-group/types.d.ts +50 -0
  209. package/dist/radio-group/types.js +1 -0
  210. package/dist/switch/root/README.md +2 -2
  211. package/dist/switch/root/switch-root.svelte +32 -18
  212. package/dist/switch/root/switch-root.svelte.d.ts +23 -18
  213. package/dist/switch/thumb/switch-thumb.svelte +1 -1
  214. package/dist/switch/thumb/switch-thumb.svelte.d.ts +1 -1
  215. package/dist/table/README.md +15 -10
  216. package/dist/table/checkbox-indicator/README.md +1 -1
  217. package/dist/table/column-header-cell/table-column-header-cell.svelte +24 -0
  218. package/dist/table/column-resizer/README.md +2 -2
  219. package/dist/table/root/README.md +1 -1
  220. package/dist/table/root/table-root.svelte +29 -6
  221. package/dist/table/sort-trigger/README.md +1 -1
  222. package/dist/table/types.d.ts +21 -20
  223. package/dist/tabs/types.d.ts +6 -6
  224. package/dist/timepicker/README.md +3 -2
  225. package/dist/timepicker/input/README.md +1 -1
  226. package/dist/timepicker/input/time-picker-input.svelte +66 -5
  227. package/dist/timepicker/input/time-picker-input.svelte.d.ts +18 -1
  228. package/dist/timepicker/root/README.md +1 -1
  229. package/dist/timepicker/trigger/README.md +1 -1
  230. package/dist/toggle/README.md +1 -1
  231. package/dist/toggle/root/toggle-root.svelte +28 -0
  232. package/dist/toggle/root/toggle-root.svelte.d.ts +19 -0
  233. package/dist/toggle-group/README.md +8 -10
  234. package/dist/toggle-group/index.d.ts +2 -0
  235. package/dist/toggle-group/index.parts.d.ts +1 -0
  236. package/dist/toggle-group/index.parts.js +4 -0
  237. package/dist/toggle-group/types.d.ts +7 -6
  238. package/dist/transfer-list/README.md +6 -17
  239. package/dist/transfer-list/item/transfer-list-item.svelte +4 -4
  240. package/dist/transfer-list/item/transfer-list-item.svelte.d.ts +4 -4
  241. package/dist/transfer-list/list/transfer-list-list.svelte +12 -12
  242. package/dist/transfer-list/list/transfer-list-list.svelte.d.ts +12 -12
  243. package/dist/transfer-list/move/transfer-list-move-button.svelte +4 -4
  244. package/dist/transfer-list/move/transfer-list-move-button.svelte.d.ts +4 -4
  245. package/dist/transfer-list/move/transfer-list-reorder-button.svelte +3 -3
  246. package/dist/transfer-list/move/transfer-list-reorder-button.svelte.d.ts +3 -3
  247. package/dist/transfer-list/root/transfer-list-root.svelte +23 -20
  248. package/dist/transfer-list/root/transfer-list-root.svelte.d.ts +23 -20
  249. package/dist/transfer-list/status/transfer-list-status.svelte +1 -1
  250. package/dist/transfer-list/status/transfer-list-status.svelte.d.ts +1 -1
  251. package/dist/tree/README.md +3 -3
  252. package/dist/tree/checkbox-indicator/README.md +1 -1
  253. package/dist/tree/root/README.md +1 -1
  254. package/dist/tree/types.d.ts +19 -10
  255. package/package.json +11 -1
@@ -0,0 +1,284 @@
1
+ import { getContext, setContext, untrack } from 'svelte';
2
+ import { SvelteMap } from 'svelte/reactivity';
3
+ import { asCommand } from '../../internal/as-command.js';
4
+ const RADIO_GROUP_CONTEXT_KEY = Symbol('radio-group');
5
+ export function createRadioGroupContext(options) {
6
+ let name = $state(options.name);
7
+ let form = $state(options.form);
8
+ let isDisabled = $state(options.isDisabled ?? false);
9
+ let isReadOnly = $state(options.isReadOnly ?? false);
10
+ let isRequired = $state(options.isRequired ?? false);
11
+ let orientation = $state(options.orientation ?? 'vertical');
12
+ let labelIds = $state([]);
13
+ let selectedValue = $state(options.initialValue ?? null);
14
+ let focusedValue = $state(null);
15
+ let focusVisible = $state(false);
16
+ // Whether the selected radio left the tree. It parts the two ways a selected value can have
17
+ // no registration: on the server, and on the first client render, the radios register one by
18
+ // one, so the selected one is simply not there yet and must keep the tab stop. Once it has
19
+ // been removed, the group has to give the tab stop to somebody else, or Tab skips it.
20
+ let selectionDetached = $state(false);
21
+ const radios = new SvelteMap();
22
+ const radioOrder = $state([]);
23
+ function getOrderedValues() {
24
+ const connected = [];
25
+ const detachedValues = [];
26
+ for (const value of radioOrder) {
27
+ const element = radios.get(value)?.element;
28
+ if (element?.isConnected) {
29
+ connected.push({ value, element });
30
+ }
31
+ else {
32
+ detachedValues.push(value);
33
+ }
34
+ }
35
+ connected.sort((left, right) => left.element.compareDocumentPosition(right.element) & Node.DOCUMENT_POSITION_FOLLOWING
36
+ ? -1
37
+ : 1);
38
+ return [...connected.map((entry) => entry.value), ...detachedValues];
39
+ }
40
+ function getEnabledValues() {
41
+ return getOrderedValues().filter((value) => radios.has(value) && !isRadioDisabled(value));
42
+ }
43
+ function getFirstEnabledValue() {
44
+ return getEnabledValues()[0] ?? null;
45
+ }
46
+ function getLastEnabledValue() {
47
+ const enabledValues = getEnabledValues();
48
+ return enabledValues[enabledValues.length - 1] ?? null;
49
+ }
50
+ function getNextEnabledValue(current, direction) {
51
+ const enabledValues = getEnabledValues();
52
+ if (enabledValues.length === 0)
53
+ return null;
54
+ if (current === null) {
55
+ return direction === 1 ? enabledValues[0] : enabledValues[enabledValues.length - 1];
56
+ }
57
+ const currentIndex = enabledValues.indexOf(current);
58
+ if (currentIndex < 0) {
59
+ return direction === 1 ? enabledValues[0] : enabledValues[enabledValues.length - 1];
60
+ }
61
+ // Wraps at both ends, which the APG asks for by name.
62
+ return (enabledValues[(currentIndex + direction + enabledValues.length) % enabledValues.length] ??
63
+ null);
64
+ }
65
+ // The single tab stop of the group. The APG puts it on the checked radio, and on the first
66
+ // enabled one while nothing is checked, so that Tab enters the group where the user left it.
67
+ function getTabStopValue() {
68
+ if (selectedValue !== null && !isRadioDisabled(selectedValue) && !selectionDetached) {
69
+ return selectedValue;
70
+ }
71
+ if (focusedValue !== null && !isRadioDisabled(focusedValue))
72
+ return focusedValue;
73
+ return getFirstEnabledValue();
74
+ }
75
+ function setSelection(next, changeOptions) {
76
+ if (selectedValue === next)
77
+ return false;
78
+ selectedValue = next;
79
+ selectionDetached = next !== null && !radios.has(next) && radioOrder.length > 0;
80
+ if (changeOptions?.notify !== false && next !== null) {
81
+ options.onValueChange?.(next);
82
+ }
83
+ return true;
84
+ }
85
+ function registerRadio(value, registerOptions) {
86
+ const existingRadio = radios.get(value);
87
+ if (existingRadio !== undefined &&
88
+ existingRadio.owner !== undefined &&
89
+ registerOptions.owner !== undefined &&
90
+ existingRadio.owner !== registerOptions.owner) {
91
+ throw new Error('RadioGroup.Item values must be unique within a RadioGroup.Root.');
92
+ }
93
+ radios.set(value, {
94
+ value,
95
+ isDisabled: Boolean(registerOptions.isDisabled),
96
+ element: registerOptions.element,
97
+ owner: registerOptions.owner
98
+ });
99
+ if (existingRadio === undefined) {
100
+ radioOrder.push(value);
101
+ }
102
+ if (selectedValue === value) {
103
+ selectionDetached = false;
104
+ }
105
+ if (focusedValue !== null && focusedValue === value && isRadioDisabled(value)) {
106
+ focusedValue = null;
107
+ focusVisible = false;
108
+ }
109
+ }
110
+ function unregisterRadio(value) {
111
+ if (!radios.has(value))
112
+ return;
113
+ radios.delete(value);
114
+ const index = radioOrder.indexOf(value);
115
+ if (index >= 0) {
116
+ radioOrder.splice(index, 1);
117
+ }
118
+ if (focusedValue === value) {
119
+ focusedValue = null;
120
+ }
121
+ if (selectedValue === value) {
122
+ selectionDetached = true;
123
+ }
124
+ // The selection survives on purpose, live or not. A radio that leaves the tree takes its
125
+ // hidden input with it, so the form stops submitting that answer, which is what a native
126
+ // radio group does. Clearing the value on top of that would rewrite the state of a parent
127
+ // that still holds it, and the answer would be lost for good if the radio came back.
128
+ }
129
+ function setName(nextName) {
130
+ if (name === nextName)
131
+ return;
132
+ name = nextName;
133
+ }
134
+ function setForm(nextForm) {
135
+ if (form === nextForm)
136
+ return;
137
+ form = nextForm;
138
+ }
139
+ function setDisabled(disabled) {
140
+ if (isDisabled === disabled)
141
+ return;
142
+ isDisabled = disabled;
143
+ if (isDisabled) {
144
+ focusedValue = null;
145
+ focusVisible = false;
146
+ }
147
+ }
148
+ function setReadOnly(readonly) {
149
+ if (isReadOnly === readonly)
150
+ return;
151
+ isReadOnly = readonly;
152
+ }
153
+ function setRequired(required) {
154
+ if (isRequired === required)
155
+ return;
156
+ isRequired = required;
157
+ }
158
+ function setOrientation(nextOrientation) {
159
+ if (orientation === nextOrientation)
160
+ return;
161
+ orientation = nextOrientation;
162
+ }
163
+ function setSelectedValue(value) {
164
+ setSelection(value ?? null, { notify: false });
165
+ }
166
+ // There is no unselecting: a radio group answers a question, and the platform gives the user
167
+ // no way to take the answer back. Thus this only ever sets.
168
+ function selectValue(value) {
169
+ if (isRadioDisabled(value) || isReadOnly)
170
+ return false;
171
+ return setSelection(value);
172
+ }
173
+ function setFocusedValue(value) {
174
+ if (focusedValue === value)
175
+ return;
176
+ focusedValue = value;
177
+ }
178
+ function focusValue(value) {
179
+ if (isRadioDisabled(value))
180
+ return;
181
+ setFocusedValue(value);
182
+ radios.get(value)?.element?.focus();
183
+ }
184
+ function setFocusVisible(visible) {
185
+ if (focusVisible === visible)
186
+ return;
187
+ focusVisible = visible;
188
+ }
189
+ function isSelected(value) {
190
+ return selectedValue === value;
191
+ }
192
+ function isFocused(value) {
193
+ return focusedValue === value;
194
+ }
195
+ function isFocusVisible(value) {
196
+ return focusVisible && isFocused(value);
197
+ }
198
+ function isRadioDisabled(value) {
199
+ return isDisabled || Boolean(radios.get(value)?.isDisabled);
200
+ }
201
+ function isRegisteredElement(element) {
202
+ return radioOrder.some((value) => radios.get(value)?.element === element);
203
+ }
204
+ function getTabIndex(value) {
205
+ if (isRadioDisabled(value))
206
+ return -1;
207
+ return getTabStopValue() === value ? 0 : -1;
208
+ }
209
+ return {
210
+ get name() {
211
+ return name;
212
+ },
213
+ get form() {
214
+ return form;
215
+ },
216
+ get isDisabled() {
217
+ return isDisabled;
218
+ },
219
+ get isReadOnly() {
220
+ return isReadOnly;
221
+ },
222
+ get isRequired() {
223
+ return isRequired;
224
+ },
225
+ get labelledBy() {
226
+ return labelIds.length > 0 ? labelIds.join(' ') : undefined;
227
+ },
228
+ get orientation() {
229
+ return orientation;
230
+ },
231
+ get selectedValue() {
232
+ return selectedValue;
233
+ },
234
+ get focusedValue() {
235
+ return focusedValue;
236
+ },
237
+ registerRadio: asCommand(registerRadio),
238
+ unregisterRadio: asCommand(unregisterRadio),
239
+ setName: asCommand(setName),
240
+ setForm: asCommand(setForm),
241
+ setDisabled: asCommand(setDisabled),
242
+ setReadOnly: asCommand(setReadOnly),
243
+ setRequired: asCommand(setRequired),
244
+ setOrientation: asCommand(setOrientation),
245
+ setSelectedValue: asCommand(setSelectedValue),
246
+ selectValue: asCommand(selectValue),
247
+ setFocusedValue: asCommand(setFocusedValue),
248
+ focusValue: asCommand(focusValue),
249
+ setFocusVisible: asCommand(setFocusVisible),
250
+ // `untrack` because the caller registers from an effect: reading `labelIds` here would
251
+ // make that effect depend on what it writes, and the pair would run without end.
252
+ registerLabel(id) {
253
+ untrack(() => {
254
+ labelIds = [...labelIds, id];
255
+ });
256
+ return () => untrack(() => {
257
+ labelIds = labelIds.filter((candidate) => candidate !== id);
258
+ });
259
+ },
260
+ isSelected,
261
+ isFocused,
262
+ isFocusVisible,
263
+ isRadioDisabled,
264
+ isRegisteredElement,
265
+ getTabIndex,
266
+ getEnabledValues,
267
+ getNextEnabledValue,
268
+ getFirstEnabledValue,
269
+ getLastEnabledValue
270
+ };
271
+ }
272
+ export function setRadioGroupContext(context) {
273
+ return setContext(RADIO_GROUP_CONTEXT_KEY, context);
274
+ }
275
+ export function getRadioGroupContext() {
276
+ return getContext(RADIO_GROUP_CONTEXT_KEY);
277
+ }
278
+ export function useRadioGroupContext() {
279
+ const context = getRadioGroupContext();
280
+ if (!context) {
281
+ throw new Error('RadioGroup.Item must be used within RadioGroup.Root.');
282
+ }
283
+ return context;
284
+ }
@@ -0,0 +1,193 @@
1
+ <script lang="ts">
2
+ import { untrack } from 'svelte';
3
+ import type { RadioGroupRootProps } from '../types.js';
4
+ import { createRadioGroupContext, setRadioGroupContext } from './context.svelte';
5
+ import { trackInteractionModality } from '../../primitives/input-modality';
6
+ import { isRtl } from '../../internal/rtl';
7
+
8
+ type RadioGroupKeyboardEvent = KeyboardEvent & {
9
+ currentTarget: EventTarget & HTMLDivElement;
10
+ };
11
+ type RadioGroupMouseEvent = MouseEvent & {
12
+ currentTarget: EventTarget & HTMLDivElement;
13
+ };
14
+
15
+ const generatedId = $props.id();
16
+
17
+ let {
18
+ id,
19
+ value = $bindable(),
20
+ defaultValue,
21
+ onChange,
22
+ name,
23
+ form,
24
+ disabled: disabledProp = false,
25
+ readonly: readOnlyProp = false,
26
+ required: requiredProp = false,
27
+ orientation = 'vertical',
28
+ children,
29
+ class: className = '',
30
+ element = $bindable<HTMLDivElement | null>(null),
31
+ context = $bindable(),
32
+ onkeydown: onKeyDownExternal,
33
+ onmousedown: onMouseDownExternal,
34
+ 'aria-labelledby': ariaLabelledBy,
35
+ ...restProps
36
+ }: RadioGroupRootProps = $props();
37
+
38
+ const instanceId = untrack(() => id) ?? generatedId;
39
+
40
+ let rootRef: HTMLDivElement | null = $state(null);
41
+
42
+ const radioGroup = setRadioGroupContext(
43
+ createRadioGroupContext({
44
+ // `value` seeds the initial selection whenever it is supplied, bound or not.
45
+ initialValue: untrack(() => value) ?? untrack(() => defaultValue),
46
+ name: (() => name)(),
47
+ form: (() => form)(),
48
+ isDisabled: (() => disabledProp)(),
49
+ isReadOnly: (() => readOnlyProp)(),
50
+ isRequired: (() => requiredProp)(),
51
+ orientation: (() => orientation)(),
52
+ onValueChange: (nextValue) => {
53
+ // One path for both usages: with `bind:value` this reaches the parent, and without a
54
+ // binding the write stays local, so nothing here has to know which one the caller chose.
55
+ value = nextValue;
56
+ onChange?.(nextValue);
57
+ }
58
+ })
59
+ );
60
+
61
+ context = radioGroup;
62
+
63
+ const disabled = $derived(radioGroup.isDisabled);
64
+ const readOnly = $derived(radioGroup.isReadOnly);
65
+ const required = $derived(radioGroup.isRequired);
66
+ const currentOrientation = $derived(radioGroup.orientation);
67
+
68
+ $effect(() => {
69
+ element = rootRef;
70
+ });
71
+
72
+ $effect(() => {
73
+ radioGroup.setName(name);
74
+ });
75
+
76
+ $effect(() => {
77
+ radioGroup.setForm(form);
78
+ });
79
+
80
+ $effect(() => {
81
+ radioGroup.setDisabled(disabledProp);
82
+ });
83
+
84
+ $effect(() => {
85
+ radioGroup.setReadOnly(readOnlyProp);
86
+ });
87
+
88
+ $effect(() => {
89
+ radioGroup.setRequired(requiredProp);
90
+ });
91
+
92
+ $effect(() => {
93
+ radioGroup.setOrientation(orientation);
94
+ });
95
+
96
+ // A parent that supplies `value` drives the selection, bound or not. Latched at init, not
97
+ // reactive — re-checking `value !== undefined` would switch this on the moment our own
98
+ // write-back defines it, and a `defaultValue`-only group would start following the echo of
99
+ // its own changes.
100
+ const adoptsValueProp = untrack(() => value !== undefined);
101
+
102
+ $effect(() => {
103
+ if (!adoptsValueProp) return;
104
+ radioGroup.setSelectedValue(value);
105
+ });
106
+
107
+ function getEventRadioTarget(event: KeyboardEvent | MouseEvent) {
108
+ const target = event.target;
109
+ if (!(target instanceof Element)) return target;
110
+ return target.closest('[data-radio-group-item="true"]') ?? target;
111
+ }
112
+
113
+ // The selection follows the focus, which is the rule that parts a radio group from every other
114
+ // group in this library: the arrows do not only move, they answer the question.
115
+ function moveSelection(nextValue: string | null) {
116
+ if (nextValue === null) return;
117
+ radioGroup.setFocusVisible(true);
118
+ radioGroup.focusValue(nextValue);
119
+ radioGroup.selectValue(nextValue);
120
+ }
121
+
122
+ function handleKeyDown(event: RadioGroupKeyboardEvent) {
123
+ onKeyDownExternal?.(event);
124
+ if (event.defaultPrevented) return;
125
+
126
+ const radioTarget = getEventRadioTarget(event);
127
+ if (!radioGroup.isRegisteredElement(radioTarget)) return;
128
+
129
+ trackInteractionModality(event, radioTarget as HTMLElement | null);
130
+
131
+ if (radioGroup.isDisabled) return;
132
+
133
+ const currentValue = radioGroup.focusedValue ?? radioGroup.selectedValue;
134
+ // In horizontal orientation the arrows are physical: under RTL the visual "next" radio is
135
+ // the previous one in DOM order (APG). Both axes work in either orientation, which is what
136
+ // the APG describes and what a native radio group does.
137
+ const rtl = isRtl(event.currentTarget);
138
+ const nextKeys = ['ArrowDown', rtl ? 'ArrowLeft' : 'ArrowRight'];
139
+ const previousKeys = ['ArrowUp', rtl ? 'ArrowRight' : 'ArrowLeft'];
140
+
141
+ if (nextKeys.includes(event.key)) {
142
+ event.preventDefault();
143
+ moveSelection(radioGroup.getNextEnabledValue(currentValue, 1));
144
+ return;
145
+ }
146
+
147
+ if (previousKeys.includes(event.key)) {
148
+ event.preventDefault();
149
+ moveSelection(radioGroup.getNextEnabledValue(currentValue, -1));
150
+ return;
151
+ }
152
+
153
+ if (event.key === 'Home') {
154
+ event.preventDefault();
155
+ moveSelection(radioGroup.getFirstEnabledValue());
156
+ return;
157
+ }
158
+
159
+ if (event.key === 'End') {
160
+ event.preventDefault();
161
+ moveSelection(radioGroup.getLastEnabledValue());
162
+ }
163
+ }
164
+
165
+ function handleMouseDown(event: RadioGroupMouseEvent) {
166
+ const radioTarget = getEventRadioTarget(event);
167
+ trackInteractionModality(event, radioTarget as HTMLElement | null);
168
+ radioGroup.setFocusVisible(false);
169
+ onMouseDownExternal?.(event);
170
+ }
171
+ </script>
172
+
173
+ <div
174
+ {...restProps}
175
+ bind:this={rootRef}
176
+ id={instanceId}
177
+ role="radiogroup"
178
+ aria-labelledby={ariaLabelledBy ?? radioGroup.labelledBy}
179
+ aria-orientation={currentOrientation}
180
+ aria-required={required || undefined}
181
+ aria-disabled={disabled || undefined}
182
+ aria-readonly={readOnly || undefined}
183
+ class={className}
184
+ data-radio-group-root="true"
185
+ data-orientation={currentOrientation}
186
+ data-disabled={disabled || undefined}
187
+ data-readonly={readOnly || undefined}
188
+ data-required={required || undefined}
189
+ onkeydown={handleKeyDown}
190
+ onmousedown={handleMouseDown}
191
+ >
192
+ {@render children?.()}
193
+ </div>
@@ -0,0 +1,4 @@
1
+ import type { RadioGroupRootProps } from '../types.js';
2
+ declare const RadioGroupRoot: import("svelte").Component<RadioGroupRootProps, {}, "value" | "element" | "context">;
3
+ type RadioGroupRoot = ReturnType<typeof RadioGroupRoot>;
4
+ export default RadioGroupRoot;
@@ -0,0 +1,50 @@
1
+ import type { Snippet } from 'svelte';
2
+ import type { HTMLAttributes } from 'svelte/elements';
3
+ import type { RadioGroupContext, RadioGroupOrientation, RadioGroupValue } from './root/context.svelte.js';
4
+ export type { RadioGroupContext, RadioGroupOrientation, RadioGroupValue } from './root/context.svelte.js';
5
+ export type RadioGroupRootProps = Omit<HTMLAttributes<HTMLDivElement>, 'children' | 'class' | 'role' | 'aria-orientation'> & {
6
+ /**
7
+ * The selected value. When you give this prop, it is the source of truth, with `bind:value` or
8
+ * without it. The component writes each change here and reports it through `onChange`.
9
+ *
10
+ * Without a binding, that write stays local. Thus a parent that hears `onChange` and refuses the
11
+ * change — it sends no new `value` down — sees the group move. The group then goes back to the
12
+ * value of the parent at the next render.
13
+ */
14
+ value?: RadioGroupValue | null;
15
+ /** The selected value at the start, for when you give no `value`. */
16
+ defaultValue?: RadioGroupValue | null;
17
+ /** The component calls it when the user selects a radio. */
18
+ onChange?: (value: RadioGroupValue) => void;
19
+ /** The `name` of the hidden input of every radio in the group, for form submission. */
20
+ name?: string;
21
+ /** The id of the form of the hidden inputs. */
22
+ form?: string;
23
+ /** Disables each radio in the group. */
24
+ disabled?: boolean;
25
+ /** Shows the value of the group, and refuses each change from the user. */
26
+ readonly?: boolean;
27
+ /** Marks the group with `aria-required`, and marks each hidden input as required. */
28
+ required?: boolean;
29
+ /**
30
+ * The orientation of the layout. It sets `aria-orientation` and `data-orientation`, and it
31
+ * chooses the arrow keys that move the focus.
32
+ */
33
+ orientation?: RadioGroupOrientation;
34
+ /** The radios of the group. */
35
+ children?: Snippet;
36
+ class?: string;
37
+ /** The id of the group element. The component makes one when you give none. */
38
+ id?: string;
39
+ /** The group element. Use `bind:element` to read it. */
40
+ element?: HTMLDivElement | null;
41
+ /** The state of the group. Use `bind:context` to read the selection from outside. */
42
+ context?: RadioGroupContext;
43
+ };
44
+ export type RadioGroupLabelProps = Omit<HTMLAttributes<HTMLSpanElement>, 'children' | 'class' | 'id'> & {
45
+ /** The name of the group. */
46
+ children?: Snippet;
47
+ class?: string;
48
+ /** The id of the label element. The component makes one when you give none. */
49
+ id?: string;
50
+ };
@@ -0,0 +1 @@
1
+ export {};
@@ -5,7 +5,7 @@
5
5
  ### Switch.Root
6
6
 
7
7
  Name: `Switch.Root`
8
- Description: Interactive boolean switch root that owns checked, focus, and hidden input synchronization.
8
+ Description: The root of a boolean switch. It holds the checked state and the focus, and it keeps the hidden input correct.
9
9
 
10
10
  | Prop | Type | Default | Description |
11
11
  | ----------------- | --------------------------------- | ------------- | ---------------------------------------------------------- |
@@ -20,7 +20,7 @@ Description: Interactive boolean switch root that owns checked, focus, and hidde
20
20
  | `readonly` | `boolean` | `false` | Allows focus but blocks user-driven state changes. |
21
21
  | `required` | `boolean` | `false` | Marks the hidden input as required and exposes data state. |
22
22
  | `children` | `Snippet` | `undefined` | Composed switch parts such as `Switch.Thumb`. |
23
- | `class` | `string` | `''` | CSS class names for the root element. |
23
+ | `class` | `string` | `''` | The CSS class names of the root element. |
24
24
  | `...restProps` | `HTMLAttributes<HTMLSpanElement>` | `-` | Additional attributes forwarded to the switch root span. |
25
25
 
26
26
  ### Context utilities
@@ -5,6 +5,7 @@
5
5
  shouldShowFocusVisible,
6
6
  trackInteractionModality
7
7
  } from '../../primitives/input-modality';
8
+ import { watchFocusVisible } from '../../primitives/focus-visible.svelte';
8
9
  import { setSwitchContext, type SwitchContext } from './context';
9
10
 
10
11
  type SwitchRootProps = Omit<
@@ -21,42 +22,47 @@
21
22
  | 'onkeydown'
22
23
  | 'value'
23
24
  > & {
24
- /** Id applied to the hidden input so `label[for]` can target the switch. Auto-generated when omitted. */
25
+ /**
26
+ * The id of the hidden input, thus a `label[for]` element can point at the switch. If you give
27
+ * none, the component makes one.
28
+ */
25
29
  id?: string;
26
- /** Bindable reference to the rendered root element. */
30
+ /** A bindable reference to the root element. */
27
31
  element?: HTMLSpanElement | null;
28
- /** Name submitted with the form when the switch is checked. */
32
+ /** The name that the form submits while the switch is on. */
29
33
  name?: string;
30
- /** Value submitted with the form when the switch is checked. Does not affect the visual state. */
34
+ /**
35
+ * The value that the form submits while the switch is on. It does not change the visual state.
36
+ */
31
37
  value?: string;
32
- /** Associates the hidden input with a form by id. */
38
+ /** The id of the form of the hidden input. */
33
39
  form?: string;
34
- /** Checked state. Two-way by default — use `bind:checked`. */
40
+ /** The checked state. By default it goes in the two directions: use `bind:checked`. */
35
41
  checked?: boolean;
36
- /** Initial checked state, for when `checked` is not supplied. */
42
+ /** The checked state at the start, for when you give no `checked`. */
37
43
  defaultChecked?: boolean;
38
44
  /**
39
- * Opt into fully controlled state: the component stops writing back to `checked`
40
- * and only reports through `onCheckedChange`, so the parent can reject a change by
41
- * not flowing the new value back down. Off by default, because `bind:checked` —
42
- * the common case — needs the write-back to work at all.
45
+ * Give your own code full control of the state. The component stops to write back to `checked`,
46
+ * and it reports only through `onCheckedChange`. Thus the parent can refuse a change: the parent
47
+ * does not send the new value down. The default is off, because `bind:checked` is the usual case
48
+ * and it needs the write-back.
43
49
  */
44
50
  controlledChecked?: boolean;
45
- /** Called when the user toggles the switch. */
51
+ /** The component calls it when the user changes the switch. */
46
52
  onCheckedChange?: (checked: boolean) => void;
47
- /** Removes the switch from interaction and focus order. */
53
+ /** Stops the interaction with the switch and removes it from the focus order. */
48
54
  disabled?: boolean;
49
- /** Keeps the switch focusable while preventing state changes. */
55
+ /** Keeps the switch focusable, but stops the changes of the state. */
50
56
  readonly?: boolean;
51
- /** Marks the hidden input as required for form validation. */
57
+ /** Makes the hidden input required for the form validation. */
52
58
  required?: boolean;
53
59
  children?: Snippet;
54
60
  class?: string;
55
- /** Accessible label when no visible label is associated. */
61
+ /** The accessible name, for when the user sees no name. */
56
62
  'aria-label'?: string;
57
- /** Id of the element that labels the switch. */
63
+ /** The id of the element that gives the switch its name. */
58
64
  'aria-labelledby'?: string;
59
- /** Overrides the default tab order. */
65
+ /** Replaces the default position in the tab order. */
60
66
  tabindex?: number;
61
67
  onclick?: HTMLAttributes<HTMLSpanElement>['onclick'];
62
68
  onkeydown?: HTMLAttributes<HTMLSpanElement>['onkeydown'];
@@ -131,6 +137,14 @@
131
137
  let pressedKey: 'Enter' | 'Space' | null = $state(null);
132
138
  let focused = $state(false);
133
139
  let focusVisible = $state(false);
140
+
141
+ // The focus ring must appear when a pointer press is followed by a key press, and the
142
+ // modality alone changes there — no focus event fires to re-read it.
143
+ watchFocusVisible({
144
+ isFocused: () => focused,
145
+ element: () => rootRef,
146
+ set: (visible) => (focusVisible = visible)
147
+ });
134
148
  let rootRef: HTMLSpanElement | null = $state(null);
135
149
  let inputRef: HTMLInputElement | null = $state(null);
136
150