@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,118 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import { untrack } from 'svelte';
|
|
3
|
+
import type { CheckboxGroupRootProps } from '../types.js';
|
|
4
|
+
import { createCheckboxGroupContext, setCheckboxGroupContext } from './context.svelte';
|
|
5
|
+
|
|
6
|
+
const generatedId = $props.id();
|
|
7
|
+
|
|
8
|
+
let {
|
|
9
|
+
id,
|
|
10
|
+
value = $bindable(),
|
|
11
|
+
defaultValue,
|
|
12
|
+
onChange,
|
|
13
|
+
name,
|
|
14
|
+
disabled: disabledProp = false,
|
|
15
|
+
readonly: readOnlyProp = false,
|
|
16
|
+
required: requiredProp = false,
|
|
17
|
+
orientation = 'vertical',
|
|
18
|
+
children,
|
|
19
|
+
class: className = '',
|
|
20
|
+
element = $bindable<HTMLDivElement | null>(null),
|
|
21
|
+
context = $bindable(),
|
|
22
|
+
'aria-labelledby': ariaLabelledBy,
|
|
23
|
+
...restProps
|
|
24
|
+
}: CheckboxGroupRootProps = $props();
|
|
25
|
+
|
|
26
|
+
const instanceId = untrack(() => id) ?? generatedId;
|
|
27
|
+
|
|
28
|
+
let rootRef: HTMLDivElement | null = $state(null);
|
|
29
|
+
|
|
30
|
+
const checkboxGroup = setCheckboxGroupContext(
|
|
31
|
+
createCheckboxGroupContext({
|
|
32
|
+
// `value` seeds the initial selection whenever it is supplied, bound or not.
|
|
33
|
+
initialValue: untrack(() => value) ?? untrack(() => defaultValue),
|
|
34
|
+
name: (() => name)(),
|
|
35
|
+
isDisabled: (() => disabledProp)(),
|
|
36
|
+
isReadOnly: (() => readOnlyProp)(),
|
|
37
|
+
isRequired: (() => requiredProp)(),
|
|
38
|
+
orientation: (() => orientation)(),
|
|
39
|
+
onValueChange: (nextValue) => {
|
|
40
|
+
// One path for both usages: with `bind:value` this reaches the parent, and
|
|
41
|
+
// without a binding the write stays local, so nothing here has to know
|
|
42
|
+
// which one the caller chose.
|
|
43
|
+
value = nextValue;
|
|
44
|
+
onChange?.(nextValue);
|
|
45
|
+
}
|
|
46
|
+
})
|
|
47
|
+
);
|
|
48
|
+
|
|
49
|
+
context = checkboxGroup;
|
|
50
|
+
|
|
51
|
+
// Marks the window in which a checkbox disappearing is a real change rather than the group
|
|
52
|
+
// itself coming or going. `$effect.pre` because Svelte destroys an effect's children
|
|
53
|
+
// before running that effect's own teardown, so an `onDestroy` here would fire *after*
|
|
54
|
+
// every checkbox had already unregistered: this one is created while the script runs,
|
|
55
|
+
// before the children exist, so it is first in destroy order and its cleanup lands ahead
|
|
56
|
+
// of theirs. On the server it never runs, which is right too — there the checkboxes
|
|
57
|
+
// unregister as the markup closes and nobody is listening any more.
|
|
58
|
+
$effect.pre(() => {
|
|
59
|
+
checkboxGroup.setLive(true);
|
|
60
|
+
return () => checkboxGroup.setLive(false);
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
const disabled = $derived(checkboxGroup.isDisabled);
|
|
64
|
+
const readOnly = $derived(checkboxGroup.isReadOnly);
|
|
65
|
+
const required = $derived(checkboxGroup.isRequired);
|
|
66
|
+
const currentOrientation = $derived(checkboxGroup.orientation);
|
|
67
|
+
|
|
68
|
+
$effect(() => {
|
|
69
|
+
element = rootRef;
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
$effect(() => {
|
|
73
|
+
checkboxGroup.setName(name);
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
$effect(() => {
|
|
77
|
+
checkboxGroup.setDisabled(disabledProp);
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
$effect(() => {
|
|
81
|
+
checkboxGroup.setReadOnly(readOnlyProp);
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
$effect(() => {
|
|
85
|
+
checkboxGroup.setRequired(requiredProp);
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
$effect(() => {
|
|
89
|
+
checkboxGroup.setOrientation(orientation);
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
// A parent that supplies `value` drives the selection, bound or not. Latched at init,
|
|
93
|
+
// not reactive — re-checking `value !== undefined` would switch this on the moment our
|
|
94
|
+
// own write-back defines it, and a `defaultValue`-only group would start following the
|
|
95
|
+
// echo of its own changes.
|
|
96
|
+
const adoptsValueProp = untrack(() => value !== undefined);
|
|
97
|
+
|
|
98
|
+
$effect(() => {
|
|
99
|
+
if (!adoptsValueProp) return;
|
|
100
|
+
checkboxGroup.setSelectedValues(value);
|
|
101
|
+
});
|
|
102
|
+
</script>
|
|
103
|
+
|
|
104
|
+
<div
|
|
105
|
+
{...restProps}
|
|
106
|
+
bind:this={rootRef}
|
|
107
|
+
id={instanceId}
|
|
108
|
+
role="group"
|
|
109
|
+
aria-labelledby={ariaLabelledBy ?? checkboxGroup.labelledBy}
|
|
110
|
+
class={className}
|
|
111
|
+
data-checkbox-group-root="true"
|
|
112
|
+
data-orientation={currentOrientation}
|
|
113
|
+
data-disabled={disabled || undefined}
|
|
114
|
+
data-readonly={readOnly || undefined}
|
|
115
|
+
data-required={required || undefined}
|
|
116
|
+
>
|
|
117
|
+
{@render children?.()}
|
|
118
|
+
</div>
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import type { CheckboxGroupRootProps } from '../types.js';
|
|
2
|
+
declare const CheckboxGroupRoot: import("svelte").Component<CheckboxGroupRootProps, {}, "value" | "element" | "context">;
|
|
3
|
+
type CheckboxGroupRoot = ReturnType<typeof CheckboxGroupRoot>;
|
|
4
|
+
export default CheckboxGroupRoot;
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
export type CheckboxGroupValue = string;
|
|
2
|
+
export type CheckboxGroupOrientation = 'horizontal' | 'vertical';
|
|
3
|
+
export type CreateCheckboxGroupContextOptions = {
|
|
4
|
+
initialValue?: CheckboxGroupValue[];
|
|
5
|
+
name?: string;
|
|
6
|
+
isDisabled?: boolean;
|
|
7
|
+
isReadOnly?: boolean;
|
|
8
|
+
isRequired?: boolean;
|
|
9
|
+
orientation?: CheckboxGroupOrientation;
|
|
10
|
+
onValueChange?: (value: CheckboxGroupValue[]) => void;
|
|
11
|
+
};
|
|
12
|
+
export type CheckboxGroupContext = {
|
|
13
|
+
name: string | undefined;
|
|
14
|
+
isDisabled: boolean;
|
|
15
|
+
isReadOnly: boolean;
|
|
16
|
+
isRequired: boolean;
|
|
17
|
+
orientation: CheckboxGroupOrientation;
|
|
18
|
+
/** The ids of the `CheckboxGroup.Label` parts, for `aria-labelledby` on the group. */
|
|
19
|
+
labelledBy: string | undefined;
|
|
20
|
+
selectedValues: Set<CheckboxGroupValue>;
|
|
21
|
+
/** Every registered checkbox is selected, and there is at least one. */
|
|
22
|
+
allSelected: boolean;
|
|
23
|
+
/** Some but not all registered checkboxes are selected. Drives a parent `indeterminate`. */
|
|
24
|
+
someSelected: boolean;
|
|
25
|
+
/** Registers a label id; the returned function unregisters it. */
|
|
26
|
+
registerLabel: (id: string) => () => void;
|
|
27
|
+
registerCheckbox: (value: CheckboxGroupValue, options: {
|
|
28
|
+
isDisabled?: boolean;
|
|
29
|
+
element?: HTMLElement | null;
|
|
30
|
+
owner?: symbol;
|
|
31
|
+
}) => void;
|
|
32
|
+
unregisterCheckbox: (value: CheckboxGroupValue) => void;
|
|
33
|
+
setLive: (live: boolean) => void;
|
|
34
|
+
setName: (name: string | undefined) => void;
|
|
35
|
+
setDisabled: (disabled: boolean) => void;
|
|
36
|
+
setReadOnly: (readonly: boolean) => void;
|
|
37
|
+
setRequired: (required: boolean) => void;
|
|
38
|
+
setOrientation: (orientation: CheckboxGroupOrientation) => void;
|
|
39
|
+
setSelectedValues: (value?: CheckboxGroupValue[]) => void;
|
|
40
|
+
toggleValue: (value: CheckboxGroupValue) => boolean | null;
|
|
41
|
+
setValueSelected: (value: CheckboxGroupValue, selected: boolean) => boolean | null;
|
|
42
|
+
selectAll: () => void;
|
|
43
|
+
clearAll: () => void;
|
|
44
|
+
isSelected: (value: CheckboxGroupValue) => boolean;
|
|
45
|
+
isCheckboxDisabled: (value: CheckboxGroupValue) => boolean;
|
|
46
|
+
getValue: () => CheckboxGroupValue[];
|
|
47
|
+
getEnabledValues: () => CheckboxGroupValue[];
|
|
48
|
+
};
|
|
49
|
+
export declare function valuesToArray(values: Set<CheckboxGroupValue>, order: CheckboxGroupValue[]): CheckboxGroupValue[];
|
|
50
|
+
export declare function createCheckboxGroupContext(options: CreateCheckboxGroupContextOptions): CheckboxGroupContext;
|
|
51
|
+
export declare function setCheckboxGroupContext(context: CheckboxGroupContext): CheckboxGroupContext;
|
|
52
|
+
export declare function getCheckboxGroupContext(): CheckboxGroupContext | undefined;
|
|
@@ -0,0 +1,251 @@
|
|
|
1
|
+
import { getContext, setContext, untrack } from 'svelte';
|
|
2
|
+
import { SvelteMap, SvelteSet } from 'svelte/reactivity';
|
|
3
|
+
import { asCommand } from '../../internal/as-command.js';
|
|
4
|
+
const CHECKBOX_GROUP_CONTEXT_KEY = Symbol('checkbox-group');
|
|
5
|
+
export function valuesToArray(values, order) {
|
|
6
|
+
const orderedValues = order.filter((value) => values.has(value));
|
|
7
|
+
const unknownValues = Array.from(values).filter((value) => !order.includes(value));
|
|
8
|
+
return [...orderedValues, ...unknownValues];
|
|
9
|
+
}
|
|
10
|
+
export function createCheckboxGroupContext(options) {
|
|
11
|
+
let name = $state(options.name);
|
|
12
|
+
let isDisabled = $state(options.isDisabled ?? false);
|
|
13
|
+
let isReadOnly = $state(options.isReadOnly ?? false);
|
|
14
|
+
let isRequired = $state(options.isRequired ?? false);
|
|
15
|
+
let orientation = $state(options.orientation ?? 'vertical');
|
|
16
|
+
let labelIds = $state([]);
|
|
17
|
+
// A `SvelteSet` carries its own reactivity, so it is mutated in place rather than
|
|
18
|
+
// reassigned: a plain Set behind `$state` would need a new instance for every change,
|
|
19
|
+
// and it would invalidate every reader of the group on each one.
|
|
20
|
+
const selectedValues = new SvelteSet(options.initialValue ?? []);
|
|
21
|
+
// Whether the group is mounted and interactive. Plain `let`, not `$state`: nothing renders
|
|
22
|
+
// from it, and the reads happen while checkboxes register and unregister, where a reactive
|
|
23
|
+
// read would only add a dependency.
|
|
24
|
+
let isLive = false;
|
|
25
|
+
const checkboxes = new SvelteMap();
|
|
26
|
+
const checkboxOrder = $state([]);
|
|
27
|
+
function getOrderedValues() {
|
|
28
|
+
const connected = [];
|
|
29
|
+
const detachedValues = [];
|
|
30
|
+
for (const value of checkboxOrder) {
|
|
31
|
+
const element = checkboxes.get(value)?.element;
|
|
32
|
+
if (element?.isConnected) {
|
|
33
|
+
connected.push({ value, element });
|
|
34
|
+
}
|
|
35
|
+
else {
|
|
36
|
+
detachedValues.push(value);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
connected.sort((left, right) => left.element.compareDocumentPosition(right.element) & Node.DOCUMENT_POSITION_FOLLOWING
|
|
40
|
+
? -1
|
|
41
|
+
: 1);
|
|
42
|
+
return [...connected.map((entry) => entry.value), ...detachedValues];
|
|
43
|
+
}
|
|
44
|
+
function getEnabledValues() {
|
|
45
|
+
return getOrderedValues().filter((value) => checkboxes.has(value) && !isCheckboxDisabled(value));
|
|
46
|
+
}
|
|
47
|
+
function getValue() {
|
|
48
|
+
return valuesToArray(selectedValues, getOrderedValues());
|
|
49
|
+
}
|
|
50
|
+
function setSelection(nextValues, changeOptions) {
|
|
51
|
+
const order = getOrderedValues();
|
|
52
|
+
const previousValue = valuesToArray(selectedValues, order);
|
|
53
|
+
const nextValue = valuesToArray(nextValues, order);
|
|
54
|
+
const didChange = previousValue.length !== nextValue.length ||
|
|
55
|
+
previousValue.some((value, index) => value !== nextValue[index]);
|
|
56
|
+
if (!didChange)
|
|
57
|
+
return false;
|
|
58
|
+
selectedValues.clear();
|
|
59
|
+
for (const value of nextValue) {
|
|
60
|
+
selectedValues.add(value);
|
|
61
|
+
}
|
|
62
|
+
if (changeOptions?.notify !== false) {
|
|
63
|
+
options.onValueChange?.(nextValue);
|
|
64
|
+
}
|
|
65
|
+
return true;
|
|
66
|
+
}
|
|
67
|
+
// Unlike `ToggleGroup`, a disabled checkbox keeps its selection: a checked and disabled
|
|
68
|
+
// checkbox is a real state the platform supports, and dropping it would silently rewrite
|
|
69
|
+
// the value a form was seeded with. Only a checkbox that leaves the tree leaves the value.
|
|
70
|
+
function reconcileSelection(removedValue, changeOptions) {
|
|
71
|
+
if (removedValue === undefined || checkboxes.has(removedValue))
|
|
72
|
+
return;
|
|
73
|
+
if (!selectedValues.has(removedValue))
|
|
74
|
+
return;
|
|
75
|
+
const nextValues = new SvelteSet(selectedValues);
|
|
76
|
+
nextValues.delete(removedValue);
|
|
77
|
+
setSelection(nextValues, changeOptions);
|
|
78
|
+
}
|
|
79
|
+
function registerCheckbox(value, registerOptions) {
|
|
80
|
+
const existingCheckbox = checkboxes.get(value);
|
|
81
|
+
if (existingCheckbox !== undefined &&
|
|
82
|
+
existingCheckbox.owner !== undefined &&
|
|
83
|
+
registerOptions.owner !== undefined &&
|
|
84
|
+
existingCheckbox.owner !== registerOptions.owner) {
|
|
85
|
+
throw new Error('Checkbox.Root values must be unique within a CheckboxGroup.Root.');
|
|
86
|
+
}
|
|
87
|
+
checkboxes.set(value, {
|
|
88
|
+
value,
|
|
89
|
+
isDisabled: Boolean(registerOptions.isDisabled),
|
|
90
|
+
element: registerOptions.element,
|
|
91
|
+
owner: registerOptions.owner
|
|
92
|
+
});
|
|
93
|
+
if (existingCheckbox === undefined) {
|
|
94
|
+
checkboxOrder.push(value);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
// Turned on by the root as it renders and off as it goes away, which is the only way to
|
|
98
|
+
// tell the two unregistrations apart: a checkbox removed from a live group leaves a group
|
|
99
|
+
// behind that still has to show something, while a group on its way out has no one left
|
|
100
|
+
// to show anything to.
|
|
101
|
+
function setLive(live) {
|
|
102
|
+
isLive = live;
|
|
103
|
+
}
|
|
104
|
+
function unregisterCheckbox(value) {
|
|
105
|
+
if (!checkboxes.has(value))
|
|
106
|
+
return;
|
|
107
|
+
checkboxes.delete(value);
|
|
108
|
+
const index = checkboxOrder.indexOf(value);
|
|
109
|
+
if (index >= 0) {
|
|
110
|
+
checkboxOrder.splice(index, 1);
|
|
111
|
+
}
|
|
112
|
+
// Outside the live window the checkbox is leaving because the group is — on the client
|
|
113
|
+
// while it unmounts, on the server as the markup closes — and reporting a value change
|
|
114
|
+
// there would tell a consumer already on its way out that the user cleared a box.
|
|
115
|
+
if (!isLive)
|
|
116
|
+
return;
|
|
117
|
+
reconcileSelection(value);
|
|
118
|
+
}
|
|
119
|
+
function setName(nextName) {
|
|
120
|
+
if (name === nextName)
|
|
121
|
+
return;
|
|
122
|
+
name = nextName;
|
|
123
|
+
}
|
|
124
|
+
function setDisabled(disabled) {
|
|
125
|
+
if (isDisabled === disabled)
|
|
126
|
+
return;
|
|
127
|
+
isDisabled = disabled;
|
|
128
|
+
}
|
|
129
|
+
function setReadOnly(readonly) {
|
|
130
|
+
if (isReadOnly === readonly)
|
|
131
|
+
return;
|
|
132
|
+
isReadOnly = readonly;
|
|
133
|
+
}
|
|
134
|
+
function setRequired(required) {
|
|
135
|
+
if (isRequired === required)
|
|
136
|
+
return;
|
|
137
|
+
isRequired = required;
|
|
138
|
+
}
|
|
139
|
+
function setOrientation(nextOrientation) {
|
|
140
|
+
if (orientation === nextOrientation)
|
|
141
|
+
return;
|
|
142
|
+
orientation = nextOrientation;
|
|
143
|
+
}
|
|
144
|
+
function setSelectedValues(value) {
|
|
145
|
+
setSelection(new SvelteSet(value ?? []), { notify: false });
|
|
146
|
+
}
|
|
147
|
+
function setValueSelected(value, selected) {
|
|
148
|
+
if (isCheckboxDisabled(value) || isReadOnly)
|
|
149
|
+
return null;
|
|
150
|
+
if (selectedValues.has(value) === selected)
|
|
151
|
+
return null;
|
|
152
|
+
const nextValues = new SvelteSet(selectedValues);
|
|
153
|
+
if (selected) {
|
|
154
|
+
nextValues.add(value);
|
|
155
|
+
}
|
|
156
|
+
else {
|
|
157
|
+
nextValues.delete(value);
|
|
158
|
+
}
|
|
159
|
+
return setSelection(nextValues) ? selected : null;
|
|
160
|
+
}
|
|
161
|
+
function toggleValue(value) {
|
|
162
|
+
return setValueSelected(value, !selectedValues.has(value));
|
|
163
|
+
}
|
|
164
|
+
function selectAll() {
|
|
165
|
+
if (isReadOnly)
|
|
166
|
+
return;
|
|
167
|
+
const nextValues = new SvelteSet(selectedValues);
|
|
168
|
+
for (const value of getEnabledValues()) {
|
|
169
|
+
nextValues.add(value);
|
|
170
|
+
}
|
|
171
|
+
setSelection(nextValues);
|
|
172
|
+
}
|
|
173
|
+
function clearAll() {
|
|
174
|
+
if (isReadOnly)
|
|
175
|
+
return;
|
|
176
|
+
const nextValues = new SvelteSet(selectedValues);
|
|
177
|
+
for (const value of getEnabledValues()) {
|
|
178
|
+
nextValues.delete(value);
|
|
179
|
+
}
|
|
180
|
+
setSelection(nextValues);
|
|
181
|
+
}
|
|
182
|
+
function isSelected(value) {
|
|
183
|
+
return selectedValues.has(value);
|
|
184
|
+
}
|
|
185
|
+
function isCheckboxDisabled(value) {
|
|
186
|
+
return isDisabled || Boolean(checkboxes.get(value)?.isDisabled);
|
|
187
|
+
}
|
|
188
|
+
return {
|
|
189
|
+
get name() {
|
|
190
|
+
return name;
|
|
191
|
+
},
|
|
192
|
+
get isDisabled() {
|
|
193
|
+
return isDisabled;
|
|
194
|
+
},
|
|
195
|
+
get isReadOnly() {
|
|
196
|
+
return isReadOnly;
|
|
197
|
+
},
|
|
198
|
+
get isRequired() {
|
|
199
|
+
return isRequired;
|
|
200
|
+
},
|
|
201
|
+
get orientation() {
|
|
202
|
+
return orientation;
|
|
203
|
+
},
|
|
204
|
+
get labelledBy() {
|
|
205
|
+
return labelIds.length > 0 ? labelIds.join(' ') : undefined;
|
|
206
|
+
},
|
|
207
|
+
get selectedValues() {
|
|
208
|
+
return selectedValues;
|
|
209
|
+
},
|
|
210
|
+
get allSelected() {
|
|
211
|
+
return checkboxOrder.length > 0 && checkboxOrder.every((value) => selectedValues.has(value));
|
|
212
|
+
},
|
|
213
|
+
get someSelected() {
|
|
214
|
+
const selectedCount = checkboxOrder.filter((value) => selectedValues.has(value)).length;
|
|
215
|
+
return selectedCount > 0 && selectedCount < checkboxOrder.length;
|
|
216
|
+
},
|
|
217
|
+
// `untrack` because the caller registers from an effect: reading `labelIds` here would
|
|
218
|
+
// make that effect depend on what it writes, and the pair would run without end.
|
|
219
|
+
registerLabel(id) {
|
|
220
|
+
untrack(() => {
|
|
221
|
+
labelIds = [...labelIds, id];
|
|
222
|
+
});
|
|
223
|
+
return () => untrack(() => {
|
|
224
|
+
labelIds = labelIds.filter((candidate) => candidate !== id);
|
|
225
|
+
});
|
|
226
|
+
},
|
|
227
|
+
registerCheckbox: asCommand(registerCheckbox),
|
|
228
|
+
unregisterCheckbox: asCommand(unregisterCheckbox),
|
|
229
|
+
setLive: asCommand(setLive),
|
|
230
|
+
setName: asCommand(setName),
|
|
231
|
+
setDisabled: asCommand(setDisabled),
|
|
232
|
+
setReadOnly: asCommand(setReadOnly),
|
|
233
|
+
setRequired: asCommand(setRequired),
|
|
234
|
+
setOrientation: asCommand(setOrientation),
|
|
235
|
+
setSelectedValues: asCommand(setSelectedValues),
|
|
236
|
+
toggleValue: asCommand(toggleValue),
|
|
237
|
+
setValueSelected: asCommand(setValueSelected),
|
|
238
|
+
selectAll: asCommand(selectAll),
|
|
239
|
+
clearAll: asCommand(clearAll),
|
|
240
|
+
isSelected,
|
|
241
|
+
isCheckboxDisabled,
|
|
242
|
+
getValue,
|
|
243
|
+
getEnabledValues
|
|
244
|
+
};
|
|
245
|
+
}
|
|
246
|
+
export function setCheckboxGroupContext(context) {
|
|
247
|
+
return setContext(CHECKBOX_GROUP_CONTEXT_KEY, context);
|
|
248
|
+
}
|
|
249
|
+
export function getCheckboxGroupContext() {
|
|
250
|
+
return getContext(CHECKBOX_GROUP_CONTEXT_KEY);
|
|
251
|
+
}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import type { Snippet } from 'svelte';
|
|
2
|
+
import type { HTMLAttributes } from 'svelte/elements';
|
|
3
|
+
import type { CheckboxGroupContext, CheckboxGroupOrientation, CheckboxGroupValue } from './root/context.svelte.js';
|
|
4
|
+
export type { CheckboxGroupContext, CheckboxGroupOrientation, CheckboxGroupValue } from './root/context.svelte.js';
|
|
5
|
+
export type CheckboxGroupRootProps = Omit<HTMLAttributes<HTMLDivElement>, 'children' | 'class'> & {
|
|
6
|
+
/**
|
|
7
|
+
* The selected values. 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?: CheckboxGroupValue[];
|
|
15
|
+
/** The selected values at the start, for when you give no `value`. */
|
|
16
|
+
defaultValue?: CheckboxGroupValue[];
|
|
17
|
+
/** The component calls it when the user changes the selection. */
|
|
18
|
+
onChange?: (value: CheckboxGroupValue[]) => void;
|
|
19
|
+
/** The `name` of the hidden input of every checkbox in the group, for form submission. */
|
|
20
|
+
name?: string;
|
|
21
|
+
/** Disables each checkbox in the group. */
|
|
22
|
+
disabled?: boolean;
|
|
23
|
+
/** Shows the value of the group, and refuses each change from the user. */
|
|
24
|
+
readonly?: boolean;
|
|
25
|
+
/**
|
|
26
|
+
* Records that the group needs an answer. It gives `data-required` for your styles, and it
|
|
27
|
+
* does not enforce a minimum count. It marks neither the group nor the boxes with
|
|
28
|
+
* `aria-required`: `role="group"` does not support that property, and native `required` on a
|
|
29
|
+
* box demands that one box. Say it in the group label, and give the reason with
|
|
30
|
+
* `aria-describedby`.
|
|
31
|
+
*/
|
|
32
|
+
required?: boolean;
|
|
33
|
+
/**
|
|
34
|
+
* The orientation of the layout. It sets `data-orientation` for your styles. It does not change
|
|
35
|
+
* the keyboard behavior: each checkbox keeps its own tab stop.
|
|
36
|
+
*/
|
|
37
|
+
orientation?: CheckboxGroupOrientation;
|
|
38
|
+
/** The checkboxes of the group. */
|
|
39
|
+
children?: Snippet;
|
|
40
|
+
class?: string;
|
|
41
|
+
/** The id of the group element. The component makes one when you give none. */
|
|
42
|
+
id?: string;
|
|
43
|
+
/** The group element. Use `bind:element` to read it. */
|
|
44
|
+
element?: HTMLDivElement | null;
|
|
45
|
+
/**
|
|
46
|
+
* The state of the group. Use `bind:context` to read `allSelected` and `someSelected` for a
|
|
47
|
+
* parent "select all" checkbox, and to call `selectAll` and `clearAll`.
|
|
48
|
+
*/
|
|
49
|
+
context?: CheckboxGroupContext;
|
|
50
|
+
};
|
|
51
|
+
export type CheckboxGroupLabelProps = Omit<HTMLAttributes<HTMLSpanElement>, 'children' | 'class' | 'id'> & {
|
|
52
|
+
/** The name of the group. */
|
|
53
|
+
children?: Snippet;
|
|
54
|
+
class?: string;
|
|
55
|
+
/** The id of the label element. The component makes one when you give none. */
|
|
56
|
+
id?: string;
|
|
57
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/dist/index.d.ts
CHANGED
|
@@ -2,6 +2,7 @@ export { Accordion } from './accordion/index.js';
|
|
|
2
2
|
export { Autocomplete } from './autocomplete/index.js';
|
|
3
3
|
export { Button } from './button/index.js';
|
|
4
4
|
export { Checkbox } from './checkbox/index.js';
|
|
5
|
+
export { CheckboxGroup } from './checkbox-group/index.js';
|
|
5
6
|
export { Collapsible } from './collapsible/index.js';
|
|
6
7
|
export { ComboBox } from './combobox/index.js';
|
|
7
8
|
export { Calendar } from './calendar/index.js';
|
|
@@ -15,6 +16,7 @@ export { ListBox } from './listbox/index.js';
|
|
|
15
16
|
export { Menu } from './menu/index.js';
|
|
16
17
|
export { NumberField } from './numberfield/index.js';
|
|
17
18
|
export { Popover } from './popover/index.js';
|
|
19
|
+
export { RadioGroup } from './radio-group/index.js';
|
|
18
20
|
export { Switch } from './switch/index.js';
|
|
19
21
|
export { Table } from './table/index.js';
|
|
20
22
|
export { Tabs } from './tabs/index.js';
|
|
@@ -34,6 +36,7 @@ export * from './accordion/index.js';
|
|
|
34
36
|
export * from './autocomplete/index.js';
|
|
35
37
|
export * from './button/index.js';
|
|
36
38
|
export * from './checkbox/index.js';
|
|
39
|
+
export * from './checkbox-group/index.js';
|
|
37
40
|
export * from './collapsible/index.js';
|
|
38
41
|
export * from './combobox/index.js';
|
|
39
42
|
export * from './calendar/index.js';
|
|
@@ -48,6 +51,7 @@ export * from './listbox/index.js';
|
|
|
48
51
|
export * from './menu/index.js';
|
|
49
52
|
export * from './numberfield/index.js';
|
|
50
53
|
export * from './popover/index.js';
|
|
54
|
+
export * from './radio-group/index.js';
|
|
51
55
|
export * from './switch/index.js';
|
|
52
56
|
export * from './table/index.js';
|
|
53
57
|
export * from './tabs/index.js';
|
package/dist/index.js
CHANGED
|
@@ -4,6 +4,7 @@ export { Accordion } from './accordion/index.js';
|
|
|
4
4
|
export { Autocomplete } from './autocomplete/index.js';
|
|
5
5
|
export { Button } from './button/index.js';
|
|
6
6
|
export { Checkbox } from './checkbox/index.js';
|
|
7
|
+
export { CheckboxGroup } from './checkbox-group/index.js';
|
|
7
8
|
export { Collapsible } from './collapsible/index.js';
|
|
8
9
|
export { ComboBox } from './combobox/index.js';
|
|
9
10
|
export { Calendar } from './calendar/index.js';
|
|
@@ -17,6 +18,7 @@ export { ListBox } from './listbox/index.js';
|
|
|
17
18
|
export { Menu } from './menu/index.js';
|
|
18
19
|
export { NumberField } from './numberfield/index.js';
|
|
19
20
|
export { Popover } from './popover/index.js';
|
|
21
|
+
export { RadioGroup } from './radio-group/index.js';
|
|
20
22
|
export { Switch } from './switch/index.js';
|
|
21
23
|
export { Table } from './table/index.js';
|
|
22
24
|
export { Tabs } from './tabs/index.js';
|
|
@@ -38,6 +40,7 @@ export * from './accordion/index.js';
|
|
|
38
40
|
export * from './autocomplete/index.js';
|
|
39
41
|
export * from './button/index.js';
|
|
40
42
|
export * from './checkbox/index.js';
|
|
43
|
+
export * from './checkbox-group/index.js';
|
|
41
44
|
export * from './collapsible/index.js';
|
|
42
45
|
export * from './combobox/index.js';
|
|
43
46
|
export * from './calendar/index.js';
|
|
@@ -52,6 +55,7 @@ export * from './listbox/index.js';
|
|
|
52
55
|
export * from './menu/index.js';
|
|
53
56
|
export * from './numberfield/index.js';
|
|
54
57
|
export * from './popover/index.js';
|
|
58
|
+
export * from './radio-group/index.js';
|
|
55
59
|
export * from './switch/index.js';
|
|
56
60
|
export * from './table/index.js';
|
|
57
61
|
export * from './tabs/index.js';
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Keeps a `focusVisible` value honest while the element holds focus.
|
|
3
|
+
*
|
|
4
|
+
* `shouldShowFocusVisible` reads the modality at the moment it is called, so a component that
|
|
5
|
+
* only calls it from its focus handler holds a snapshot. A pointer press seeds `false`, and a
|
|
6
|
+
* key press after it flips the modality — and the `:focus-visible` of the browser — with nothing
|
|
7
|
+
* to tell the component. This subscribes for as long as the element has focus, and it re-reads
|
|
8
|
+
* on each change, which is what React Aria and Base UI do.
|
|
9
|
+
*/
|
|
10
|
+
export declare function watchFocusVisible(options: {
|
|
11
|
+
isFocused: () => boolean;
|
|
12
|
+
element: () => HTMLElement | null;
|
|
13
|
+
set: (focusVisible: boolean) => void;
|
|
14
|
+
}): void;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { shouldShowFocusVisible, subscribeInputModality } from './input-modality.js';
|
|
2
|
+
/**
|
|
3
|
+
* Keeps a `focusVisible` value honest while the element holds focus.
|
|
4
|
+
*
|
|
5
|
+
* `shouldShowFocusVisible` reads the modality at the moment it is called, so a component that
|
|
6
|
+
* only calls it from its focus handler holds a snapshot. A pointer press seeds `false`, and a
|
|
7
|
+
* key press after it flips the modality — and the `:focus-visible` of the browser — with nothing
|
|
8
|
+
* to tell the component. This subscribes for as long as the element has focus, and it re-reads
|
|
9
|
+
* on each change, which is what React Aria and Base UI do.
|
|
10
|
+
*/
|
|
11
|
+
export function watchFocusVisible(options) {
|
|
12
|
+
$effect(() => {
|
|
13
|
+
if (!options.isFocused())
|
|
14
|
+
return;
|
|
15
|
+
return subscribeInputModality(() => {
|
|
16
|
+
options.set(shouldShowFocusVisible(options.element()));
|
|
17
|
+
});
|
|
18
|
+
});
|
|
19
|
+
}
|
|
@@ -6,5 +6,6 @@ export * from './scroll-lock.js';
|
|
|
6
6
|
export * from './aria-hide-outside.js';
|
|
7
7
|
export * from './click-outside.js';
|
|
8
8
|
export * from './input-modality.js';
|
|
9
|
+
export * from './focus-visible.svelte.js';
|
|
9
10
|
export * from './long-press.js';
|
|
10
11
|
export * from './swipe-gesture.js';
|
package/dist/primitives/index.js
CHANGED
|
@@ -7,5 +7,6 @@ export * from './scroll-lock.js';
|
|
|
7
7
|
export * from './aria-hide-outside.js';
|
|
8
8
|
export * from './click-outside.js';
|
|
9
9
|
export * from './input-modality.js';
|
|
10
|
+
export * from './focus-visible.svelte.js';
|
|
10
11
|
export * from './long-press.js';
|
|
11
12
|
export * from './swipe-gesture.js';
|
|
@@ -2,6 +2,13 @@ export type InputModality = 'keyboard' | 'pointer' | 'virtual';
|
|
|
2
2
|
export declare function initInputModality(target?: HTMLElement | null): void;
|
|
3
3
|
export declare function trackInteractionModality(event?: Event, target?: HTMLElement | null): void;
|
|
4
4
|
export declare function getInteractionModality(): InputModality;
|
|
5
|
+
/**
|
|
6
|
+
* Calls `listener` whenever the interaction modality changes, and returns the unsubscribe
|
|
7
|
+
* function. Components use it to re-read `shouldShowFocusVisible` for an element that already
|
|
8
|
+
* holds focus: the browser re-evaluates `:focus-visible` on its own, and this is what lets a
|
|
9
|
+
* component notice.
|
|
10
|
+
*/
|
|
11
|
+
export declare function subscribeInputModality(listener: (modality: InputModality) => void): () => void;
|
|
5
12
|
export declare function shouldShowFocusVisible(target: HTMLElement | null): boolean;
|
|
6
13
|
export declare function focusWithModality(target: HTMLElement, modality: InputModality, options?: FocusOptions): void;
|
|
7
14
|
export declare function resolveCloseInteractionModality(reason: string, event?: Event): InputModality;
|