@glasshome/ui 1.41.0 → 1.43.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (188) hide show
  1. package/SPEC.md +5 -0
  2. package/dist/index.d.ts +1 -1
  3. package/dist/index.js +3 -3
  4. package/dist/lib/anchor-placement.d.ts +34 -0
  5. package/dist/lib/balance-rows.d.ts +10 -4
  6. package/dist/lib/behavior/anchored.d.ts +26 -0
  7. package/dist/lib/behavior/button-root.d.ts +7 -0
  8. package/dist/lib/behavior/collection.d.ts +32 -0
  9. package/dist/lib/behavior/controllable.d.ts +8 -0
  10. package/dist/lib/behavior/dismiss.d.ts +13 -0
  11. package/dist/lib/behavior/focus-scope.d.ts +24 -0
  12. package/dist/lib/behavior/hide-outside.d.ts +7 -0
  13. package/dist/lib/behavior/layer-stack.d.ts +34 -0
  14. package/dist/lib/behavior/menu.d.ts +59 -0
  15. package/dist/lib/behavior/modal-root.d.ts +68 -0
  16. package/dist/lib/behavior/polymorphic.d.ts +6 -0
  17. package/dist/lib/behavior/presence.d.ts +18 -0
  18. package/dist/lib/behavior/range.d.ts +44 -0
  19. package/dist/lib/behavior/solid.d.ts +13 -0
  20. package/dist/lib/color/color.d.ts +41 -0
  21. package/dist/lib/color/index.d.ts +2 -0
  22. package/dist/lib/color/intl.d.ts +40 -0
  23. package/dist/lib/dock-slot-classes.d.ts +2 -0
  24. package/dist/lib/hold.d.ts +1 -1
  25. package/dist/lib/locale.d.ts +3 -0
  26. package/dist/lib/menu-classes.d.ts +2 -0
  27. package/dist/lib/motion-classes.d.ts +5 -2
  28. package/dist/lib/overlay-classes.d.ts +4 -0
  29. package/dist/lib/page-cover.d.ts +8 -0
  30. package/dist/lib/scroll-track.d.ts +3 -2
  31. package/dist/lib/section-tokens.d.ts +1 -1
  32. package/dist/lib/segment-classes.d.ts +2 -0
  33. package/dist/lib/shadow-layer.d.ts +0 -43
  34. package/dist/lib/toggle-set.d.ts +44 -0
  35. package/dist/lib/visually-hidden.d.ts +3 -0
  36. package/dist/solid/accordion.d.ts +35 -11
  37. package/dist/solid/alert-dialog.d.ts +5 -8
  38. package/dist/solid/avatar.d.ts +8 -4
  39. package/dist/solid/bottom-sheet/bottom-sheet.d.ts +2 -2
  40. package/dist/solid/bottom-sheet/constants.d.ts +1 -1
  41. package/dist/solid/button.d.ts +5 -4
  42. package/dist/solid/charts.d.ts +1 -1
  43. package/dist/solid/checkbox.d.ts +14 -4
  44. package/dist/solid/collapsible.d.ts +33 -5
  45. package/dist/solid/color-disc.d.ts +1 -1
  46. package/dist/solid/color-slider.d.ts +1 -1
  47. package/dist/solid/color-wheel.d.ts +1 -1
  48. package/dist/solid/context-menu.d.ts +58 -24
  49. package/dist/solid/dialog-parts.d.ts +53 -16
  50. package/dist/solid/dialog.d.ts +7 -10
  51. package/dist/solid/dropdown-menu.d.ts +55 -24
  52. package/dist/solid/empty.d.ts +1 -1
  53. package/dist/solid/form.d.ts +5 -6
  54. package/dist/solid/hover-card.d.ts +65 -9
  55. package/dist/solid/index.d.ts +5 -2
  56. package/dist/solid/index.js +8671 -14317
  57. package/dist/solid/input-group.d.ts +6 -6
  58. package/dist/solid/item.d.ts +1 -1
  59. package/dist/solid/menu-parts.d.ts +175 -24
  60. package/dist/solid/option-card.d.ts +1 -4
  61. package/dist/solid/picker-row.d.ts +2 -0
  62. package/dist/solid/popover.d.ts +77 -12
  63. package/dist/solid/progress.d.ts +11 -3
  64. package/dist/solid/radio-group.d.ts +31 -6
  65. package/dist/solid/responsive-dialog.d.ts +3 -1
  66. package/dist/solid/scroll-area.d.ts +1 -1
  67. package/dist/solid/section-card.d.ts +2 -3
  68. package/dist/solid/select.d.ts +107 -10
  69. package/dist/solid/selection.d.ts +10 -1
  70. package/dist/solid/sheet.d.ts +6 -9
  71. package/dist/solid/sliding-indicator.d.ts +1 -1
  72. package/dist/solid/sonner.d.ts +1 -1
  73. package/dist/solid/stale-veil.d.ts +7 -0
  74. package/dist/solid/tabs.d.ts +28 -9
  75. package/dist/solid/toggle-group.d.ts +30 -5
  76. package/dist/solid/toggle.d.ts +11 -3
  77. package/dist/solid/tooltip.d.ts +71 -9
  78. package/dist/tokens/index.js +54 -51
  79. package/dist/{utils-CGN4X-2O.js → utils-3Codx-NZ.js} +848 -618
  80. package/package.json +6 -7
  81. package/src/index.ts +1 -0
  82. package/src/lib/anchor-placement.ts +77 -4
  83. package/src/lib/balance-rows.ts +115 -40
  84. package/src/lib/behavior/anchored.ts +106 -0
  85. package/src/lib/behavior/button-root.tsx +103 -0
  86. package/src/lib/behavior/collection.ts +218 -0
  87. package/src/lib/behavior/controllable.ts +22 -0
  88. package/src/lib/behavior/dismiss.ts +185 -0
  89. package/src/lib/behavior/focus-scope.ts +227 -0
  90. package/src/lib/behavior/hide-outside.ts +85 -0
  91. package/src/lib/behavior/layer-stack.ts +150 -0
  92. package/src/lib/behavior/menu.ts +470 -0
  93. package/src/lib/behavior/modal-root.tsx +452 -0
  94. package/src/lib/behavior/polymorphic.tsx +30 -0
  95. package/src/lib/behavior/presence.ts +89 -0
  96. package/src/lib/behavior/range.ts +212 -0
  97. package/src/lib/behavior/solid.ts +30 -0
  98. package/src/lib/clipboard.ts +4 -4
  99. package/src/lib/color/color.ts +746 -0
  100. package/src/lib/color/index.ts +2 -0
  101. package/src/lib/color/intl.ts +60 -0
  102. package/src/lib/dock-slot-classes.ts +3 -0
  103. package/src/lib/hold.ts +1 -1
  104. package/src/lib/locale.ts +62 -0
  105. package/src/lib/menu-classes.ts +3 -0
  106. package/src/lib/motion-classes.ts +33 -8
  107. package/src/lib/overlay-classes.ts +8 -3
  108. package/src/lib/page-cover.ts +47 -0
  109. package/src/lib/scroll-track.ts +11 -6
  110. package/src/lib/section-tokens.ts +1 -1
  111. package/src/lib/segment-classes.ts +3 -0
  112. package/src/lib/shadow-layer.ts +0 -136
  113. package/src/lib/toggle-set.ts +133 -0
  114. package/src/lib/visually-hidden.ts +15 -0
  115. package/src/solid/accordion.tsx +294 -20
  116. package/src/solid/alert-dialog.tsx +36 -85
  117. package/src/solid/area-picker.tsx +5 -3
  118. package/src/solid/avatar.tsx +98 -24
  119. package/src/solid/badge.tsx +5 -5
  120. package/src/solid/bottom-sheet/bottom-sheet.tsx +73 -129
  121. package/src/solid/bottom-sheet/constants.ts +3 -1
  122. package/src/solid/bottom-sheet/drag-controller.ts +11 -0
  123. package/src/solid/breadcrumb.tsx +4 -4
  124. package/src/solid/button-group.tsx +4 -4
  125. package/src/solid/button.tsx +12 -6
  126. package/src/solid/card.tsx +5 -4
  127. package/src/solid/charts.tsx +1 -1
  128. package/src/solid/checkbox.tsx +167 -44
  129. package/src/solid/collapsible.tsx +260 -12
  130. package/src/solid/color-disc.tsx +11 -12
  131. package/src/solid/color-slider.tsx +114 -47
  132. package/src/solid/color-wheel.tsx +137 -39
  133. package/src/solid/context-menu.tsx +287 -86
  134. package/src/solid/dialog-parts.tsx +135 -46
  135. package/src/solid/dialog.tsx +44 -76
  136. package/src/solid/dock-edit.tsx +5 -4
  137. package/src/solid/dock.tsx +6 -5
  138. package/src/solid/dropdown-menu.tsx +252 -107
  139. package/src/solid/empty.tsx +1 -1
  140. package/src/solid/entity-selector.tsx +5 -3
  141. package/src/solid/face-toggle.tsx +43 -18
  142. package/src/solid/form.tsx +9 -9
  143. package/src/solid/glyph-tile.tsx +48 -21
  144. package/src/solid/hover-card.tsx +288 -21
  145. package/src/solid/icon-picker.tsx +2 -1
  146. package/src/solid/image-picker.tsx +6 -5
  147. package/src/solid/index.ts +5 -4
  148. package/src/solid/input-group.tsx +7 -5
  149. package/src/solid/input-otp.tsx +2 -1
  150. package/src/solid/item.tsx +5 -5
  151. package/src/solid/logo.tsx +5 -4
  152. package/src/solid/menu-parts.tsx +619 -145
  153. package/src/solid/option-card.tsx +6 -10
  154. package/src/solid/picker-row.tsx +10 -5
  155. package/src/solid/picker-search.tsx +2 -1
  156. package/src/solid/popover.tsx +424 -71
  157. package/src/solid/preview-tile.tsx +1 -1
  158. package/src/solid/progress.tsx +84 -15
  159. package/src/solid/radio-group.tsx +271 -34
  160. package/src/solid/responsive-dialog.tsx +10 -10
  161. package/src/solid/schema-form.tsx +1 -6
  162. package/src/solid/scroll-area.tsx +1 -1
  163. package/src/solid/section-card.tsx +3 -4
  164. package/src/solid/select.tsx +950 -95
  165. package/src/solid/selection.tsx +142 -66
  166. package/src/solid/sheet.tsx +48 -80
  167. package/src/solid/slider.tsx +115 -65
  168. package/src/solid/sliding-indicator.tsx +1 -1
  169. package/src/solid/sonner.tsx +1 -1
  170. package/src/solid/spotlight.tsx +23 -11
  171. package/src/solid/stale-veil.tsx +20 -0
  172. package/src/solid/switch.tsx +8 -6
  173. package/src/solid/tabs.tsx +377 -52
  174. package/src/solid/term.tsx +3 -4
  175. package/src/solid/toggle-group.tsx +237 -95
  176. package/src/solid/toggle.tsx +43 -9
  177. package/src/solid/tooltip.tsx +285 -26
  178. package/src/styles/globals.css +64 -10
  179. package/src/styles/theme.css +12 -4
  180. package/src/tokens/material.ts +11 -2
  181. package/dist/lib/when-animations-settle.d.ts +0 -4
  182. package/dist/solid/bottom-sheet/focus-trap.d.ts +0 -6
  183. package/dist/solid/bottom-sheet/scroll-lock.d.ts +0 -2
  184. package/dist/solid/bottom-sheet/sheet-motion.d.ts +0 -1
  185. package/src/lib/when-animations-settle.ts +0 -12
  186. package/src/solid/bottom-sheet/focus-trap.ts +0 -63
  187. package/src/solid/bottom-sheet/scroll-lock.ts +0 -66
  188. package/src/solid/bottom-sheet/sheet-motion.ts +0 -4
