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

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.
@@ -19,6 +19,12 @@
19
19
  ## Usage guidelines
20
20
 
21
21
  - Use `value` / `defaultValue` arrays for both single and multiple selection.
22
+ - Use `bind:value` to hand the group your state, or plain `value` + `onChange` to own it
23
+ yourself. There is no flag to declare which: supplying `value` makes it the source of
24
+ truth either way, and `onChange` always reports.
25
+ - A parent that owns `value` and rejects a change — never flowing a new one down — still
26
+ sees the group move, and the group snaps back to the supplied `value` on the parent's
27
+ next render. Reject by rendering, not by staying silent.
22
28
  - Use `selectionMode="single"` when only one toggle can be selected.
23
29
  - Use `selectionMode="multiple"` when several toggles can be selected.
24
30
  - Use `disallowEmptySelection` when at least one enabled toggle must remain selected.
@@ -48,4 +54,8 @@
48
54
  ## Notes
49
55
 
50
56
  - Grouped toggles ignore their standalone `selected` and `defaultSelected` props.
57
+ - Removing one `Toggle.Root` from a mounted group is a real selection change: with
58
+ `disallowEmptySelection` the group picks a fallback and fires `onChange`. Unmounting the
59
+ whole group is not, and reports nothing — otherwise leaving a screen would look like the
60
+ user pressing the last toggle on the way out.
51
61
  - Form serialization is out of scope for this primitive.
@@ -3,7 +3,6 @@ export type ToggleGroupSelectionMode = 'single' | 'multiple';
3
3
  export type ToggleGroupOrientation = 'horizontal' | 'vertical';
4
4
  export type CreateToggleGroupContextOptions = {
5
5
  initialValue?: ToggleGroupValue[];
6
- isControlled?: boolean;
7
6
  selectionMode?: ToggleGroupSelectionMode;
8
7
  isDisabled?: boolean;
9
8
  orientation?: ToggleGroupOrientation;
@@ -23,6 +22,7 @@ export type ToggleGroupContext = {
23
22
  owner?: symbol;
24
23
  }) => void;
25
24
  unregisterToggle: (value: ToggleGroupValue) => void;
25
+ setLive: (live: boolean) => void;
26
26
  setSelectionMode: (mode: ToggleGroupSelectionMode) => void;
27
27
  setDisabled: (disabled: boolean) => void;
28
28
  setOrientation: (orientation: ToggleGroupOrientation) => void;
@@ -29,7 +29,6 @@ export function valuesToArray(values, order) {
29
29
  return [...orderedValues, ...unknownValues];
30
30
  }
