@dinachi/cli 0.12.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 +117 -0
- package/dist/index.js +73 -7
- package/package.json +1 -1
- 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/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 +297 -23
- 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/menu.tsx +26 -9
- 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
package/templates/menu/menu.tsx
CHANGED
|
@@ -4,10 +4,27 @@ import * as React from "react"
|
|
|
4
4
|
import { Menu as MenuPrimitive } from "@base-ui/react/menu"
|
|
5
5
|
import { Check, ChevronRight, Circle, Search, X } from "lucide-react"
|
|
6
6
|
import { cn } from "@/lib/utils"
|
|
7
|
+
import { type AgentOpenParts, useAgentOpen, useHasAgent, useMergedRef } from "@/components/ui/agent"
|
|
7
8
|
|
|
8
|
-
const
|
|
9
|
-
|
|
10
|
-
|
|
9
|
+
const MenuAgentContext = React.createContext<AgentOpenParts | null>(null)
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Base UI's root, holding its own open state so an agent can open and close it. Closed,
|
|
13
|
+
* the agent sees the trigger; open, the popup, with its items listed as menu items.
|
|
14
|
+
*/
|
|
15
|
+
const Menu: React.FC<React.ComponentProps<typeof MenuPrimitive.Root>> = ({
|
|
16
|
+
open,
|
|
17
|
+
defaultOpen,
|
|
18
|
+
onOpenChange,
|
|
19
|
+
...props
|
|
20
|
+
}) => {
|
|
21
|
+
const [state, change, parts] = useAgentOpen("menu", open, defaultOpen, onOpenChange)
|
|
22
|
+
return (
|
|
23
|
+
<MenuAgentContext.Provider value={useHasAgent() ? parts : null}>
|
|
24
|
+
<MenuPrimitive.Root {...props} open={state} onOpenChange={change} />
|
|
25
|
+
</MenuAgentContext.Provider>
|
|
26
|
+
)
|
|
27
|
+
}
|
|
11
28
|
Menu.displayName = "Menu"
|
|
12
29
|
|
|
13
30
|
const MenuTrigger = React.forwardRef<
|
|
@@ -15,7 +32,7 @@ const MenuTrigger = React.forwardRef<
|
|
|
15
32
|
React.ComponentProps<typeof MenuPrimitive.Trigger>
|
|
16
33
|
>(({ className, ...props }, ref) => (
|
|
17
34
|
<MenuPrimitive.Trigger
|
|
18
|
-
ref={ref}
|
|
35
|
+
ref={useMergedRef(ref, React.useContext(MenuAgentContext)?.trigger)}
|
|
19
36
|
className={cn(
|
|
20
37
|
"inline-flex h-10 items-center justify-center rounded-md border border-input bg-background px-4 py-2 text-sm font-medium",
|
|
21
38
|
"hover:bg-accent hover:text-accent-foreground",
|
|
@@ -40,7 +57,7 @@ const MenuPositioner = React.forwardRef<
|
|
|
40
57
|
>(({ className, ...props }, ref) => (
|
|
41
58
|
<MenuPrimitive.Positioner
|
|
42
59
|
ref={ref}
|
|
43
|
-
className={cn("outline-none", className)}
|
|
60
|
+
className={cn("z-50 outline-none", className)}
|
|
44
61
|
sideOffset={6}
|
|
45
62
|
{...props}
|
|
46
63
|
/>
|
|
@@ -54,9 +71,9 @@ const MenuContent = React.forwardRef<
|
|
|
54
71
|
<MenuPortal>
|
|
55
72
|
<MenuPositioner>
|
|
56
73
|
<MenuPrimitive.Popup
|
|
57
|
-
ref={ref}
|
|
74
|
+
ref={useMergedRef(ref, React.useContext(MenuAgentContext)?.popup)}
|
|
58
75
|
className={cn(
|
|
59
|
-
"
|
|
76
|
+
"min-w-40 overflow-hidden rounded-md border bg-popover p-1 text-popover-foreground shadow-md",
|
|
60
77
|
"origin-[var(--transform-origin)] outline-none",
|
|
61
78
|
"transition-[transform,scale,opacity] duration-[var(--motion-duration-fast,150ms)] ease-[var(--motion-ease-out,cubic-bezier(0.23,1,0.32,1))]",
|
|
62
79
|
"data-[starting-style]:scale-95 data-[starting-style]:opacity-0",
|
|
@@ -243,11 +260,11 @@ const MenuSubContent = React.forwardRef<
|
|
|
243
260
|
React.ComponentProps<typeof MenuPrimitive.Popup>
|
|
244
261
|
>(({ className, ...props }, ref) => (
|
|
245
262
|
<MenuPrimitive.Portal>
|
|
246
|
-
<MenuPrimitive.Positioner className="outline-none" alignOffset={-4} sideOffset={8}>
|
|
263
|
+
<MenuPrimitive.Positioner className="z-50 outline-none" alignOffset={-4} sideOffset={8}>
|
|
247
264
|
<MenuPrimitive.Popup
|
|
248
265
|
ref={ref}
|
|
249
266
|
className={cn(
|
|
250
|
-
"
|
|
267
|
+
"min-w-32 overflow-hidden rounded-md border bg-popover p-1 text-popover-foreground shadow-md",
|
|
251
268
|
"origin-[var(--transform-origin)] outline-none",
|
|
252
269
|
"transition-[transform,scale,opacity] duration-[var(--motion-duration-fast,150ms)] ease-[var(--motion-ease-out,cubic-bezier(0.23,1,0.32,1))]",
|
|
253
270
|
"data-[starting-style]:scale-95 data-[starting-style]:opacity-0",
|
|
@@ -5,6 +5,7 @@ import { Menubar as BaseMenubar } from "@base-ui/react/menubar"
|
|
|
5
5
|
import { Menu } from "@base-ui/react/menu"
|
|
6
6
|
import { cn } from "@/lib/utils"
|
|
7
7
|
import { Check, ChevronRight, Circle } from "lucide-react"
|
|
8
|
+
import { useAgentOpen, useHasAgent, useMergedRef } from "@/components/ui/agent"
|
|
8
9
|
|
|
9
10
|
const Menubar = React.forwardRef<
|
|
10
11
|
React.ComponentRef<typeof BaseMenubar>,
|
|
@@ -21,9 +22,26 @@ const Menubar = React.forwardRef<
|
|
|
21
22
|
))
|
|
22
23
|
Menubar.displayName = "Menubar"
|
|
23
24
|
|
|
24
|
-
const
|
|
25
|
-
|
|
26
|
-
|
|
25
|
+
const MenubarAgentContext = React.createContext<React.RefCallback<HTMLElement> | null>(null)
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Base UI's menu, holding its own open state so an agent can open and close it. The agent
|
|
29
|
+
* sees it as its trigger, open or closed, so it stays in the menubar; its items are in the
|
|
30
|
+
* trigger's menu.
|
|
31
|
+
*/
|
|
32
|
+
const MenubarMenu: React.FC<React.ComponentProps<typeof Menu.Root>> = ({
|
|
33
|
+
open,
|
|
34
|
+
defaultOpen,
|
|
35
|
+
onOpenChange,
|
|
36
|
+
...props
|
|
37
|
+
}) => {
|
|
38
|
+
const [state, change, parts] = useAgentOpen("menu", open, defaultOpen, onOpenChange)
|
|
39
|
+
return (
|
|
40
|
+
<MenubarAgentContext.Provider value={useHasAgent() ? parts.trigger : null}>
|
|
41
|
+
<Menu.Root {...props} open={state} onOpenChange={change} />
|
|
42
|
+
</MenubarAgentContext.Provider>
|
|
43
|
+
)
|
|
44
|
+
}
|
|
27
45
|
MenubarMenu.displayName = "MenubarMenu"
|
|
28
46
|
|
|
29
47
|
const MenubarTrigger = React.forwardRef<
|
|
@@ -31,7 +49,7 @@ const MenubarTrigger = React.forwardRef<
|
|
|
31
49
|
React.ComponentProps<typeof Menu.Trigger>
|
|
32
50
|
>(({ className, ...props }, ref) => (
|
|
33
51
|
<Menu.Trigger
|
|
34
|
-
ref={ref}
|
|
52
|
+
ref={useMergedRef(ref, React.useContext(MenubarAgentContext))}
|
|
35
53
|
className={cn(
|
|
36
54
|
"flex cursor-default select-none items-center rounded-sm px-3 py-1.5 text-sm font-medium outline-none",
|
|
37
55
|
"focus:bg-accent focus:text-accent-foreground",
|
|
@@ -56,7 +74,7 @@ const MenubarPositioner = React.forwardRef<
|
|
|
56
74
|
>(({ className, ...props }, ref) => (
|
|
57
75
|
<Menu.Positioner
|
|
58
76
|
ref={ref}
|
|
59
|
-
className={cn("outline-none", className)}
|
|
77
|
+
className={cn("z-50 outline-none", className)}
|
|
60
78
|
sideOffset={4}
|
|
61
79
|
{...props}
|
|
62
80
|
/>
|
|
@@ -70,7 +88,7 @@ const MenubarContent = React.forwardRef<
|
|
|
70
88
|
<Menu.Popup
|
|
71
89
|
ref={ref}
|
|
72
90
|
className={cn(
|
|
73
|
-
"
|
|
91
|
+
"min-w-48 overflow-hidden rounded-md border bg-popover p-1 text-popover-foreground shadow-md",
|
|
74
92
|
"origin-[var(--transform-origin)]",
|
|
75
93
|
"outline-none focus:outline-none focus-visible:outline-none",
|
|
76
94
|
"transition-[transform,scale,opacity] duration-[var(--motion-duration-fast,150ms)] ease-[var(--motion-ease-out,cubic-bezier(0.23,1,0.32,1))]",
|
|
@@ -246,11 +264,11 @@ const MenubarSubContent = React.forwardRef<
|
|
|
246
264
|
React.ComponentProps<typeof Menu.Popup>
|
|
247
265
|
>(({ className, ...props }, ref) => (
|
|
248
266
|
<Menu.Portal>
|
|
249
|
-
<Menu.Positioner className="outline-none" alignOffset={-4} sideOffset={8}>
|
|
267
|
+
<Menu.Positioner className="z-50 outline-none" alignOffset={-4} sideOffset={8}>
|
|
250
268
|
<Menu.Popup
|
|
251
269
|
ref={ref}
|
|
252
270
|
className={cn(
|
|
253
|
-
"
|
|
271
|
+
"min-w-32 overflow-hidden rounded-md border bg-popover p-1 text-popover-foreground shadow-lg",
|
|
254
272
|
"origin-[var(--transform-origin)]",
|
|
255
273
|
"outline-none focus:outline-none focus-visible:outline-none",
|
|
256
274
|
"transition-[transform,scale,opacity] duration-[var(--motion-duration-fast,150ms)] ease-[var(--motion-ease-out,cubic-bezier(0.23,1,0.32,1))]",
|
|
@@ -4,22 +4,45 @@ import * as React from "react"
|
|
|
4
4
|
import { NavigationMenu as BaseNavigationMenu } from "@base-ui/react/navigation-menu"
|
|
5
5
|
import { cn } from "@/lib/utils"
|
|
6
6
|
import { ChevronDown } from "lucide-react"
|
|
7
|
+
import { changeAsAgent, useAgentControl, useAgentState, useHasAgent, useMergedRef } from "@/components/ui/agent"
|
|
8
|
+
|
|
9
|
+
type NavigationMenuDetails = BaseNavigationMenu.Root.ChangeEventDetails
|
|
10
|
+
|
|
11
|
+
interface NavigationMenuAgent {
|
|
12
|
+
value: unknown
|
|
13
|
+
change: (value: unknown, details: NavigationMenuDetails) => void
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
const NavigationMenuAgentContext = React.createContext<NavigationMenuAgent | null>(null)
|
|
17
|
+
const NavigationMenuItemAgentContext = React.createContext<React.RefObject<HTMLElement | null> | null>(null)
|
|
7
18
|
|
|
8
19
|
const NavigationMenu = React.forwardRef<
|
|
9
20
|
React.ComponentRef<typeof BaseNavigationMenu.Root>,
|
|
10
21
|
React.ComponentProps<typeof BaseNavigationMenu.Root>
|
|
11
|
-
>(({ className, children, ...props }, ref) =>
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
22
|
+
>(({ className, children, value: valueProp, defaultValue, onValueChange, ...props }, ref) => {
|
|
23
|
+
// Base UI's root, holding which item is open so an agent can open one the way a parent would.
|
|
24
|
+
const [value, change] = useAgentState<unknown, NavigationMenuDetails>(
|
|
25
|
+
valueProp,
|
|
26
|
+
defaultValue ?? null,
|
|
27
|
+
onValueChange as NavigationMenuAgent["change"] | undefined
|
|
28
|
+
)
|
|
29
|
+
return (
|
|
30
|
+
<NavigationMenuAgentContext.Provider value={useHasAgent() ? { value, change } : null}>
|
|
31
|
+
<BaseNavigationMenu.Root
|
|
32
|
+
ref={ref}
|
|
33
|
+
className={cn(
|
|
34
|
+
"relative z-10 flex max-w-max flex-1 items-center justify-center",
|
|
35
|
+
className
|
|
36
|
+
)}
|
|
37
|
+
value={value}
|
|
38
|
+
onValueChange={change}
|
|
39
|
+
{...props}
|
|
40
|
+
>
|
|
41
|
+
{children}
|
|
42
|
+
</BaseNavigationMenu.Root>
|
|
43
|
+
</NavigationMenuAgentContext.Provider>
|
|
44
|
+
)
|
|
45
|
+
})
|
|
23
46
|
NavigationMenu.displayName = "NavigationMenu"
|
|
24
47
|
|
|
25
48
|
const NavigationMenuList = React.forwardRef<
|
|
@@ -40,13 +63,30 @@ NavigationMenuList.displayName = "NavigationMenuList"
|
|
|
40
63
|
const NavigationMenuItem = React.forwardRef<
|
|
41
64
|
React.ComponentRef<typeof BaseNavigationMenu.Item>,
|
|
42
65
|
React.ComponentProps<typeof BaseNavigationMenu.Item>
|
|
43
|
-
>(({ className, ...props }, ref) =>
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
66
|
+
>(({ className, value: valueProp, ...props }, ref) => {
|
|
67
|
+
const root = React.useContext(NavigationMenuAgentContext)
|
|
68
|
+
const id = React.useId()
|
|
69
|
+
const value = valueProp ?? id
|
|
70
|
+
const trigger = React.useRef<HTMLElement | null>(null)
|
|
71
|
+
|
|
72
|
+
// An item with a trigger is one menu, open while the root's value is this item's.
|
|
73
|
+
useAgentControl(() => {
|
|
74
|
+
if (!root) return null
|
|
75
|
+
const open = root.value === value
|
|
76
|
+
return {
|
|
77
|
+
kind: "menu",
|
|
78
|
+
element: trigger.current,
|
|
79
|
+
open,
|
|
80
|
+
actions: { [open ? "close" : "open"]: () => changeAsAgent(root.change, open ? null : value) },
|
|
81
|
+
}
|
|
82
|
+
})
|
|
83
|
+
|
|
84
|
+
return (
|
|
85
|
+
<NavigationMenuItemAgentContext.Provider value={root ? trigger : null}>
|
|
86
|
+
<BaseNavigationMenu.Item ref={ref} className={className} value={value} {...props} />
|
|
87
|
+
</NavigationMenuItemAgentContext.Provider>
|
|
88
|
+
)
|
|
89
|
+
})
|
|
50
90
|
NavigationMenuItem.displayName = "NavigationMenuItem"
|
|
51
91
|
|
|
52
92
|
const NavigationMenuTrigger = React.forwardRef<
|
|
@@ -54,7 +94,7 @@ const NavigationMenuTrigger = React.forwardRef<
|
|
|
54
94
|
React.ComponentProps<typeof BaseNavigationMenu.Trigger>
|
|
55
95
|
>(({ className, children, ...props }, ref) => (
|
|
56
96
|
<BaseNavigationMenu.Trigger
|
|
57
|
-
ref={ref}
|
|
97
|
+
ref={useMergedRef(ref, React.useContext(NavigationMenuItemAgentContext))}
|
|
58
98
|
className={cn(
|
|
59
99
|
"group inline-flex h-10 w-max items-center justify-center rounded-md bg-background px-4 py-2 text-sm font-medium transition-colors",
|
|
60
100
|
"hover:bg-accent hover:text-accent-foreground",
|
|
@@ -95,9 +135,12 @@ NavigationMenuContent.displayName = "NavigationMenuContent"
|
|
|
95
135
|
const NavigationMenuLink = React.forwardRef<
|
|
96
136
|
React.ComponentRef<typeof BaseNavigationMenu.Link>,
|
|
97
137
|
React.ComponentProps<typeof BaseNavigationMenu.Link>
|
|
98
|
-
>(({ className, ...props }, ref) =>
|
|
138
|
+
>(({ className, ...props }, ref) => {
|
|
139
|
+
const element = React.useRef<HTMLAnchorElement | null>(null)
|
|
140
|
+
useAgentControl(() => ({ kind: "link", element: element.current, actions: { press: () => element.current?.click() } }))
|
|
141
|
+
return (
|
|
99
142
|
<BaseNavigationMenu.Link
|
|
100
|
-
ref={ref}
|
|
143
|
+
ref={useMergedRef(ref, element)}
|
|
101
144
|
className={cn(
|
|
102
145
|
"block select-none space-y-1 rounded-md p-3 leading-none no-underline outline-none transition-colors",
|
|
103
146
|
"hover:bg-accent hover:text-accent-foreground",
|
|
@@ -106,7 +149,8 @@ const NavigationMenuLink = React.forwardRef<
|
|
|
106
149
|
)}
|
|
107
150
|
{...props}
|
|
108
151
|
/>
|
|
109
|
-
)
|
|
152
|
+
)
|
|
153
|
+
})
|
|
110
154
|
NavigationMenuLink.displayName = "NavigationMenuLink"
|
|
111
155
|
|
|
112
156
|
const NavigationMenuPortal: React.FC<
|
|
@@ -4,8 +4,73 @@ import * as React from "react"
|
|
|
4
4
|
import { NumberField as NumberFieldPrimitive } from "@base-ui/react/number-field"
|
|
5
5
|
import { Minus, Plus } from "lucide-react"
|
|
6
6
|
import { cn } from "@/lib/utils"
|
|
7
|
+
import { agentRound, changeAsAgent, createAgentChangeDetails, useAgentControl, useAgentState, useHasAgent, useMergedRef } from "@/components/ui/agent"
|
|
7
8
|
|
|
8
|
-
|
|
9
|
+
type NumberFieldProps = React.ComponentPropsWithoutRef<typeof NumberFieldPrimitive.Root>
|
|
10
|
+
|
|
11
|
+
const NumberFieldAgentContext = React.createContext<React.MutableRefObject<HTMLInputElement | null> | null>(null)
|
|
12
|
+
|
|
13
|
+
const clamp = (n: number, min = -Infinity, max = Infinity) => Math.min(max, Math.max(min, n))
|
|
14
|
+
|
|
15
|
+
function toNumber(value: unknown): number | string {
|
|
16
|
+
const n = typeof value === "number" ? value : Number(String(value ?? "").trim() || NaN)
|
|
17
|
+
return Number.isFinite(n) ? n : `"${String(value)}" is not a number.`
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
// Base UI also takes "any", which has no size to step by, so the agent steps by 1.
|
|
21
|
+
const stepOf = ({ step }: NumberFieldProps) => (typeof step === "number" ? step : 1)
|
|
22
|
+
|
|
23
|
+
/** The agent's actions, each clamped to min and max the way Base UI clamps a person's. */
|
|
24
|
+
function numberActions(props: NumberFieldProps, value: number | null, set: (next: number) => string | undefined) {
|
|
25
|
+
const { min, max, readOnly } = props
|
|
26
|
+
const step = stepOf(props)
|
|
27
|
+
const to = (next: unknown, from = 0, scale = 1) => {
|
|
28
|
+
if (readOnly) return "The number field is read-only."
|
|
29
|
+
const n = toNumber(next)
|
|
30
|
+
return typeof n === "string" ? n : set(clamp(agentRound(from + n * scale, from, scale, n), min, max))
|
|
31
|
+
}
|
|
32
|
+
const from = value ?? min ?? 0
|
|
33
|
+
return {
|
|
34
|
+
setValue: (next: unknown) => to(next),
|
|
35
|
+
increment: (steps: unknown = 1) => to(steps, from, step),
|
|
36
|
+
decrement: (steps: unknown = 1) => to(steps, from, -step),
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Base UI's root, holding its own value so an agent can set it the way a parent would.
|
|
42
|
+
* `onValueChange` sees the agent's changes first and can cancel them.
|
|
43
|
+
*/
|
|
44
|
+
const NumberField = React.forwardRef<HTMLDivElement, NumberFieldProps>(
|
|
45
|
+
({ value: valueProp, defaultValue = null, onValueChange, ...props }, ref) => {
|
|
46
|
+
type Details = NumberFieldPrimitive.Root.ChangeEventDetails
|
|
47
|
+
const [value, changeValue] = useAgentState<number | null, Details>(valueProp, defaultValue, onValueChange)
|
|
48
|
+
const input = React.useRef<HTMLInputElement | null>(null)
|
|
49
|
+
|
|
50
|
+
useAgentControl(() => ({
|
|
51
|
+
kind: "number" as const,
|
|
52
|
+
element: input.current,
|
|
53
|
+
value: value ?? undefined,
|
|
54
|
+
min: props.min,
|
|
55
|
+
max: props.max,
|
|
56
|
+
step: stepOf(props),
|
|
57
|
+
disabled: props.disabled,
|
|
58
|
+
// An agent's change is whole at once, so it is committed too, as a person's is on blur.
|
|
59
|
+
actions: numberActions(
|
|
60
|
+
props,
|
|
61
|
+
value,
|
|
62
|
+
(next) => changeAsAgent(changeValue, next) ?? void props.onValueCommitted?.(next, createAgentChangeDetails("none"))
|
|
63
|
+
),
|
|
64
|
+
}))
|
|
65
|
+
|
|
66
|
+
return (
|
|
67
|
+
<NumberFieldAgentContext.Provider value={useHasAgent() ? input : null}>
|
|
68
|
+
<NumberFieldPrimitive.Root ref={ref} {...props} value={value} onValueChange={changeValue} />
|
|
69
|
+
</NumberFieldAgentContext.Provider>
|
|
70
|
+
)
|
|
71
|
+
}
|
|
72
|
+
)
|
|
73
|
+
NumberField.displayName = "NumberField"
|
|
9
74
|
|
|
10
75
|
const NumberFieldGroup = React.forwardRef<
|
|
11
76
|
React.ComponentRef<typeof NumberFieldPrimitive.Group>,
|
|
@@ -27,7 +92,7 @@ const NumberFieldInput = React.forwardRef<
|
|
|
27
92
|
React.ComponentPropsWithoutRef<typeof NumberFieldPrimitive.Input>
|
|
28
93
|
>(({ className, ...props }, ref) => (
|
|
29
94
|
<NumberFieldPrimitive.Input
|
|
30
|
-
ref={ref}
|
|
95
|
+
ref={useMergedRef(ref, React.useContext(NumberFieldAgentContext))}
|
|
31
96
|
className={cn(
|
|
32
97
|
"w-full bg-transparent px-3 text-sm outline-none placeholder:text-muted-foreground",
|
|
33
98
|
"disabled:cursor-not-allowed disabled:opacity-50",
|
|
@@ -44,6 +109,7 @@ const NumberFieldIncrement = React.forwardRef<
|
|
|
44
109
|
>(({ className, children, ...props }, ref) => (
|
|
45
110
|
<NumberFieldPrimitive.Increment
|
|
46
111
|
ref={ref}
|
|
112
|
+
data-agent-part={useHasAgent() ? "" : undefined}
|
|
47
113
|
className={cn(
|
|
48
114
|
"inline-flex h-full items-center justify-center border-l border-input px-3 text-muted-foreground transition-colors",
|
|
49
115
|
"hover:bg-accent hover:text-accent-foreground",
|
|
@@ -64,6 +130,7 @@ const NumberFieldDecrement = React.forwardRef<
|
|
|
64
130
|
>(({ className, children, ...props }, ref) => (
|
|
65
131
|
<NumberFieldPrimitive.Decrement
|
|
66
132
|
ref={ref}
|
|
133
|
+
data-agent-part={useHasAgent() ? "" : undefined}
|
|
67
134
|
className={cn(
|
|
68
135
|
"inline-flex h-full items-center justify-center border-r border-input px-3 text-muted-foreground transition-colors",
|
|
69
136
|
"hover:bg-accent hover:text-accent-foreground",
|
|
@@ -102,6 +169,8 @@ const NumberFieldScrubAreaCursor = React.forwardRef<
|
|
|
102
169
|
))
|
|
103
170
|
NumberFieldScrubAreaCursor.displayName = "NumberFieldScrubAreaCursor"
|
|
104
171
|
|
|
172
|
+
export type { NumberFieldProps }
|
|
173
|
+
|
|
105
174
|
export {
|
|
106
175
|
NumberField,
|
|
107
176
|
NumberFieldGroup,
|
|
@@ -3,21 +3,65 @@
|
|
|
3
3
|
import * as React from "react"
|
|
4
4
|
import { OTPField as OTPFieldPrimitive } from "@base-ui/react/otp-field"
|
|
5
5
|
import { cn } from "@/lib/utils"
|
|
6
|
+
import { changeAsAgent, useAgentControl, useAgentState, useHasAgent, useMergedRef } from "@/components/ui/agent"
|
|
6
7
|
|
|
8
|
+
const CODE_PATTERNS: Record<string, RegExp> = {
|
|
9
|
+
numeric: /^\d*$/,
|
|
10
|
+
alpha: /^[a-z]*$/i,
|
|
11
|
+
alphanumeric: /^[a-z\d]*$/i,
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
function checkCode(code: string, length: number, validationType = "numeric") {
|
|
15
|
+
if (code.length > length) return `The code is ${length} characters.`
|
|
16
|
+
if (!(CODE_PATTERNS[validationType]?.test(code) ?? true)) return `The code takes ${validationType} characters only.`
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Base UI's root, holding its own value so an agent can fill the code the way a parent
|
|
21
|
+
* would. `onValueChange` sees the agent's changes first and can cancel them.
|
|
22
|
+
*/
|
|
7
23
|
const OTPField = React.forwardRef<
|
|
8
24
|
React.ComponentRef<typeof OTPFieldPrimitive.Root>,
|
|
9
25
|
React.ComponentPropsWithoutRef<typeof OTPFieldPrimitive.Root>
|
|
10
|
-
>(({ className, ...props }, ref) =>
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
)
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
))
|
|
26
|
+
>(({ className, value: valueProp, defaultValue = "", onValueChange, ...props }, ref) => {
|
|
27
|
+
type Details = OTPFieldPrimitive.Root.ChangeEventDetails
|
|
28
|
+
const [value, changeValue] = useAgentState<string, Details>(valueProp, defaultValue, onValueChange)
|
|
29
|
+
const element = React.useRef<HTMLDivElement>(null)
|
|
30
|
+
const mergedRef = useMergedRef(ref, element)
|
|
31
|
+
|
|
32
|
+
useAgentControl(() => {
|
|
33
|
+
const setValue = (next: unknown) => {
|
|
34
|
+
if (props.readOnly) return "The code is read-only."
|
|
35
|
+
const code = String(next ?? "").replace(/\s+/g, "")
|
|
36
|
+
return checkCode(code, props.length, props.validationType) ?? changeAsAgent(changeValue, code)
|
|
37
|
+
}
|
|
38
|
+
return {
|
|
39
|
+
kind: "input" as const,
|
|
40
|
+
element: element.current,
|
|
41
|
+
value,
|
|
42
|
+
disabled: props.disabled,
|
|
43
|
+
length: props.length,
|
|
44
|
+
// A one-time code is a credential, like the autocomplete token Base UI gives it,
|
|
45
|
+
// and a masked one is hidden from the person too.
|
|
46
|
+
secret: Boolean(props.mask) || (props.autoComplete ?? "one-time-code") === "one-time-code",
|
|
47
|
+
actions: { setValue, clear: () => setValue("") },
|
|
48
|
+
}
|
|
49
|
+
})
|
|
50
|
+
|
|
51
|
+
return (
|
|
52
|
+
<OTPFieldPrimitive.Root
|
|
53
|
+
ref={mergedRef}
|
|
54
|
+
className={cn(
|
|
55
|
+
"flex items-center gap-2",
|
|
56
|
+
"data-[disabled]:opacity-50",
|
|
57
|
+
className
|
|
58
|
+
)}
|
|
59
|
+
{...props}
|
|
60
|
+
value={value}
|
|
61
|
+
onValueChange={changeValue}
|
|
62
|
+
/>
|
|
63
|
+
)
|
|
64
|
+
})
|
|
21
65
|
OTPField.displayName = "OTPField"
|
|
22
66
|
|
|
23
67
|
const OTPFieldGroup = React.forwardRef<
|
|
@@ -38,6 +82,8 @@ const OTPFieldInput = React.forwardRef<
|
|
|
38
82
|
>(({ className, ...props }, ref) => (
|
|
39
83
|
<OTPFieldPrimitive.Input
|
|
40
84
|
ref={ref}
|
|
85
|
+
// The root reports the whole code, so a slot is part of it rather than a control.
|
|
86
|
+
data-agent-part={useHasAgent() ? "" : undefined}
|
|
41
87
|
className={cn(
|
|
42
88
|
"relative h-10 w-10 border-y border-r border-input bg-background text-center text-sm shadow-sm outline-none",
|
|
43
89
|
"transition-[color,background-color,border-color,box-shadow] duration-[var(--motion-duration-fast,150ms)]",
|
|
@@ -3,9 +3,24 @@
|
|
|
3
3
|
import * as React from "react"
|
|
4
4
|
import { Popover as PopoverPrimitive } from "@base-ui/react/popover"
|
|
5
5
|
import { cn } from "@/lib/utils"
|
|
6
|
+
import { type AgentOpenParts, useAgentOpen, useHasAgent, useMergedRef } from "@/components/ui/agent"
|
|
6
7
|
|
|
7
|
-
const
|
|
8
|
-
|
|
8
|
+
const PopoverAgentContext = React.createContext<AgentOpenParts | null>(null)
|
|
9
|
+
|
|
10
|
+
/** Base UI's root, holding its own open state so an agent can open and close it. */
|
|
11
|
+
function Popover<Payload>({ open, defaultOpen, onOpenChange, ...props }: PopoverPrimitive.Root.Props<Payload>) {
|
|
12
|
+
const [state, change, parts] = useAgentOpen("popover", open, defaultOpen, onOpenChange)
|
|
13
|
+
return (
|
|
14
|
+
<PopoverAgentContext.Provider value={useHasAgent() ? parts : null}>
|
|
15
|
+
<PopoverPrimitive.Root<Payload> {...props} open={state} onOpenChange={change} />
|
|
16
|
+
</PopoverAgentContext.Provider>
|
|
17
|
+
)
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function PopoverTrigger<Payload>({ ref, ...props }: PopoverPrimitive.Trigger.Props<Payload> & React.RefAttributes<HTMLElement>) {
|
|
21
|
+
const parts = React.useContext(PopoverAgentContext)
|
|
22
|
+
return <PopoverPrimitive.Trigger<Payload> ref={useMergedRef(ref, parts?.trigger)} {...props} />
|
|
23
|
+
}
|
|
9
24
|
const PopoverPortal = PopoverPrimitive.Portal
|
|
10
25
|
const PopoverClose = PopoverPrimitive.Close
|
|
11
26
|
const createPopoverHandle = PopoverPrimitive.createHandle
|
|
@@ -28,7 +43,7 @@ const PopoverPopup = React.forwardRef<
|
|
|
28
43
|
React.ComponentProps<typeof PopoverPrimitive.Popup>
|
|
29
44
|
>(({ className, ...props }, ref) => (
|
|
30
45
|
<PopoverPrimitive.Popup
|
|
31
|
-
ref={ref}
|
|
46
|
+
ref={useMergedRef(ref, React.useContext(PopoverAgentContext)?.popup)}
|
|
32
47
|
className={cn(
|
|
33
48
|
"rounded-lg border bg-popover px-6 py-4 text-popover-foreground shadow-lg outline-none",
|
|
34
49
|
"origin-[var(--transform-origin)]",
|
|
@@ -3,15 +3,29 @@
|
|
|
3
3
|
import * as React from "react"
|
|
4
4
|
import { PreviewCard as BasePreviewCard } from "@base-ui/react/preview-card"
|
|
5
5
|
import { cn } from "@/lib/utils"
|
|
6
|
+
import { type AgentOpenParts, useAgentOpen, useHasAgent, useMergedRef } from "@/components/ui/agent"
|
|
6
7
|
|
|
7
|
-
const
|
|
8
|
+
const PreviewCardAgentContext = React.createContext<AgentOpenParts | null>(null)
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Base UI's root, holding its own open state so an agent can open and close it. The agent
|
|
12
|
+
* sees the trigger either way: the card holds content to read, not a control.
|
|
13
|
+
*/
|
|
14
|
+
function PreviewCard<Payload>({ open, defaultOpen, onOpenChange, ...props }: BasePreviewCard.Root.Props<Payload>) {
|
|
15
|
+
const [state, change, parts] = useAgentOpen("popover", open, defaultOpen, onOpenChange)
|
|
16
|
+
return (
|
|
17
|
+
<PreviewCardAgentContext.Provider value={useHasAgent() ? parts : null}>
|
|
18
|
+
<BasePreviewCard.Root<Payload> {...props} open={state} onOpenChange={change} />
|
|
19
|
+
</PreviewCardAgentContext.Provider>
|
|
20
|
+
)
|
|
21
|
+
}
|
|
8
22
|
|
|
9
23
|
const PreviewCardTrigger = React.forwardRef<
|
|
10
24
|
HTMLAnchorElement,
|
|
11
25
|
React.ComponentProps<typeof BasePreviewCard.Trigger>
|
|
12
26
|
>(({ className, ...props }, ref) => (
|
|
13
27
|
<BasePreviewCard.Trigger
|
|
14
|
-
ref={ref}
|
|
28
|
+
ref={useMergedRef(ref, React.useContext(PreviewCardAgentContext)?.trigger)}
|
|
15
29
|
className={cn(
|
|
16
30
|
"text-blue-600 no-underline decoration-blue-600/60 decoration-1 underline-offset-2 outline-none",
|
|
17
31
|
"hover:underline focus-visible:rounded-sm focus-visible:no-underline focus-visible:outline focus-visible:outline-2 focus-visible:outline-blue-600",
|
|
@@ -4,13 +4,71 @@ import * as React from "react"
|
|
|
4
4
|
import { Radio as RadioPrimitive } from "@base-ui/react/radio"
|
|
5
5
|
import { RadioGroup as RadioGroupPrimitive } from "@base-ui/react/radio-group"
|
|
6
6
|
import { cn } from "@/lib/utils"
|
|
7
|
+
import {
|
|
8
|
+
agentChoicesOf,
|
|
9
|
+
changeAsAgent,
|
|
10
|
+
matchAgentChoice,
|
|
11
|
+
sameAgentValue,
|
|
12
|
+
useAgentControl,
|
|
13
|
+
useAgentItem,
|
|
14
|
+
useAgentState,
|
|
15
|
+
useHasAgent,
|
|
16
|
+
useMergedRef,
|
|
17
|
+
type AgentItems,
|
|
18
|
+
} from "@/components/ui/agent"
|
|
7
19
|
|
|
20
|
+
type RadioItems = AgentItems
|
|
21
|
+
|
|
22
|
+
const RadioAgentContext = React.createContext<RadioItems | null>(null)
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Base UI's group, holding its own value so an agent can choose a radio the way a parent
|
|
26
|
+
* would. Controlled use is unchanged, and `onValueChange` sees the agent's choice first
|
|
27
|
+
* and can cancel it.
|
|
28
|
+
*/
|
|
8
29
|
const RadioGroup = React.forwardRef<
|
|
9
30
|
React.ComponentRef<typeof RadioGroupPrimitive>,
|
|
10
31
|
React.ComponentPropsWithoutRef<typeof RadioGroupPrimitive>
|
|
11
|
-
>(({ className, ...props }, ref) =>
|
|
12
|
-
|
|
13
|
-
|
|
32
|
+
>(({ className, value: valueProp, defaultValue, onValueChange, ...props }, ref) => {
|
|
33
|
+
const [value, change] = useAgentState<unknown, RadioGroupPrimitive.ChangeEventDetails>(
|
|
34
|
+
valueProp,
|
|
35
|
+
defaultValue ?? null,
|
|
36
|
+
onValueChange
|
|
37
|
+
)
|
|
38
|
+
const element = React.useRef<HTMLDivElement | null>(null)
|
|
39
|
+
const mergedRef = useMergedRef(ref, element)
|
|
40
|
+
const [items] = React.useState<RadioItems>(() => new Map())
|
|
41
|
+
|
|
42
|
+
const choose = (wanted: unknown) => {
|
|
43
|
+
if (props.readOnly) return "The radio group is read-only."
|
|
44
|
+
const match = matchAgentChoice(agentChoicesOf(items), wanted)
|
|
45
|
+
return typeof match === "string" ? match : changeAsAgent(change, match.value)
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
useAgentControl(() => {
|
|
49
|
+
const choices = agentChoicesOf(items)
|
|
50
|
+
return {
|
|
51
|
+
kind: "radio-group",
|
|
52
|
+
element: choices.length ? element.current : null,
|
|
53
|
+
value: choices.find((c) => sameAgentValue(c.value, value))?.label ?? "",
|
|
54
|
+
items: choices.map((c) => c.label),
|
|
55
|
+
disabled: props.disabled,
|
|
56
|
+
actions: { select: choose },
|
|
57
|
+
}
|
|
58
|
+
})
|
|
59
|
+
|
|
60
|
+
return (
|
|
61
|
+
<RadioAgentContext.Provider value={useHasAgent() ? items : null}>
|
|
62
|
+
<RadioGroupPrimitive
|
|
63
|
+
ref={mergedRef}
|
|
64
|
+
className={cn("grid gap-2", className)}
|
|
65
|
+
{...props}
|
|
66
|
+
value={value}
|
|
67
|
+
onValueChange={change}
|
|
68
|
+
/>
|
|
69
|
+
</RadioAgentContext.Provider>
|
|
70
|
+
)
|
|
71
|
+
})
|
|
14
72
|
RadioGroup.displayName = "RadioGroup"
|
|
15
73
|
|
|
16
74
|
const RadioIndicator = React.forwardRef<
|
|
@@ -32,10 +90,16 @@ RadioIndicator.displayName = "RadioIndicator"
|
|
|
32
90
|
const Radio = React.forwardRef<
|
|
33
91
|
React.ComponentRef<typeof RadioPrimitive.Root>,
|
|
34
92
|
React.ComponentPropsWithoutRef<typeof RadioPrimitive.Root>
|
|
35
|
-
>(({ className, ...props }, ref) =>
|
|
93
|
+
>(({ className, ...props }, ref) => {
|
|
94
|
+
const items = React.useContext(RadioAgentContext)
|
|
95
|
+
const element = React.useRef<React.ComponentRef<typeof RadioPrimitive.Root> | null>(null)
|
|
96
|
+
const mergedRef = useMergedRef(ref, element)
|
|
97
|
+
useAgentItem(items, element, props.value)
|
|
98
|
+
|
|
99
|
+
return (
|
|
36
100
|
<span className="relative inline-flex [&>input]:!absolute">
|
|
37
101
|
<RadioPrimitive.Root
|
|
38
|
-
ref={
|
|
102
|
+
ref={mergedRef}
|
|
39
103
|
className={cn(
|
|
40
104
|
"flex items-center justify-center aspect-square h-4 w-4 rounded-full border border-primary text-primary ring-offset-background",
|
|
41
105
|
"focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2",
|
|
@@ -48,7 +112,8 @@ const Radio = React.forwardRef<
|
|
|
48
112
|
<RadioIndicator />
|
|
49
113
|
</RadioPrimitive.Root>
|
|
50
114
|
</span>
|
|
51
|
-
)
|
|
115
|
+
)
|
|
116
|
+
})
|
|
52
117
|
Radio.displayName = "Radio"
|
|
53
118
|
|
|
54
119
|
export { Radio, RadioGroup, RadioIndicator }
|