@@ -1,145 +1,287 @@
1
- import { ToggleGroup as ToggleGroupPrimitive } from "@kobalte/core/toggle-group";
2
1
  import type { VariantProps } from "cva";
3
2
  import {
3
+ type Accessor,
4
+ children,
4
5
  type Component,
5
6
  type ComponentProps,
6
7
  createContext,
7
- type ParentComponent,
8
+ createSignal,
9
+ type JSX,
8
10
  onCleanup,
9
11
  onMount,
12
+ type ParentComponent,
10
13
  Show,
11
14
  splitProps,
12
15
  useContext,
16
+ type ValidComponent,
13
17
  } from "solid-js";
14
- import { BALANCED_ROWS, balanceRows } from "../lib/balance-rows.js";
18
+ import { createControllableSignal } from "../lib/behavior/controllable.js";
19
+ import { Polymorphic } from "../lib/behavior/polymorphic.js";
20
+ import { merge, omit } from "../lib/behavior/solid.js";
21
+ import { BALANCE_CONTENT, BALANCED_ROWS, balanceRows } from "../lib/balance-rows.js";
15
22
  import { TRACK_SURFACE } from "../lib/card-classes.js";
16
23
  import { SCROLL_TRACK_FADE, trackScroll } from "../lib/scroll-track.js";