31
31
  export function createToggleGroupContext(options) {
32
- const isControlled = options.isControlled ?? false;
33
32
  let selectionMode = $state(options.selectionMode ?? 'single');
34
33
  let isDisabled = $state(options.isDisabled ?? false);
35
34
  let orientation = $state(options.orientation ?? 'horizontal');
@@ -39,6 +38,10 @@ export function createToggleGroupContext(options) {
39
38
  let selectedValues = $state(normalizeValues(options.initialValue, options.selectionMode ?? 'single'));
40
39
  let focusedValue = $state(null);
41
40
  let focusVisible = $state(false);
41
+ // Whether the group is mounted and interactive. Plain `let`, not `$state`: nothing renders
42
+ // from it, and the reads happen while toggles register and unregister, where a reactive
43
+ // read would only add a dependency.
44
+ let isLive = false;
42
45
  const toggles = new SvelteMap();
43
46
  const toggleOrder = $state([]);
44
47
  function getOrderedValues() {
@@ -115,13 +118,6 @@ export function createToggleGroupContext(options) {
115
118
  previousValue.some((value, index) => !valuesMatch(value, nextValue[index]));
116
119
  if (!didChange)
117
120
  return false;
118
- if (isControlled && changeOptions?.notify !== false) {
119
- // Controlled mode: request the change and let the parent apply it via props.
120
- // Mirrors Accordion's identical `setOpen`, which had this guard while this one
121
- // silently fell through and applied the change the parent might have rejected.
122
- options.onValueChange?.(nextValue);
123
- return true;
124
- }
125
121
  selectedValues = normalizedValues;
126
122
  if (changeOptions?.notify !== false) {
127
123
  options.onValueChange?.(nextValue);
@@ -129,8 +125,6 @@ export function createToggleGroupContext(options) {
129
125
  return true;
130
126
  }
131
127
  function reconcileSelection(changedValue, changeOptions) {
132
- if (isControlled)
133
- return;
134
128
  const nextValues = new SvelteSet(selectedValues);
135
129
  let removedSelectedValue;
136
130
  for (const selectedValue of selectedValues) {
@@ -175,6 +169,13 @@ export function createToggleGroupContext(options) {
175
169
  }
176
170
  reconcileSelection(value, { notify: isExisting });
177
171
  }
172
+ // Turned on by the root as it renders and off as it goes away, which is the only way to
173
+ // tell the two unregistrations apart: a toggle removed from a live group leaves a group
174
+ // behind that still has to show something, while a group on its way out has no one left
175
+ // to show anything to.
176
+ function setLive(live) {
177
+ isLive = live;
178
+ }
178
179
  function unregisterToggle(value) {
179
180
  if (!toggles.has(value))
180
181
  return;
@@ -186,13 +187,20 @@ export function createToggleGroupContext(options) {
186
187
  if (focusedValue !== null && valuesMatch(focusedValue, value)) {
187
188
  focusedValue = null;
188
189
  }
190
+ // Only a live group can lose a toggle from under it. Outside that window the toggle is
191
+ // leaving because the group is — on the client while it unmounts, on the server as the
192
+ // markup closes — and picking a fallback would report a selection the user never made,
193
+ // to a consumer already on its way out. Bound to a URL, that fake change navigated
194
+ // straight back to the screen being left.
195
+ if (!isLive)
196
+ return;
189
197
  reconcileSelection(value);
190
198
  }
191
199
  function setSelectionMode(mode) {
192
200
  if (selectionMode === mode)
193
201
  return;
194
202
  selectionMode = mode;
195
- setSelection(selectedValues, { notify: !isControlled });
203
+ setSelection(selectedValues);
196
204
  reconcileSelection();
197
205
  }
198
206
  function setDisabled(disabled) {
@@ -307,6 +315,7 @@ export function createToggleGroupContext(options) {
307
315
  },
308
316
  registerToggle: asCommand(registerToggle),
309
317
  unregisterToggle: asCommand(unregisterToggle),
318
+ setLive: asCommand(setLive),
310
319
  setSelectionMode: asCommand(setSelectionMode),
311
320
  setDisabled: asCommand(setDisabled),
312
321
  setOrientation: asCommand(setOrientation),
@@ -18,7 +18,6 @@
18
18
  id,
19
19
  value = $bindable(),
20
20
  defaultValue,
21
- controlledValue = false,
22
21
  onChange,
23
22
  selectionMode = 'single',
24
23
  disabled: disabledProp = false,
@@ -33,18 +32,12 @@
33
32
  ...restProps
34
33
  }: ToggleGroupRootProps = $props();
35
34
 
36
- // Controlled-ness is opt-in, not inferred from `value` being defined: `bind:value` and
37
- // `value={...}` are indistinguishable at runtime. It used to be inferred here, and the
38
- // write-back below ignored the result anyway, so a controlled parent could not reject
39
- // a change — the selection moved regardless of what `onChange` decided.
40
- const isControlled = untrack(() => controlledValue);
41
35
  const instanceId = untrack(() => id) ?? generatedId;
42
36
 
43
37
  let rootRef: HTMLDivElement | null = $state(null);
44
38
 
45
39
  const toggleGroup = setToggleGroupContext(
46
40
  createToggleGroupContext({
47
- isControlled,
48
41
  // `value` seeds the initial selection whenever it is supplied, bound or not.
49
42
  initialValue: untrack(() => value) ?? untrack(() => defaultValue),
50
43
  selectionMode: (() => selectionMode)(),
@@ -52,9 +45,10 @@
52
45
  orientation: (() => orientation)(),
53
46
  disallowEmptySelection: (() => disallowEmptySelection)(),
54
47
  onValueChange: (nextValue) => {
55
- if (!isControlled) {
56
- value = nextValue;
57
- }
48
+ // One path for both usages: with `bind:value` this reaches the parent, and
49
+ // without a binding the write stays local, so nothing here has to know
50
+ // which one the caller chose.
51
+ value = nextValue;
58
52
  onChange?.(nextValue);
59
53
  }
60
54
  })
@@ -62,6 +56,18 @@
62
56
 
63
57
  context = toggleGroup;
64
58
 
59
+ // Marks the window in which a toggle disappearing is a real change rather than the group
60
+ // itself coming or going. `$effect.pre` because Svelte destroys an effect's children
61
+ // before running that effect's own teardown, so an `onDestroy` here would fire *after*
62
+ // every toggle had already unregistered: this one is created while the script runs,
63
+ // before the children exist, so it is first in destroy order and its cleanup lands ahead
64
+ // of theirs. On the server it never runs, which is right too — there the toggles
65
+ // unregister as the markup closes and nobody is listening any more.
66
+ $effect.pre(() => {
67
+ toggleGroup.setLive(true);
68
+ return () => toggleGroup.setLive(false);
69
+ });
70
+
65
71
  const disabled = $derived(toggleGroup.isDisabled);
66
72
  const multiple = $derived(toggleGroup.selectionMode === 'multiple');
67
73
  const currentOrientation = $derived(toggleGroup.orientation);
@@ -86,11 +92,10 @@
86
92
  toggleGroup.setDisallowEmptySelection(disallowEmptySelection);
87
93
  });
88
94
 
89
- // Whether to adopt an incoming `value` is a separate question from who owns the state,
90
- // so it is latched at init off the prop rather than off `controlledValue`: a parent
91
- // that supplies `value` drives the selection, bound or not. Latched, not reactive —
92
- // re-checking `value !== undefined` would switch this on the moment our own write-back
93
- // defines it, and the component would then re-adopt the echo of its own change.
95
+ // A parent that supplies `value` drives the selection, bound or not. Latched at init,
96
+ // not reactive — re-checking `value !== undefined` would switch this on the moment our
97
+ // own write-back defines it, and a `defaultValue`-only group would start following the
98
+ // echo of its own changes.
94
99
  const adoptsValueProp = untrack(() => value !== undefined);
95
100
 
96
101
  $effect(() => {
@@ -0,0 +1,13 @@
1
+ <script lang="ts">
2
+ import { onDestroy } from 'svelte';
3
+
4
+ type Props = {
5
+ /** Shared log, so the parent can assert who tore down first. */
6
+ record: (step: string) => void;
7
+ };
8
+
9
+ let { record }: Props = $props();
10
+
11
+ // Stands in for `Toggle.Root`, which unregisters from `onDestroy`.
12
+ onDestroy(() => record('child-destroy'));
13
+ </script>
@@ -0,0 +1,7 @@
1
+ type Props = {
2
+ /** Shared log, so the parent can assert who tore down first. */
3
+ record: (step: string) => void;
4
+ };
5
+ declare const ToggleGroupTeardownOrderChild: import("svelte").Component<Props, {}, "">;
6
+ type ToggleGroupTeardownOrderChild = ReturnType<typeof ToggleGroupTeardownOrderChild>;
7
+ export default ToggleGroupTeardownOrderChild;
@@ -3,17 +3,16 @@ import type { HTMLAttributes } from 'svelte/elements';
3
3
  import type { ToggleGroupContext, ToggleGroupOrientation, ToggleGroupSelectionMode, ToggleGroupValue } from './root/context.svelte';
4
4
  export type { ToggleGroupContext, ToggleGroupOrientation, ToggleGroupSelectionMode, ToggleGroupValue } from './root/context.svelte';
5
5
  export type ToggleGroupRootProps = Omit<HTMLAttributes<HTMLDivElement>, 'children' | 'class'> & {
6
- /** Selected values. Two-way by default — use `bind:value`. */
6
+ /**
7
+ * Selected values, and the source of truth whenever it is supplied — with `bind:value`
8
+ * or without. Every change is written back here and reported through `onChange`; without
9
+ * a binding that write only lands locally, so a parent that hears `onChange` and refuses
10
+ * the change (never flowing a new `value` down) sees the group move anyway, and snap back
11
+ * to the supplied `value` the next time the parent renders.
12
+ */
7
13
  value?: ToggleGroupValue[];
8
14
  /** Initially selected values, for when `value` is not supplied. */
9
15
  defaultValue?: ToggleGroupValue[];
10
- /**
11
- * Opt into fully controlled state: the component stops writing back to `value` and
12
- * only reports through `onChange`, so the parent can reject a change by not flowing
13
- * the new value back down. Off by default, because `bind:value` — the common case —
14
- * needs the write-back to work at all.
15
- */
16
- controlledValue?: boolean;
17
16
  onChange?: (value: ToggleGroupValue[]) => void;
18
17
  selectionMode?: ToggleGroupSelectionMode;
19
18
  disabled?: boolean;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@human-kit/ui",
3
- "version": "1.0.0-beta.4",
3
+ "version": "1.0.0-beta.5",
4
4
  "description": "Accessible, typed UI components for Svelte 5 — shipped as native ESM with per-component subpath exports so bundlers only include what you import.",
5
5
  "license": "MIT",
6
6
  "author": "Agustin Delgado <agustindariodelgado@gmail.com>",