@human-kit/ui 1.0.0-beta.6 → 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.
- package/dist/autocomplete/item/autocomplete-item.svelte +10 -0
- package/dist/button/root/button-root.svelte +9 -0
- package/dist/calendar/body-cell/calendar-body-cell.svelte +7 -0
- package/dist/checkbox/indicator/checkbox-indicator.svelte +1 -0
- package/dist/checkbox/indicator/checkbox-indicator.svelte.d.ts +1 -0
- package/dist/checkbox/root/checkbox-root.svelte +106 -30
- package/dist/checkbox/root/checkbox-root.svelte.d.ts +5 -0
- package/dist/checkbox-group/README.md +95 -0
- package/dist/checkbox-group/index.d.ts +15 -0
- package/dist/checkbox-group/index.js +6 -0
- package/dist/checkbox-group/index.parts.d.ts +4 -0
- package/dist/checkbox-group/index.parts.js +7 -0
- package/dist/checkbox-group/label/checkbox-group-label.svelte +36 -0
- package/dist/checkbox-group/label/checkbox-group-label.svelte.d.ts +4 -0
- package/dist/checkbox-group/root/checkbox-group-root.svelte +118 -0
- package/dist/checkbox-group/root/checkbox-group-root.svelte.d.ts +4 -0
- package/dist/checkbox-group/root/context.svelte.d.ts +52 -0
- package/dist/checkbox-group/root/context.svelte.js +251 -0
- package/dist/checkbox-group/types.d.ts +57 -0
- package/dist/checkbox-group/types.js +1 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +4 -0
- package/dist/primitives/focus-visible.svelte.d.ts +14 -0
- package/dist/primitives/focus-visible.svelte.js +19 -0
- package/dist/primitives/index.d.ts +1 -0
- package/dist/primitives/index.js +1 -0
- package/dist/primitives/input-modality.d.ts +7 -0
- package/dist/primitives/input-modality.js +32 -5
- package/dist/radio-group/README.md +103 -0
- package/dist/radio-group/index.d.ts +18 -0
- package/dist/radio-group/index.js +9 -0
- package/dist/radio-group/index.parts.d.ts +4 -0
- package/dist/radio-group/index.parts.js +4 -0
- package/dist/radio-group/indicator/radio-group-indicator.svelte +43 -0
- package/dist/radio-group/indicator/radio-group-indicator.svelte.d.ts +12 -0
- package/dist/radio-group/item/context.d.ts +18 -0
- package/dist/radio-group/item/context.js +15 -0
- package/dist/radio-group/item/radio-group-item.svelte +377 -0
- package/dist/radio-group/item/radio-group-item.svelte.d.ts +35 -0
- package/dist/radio-group/label/radio-group-label.svelte +37 -0
- package/dist/radio-group/label/radio-group-label.svelte.d.ts +4 -0
- package/dist/radio-group/root/context.svelte.d.ts +57 -0
- package/dist/radio-group/root/context.svelte.js +284 -0
- package/dist/radio-group/root/radio-group-root.svelte +193 -0
- package/dist/radio-group/root/radio-group-root.svelte.d.ts +4 -0
- package/dist/radio-group/types.d.ts +50 -0
- package/dist/radio-group/types.js +1 -0
- package/dist/switch/root/switch-root.svelte +9 -0
- package/dist/table/column-header-cell/table-column-header-cell.svelte +24 -0
- package/dist/table/root/table-root.svelte +29 -6
- package/dist/timepicker/input/time-picker-input.svelte +12 -4
- package/dist/timepicker/input/time-picker-input.svelte.d.ts +12 -4
- package/dist/toggle/root/toggle-root.svelte +28 -0
- package/dist/toggle/root/toggle-root.svelte.d.ts +19 -0
- package/dist/toggle-group/README.md +6 -4
- package/dist/toggle-group/index.d.ts +2 -0
- package/dist/toggle-group/index.parts.d.ts +1 -0
- package/dist/toggle-group/index.parts.js +4 -0
- 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,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,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<
|
|
@@ -136,6 +137,14 @@
|
|
|
136
137
|
let pressedKey: 'Enter' | 'Space' | null = $state(null);
|
|
137
138
|
let focused = $state(false);
|
|
138
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
|
+
});
|
|
139
148
|
let rootRef: HTMLSpanElement | null = $state(null);
|
|
140
149
|
let inputRef: HTMLInputElement | null = $state(null);
|
|
141
150
|
|
|
@@ -11,6 +11,8 @@
|
|
|
11
11
|
shouldShowFocusVisible,
|
|
12
12
|
trackInteractionModality
|
|
13
13
|
} from '../../primitives/input-modality';
|
|
14
|
+
|
|
15
|
+
import { watchFocusVisible } from '../../primitives/focus-visible.svelte';
|
|
14
16
|
import { isRtl } from '../../internal/rtl';
|
|
15
17
|
|
|
16
18
|
let { children, class: className = '', ...restProps }: TableColumnHeaderCellProps = $props();
|
|
@@ -174,6 +176,28 @@
|
|
|
174
176
|
isFocusVisibleWithin = false;
|
|
175
177
|
}
|
|
176
178
|
|
|
179
|
+
function getFocusedDescendant() {
|
|
180
|
+
if (!element) return null;
|
|
181
|
+
const active = element.ownerDocument.activeElement;
|
|
182
|
+
if (!(active instanceof HTMLElement) || active === element) return null;
|
|
183
|
+
return element.contains(active) ? active : null;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
watchFocusVisible({
|
|
187
|
+
isFocused: () => isElementFocused,
|
|
188
|
+
element: () => element ?? null,
|
|
189
|
+
set: (visible) => {
|
|
190
|
+
isElementFocusVisible = visible;
|
|
191
|
+
table.setFocusVisible(visible);
|
|
192
|
+
}
|
|
193
|
+
});
|
|
194
|
+
|
|
195
|
+
watchFocusVisible({
|
|
196
|
+
isFocused: () => isFocusWithin,
|
|
197
|
+
element: getFocusedDescendant,
|
|
198
|
+
set: (visible) => (isFocusVisibleWithin = visible)
|
|
199
|
+
});
|
|
200
|
+
|
|
177
201
|
function handleClick() {
|
|
178
202
|
// A drag-resize that just ended emits a residual click on the header;
|
|
179
203
|
// consume the one-shot suppression flag BEFORE focusing the cell.
|
|
@@ -21,6 +21,7 @@
|
|
|
21
21
|
shouldShowFocusVisible,
|
|
22
22
|
trackInteractionModality
|
|
23
23
|
} from '../../primitives/input-modality';
|
|
24
|
+
import { watchFocusVisible } from '../../primitives/focus-visible.svelte';
|
|
24
25
|
import { visuallyHiddenStyle } from '../utils/visually-hidden-style';
|
|
25
26
|
import type { TableRootProps } from '../types.js';
|
|
26
27
|
import {
|
|
@@ -499,30 +500,52 @@
|
|
|
499
500
|
focusWithin =
|
|
500
501
|
!!tableElement && !!document.activeElement && tableElement.contains(document.activeElement);
|
|
501
502
|
if (!focusWithin) {
|
|
502
|
-
|
|
503
|
+
setFocusVisible(false);
|
|
503
504
|
ctx.setFocusedCell(null);
|
|
504
505
|
}
|
|
505
506
|
}
|
|
506
507
|
|
|
507
508
|
function handleFocusIn(event: FocusEvent) {
|
|
508
509
|
focusWithin = true;
|
|
509
|
-
|
|
510
|
+
setFocusVisible(shouldShowFocusVisible(event.target as HTMLElement | null));
|
|
510
511
|
}
|
|
511
512
|
|
|
513
|
+
// Every part of the table reads one focus-visible value, and each writes it from its own
|
|
514
|
+
// focus handler. One watch on the focused descendant therefore serves them all: a key press
|
|
515
|
+
// that follows a pointer press brings the ring back on the cell, the row and the checkbox
|
|
516
|
+
// at once.
|
|
517
|
+
function getFocusedDescendant() {
|
|
518
|
+
if (!tableElement) return null;
|
|
519
|
+
const active = tableElement.ownerDocument.activeElement;
|
|
520
|
+
if (!(active instanceof HTMLElement)) return null;
|
|
521
|
+
return tableElement.contains(active) ? active : null;
|
|
522
|
+
}
|
|
523
|
+
|
|
524
|
+
function setFocusVisible(visible: boolean) {
|
|
525
|
+
focusVisible = visible;
|
|
526
|
+
ctx.setFocusVisible(visible);
|
|
527
|
+
}
|
|
528
|
+
|
|
529
|
+
watchFocusVisible({
|
|
530
|
+
isFocused: () => focusWithin,
|
|
531
|
+
element: getFocusedDescendant,
|
|
532
|
+
set: setFocusVisible
|
|
533
|
+
});
|
|
534
|
+
|
|
512
535
|
function handleFocusOut() {
|
|
513
536
|
queueMicrotask(syncFocusWithin);
|
|
514
537
|
}
|
|
515
538
|
|
|
516
539
|
function handleMouseDown(event: MouseEvent) {
|
|
517
540
|
trackInteractionModality(event, event.target as HTMLElement | null);
|
|
518
|
-
|
|
541
|
+
setFocusVisible(false);
|
|
519
542
|
}
|
|
520
543
|
|
|
544
|
+
// No focus-visible write here: `trackInteractionModality` reports the key to the modality,
|
|
545
|
+
// and the watch above answers. A key that carries a modifier is not a modality change, and
|
|
546
|
+
// must leave the ring as the pointer left it.
|
|
521
547
|
function handleKeyDown(event: KeyboardEvent) {
|
|
522
548
|
trackInteractionModality(event, event.target as HTMLElement | null);
|
|
523
|
-
if (focusWithin) {
|
|
524
|
-
focusVisible = true;
|
|
525
|
-
}
|
|
526
549
|
}
|
|
527
550
|
</script>
|
|
528
551
|
|
|
@@ -14,17 +14,25 @@
|
|
|
14
14
|
HTMLAttributes<HTMLDivElement>,
|
|
15
15
|
'children' | 'class' | 'id' | 'role' | 'tabindex' | 'aria-invalid'
|
|
16
16
|
> & {
|
|
17
|
+
/**
|
|
18
|
+
* The id of the hidden proxy input. A `<label for>` points to it, and the segmented group
|
|
19
|
+
* then takes a derived id.
|
|
20
|
+
*/
|
|
17
21
|
id?: string;
|
|
18
22
|
/**
|
|
19
|
-
* Name of the hidden proxy input
|
|
20
|
-
*
|
|
21
|
-
*
|
|
22
|
-
*
|
|
23
|
+
* Name of the hidden proxy input for form submission. The proxy shows only the committed
|
|
24
|
+
* value. It does not support autofill: it renders with `autocomplete="off"`, thus browsers
|
|
25
|
+
* and password managers do not write into it. The user gives the time through the editable
|
|
26
|
+
* segments.
|
|
23
27
|
*/
|
|
24
28
|
name?: string;
|
|
25
29
|
children?: Snippet<[TimePickerSegmentPart]>;
|
|
26
30
|
class?: string;
|
|
27
31
|
'aria-label'?: string;
|
|
32
|
+
/**
|
|
33
|
+
* Marks the input as invalid. The group is also invalid while a segment draft is not
|
|
34
|
+
* committable.
|
|
35
|
+
*/
|
|
28
36
|
'aria-invalid'?: HTMLAttributes<HTMLDivElement>['aria-invalid'];
|
|
29
37
|
};
|
|
30
38
|
|