@dinachi/cli 0.11.0 → 1.0.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.
- package/CHANGELOG.md +146 -0
- package/dist/index.js +74 -8
- package/package.json +2 -2
- package/templates/accordion/accordion.tsx +66 -12
- package/templates/agent/agent.tsx +1865 -0
- package/templates/agent/index.ts +52 -0
- package/templates/alert-dialog/alert-dialog.tsx +46 -17
- package/templates/alert-dialog/index.ts +2 -0
- package/templates/animated-tabs/animated-tabs.tsx +65 -5
- package/templates/autocomplete/autocomplete.tsx +174 -18
- package/templates/avatar/avatar.tsx +5 -2
- package/templates/carousel/carousel.tsx +102 -24
- package/templates/checkbox/checkbox.tsx +60 -4
- package/templates/checkbox-group/checkbox-group.tsx +82 -8
- package/templates/collapsible/collapsible.tsx +13 -4
- package/templates/combobox/combobox.tsx +299 -23
- package/templates/combobox/index.ts +1 -0
- package/templates/compare-slider/compare-slider.tsx +103 -28
- package/templates/compare-slider/index.ts +1 -1
- package/templates/context-menu/context-menu.tsx +37 -15
- package/templates/dialog/dialog.tsx +41 -12
- package/templates/drawer/drawer.tsx +18 -3
- package/templates/expandable-card/expandable-card.tsx +65 -42
- package/templates/field/field.tsx +2 -0
- package/templates/hold-to-confirm/hold-to-confirm.tsx +99 -16
- package/templates/hold-to-confirm/index.ts +1 -1
- package/templates/input/input.tsx +59 -1
- package/templates/menu/index.ts +5 -0
- package/templates/menu/menu.tsx +105 -10
- package/templates/menubar/menubar.tsx +26 -8
- package/templates/navigation-menu/navigation-menu.tsx +67 -23
- package/templates/number-field/index.ts +1 -0
- package/templates/number-field/number-field.tsx +71 -2
- package/templates/otp-field/otp-field.tsx +57 -11
- package/templates/popover/popover.tsx +18 -3
- package/templates/preview-card/preview-card.tsx +16 -2
- package/templates/radio/radio.tsx +71 -6
- package/templates/select/select.tsx +223 -10
- package/templates/slider/slider.tsx +77 -8
- package/templates/sortable/sortable.tsx +76 -9
- package/templates/swipeable-row/index.ts +1 -1
- package/templates/swipeable-row/swipeable-row.tsx +125 -41
- package/templates/switch/switch.tsx +41 -4
- package/templates/tabs/tabs.tsx +74 -10
- package/templates/textarea/index.ts +1 -1
- package/templates/textarea/textarea.tsx +70 -4
- package/templates/toast/toast.tsx +3 -1
- package/templates/toggle/toggle.tsx +39 -7
- package/templates/toggle-group/toggle-group.tsx +96 -18
|
@@ -10,6 +10,13 @@ import {
|
|
|
10
10
|
type PanInfo,
|
|
11
11
|
} from "motion/react"
|
|
12
12
|
import { cn } from "@/lib/utils"
|
|
13
|
+
import {
|
|
14
|
+
changeAsAgent,
|
|
15
|
+
matchAgentChoice,
|
|
16
|
+
useAgentControl,
|
|
17
|
+
useHasAgent,
|
|
18
|
+
useMergedRef,
|
|
19
|
+
} from "@/components/ui/agent"
|
|
13
20
|
|
|
14
21
|
/**
|
|
15
22
|
* A carousel that lands where the flick was aimed.
|
|
@@ -42,10 +49,34 @@ const PROJECTION = 0.2
|
|
|
42
49
|
|
|
43
50
|
const SNAP = { type: "spring", stiffness: 380, damping: 40, mass: 0.9 } as const
|
|
44
51
|
|
|
52
|
+
/** What moved the carousel. Cancel one from `onIndexChange` and it stays where it was. */
|
|
53
|
+
export interface CarouselChangeDetails {
|
|
54
|
+
reason: "arrow-press" | "dot-press" | "drag" | "scroll" | "imperative-action"
|
|
55
|
+
event: Event | undefined
|
|
56
|
+
isCanceled: boolean
|
|
57
|
+
cancel: () => void
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
function personChange(reason: Exclude<CarouselChangeDetails["reason"], "imperative-action">, event?: Event) {
|
|
61
|
+
const details: CarouselChangeDetails = {
|
|
62
|
+
reason,
|
|
63
|
+
event,
|
|
64
|
+
isCanceled: false,
|
|
65
|
+
cancel: () => {
|
|
66
|
+
details.isCanceled = true
|
|
67
|
+
},
|
|
68
|
+
}
|
|
69
|
+
return details
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/** Inside `AgentProvider` the carousel is one control, so its arrows and dots stay out. */
|
|
73
|
+
const usePart = () => (useHasAgent() ? "" : undefined)
|
|
74
|
+
|
|
45
75
|
interface CarouselContextValue {
|
|
46
76
|
index: number
|
|
47
77
|
count: number
|
|
48
|
-
goTo: (next: number, velocity?: number) => void
|
|
78
|
+
goTo: (next: number, details: CarouselChangeDetails, velocity?: number) => void
|
|
79
|
+
disabled: boolean
|
|
49
80
|
reducedMotion: boolean
|
|
50
81
|
x: ReturnType<typeof useMotionValue<number>>
|
|
51
82
|
offsets: number[]
|
|
@@ -56,7 +87,7 @@ interface CarouselContextValue {
|
|
|
56
87
|
* shortened list leaves stale nodes behind and a stale node still reports an offset. */
|
|
57
88
|
measureSlides: (count: number) => number[]
|
|
58
89
|
currentIndex: () => number
|
|
59
|
-
setIndex: (next: number) =>
|
|
90
|
+
setIndex: (next: number, details: CarouselChangeDetails) => number
|
|
60
91
|
}
|
|
61
92
|
|
|
62
93
|
const CarouselContext = React.createContext<CarouselContextValue | null>(null)
|
|
@@ -77,7 +108,10 @@ export interface CarouselProps extends React.ComponentProps<"div"> {
|
|
|
77
108
|
label?: string
|
|
78
109
|
index?: number
|
|
79
110
|
defaultIndex?: number
|
|
80
|
-
|
|
111
|
+
/** Fired when the slide changes. Cancel `details` to stay on the current slide. */
|
|
112
|
+
onIndexChange?: (index: number, details: CarouselChangeDetails) => void
|
|
113
|
+
/** Turns off the drag, the arrows and the dots. */
|
|
114
|
+
disabled?: boolean
|
|
81
115
|
}
|
|
82
116
|
|
|
83
117
|
const Carousel = React.forwardRef<HTMLDivElement, CarouselProps>(
|
|
@@ -87,6 +121,7 @@ const Carousel = React.forwardRef<HTMLDivElement, CarouselProps>(
|
|
|
87
121
|
index: indexProp,
|
|
88
122
|
defaultIndex = 0,
|
|
89
123
|
onIndexChange,
|
|
124
|
+
disabled = false,
|
|
90
125
|
className,
|
|
91
126
|
children,
|
|
92
127
|
...props
|
|
@@ -112,13 +147,16 @@ const Carousel = React.forwardRef<HTMLDivElement, CarouselProps>(
|
|
|
112
147
|
indexRef.current = index
|
|
113
148
|
}, [index])
|
|
114
149
|
|
|
150
|
+
/** Moves to `next` unless the app cancels, and returns the slide it is on after. */
|
|
115
151
|
const setIndex = React.useCallback(
|
|
116
|
-
(next: number) => {
|
|
152
|
+
(next: number, details: CarouselChangeDetails) => {
|
|
117
153
|
// Scrolling reports the same slide many times over. Only a change is news.
|
|
118
|
-
if (next === indexRef.current) return
|
|
154
|
+
if (next === indexRef.current) return next
|
|
155
|
+
onIndexChange?.(next, details)
|
|
156
|
+
if (details.isCanceled) return indexRef.current
|
|
119
157
|
indexRef.current = next
|
|
120
158
|
if (indexProp === undefined) setUncontrolled(next)
|
|
121
|
-
|
|
159
|
+
return next
|
|
122
160
|
},
|
|
123
161
|
[indexProp, onIndexChange]
|
|
124
162
|
)
|
|
@@ -138,13 +176,13 @@ const Carousel = React.forwardRef<HTMLDivElement, CarouselProps>(
|
|
|
138
176
|
}, [])
|
|
139
177
|
|
|
140
178
|
const goTo = React.useCallback(
|
|
141
|
-
(next: number, velocity = 0) => {
|
|
179
|
+
(next: number, details: CarouselChangeDetails, velocity = 0) => {
|
|
142
180
|
const last = offsets.length - 1
|
|
143
181
|
if (last < 0) return
|
|
144
|
-
|
|
145
|
-
setIndex(
|
|
182
|
+
// A cancelled change still settles the strip, back onto the slide it stayed on.
|
|
183
|
+
const landed = setIndex(Math.min(Math.max(next, 0), last), details)
|
|
146
184
|
|
|
147
|
-
const to = offsets[
|
|
185
|
+
const to = offsets[landed]
|
|
148
186
|
if (to === undefined) return
|
|
149
187
|
|
|
150
188
|
if (reducedMotion) {
|
|
@@ -156,11 +194,33 @@ const Carousel = React.forwardRef<HTMLDivElement, CarouselProps>(
|
|
|
156
194
|
[offsets, setIndex, reducedMotion, x]
|
|
157
195
|
)
|
|
158
196
|
|
|
197
|
+
const root = React.useRef<HTMLDivElement>(null)
|
|
198
|
+
useAgentControl(() => {
|
|
199
|
+
const nodes = slides.current.slice(0, count)
|
|
200
|
+
const choices = nodes.map((node, i) => ({ value: i + 1, label: slideName(node, i) }))
|
|
201
|
+
return {
|
|
202
|
+
kind: "carousel",
|
|
203
|
+
element: root.current,
|
|
204
|
+
value: choices[index]?.label,
|
|
205
|
+
items: choices.map((c) => c.label),
|
|
206
|
+
disabled,
|
|
207
|
+
actions: {
|
|
208
|
+
goTo: (wanted: unknown) => {
|
|
209
|
+
const match = matchAgentChoice(choices, wanted)
|
|
210
|
+
if (typeof match === "string") return match
|
|
211
|
+
if (offsets.length === 0) return "The carousel has not measured its slides yet"
|
|
212
|
+
return changeAsAgent<number, CarouselChangeDetails>(goTo, match.value - 1)
|
|
213
|
+
},
|
|
214
|
+
},
|
|
215
|
+
}
|
|
216
|
+
})
|
|
217
|
+
|
|
159
218
|
const context = React.useMemo(
|
|
160
219
|
() => ({
|
|
161
220
|
index,
|
|
162
221
|
count,
|
|
163
222
|
goTo,
|
|
223
|
+
disabled,
|
|
164
224
|
reducedMotion,
|
|
165
225
|
x,
|
|
166
226
|
offsets,
|
|
@@ -175,6 +235,7 @@ const Carousel = React.forwardRef<HTMLDivElement, CarouselProps>(
|
|
|
175
235
|
index,
|
|
176
236
|
count,
|
|
177
237
|
goTo,
|
|
238
|
+
disabled,
|
|
178
239
|
reducedMotion,
|
|
179
240
|
x,
|
|
180
241
|
offsets,
|
|
@@ -188,10 +249,11 @@ const Carousel = React.forwardRef<HTMLDivElement, CarouselProps>(
|
|
|
188
249
|
return (
|
|
189
250
|
<CarouselContext.Provider value={context}>
|
|
190
251
|
<div
|
|
191
|
-
ref={ref}
|
|
252
|
+
ref={useMergedRef(ref, root)}
|
|
192
253
|
role="group"
|
|
193
254
|
aria-roledescription="carousel"
|
|
194
255
|
aria-label={label}
|
|
256
|
+
data-disabled={disabled ? "" : undefined}
|
|
195
257
|
className={cn("space-y-4", className)}
|
|
196
258
|
{...props}
|
|
197
259
|
>
|
|
@@ -213,6 +275,7 @@ const CarouselViewport = React.forwardRef<HTMLDivElement, CarouselViewportProps>
|
|
|
213
275
|
offsets,
|
|
214
276
|
setOffsets,
|
|
215
277
|
goTo,
|
|
278
|
+
disabled,
|
|
216
279
|
reducedMotion,
|
|
217
280
|
scroller,
|
|
218
281
|
measureSlides,
|
|
@@ -271,10 +334,13 @@ const CarouselViewport = React.forwardRef<HTMLDivElement, CarouselViewportProps>
|
|
|
271
334
|
// thing left to do is keep the dots honest about where it stopped.
|
|
272
335
|
onScroll={(event) => {
|
|
273
336
|
if (offsets.length === 0) return
|
|
274
|
-
|
|
337
|
+
const target = nearest(offsets, event.currentTarget.scrollLeft)
|
|
338
|
+
const landed = setIndex(target, personChange("scroll", event.nativeEvent))
|
|
339
|
+
if (landed !== target) event.currentTarget.scrollTo({ left: offsets[landed], behavior: "auto" })
|
|
275
340
|
}}
|
|
276
341
|
className={cn(
|
|
277
|
-
"relative flex snap-x snap-mandatory gap-3
|
|
342
|
+
"relative flex snap-x snap-mandatory gap-3 pb-2",
|
|
343
|
+
disabled ? "overflow-x-hidden" : "overflow-x-auto",
|
|
278
344
|
className
|
|
279
345
|
)}
|
|
280
346
|
{...props}
|
|
@@ -284,13 +350,13 @@ const CarouselViewport = React.forwardRef<HTMLDivElement, CarouselViewportProps>
|
|
|
284
350
|
)
|
|
285
351
|
}
|
|
286
352
|
|
|
287
|
-
const onDragEnd = (
|
|
353
|
+
const onDragEnd = (event: MouseEvent | TouchEvent | PointerEvent, info: PanInfo) => {
|
|
288
354
|
if (offsets.length === 0) return
|
|
289
355
|
|
|
290
356
|
const projected = -(x.get() + info.velocity.x * PROJECTION)
|
|
291
357
|
// Nearest by measured position rather than by dividing through a nominal slide width,
|
|
292
358
|
// so uneven slides land as accurately as even ones.
|
|
293
|
-
goTo(nearest(offsets, projected), info.velocity.x)
|
|
359
|
+
goTo(nearest(offsets, projected), personChange("drag", event), info.velocity.x)
|
|
294
360
|
}
|
|
295
361
|
|
|
296
362
|
return (
|
|
@@ -300,7 +366,7 @@ const CarouselViewport = React.forwardRef<HTMLDivElement, CarouselViewportProps>
|
|
|
300
366
|
{...props}
|
|
301
367
|
>
|
|
302
368
|
<motion.div
|
|
303
|
-
drag="x"
|
|
369
|
+
drag={disabled ? false : "x"}
|
|
304
370
|
style={{ x }}
|
|
305
371
|
dragConstraints={{ left: -(offsets[offsets.length - 1] ?? 0), right: 0 }}
|
|
306
372
|
// Give at both ends rather than a wall, so the edge of the set is something you
|
|
@@ -366,7 +432,8 @@ export interface CarouselDotsProps extends React.ComponentProps<"div"> {
|
|
|
366
432
|
*/
|
|
367
433
|
const CarouselDots = React.forwardRef<HTMLDivElement, CarouselDotsProps>(
|
|
368
434
|
({ labels, dotClassName, className, ...props }, ref) => {
|
|
369
|
-
const { index, count, goTo } = useCarousel("CarouselDots")
|
|
435
|
+
const { index, count, goTo, disabled } = useCarousel("CarouselDots")
|
|
436
|
+
const part = usePart()
|
|
370
437
|
|
|
371
438
|
return (
|
|
372
439
|
<div
|
|
@@ -384,7 +451,9 @@ const CarouselDots = React.forwardRef<HTMLDivElement, CarouselDotsProps>(
|
|
|
384
451
|
labels?.[i] ? `Go to slide ${i + 1}: ${labels[i]}` : `Go to slide ${i + 1}`
|
|
385
452
|
}
|
|
386
453
|
aria-current={i === index ? "true" : undefined}
|
|
387
|
-
|
|
454
|
+
disabled={disabled}
|
|
455
|
+
data-agent-part={part}
|
|
456
|
+
onClick={(event) => goTo(i, personChange("dot-press", event.nativeEvent))}
|
|
388
457
|
className={cn(
|
|
389
458
|
"h-1.5 rounded-full transition-all duration-200 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 focus-visible:ring-offset-background",
|
|
390
459
|
i === index ? "w-6 bg-primary" : "w-1.5 bg-border hover:bg-muted-foreground",
|
|
@@ -402,15 +471,16 @@ export type CarouselArrowProps = React.ComponentProps<"button">
|
|
|
402
471
|
|
|
403
472
|
const CarouselPrevious = React.forwardRef<HTMLButtonElement, CarouselArrowProps>(
|
|
404
473
|
({ className, children, ...props }, ref) => {
|
|
405
|
-
const { index, goTo } = useCarousel("CarouselPrevious")
|
|
474
|
+
const { index, goTo, disabled } = useCarousel("CarouselPrevious")
|
|
406
475
|
|
|
407
476
|
return (
|
|
408
477
|
<button
|
|
409
478
|
ref={ref}
|
|
410
479
|
type="button"
|
|
411
480
|
aria-label="Previous slide"
|
|
412
|
-
disabled={index === 0}
|
|
413
|
-
|
|
481
|
+
disabled={disabled || index === 0}
|
|
482
|
+
data-agent-part={usePart()}
|
|
483
|
+
onClick={(event) => goTo(index - 1, personChange("arrow-press", event.nativeEvent))}
|
|
414
484
|
className={cn(arrowClass, className)}
|
|
415
485
|
{...props}
|
|
416
486
|
>
|
|
@@ -423,15 +493,16 @@ CarouselPrevious.displayName = "CarouselPrevious"
|
|
|
423
493
|
|
|
424
494
|
const CarouselNext = React.forwardRef<HTMLButtonElement, CarouselArrowProps>(
|
|
425
495
|
({ className, children, ...props }, ref) => {
|
|
426
|
-
const { index, count, goTo } = useCarousel("CarouselNext")
|
|
496
|
+
const { index, count, goTo, disabled } = useCarousel("CarouselNext")
|
|
427
497
|
|
|
428
498
|
return (
|
|
429
499
|
<button
|
|
430
500
|
ref={ref}
|
|
431
501
|
type="button"
|
|
432
502
|
aria-label="Next slide"
|
|
433
|
-
disabled={count === 0 || index === count - 1}
|
|
434
|
-
|
|
503
|
+
disabled={disabled || count === 0 || index === count - 1}
|
|
504
|
+
data-agent-part={usePart()}
|
|
505
|
+
onClick={(event) => goTo(index + 1, personChange("arrow-press", event.nativeEvent))}
|
|
435
506
|
className={cn(arrowClass, className)}
|
|
436
507
|
{...props}
|
|
437
508
|
>
|
|
@@ -463,6 +534,13 @@ function ChevronIcon({ direction }: { direction: "left" | "right" }) {
|
|
|
463
534
|
)
|
|
464
535
|
}
|
|
465
536
|
|
|
537
|
+
/** A slide's own name, without the "2 of 5: " its accessible name starts with. */
|
|
538
|
+
function slideName(node: HTMLElement | null | undefined, i: number) {
|
|
539
|
+
const name = node?.getAttribute("aria-label") ?? ""
|
|
540
|
+
const at = name.indexOf(": ")
|
|
541
|
+
return at < 0 ? String(i + 1) : name.slice(at + 2)
|
|
542
|
+
}
|
|
543
|
+
|
|
466
544
|
function nearest(offsets: number[], target: number) {
|
|
467
545
|
let closest = 0
|
|
468
546
|
for (let i = 1; i < offsets.length; i += 1) {
|
|
@@ -2,20 +2,75 @@ import * as React from "react";
|
|
|
2
2
|
import { Checkbox as BaseCheckbox } from '@base-ui/react/checkbox';
|
|
3
3
|
import { cn } from "@/lib/utils";
|
|
4
4
|
import { Check } from "lucide-react";
|
|
5
|
+
import {
|
|
6
|
+
AgentGroupContext,
|
|
7
|
+
agentChecked,
|
|
8
|
+
changeAsAgent,
|
|
9
|
+
useAgentControl,
|
|
10
|
+
useAgentItem,
|
|
11
|
+
useAgentState,
|
|
12
|
+
useMergedRef,
|
|
13
|
+
} from "@/components/ui/agent";
|
|
5
14
|
|
|
15
|
+
type CheckboxProps = React.ComponentPropsWithoutRef<typeof BaseCheckbox.Root>;
|
|
16
|
+
|
|
17
|
+
function ariaChecked(el: Element | null): boolean | "mixed" {
|
|
18
|
+
const state = el?.getAttribute("aria-checked");
|
|
19
|
+
return state === "mixed" ? "mixed" : state === "true";
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Base UI's checkbox, holding its own checked state so an agent can check it the way a
|
|
24
|
+
* parent would. Controlled use is unchanged, and `onCheckedChange` sees the agent's
|
|
25
|
+
* changes first and can cancel them. In a group the group's value decides, so the agent
|
|
26
|
+
* goes through the group's `onValueChange` instead.
|
|
27
|
+
*/
|
|
6
28
|
const Checkbox = React.forwardRef<
|
|
7
29
|
React.ComponentRef<typeof BaseCheckbox.Root>,
|
|
8
|
-
|
|
9
|
-
>(({ className, ...props }, ref) =>
|
|
30
|
+
CheckboxProps
|
|
31
|
+
>(({ className, checked: checkedProp, defaultChecked = false, onCheckedChange, ...props }, ref) => {
|
|
32
|
+
const [checked, change] = useAgentState<boolean, BaseCheckbox.Root.ChangeEventDetails>(
|
|
33
|
+
checkedProp,
|
|
34
|
+
defaultChecked,
|
|
35
|
+
onCheckedChange
|
|
36
|
+
);
|
|
37
|
+
const group = React.useContext(AgentGroupContext);
|
|
38
|
+
const element = React.useRef<HTMLElement | null>(null);
|
|
39
|
+
const mergedRef = useMergedRef(ref, element);
|
|
40
|
+
const grouped = group && (props.parent || props.value !== undefined) ? group : null;
|
|
41
|
+
useAgentItem(grouped?.items ?? null, element, props.parent ? undefined : props.value);
|
|
42
|
+
|
|
43
|
+
useAgentControl(() => {
|
|
44
|
+
const now = ariaChecked(element.current);
|
|
45
|
+
const set = (next: boolean) => {
|
|
46
|
+
if (props.readOnly) return "The checkbox is read-only.";
|
|
47
|
+
if (next === now) return undefined;
|
|
48
|
+
return grouped ? grouped.set(props.parent ? undefined : props.value, next) : changeAsAgent(change, next);
|
|
49
|
+
};
|
|
50
|
+
return {
|
|
51
|
+
kind: "checkbox",
|
|
52
|
+
element: element.current,
|
|
53
|
+
checked: now,
|
|
54
|
+
disabled: props.disabled,
|
|
55
|
+
actions: {
|
|
56
|
+
toggle: () => set(now !== true),
|
|
57
|
+
setChecked: (value: unknown) => set(agentChecked(value)),
|
|
58
|
+
},
|
|
59
|
+
};
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
return (
|
|
10
63
|
<span className="relative inline-flex [&>input]:!absolute">
|
|
11
64
|
<BaseCheckbox.Root
|
|
12
|
-
ref={
|
|
65
|
+
ref={mergedRef}
|
|
13
66
|
className={cn(
|
|
14
67
|
"peer h-4 w-4 shrink-0 rounded-sm border border-primary ring-offset-background focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 disabled:cursor-not-allowed disabled:opacity-50",
|
|
15
68
|
"data-[checked]:bg-primary data-[checked]:text-primary-foreground",
|
|
16
69
|
className
|
|
17
70
|
)}
|
|
18
71
|
{...props}
|
|
72
|
+
checked={checked}
|
|
73
|
+
onCheckedChange={change}
|
|
19
74
|
>
|
|
20
75
|
<BaseCheckbox.Indicator
|
|
21
76
|
className={cn("flex items-center justify-center text-current")}
|
|
@@ -24,7 +79,8 @@ const Checkbox = React.forwardRef<
|
|
|
24
79
|
</BaseCheckbox.Indicator>
|
|
25
80
|
</BaseCheckbox.Root>
|
|
26
81
|
</span>
|
|
27
|
-
)
|
|
82
|
+
);
|
|
83
|
+
});
|
|
28
84
|
Checkbox.displayName = "Checkbox";
|
|
29
85
|
|
|
30
86
|
export { Checkbox };
|
|
@@ -1,17 +1,91 @@
|
|
|
1
1
|
import * as React from "react";
|
|
2
2
|
import { CheckboxGroup as BaseCheckboxGroup } from "@base-ui/react/checkbox-group";
|
|
3
3
|
import { cn } from "@/lib/utils";
|
|
4
|
+
import {
|
|
5
|
+
AgentGroupContext,
|
|
6
|
+
agentChoicesOf,
|
|
7
|
+
changeAsAgent,
|
|
8
|
+
matchAgentChoice,
|
|
9
|
+
useAgentControl,
|
|
10
|
+
useAgentLatest,
|
|
11
|
+
useAgentState,
|
|
12
|
+
useHasAgent,
|
|
13
|
+
useMergedRef,
|
|
14
|
+
type AgentChoice,
|
|
15
|
+
type AgentGroup,
|
|
16
|
+
} from "@/components/ui/agent";
|
|
4
17
|
|
|
18
|
+
type Details = BaseCheckboxGroup.ChangeEventDetails;
|
|
19
|
+
|
|
20
|
+
function valuesFor(choices: AgentChoice<string>[], value: string[], wanted: unknown): string[] | string {
|
|
21
|
+
const next = value.filter((v) => !choices.some((c) => c.value === v && !c.disabled));
|
|
22
|
+
for (const one of Array.isArray(wanted) ? wanted : [wanted]) {
|
|
23
|
+
const match = matchAgentChoice(choices, one);
|
|
24
|
+
if (typeof match === "string") return match;
|
|
25
|
+
if (!next.includes(match.value)) next.push(match.value);
|
|
26
|
+
}
|
|
27
|
+
return next;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Base UI's group, holding its own value so an agent can check boxes the way a parent
|
|
32
|
+
* would. Controlled use is unchanged, and `onValueChange` sees the agent's changes first
|
|
33
|
+
* and can cancel them.
|
|
34
|
+
*/
|
|
5
35
|
const CheckboxGroup = React.forwardRef<
|
|
6
36
|
React.ComponentRef<typeof BaseCheckboxGroup>,
|
|
7
37
|
React.ComponentPropsWithoutRef<typeof BaseCheckboxGroup>
|
|
8
|
-
>(({ className, ...props }, ref) =>
|
|
9
|
-
<
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
)
|
|
38
|
+
>(({ className, value: valueProp, defaultValue, onValueChange, ...props }, ref) => {
|
|
39
|
+
const [value, change] = useAgentState<string[], Details>(valueProp, defaultValue ?? [], onValueChange);
|
|
40
|
+
const element = React.useRef<HTMLDivElement | null>(null);
|
|
41
|
+
const mergedRef = useMergedRef(ref, element);
|
|
42
|
+
const latest = useAgentLatest({ value, change, allValues: props.allValues });
|
|
43
|
+
|
|
44
|
+
const [agent] = React.useState<AgentGroup>(() => {
|
|
45
|
+
const items: AgentGroup["items"] = new Map();
|
|
46
|
+
return {
|
|
47
|
+
items,
|
|
48
|
+
set(item, checked) {
|
|
49
|
+
const { value: now, change: commit, allValues = [] } = latest.current;
|
|
50
|
+
// The parent checkbox leaves disabled children as they are, as Base UI's does.
|
|
51
|
+
const disabled = [...items].filter(([el]) => el.hasAttribute("data-disabled")).map(([, v]) => v);
|
|
52
|
+
const changed = item === undefined ? allValues.filter((v) => !disabled.includes(v)) : [item];
|
|
53
|
+
if (!changed.length) return undefined;
|
|
54
|
+
const next = checked ? [...new Set([...now, ...changed])] : now.filter((v) => !changed.includes(v));
|
|
55
|
+
return changeAsAgent(commit, next);
|
|
56
|
+
},
|
|
57
|
+
};
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
useAgentControl(() => {
|
|
61
|
+
const choices = agentChoicesOf(agent.items);
|
|
62
|
+
const setValue = (wanted: unknown) => {
|
|
63
|
+
const next = valuesFor(choices, value, wanted);
|
|
64
|
+
return typeof next === "string" ? next : changeAsAgent(change, next);
|
|
65
|
+
};
|
|
66
|
+
return {
|
|
67
|
+
kind: "checkbox-group",
|
|
68
|
+
element: choices.length ? element.current : null,
|
|
69
|
+
value: choices.filter((c) => value.includes(c.value)).map((c) => c.label),
|
|
70
|
+
items: choices.map((c) => c.label),
|
|
71
|
+
multiple: true,
|
|
72
|
+
disabled: props.disabled,
|
|
73
|
+
actions: { setValue },
|
|
74
|
+
};
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
return (
|
|
78
|
+
<AgentGroupContext.Provider value={useHasAgent() ? agent : null}>
|
|
79
|
+
<BaseCheckboxGroup
|
|
80
|
+
ref={mergedRef}
|
|
81
|
+
className={cn("flex flex-col items-start gap-1", className)}
|
|
82
|
+
{...props}
|
|
83
|
+
value={value}
|
|
84
|
+
onValueChange={change}
|
|
85
|
+
/>
|
|
86
|
+
</AgentGroupContext.Provider>
|
|
87
|
+
);
|
|
88
|
+
});
|
|
15
89
|
CheckboxGroup.displayName = "CheckboxGroup";
|
|
16
90
|
|
|
17
|
-
export { CheckboxGroup };
|
|
91
|
+
export { CheckboxGroup };
|
|
@@ -3,13 +3,22 @@
|
|
|
3
3
|
import * as React from "react"
|
|
4
4
|
import { Collapsible as BaseCollapsible } from "@base-ui/react/collapsible"
|
|
5
5
|
import { cn } from "@/lib/utils"
|
|
6
|
+
import { type AgentOpenParts, useAgentOpen, useHasAgent, useMergedRef } from "@/components/ui/agent"
|
|
6
7
|
|
|
8
|
+
const CollapsibleAgentContext = React.createContext<AgentOpenParts | null>(null)
|
|
9
|
+
|
|
10
|
+
/** Base UI's root, holding its own open state so an agent can open and close it. */
|
|
7
11
|
const Collapsible = React.forwardRef<
|
|
8
12
|
React.ComponentRef<typeof BaseCollapsible.Root>,
|
|
9
13
|
React.ComponentPropsWithoutRef<typeof BaseCollapsible.Root>
|
|
10
|
-
>(({ className, ...props }, ref) =>
|
|
11
|
-
|
|
12
|
-
|
|
14
|
+
>(({ className, open, defaultOpen, onOpenChange, ...props }, ref) => {
|
|
15
|
+
const [state, change, parts] = useAgentOpen("disclosure", open, defaultOpen, onOpenChange)
|
|
16
|
+
return (
|
|
17
|
+
<CollapsibleAgentContext.Provider value={useHasAgent() ? parts : null}>
|
|
18
|
+
<BaseCollapsible.Root ref={ref} className={cn("w-full", className)} {...props} open={state} onOpenChange={change} />
|
|
19
|
+
</CollapsibleAgentContext.Provider>
|
|
20
|
+
)
|
|
21
|
+
})
|
|
13
22
|
Collapsible.displayName = "Collapsible"
|
|
14
23
|
|
|
15
24
|
const CollapsibleTrigger = React.forwardRef<
|
|
@@ -17,7 +26,7 @@ const CollapsibleTrigger = React.forwardRef<
|
|
|
17
26
|
React.ComponentPropsWithoutRef<typeof BaseCollapsible.Trigger>
|
|
18
27
|
>(({ className, ...props }, ref) => (
|
|
19
28
|
<BaseCollapsible.Trigger
|
|
20
|
-
ref={ref}
|
|
29
|
+
ref={useMergedRef(ref, React.useContext(CollapsibleAgentContext)?.trigger)}
|
|
21
30
|
className={cn(
|
|
22
31
|
"flex w-full items-center justify-between rounded-md p-4 font-medium transition-colors",
|
|
23
32
|
"hover:bg-accent hover:text-accent-foreground",
|