24
+ import { SEGMENT_RADIUS } from "../lib/segment-classes.js";
25
+ import { createToggleItem, createToggleSet, type ToggleSet } from "../lib/toggle-set.js";
17
26
  import { cn } from "../lib/utils.js";
18
27
  import { SelectionHighlight, SelectionScope } from "./selection.js";
19
28
  import { toggleVariants } from "./toggle.js";
20
29
 
30
+ type Orientation = "horizontal" | "vertical";
31
+
32
+ type SingleSelection = {
33
+ multiple?: false;
34
+ value?: string | null;
35
+ defaultValue?: string | null;
36
+ onChange?: (value: string | null) => void;
37
+ };
38
+
39
+ type MultipleSelection = {
40
+ multiple: true;
41
+ value?: string[];
42
+ defaultValue?: string[];
43
+ onChange?: (value: string[]) => void;
44
+ };
45
+
46
+ type ToggleGroupProps = Omit<ComponentProps<"div">, "onChange"> &
47
+ VariantProps<typeof toggleVariants> &
48
+ (SingleSelection | MultipleSelection) & {
49
+ as?: ValidComponent;
50
+ orientation?: Orientation;
51
+ disabled?: boolean;
52
+ /** CSS colour for the selected segment's glass. Default `var(--material-accent)`. */
53
+ tone?: string;
54
+ /** One row that scrolls sideways when it outgrows its container, instead of wrapping. */
55
+ scroll?: boolean;
56
+ };
57
+
21
58
  // Single-select paints the pressed segment with the selection highlight; multi-select has none.
22
- type ToggleGroupContextValue = VariantProps<typeof toggleVariants> & { sliding: boolean };
23
-
24
- const ToggleGroupContext = createContext<ToggleGroupContextValue>({
25
- size: "default",
26
- variant: "default",
27
- sliding: true,
28
- });
29
-
30
- const ToggleGroup: ParentComponent<
31
- ComponentProps<typeof ToggleGroupPrimitive> &
32
- VariantProps<typeof toggleVariants> & {
33
- /** CSS colour for the selected segment's glass. Default `var(--material-accent)`. */
34
- tone?: string;
35
- /** One row that scrolls sideways when it outgrows its container, instead of wrapping. */
36
- scroll?: boolean;
37
- }
38
- > = (props) => {
39
- const [local, rest] = splitProps(props, [
59
+ type ToggleGroupContextValue = VariantProps<typeof toggleVariants> & {
60
+ sliding: boolean;
61
+ disabled: Accessor<boolean>;
62
+ set: ToggleSet;
63
+ };
64
+
65
+ const ToggleGroupContext = createContext<ToggleGroupContextValue | null>(null);
66
+
67
+ const useToggleGroup = (): ToggleGroupContextValue => {
68
+ const ctx = useContext(ToggleGroupContext);
69
+ if (!ctx) throw new Error("ToggleGroupItem renders inside a ToggleGroup");
70
+ return ctx;
71
+ };
72
+
73
+ const ITEM = '[data-slot="toggle-group-item"]';
74
+
75
+ const toList = (value: string | string[] | null | undefined): string[] | undefined => {
76
+ if (value === undefined) return undefined;
77
+ if (value === null) return [];
78
+ return typeof value === "string" ? [value] : value;
79
+ };
80
+
81
+ const sameList = (a: string[], b: string[]) =>
82
+ a.length === b.length && a.every((v, i) => v === b[i]);
83
+
84
+ const ToggleGroup: ParentComponent<ToggleGroupProps> = (props) => {
85
+ const rest = omit(
86
+ props,
40
87
  "class",
41
88
  "variant",
42
89
  "size",
43
90
  "tone",
44
91
  "scroll",
45
92
  "children",
46
- ] as const);
47
- // Read `multiple` without splitting it out: Kobalte's root is a single/multiple
48
- // discriminated union, so pulling `multiple` into a separate prop collapses the
49
- // union and mistypes `value`. Leave it in `rest` and just peek at it here.
50
- const sliding = () => !(props as { multiple?: boolean }).multiple;
51
- // Same peek for `orientation`: a column stacks its segments instead of balancing rows.
52
- const vertical = () => (props as { orientation?: string }).orientation === "vertical";
53
- let root: HTMLDivElement | undefined;
54
- let scope: HTMLDivElement | undefined;
93
+ "value",
94
+ "defaultValue",
95
+ "onChange",
96
+ "multiple",
97
+ "orientation",
98
+ "disabled",
99
+ "id",
100
+ "ref",
101
+ "as",
102
+ );
103
+ const sliding = () => !props.multiple;
104
+ const orientation = () => props.orientation ?? "horizontal";
105
+ const vertical = () => orientation() === "vertical";
106
+ const [values, setValues] = createControllableSignal<string[]>({
107
+ value: () => toList(props.value),
108
+ defaultValue: () => toList(props.defaultValue) ?? [],
109
+ equals: sameList,
110
+ onChange: (next) => {
111
+ if (props.multiple) props.onChange?.(next);
112
+ else props.onChange?.(next[0] ?? null);
113
+ },
114
+ });
115
+ const [root, setRoot] = createSignal<HTMLElement>();
116
+ const [scope, setScope] = createSignal<HTMLElement>();
117
+ const set = createToggleSet({
118
+ root,
119
+ itemSelector: `${ITEM}[data-key]`,
120
+ values,
121
+ setValues,
122
+ multiple: () => props.multiple ?? false,
123
+ orientation,
124
+ id: () => props.id,
125
+ });
126
+
55
127
  onMount(() => {
56
- if (!root) return;
57
- if (local.scroll) onCleanup(trackScroll(root, "[data-pressed]", "data-pressed"));
58
- else if (!vertical())
59
- onCleanup(balanceRows(root, scope ?? root, '[data-slot="toggle-group-item"]'));
128
+ const el = root();
129
+ if (!el) return;
130
+ if (props.scroll) onCleanup(trackScroll(el, "[data-pressed]", values));
131
+ else if (!vertical()) onCleanup(balanceRows(el, scope() ?? el, ITEM, set.members));
60
132
  });
133
+
134
+ const context: ToggleGroupContextValue = {
135
+ get variant() {
136
+ return props.variant;
137
+ },
138
+ get size() {
139
+ return props.size;
140
+ },
141
+ get sliding() {
142
+ return sliding();
143
+ },
144
+ disabled: () => props.disabled ?? false,
145
+ set,
146
+ };
147
+
61
148
  return (
62
- <ToggleGroupPrimitive
63
- ref={root}
64
- data-slot="toggle-group"
65
- data-variant={local.variant}
66
- data-size={local.size}
67
- data-scroll={local.scroll ? "" : undefined}
68
- class={cn(
69
- `group/toggle-group w-fit max-w-full items-center gap-y-1 rounded-lg data-[wrapped]:w-full ${TRACK_SURFACE} p-1 data-[variant=outline]:shadow-xs`,
70
- local.scroll
71
- ? `flex [scrollbar-width:none] flex-nowrap overflow-x-auto ${SCROLL_TRACK_FADE}`
72
- : vertical()
73
- ? "flex flex-col items-stretch"
74
- : sliding()
75
- ? "flex"
76
- : BALANCED_ROWS,
77
- local.class,
78
- )}
79
- {...rest}
149
+ <Polymorphic
150
+ {...merge({ "data-slot": "toggle-group" }, rest, {
151
+ as: props.as,
152
+ defaultAs: "div" as ValidComponent,
153
+ ref: (el: HTMLElement) => {
154
+ setRoot(el);
155
+ if (typeof props.ref === "function") props.ref(el as HTMLDivElement);
156
+ },
157
+ role: "group",
158
+ get id() {
159
+ return set.id();
160
+ },
161
+ get "data-orientation"() {
162
+ return orientation();
163
+ },
164
+ get "data-variant"() {
165
+ return props.variant;
166
+ },
167
+ get "data-size"() {
168
+ return props.size;
169
+ },
170
+ get "data-scroll"() {
171
+ return props.scroll ? "" : undefined;
172
+ },
173
+ get [BALANCED_ROWS]() {
174
+ return props.scroll || vertical() || sliding() ? undefined : "";
175
+ },
176
+ "on:keydown": (event: KeyboardEvent) => set.collection.onKeyDown(event),
177
+ get class() {
178
+ return cn(
179
+ `group/toggle-group w-fit max-w-full items-center gap-y-1 rounded-lg data-[wrapped]:w-full ${TRACK_SURFACE} p-1 data-[variant=outline]:shadow-xs`,
180
+ props.scroll
181
+ ? `flex [scrollbar-width:none] flex-nowrap overflow-x-auto ${SCROLL_TRACK_FADE}`
182
+ : vertical()
183
+ ? "flex flex-col items-stretch"
184
+ : sliding()
185
+ ? "flex"
186
+ : undefined,
187
+ props.class,
188
+ );
189
+ },
190
+ })}
80
191
  >
81
- <ToggleGroupContext.Provider
82
- value={{ variant: local.variant, size: local.size, sliding: sliding() }}
83
- >
84
- <Show when={sliding()} fallback={local.children}>
192
+ <ToggleGroupContext.Provider value={context}>
193
+ <Show when={sliding()} fallback={props.children}>
85
194
  {/* w-full, not w-fit: a shrink-to-fit root still sizes to content, and a
86
195
  `class="w-full"` root can finally let its items share the width. */}
87
196
  <SelectionScope
88
- ref={scope}
197
+ ref={setScope}
89
198
  marker="pressed"
90
- tone={local.tone}
199
+ radius={SEGMENT_RADIUS}
200
+ tone={props.tone}
201
+ {...{ [BALANCED_ROWS]: props.scroll || vertical() ? undefined : "" }}
91
202
  class={
92
- local.scroll
203
+ props.scroll
93
204
  ? "flex w-max flex-nowrap items-center"
94
205
  : vertical()
95
206
  ? "flex w-full flex-col items-stretch gap-y-0.5"
96
- : `w-full items-center gap-y-1 ${BALANCED_ROWS}`
207
+ : "w-full items-center gap-y-1"
97
208
  }
98
209
  >
99
- {local.children}
210
+ {props.children}
100
211
  </SelectionScope>
101
212
  </Show>
102
213
  </ToggleGroupContext.Provider>
103
- </ToggleGroupPrimitive>
214
+ </Polymorphic>
104
215
  );
105
216
  };
106
217
 
107
- const ToggleGroupItem: Component<
108
- ComponentProps<typeof ToggleGroupPrimitive.Item> & VariantProps<typeof toggleVariants>
109
- > = (props) => {
110
- const [local, rest] = splitProps(props, ["class", "variant", "size", "children"] as const);
111
- const context = useContext(ToggleGroupContext);
218
+ type ToggleGroupItemState = { pressed: Accessor<boolean> };
219
+
220
+ type ToggleGroupItemProps = Omit<ComponentProps<"button">, "children"> &
221
+ VariantProps<typeof toggleVariants> & {
222
+ as?: ValidComponent;
223
+ value: string;
224
+ disabled?: boolean;
225
+ children?: JSX.Element | ((state: ToggleGroupItemState) => JSX.Element);
226
+ };
227
+
228
+ const ToggleGroupItem: Component<ToggleGroupItemProps> = (props) => {
229
+ const context = useToggleGroup();
230
+ const [local, rest] = splitProps(props, [
231
+ "class",
232
+ "variant",
233
+ "size",
234
+ "children",
235
+ "value",
236
+ "disabled",
237
+ "id",
238
+ "ref",
239
+ "as",
240
+ ]);
241
+ const disabled = () => local.disabled || context.disabled();
242
+ const item = createToggleItem(context.set, { value: () => local.value, disabled });
243
+ const content = children(() => {
244
+ const body = local.children;
245
+ return typeof body === "function" ? body({ pressed: item.pressed }) : body;
246
+ });
112
247
 
113
248
  return (
114
- <ToggleGroupPrimitive.Item
115
- data-slot="toggle-group-item"
116
- data-variant={context.variant || local.variant}
117
- data-size={context.size || local.size}
118
- class={cn(
119
- toggleVariants({
120
- variant: context.variant || local.variant,
121
- size: context.size || local.size,
122
- }),
123
- "relative isolate min-w-max flex-1 shrink-0 rounded-md px-4 shadow-none focus:z-10 focus-visible:z-10 data-[variant=outline]:border-l-0 data-[variant=outline]:first-of-type:border-l",
124
- // A hover fill would read as a second, squarer selection, so hover only tints the text.
125
- context.sliding
126
- ? "hover:text-(--material-accent)"
127
- : "data-[pressed]:bg-muted data-[pressed]:text-foreground not-data-[pressed]:hover:text-(--material-accent)",
128
- local.class,
129
- )}
130
- {...rest}
249
+ <Polymorphic
250
+ {...merge({ "data-slot": "toggle-group-item" }, rest, item.props, {
251
+ as: local.as,
252
+ defaultAs: "button" as ValidComponent,
253
+ ref: (node: HTMLElement) => {
254
+ item.ref(node);
255
+ if (typeof local.ref === "function") local.ref(node as HTMLButtonElement);
256
+ },
257
+ get id() {
258
+ return local.id ?? item.props.id;
259
+ },
260
+ get "data-variant"() {
261
+ return context.variant || local.variant;
262
+ },
263
+ get "data-size"() {
264
+ return context.size || local.size;
265
+ },
266
+ get class() {
267
+ return cn(
268
+ toggleVariants({
269
+ variant: context.variant || local.variant,
270
+ size: context.size || local.size,
271
+ }),
272
+ "relative isolate min-w-max flex-1 shrink-0 rounded-md px-4 shadow-none focus:z-10 focus-visible:z-10 data-[variant=outline]:border-l-0 data-[variant=outline]:first-of-type:border-l",
273
+ // A hover fill would read as a second, squarer selection, so hover only tints the text.
274
+ context.sliding
275
+ ? "hover:text-(--material-accent)"
276
+ : "data-[pressed]:bg-muted data-[pressed]:text-foreground not-data-[pressed]:hover:text-(--material-accent)",
277
+ local.class,
278
+ );
279
+ },
280
+ })}
131
281
  >
132
- {(state) => {
133
- // Each read of a JSX child getter builds it again.
134
- const content = local.children;
135
- return (
136
- <>
137
- <SelectionHighlight />
138
- {typeof content === "function" ? content(state) : content}
139
- </>
140
- );
141
- }}
142
- </ToggleGroupPrimitive.Item>
282
+ <SelectionHighlight active={item.pressed()} />
283
+ <span {...{ [BALANCE_CONTENT]: "" }}>{content()}</span>
284
+ </Polymorphic>
143
285
  );
144
286
  };
145
287
 
@@ -1,6 +1,8 @@
1
- import { ToggleButton as ToggleButtonPrimitive } from "@kobalte/core/toggle-button";
2
1
  import { cva, type VariantProps } from "cva";
3
- import { type Component, type ComponentProps, splitProps } from "solid-js";
2
+ import { type Accessor, children, type JSX, splitProps } from "solid-js";
3
+ import { ButtonRoot, type ButtonRootProps } from "../lib/behavior/button-root.js";
4
+ import { createControllableSignal } from "../lib/behavior/controllable.js";
5
+ import { callEventHandler } from "../lib/behavior/solid.js";
4
6
  import { OUTLINE_SURFACE } from "../lib/button-variants.js";
5
7
  import { CONTROL_H } from "../lib/input-classes.js";
6
8
  import { SEGMENT_ITEM } from "../lib/segment-classes.js";
@@ -36,13 +38,39 @@ const TOGGLE_HOVER: Record<"default" | "outline", string> = {
36
38
  outline: "",
37
39
  };
38
40
 
39
- const Toggle: Component<
40
- ComponentProps<typeof ToggleButtonPrimitive> & VariantProps<typeof toggleVariants>
41
- > = (props) => {
42
- const [local, rest] = splitProps(props, ["class", "variant", "size"] as const);
41
+ type ToggleProps = Omit<ButtonRootProps<"button">, "children" | "onChange"> &
42
+ VariantProps<typeof toggleVariants> & {
43
+ pressed?: boolean;
44
+ defaultPressed?: boolean;
45
+ onChange?: (pressed: boolean) => void;
46
+ children?: JSX.Element | ((state: { pressed: Accessor<boolean> }) => JSX.Element);
47
+ };
48
+
49
+ function Toggle(props: ToggleProps) {
50
+ const [local, rest] = splitProps(props, [
51
+ "class",
52
+ "variant",
53
+ "size",
54
+ "pressed",
55
+ "defaultPressed",
56
+ "onChange",
57
+ "onClick",
58
+ "children",
59
+ ]);
60
+ const [pressed, setPressed] = createControllableSignal({
61
+ value: () => local.pressed,
62
+ defaultValue: () => local.defaultPressed ?? false,
63
+ onChange: (next) => local.onChange?.(next),
64
+ });
65
+ const content = children(() => {
66
+ const body = local.children;
67
+ return typeof body === "function" ? body({ pressed }) : body;
68
+ });
43
69
  return (
44
- <ToggleButtonPrimitive
70
+ <ButtonRoot
45
71
  data-slot="toggle"
72
+ aria-pressed={pressed()}
73
+ data-pressed={pressed() ? "" : undefined}
46
74
  class={cn(
47
75
  toggleVariants({ variant: local.variant, size: local.size }),
48
76
  TOGGLE_HOVER[local.variant ?? "default"],
@@ -50,8 +78,14 @@ const Toggle: Component<
50
78
  local.class,
51
79
  )}
52
80
  {...rest}
53
- />
81
+ onClick={(event: MouseEvent & { currentTarget: HTMLButtonElement; target: Element }) => {
82
+ callEventHandler(event, local.onClick);
83
+ if (!rest.disabled) setPressed(!pressed());
84
+ }}
85
+ >
86
+ {content()}
87
+ </ButtonRoot>
54
88
  );
55
- };
89
+ }
56
90
 
57
91
  export { Toggle, toggleVariants };