@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
|
@@ -3,9 +3,29 @@
|
|
|
3
3
|
import * as React from "react"
|
|
4
4
|
import { Dialog as BaseDialog } from "@base-ui/react/dialog"
|
|
5
5
|
import { cn } from "@/lib/utils"
|
|
6
|
+
import { type AgentOpenParts, useAgentOpen, useHasAgent, useMergedRef } from "@/components/ui/agent"
|
|
7
|
+
|
|
8
|
+
const DialogAgentContext = 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 way a
|
|
12
|
+
* parent would. Controlled use is unchanged, and `onOpenChange` sees the agent's changes
|
|
13
|
+
* first and can cancel them. Closed, the agent sees the trigger; open, the popup.
|
|
14
|
+
*/
|
|
15
|
+
function Dialog<Payload>({ open, defaultOpen, onOpenChange, ...props }: BaseDialog.Root.Props<Payload>) {
|
|
16
|
+
const [state, change, parts] = useAgentOpen("dialog", open, defaultOpen, onOpenChange)
|
|
17
|
+
return (
|
|
18
|
+
<DialogAgentContext.Provider value={useHasAgent() ? parts : null}>
|
|
19
|
+
<BaseDialog.Root<Payload> {...props} open={state} onOpenChange={change} />
|
|
20
|
+
</DialogAgentContext.Provider>
|
|
21
|
+
)
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function DialogTrigger<Payload>({ ref, ...props }: BaseDialog.Trigger.Props<Payload> & React.RefAttributes<HTMLElement>) {
|
|
25
|
+
const dialog = React.useContext(DialogAgentContext)
|
|
26
|
+
return <BaseDialog.Trigger<Payload> ref={useMergedRef(ref, dialog?.trigger)} {...props} />
|
|
27
|
+
}
|
|
6
28
|
|
|
7
|
-
const Dialog = BaseDialog.Root
|
|
8
|
-
const DialogTrigger = BaseDialog.Trigger
|
|
9
29
|
const DialogPortal = BaseDialog.Portal
|
|
10
30
|
|
|
11
31
|
const DialogBackdrop = React.forwardRef<
|
|
@@ -44,9 +64,12 @@ DialogViewport.displayName = "DialogViewport"
|
|
|
44
64
|
const DialogPopup = React.forwardRef<
|
|
45
65
|
HTMLDivElement,
|
|
46
66
|
React.ComponentProps<typeof BaseDialog.Popup>
|
|
47
|
-
>(({ className, ...props }, ref) =>
|
|
67
|
+
>(({ className, ...props }, ref) => {
|
|
68
|
+
const dialog = React.useContext(DialogAgentContext)
|
|
69
|
+
const mergedRef = useMergedRef(ref, dialog?.popup)
|
|
70
|
+
return (
|
|
48
71
|
<BaseDialog.Popup
|
|
49
|
-
ref={
|
|
72
|
+
ref={mergedRef}
|
|
50
73
|
className={cn(
|
|
51
74
|
"w-[95%] max-w-lg gap-4 rounded-lg border bg-background p-6 shadow-lg",
|
|
52
75
|
// A dialog is not anchored to its trigger, so scaling from the centre is correct
|
|
@@ -62,7 +85,8 @@ const DialogPopup = React.forwardRef<
|
|
|
62
85
|
)}
|
|
63
86
|
{...props}
|
|
64
87
|
/>
|
|
65
|
-
)
|
|
88
|
+
)
|
|
89
|
+
})
|
|
66
90
|
DialogPopup.displayName = "DialogPopup"
|
|
67
91
|
|
|
68
92
|
const DialogTitle = React.forwardRef<
|
|
@@ -92,15 +116,20 @@ DialogDescription.displayName = "DialogDescription"
|
|
|
92
116
|
const DialogClose = React.forwardRef<
|
|
93
117
|
HTMLButtonElement,
|
|
94
118
|
React.ComponentProps<typeof BaseDialog.Close>
|
|
95
|
-
>(({ className, ...props }, ref) => (
|
|
119
|
+
>(({ className, render, ...props }, ref) => (
|
|
96
120
|
<BaseDialog.Close
|
|
97
121
|
ref={ref}
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
122
|
+
render={render}
|
|
123
|
+
className={
|
|
124
|
+
render
|
|
125
|
+
? className
|
|
126
|
+
: cn(
|
|
127
|
+
"inline-flex h-10 items-center justify-center rounded-md border border-input bg-background px-4 py-2 text-sm font-medium shadow-sm transition-colors",
|
|
128
|
+
"hover:bg-accent hover:text-accent-foreground focus:outline-none focus:ring-2 focus:ring-ring focus:ring-offset-2",
|
|
129
|
+
"disabled:cursor-not-allowed disabled:opacity-50",
|
|
130
|
+
className,
|
|
131
|
+
)
|
|
132
|
+
}
|
|
104
133
|
{...props}
|
|
105
134
|
/>
|
|
106
135
|
))
|
|
@@ -3,9 +3,24 @@
|
|
|
3
3
|
import * as React from "react"
|
|
4
4
|
import { Drawer as DrawerPrimitive } from "@base-ui/react/drawer"
|
|
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 DrawerAgentContext = 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 Drawer<Payload>({ open, defaultOpen, onOpenChange, ...props }: DrawerPrimitive.Root.Props<Payload>) {
|
|
12
|
+
const [state, change, parts] = useAgentOpen("dialog", open, defaultOpen, onOpenChange)
|
|
13
|
+
return (
|
|
14
|
+
<DrawerAgentContext.Provider value={useHasAgent() ? parts : null}>
|
|
15
|
+
<DrawerPrimitive.Root<Payload> {...props} open={state} onOpenChange={change} />
|
|
16
|
+
</DrawerAgentContext.Provider>
|
|
17
|
+
)
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function DrawerTrigger<Payload>({ ref, ...props }: DrawerPrimitive.Trigger.Props<Payload> & React.RefAttributes<HTMLElement>) {
|
|
21
|
+
const parts = React.useContext(DrawerAgentContext)
|
|
22
|
+
return <DrawerPrimitive.Trigger<Payload> ref={useMergedRef(ref, parts?.trigger)} {...props} />
|
|
23
|
+
}
|
|
9
24
|
const DrawerPortal = DrawerPrimitive.Portal
|
|
10
25
|
const DrawerClose = DrawerPrimitive.Close
|
|
11
26
|
const DrawerSwipeArea = DrawerPrimitive.SwipeArea
|
|
@@ -46,7 +61,7 @@ const DrawerContent = React.forwardRef<
|
|
|
46
61
|
<DrawerBackdrop />
|
|
47
62
|
<DrawerPrimitive.Viewport>
|
|
48
63
|
<DrawerPrimitive.Popup
|
|
49
|
-
ref={ref}
|
|
64
|
+
ref={useMergedRef(ref, React.useContext(DrawerAgentContext)?.popup)}
|
|
50
65
|
className={cn(
|
|
51
66
|
"fixed z-50 flex flex-col gap-4 border bg-background p-6 shadow-lg",
|
|
52
67
|
// A surface that travels in from an edge gets the drawer curve, which decays
|
|
@@ -6,10 +6,12 @@ import {
|
|
|
6
6
|
AnimatePresence,
|
|
7
7
|
motion,
|
|
8
8
|
MotionConfigContext,
|
|
9
|
+
useIsPresent,
|
|
9
10
|
useReducedMotion,
|
|
10
11
|
type Transition,
|
|
11
12
|
} from "motion/react"
|
|
12
13
|
import { cn } from "@/lib/utils"
|
|
14
|
+
import { type AgentOpenParts, useAgentOpen, useHasAgent, useMergedRef } from "@/components/ui/agent"
|
|
13
15
|
|
|
14
16
|
/** `useReducedMotion` reads the OS setting only, and only at mount. Also honour an
|
|
15
17
|
* app-level `<MotionConfig reducedMotion>`, so a user preference in the host app
|
|
@@ -25,9 +27,32 @@ function useReducedMotionConfig() {
|
|
|
25
27
|
const FOCUSABLE =
|
|
26
28
|
'a[href], button:not([disabled]), textarea, input, select, [tabindex]:not([tabindex="-1"])'
|
|
27
29
|
|
|
30
|
+
export interface ExpandableCardChangeDetails {
|
|
31
|
+
reason: "trigger-press" | "close-press" | "outside-press" | "escape-key" | "imperative-action"
|
|
32
|
+
event: Event | undefined
|
|
33
|
+
isCanceled: boolean
|
|
34
|
+
cancel: () => void
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
type PersonReason = Exclude<ExpandableCardChangeDetails["reason"], "imperative-action">
|
|
38
|
+
|
|
39
|
+
function personChange(reason: PersonReason, event?: Event): ExpandableCardChangeDetails {
|
|
40
|
+
const details: ExpandableCardChangeDetails = {
|
|
41
|
+
reason,
|
|
42
|
+
event,
|
|
43
|
+
isCanceled: false,
|
|
44
|
+
cancel: () => {
|
|
45
|
+
details.isCanceled = true
|
|
46
|
+
},
|
|
47
|
+
}
|
|
48
|
+
return details
|
|
49
|
+
}
|
|
50
|
+
|
|
28
51
|
interface ExpandableCardContextValue {
|
|
29
52
|
open: boolean
|
|
30
|
-
setOpen: (open: boolean) => void
|
|
53
|
+
setOpen: (open: boolean, reason: PersonReason, event?: Event) => void
|
|
54
|
+
/** Set inside an `AgentProvider`: the card and its panel are one control to the agent. */
|
|
55
|
+
agent: AgentOpenParts | null
|
|
31
56
|
/** Pairs an element in the card with its counterpart in the panel. */
|
|
32
57
|
layoutId: (part: string) => string
|
|
33
58
|
transition: Transition
|
|
@@ -48,7 +73,8 @@ function useExpandableCard(component: string) {
|
|
|
48
73
|
export interface ExpandableCardProps extends React.ComponentProps<"div"> {
|
|
49
74
|
open?: boolean
|
|
50
75
|
defaultOpen?: boolean
|
|
51
|
-
|
|
76
|
+
/** Called before the card opens or closes. `details.cancel()` keeps it as it is. */
|
|
77
|
+
onOpenChange?: (open: boolean, details: ExpandableCardChangeDetails) => void
|
|
52
78
|
}
|
|
53
79
|
|
|
54
80
|
/**
|
|
@@ -74,15 +100,12 @@ export interface ExpandableCardProps extends React.ComponentProps<"div"> {
|
|
|
74
100
|
const ExpandableCard = React.forwardRef<HTMLDivElement, ExpandableCardProps>(
|
|
75
101
|
({ open: openProp, defaultOpen = false, onOpenChange, className, ...props }, ref) => {
|
|
76
102
|
const reducedMotion = useReducedMotionConfig()
|
|
77
|
-
const [
|
|
78
|
-
const
|
|
103
|
+
const [open, change, parts] = useAgentOpen("disclosure", openProp, defaultOpen, onOpenChange)
|
|
104
|
+
const agent = useHasAgent() ? parts : null
|
|
79
105
|
|
|
80
106
|
const setOpen = React.useCallback(
|
|
81
|
-
(next: boolean) =>
|
|
82
|
-
|
|
83
|
-
onOpenChange?.(next)
|
|
84
|
-
},
|
|
85
|
-
[openProp, onOpenChange]
|
|
107
|
+
(next: boolean, reason: PersonReason, event?: Event) => change(next, personChange(reason, event)),
|
|
108
|
+
[change]
|
|
86
109
|
)
|
|
87
110
|
|
|
88
111
|
// Scopes the shared layout ids to this card, so a grid of them does not have every
|
|
@@ -105,8 +128,8 @@ const ExpandableCard = React.forwardRef<HTMLDivElement, ExpandableCardProps>(
|
|
|
105
128
|
)
|
|
106
129
|
|
|
107
130
|
const context = React.useMemo(
|
|
108
|
-
() => ({ open, setOpen, layoutId, transition, reducedMotion }),
|
|
109
|
-
[open, setOpen, layoutId, transition, reducedMotion]
|
|
131
|
+
() => ({ open, setOpen, agent, layoutId, transition, reducedMotion }),
|
|
132
|
+
[open, setOpen, agent, layoutId, transition, reducedMotion]
|
|
110
133
|
)
|
|
111
134
|
|
|
112
135
|
return (
|
|
@@ -125,12 +148,12 @@ const ExpandableCardTrigger = React.forwardRef<
|
|
|
125
148
|
HTMLButtonElement,
|
|
126
149
|
ExpandableCardTriggerProps
|
|
127
150
|
>(({ className, onClick, ...props }, ref) => {
|
|
128
|
-
const { open, setOpen, layoutId, transition } =
|
|
151
|
+
const { open, setOpen, agent, layoutId, transition } =
|
|
129
152
|
useExpandableCard("ExpandableCardTrigger")
|
|
130
153
|
|
|
131
154
|
return (
|
|
132
155
|
<motion.button
|
|
133
|
-
ref={ref}
|
|
156
|
+
ref={useMergedRef(ref, agent?.trigger)}
|
|
134
157
|
type="button"
|
|
135
158
|
layoutId={layoutId("card")}
|
|
136
159
|
transition={transition}
|
|
@@ -142,7 +165,7 @@ const ExpandableCardTrigger = React.forwardRef<
|
|
|
142
165
|
onClick={(event) => {
|
|
143
166
|
onClick?.(event)
|
|
144
167
|
if (event.defaultPrevented) return
|
|
145
|
-
setOpen(true)
|
|
168
|
+
setOpen(true, "trigger-press", event.nativeEvent)
|
|
146
169
|
}}
|
|
147
170
|
className={cn(
|
|
148
171
|
"block w-full overflow-hidden rounded-2xl border border-border bg-background text-left focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring",
|
|
@@ -202,7 +225,7 @@ function PanelClose({
|
|
|
202
225
|
}: {
|
|
203
226
|
fullscreen: boolean
|
|
204
227
|
label: string
|
|
205
|
-
onClick: () => void
|
|
228
|
+
onClick: (event: React.MouseEvent) => void
|
|
206
229
|
}) {
|
|
207
230
|
return (
|
|
208
231
|
<button
|
|
@@ -230,6 +253,15 @@ function panelSurface(fullscreen: boolean): string {
|
|
|
230
253
|
}
|
|
231
254
|
|
|
232
255
|
/** The opened card. Mounted only while open, so the shared layout has somewhere to land. */
|
|
256
|
+
/** Marks the panel while it plays its exit, so it stops counting as an open modal. */
|
|
257
|
+
function ExitMark({ panel }: { panel: React.RefObject<HTMLDivElement | null> }) {
|
|
258
|
+
const present = useIsPresent()
|
|
259
|
+
React.useLayoutEffect(() => {
|
|
260
|
+
panel.current?.toggleAttribute("data-ending-style", !present)
|
|
261
|
+
}, [present, panel])
|
|
262
|
+
return null
|
|
263
|
+
}
|
|
264
|
+
|
|
233
265
|
const ExpandableCardPanel = React.forwardRef<HTMLDivElement, ExpandableCardPanelProps>(
|
|
234
266
|
(
|
|
235
267
|
{
|
|
@@ -245,13 +277,14 @@ const ExpandableCardPanel = React.forwardRef<HTMLDivElement, ExpandableCardPanel
|
|
|
245
277
|
},
|
|
246
278
|
ref
|
|
247
279
|
) => {
|
|
248
|
-
const { open, setOpen, layoutId, transition } =
|
|
280
|
+
const { open, setOpen, agent, layoutId, transition } =
|
|
249
281
|
useExpandableCard("ExpandableCardPanel")
|
|
250
282
|
|
|
251
283
|
const panel = React.useRef<HTMLDivElement>(null)
|
|
252
|
-
|
|
284
|
+
// The card goes inert while open, so the agent finds the control at the panel then.
|
|
285
|
+
const panelRef = useMergedRef(ref, panel, agent?.popup)
|
|
253
286
|
|
|
254
|
-
useModalBehaviour(open, panel,
|
|
287
|
+
useModalBehaviour(open, panel, setOpen)
|
|
255
288
|
|
|
256
289
|
const target = usePortalTarget(container)
|
|
257
290
|
if (!target) return null
|
|
@@ -266,7 +299,7 @@ const ExpandableCardPanel = React.forwardRef<HTMLDivElement, ExpandableCardPanel
|
|
|
266
299
|
animate={{ opacity: 1 }}
|
|
267
300
|
exit={{ opacity: 0 }}
|
|
268
301
|
transition={{ duration: 0.2 }}
|
|
269
|
-
onClick={
|
|
302
|
+
onClick={(event) => setOpen(false, "outside-press", event.nativeEvent)}
|
|
270
303
|
className={cn(
|
|
271
304
|
"fixed inset-0 z-40 bg-background/80 backdrop-blur-sm",
|
|
272
305
|
backdropClassName
|
|
@@ -282,7 +315,7 @@ const ExpandableCardPanel = React.forwardRef<HTMLDivElement, ExpandableCardPanel
|
|
|
282
315
|
)}
|
|
283
316
|
>
|
|
284
317
|
<motion.div
|
|
285
|
-
ref={
|
|
318
|
+
ref={panelRef}
|
|
286
319
|
layoutId={layoutId("card")}
|
|
287
320
|
transition={transition}
|
|
288
321
|
role="dialog"
|
|
@@ -295,12 +328,13 @@ const ExpandableCardPanel = React.forwardRef<HTMLDivElement, ExpandableCardPanel
|
|
|
295
328
|
)}
|
|
296
329
|
{...props}
|
|
297
330
|
>
|
|
331
|
+
<ExitMark panel={panel} />
|
|
298
332
|
<PanelBody fullscreen={fullscreen}>{children}</PanelBody>
|
|
299
333
|
{showClose ? (
|
|
300
334
|
<PanelClose
|
|
301
335
|
fullscreen={fullscreen}
|
|
302
336
|
label={closeLabel}
|
|
303
|
-
onClick={close}
|
|
337
|
+
onClick={(event) => setOpen(false, "close-press", event.nativeEvent)}
|
|
304
338
|
/>
|
|
305
339
|
) : null}
|
|
306
340
|
</motion.div>
|
|
@@ -324,19 +358,18 @@ ExpandableCardPanel.displayName = "ExpandableCardPanel"
|
|
|
324
358
|
* fold. A consumer cannot be asked to audit their layout for that, so the panel does not
|
|
325
359
|
* rely on the layout at all.
|
|
326
360
|
*
|
|
327
|
-
*
|
|
328
|
-
*
|
|
361
|
+
* Null on the server and while hydrating, because `document` does not exist on the server
|
|
362
|
+
* and a portal cannot be part of the first paint.
|
|
329
363
|
*/
|
|
330
364
|
function usePortalTarget(container?: Element | null) {
|
|
331
|
-
const
|
|
332
|
-
|
|
333
|
-
React.useEffect(() => {
|
|
334
|
-
setTarget(container ?? document.body)
|
|
335
|
-
}, [container])
|
|
336
|
-
|
|
337
|
-
return target
|
|
365
|
+
const client = React.useSyncExternalStore(subscribeNever, isClient, isServer)
|
|
366
|
+
return client ? (container ?? document.body) : null
|
|
338
367
|
}
|
|
339
368
|
|
|
369
|
+
const subscribeNever = () => () => {}
|
|
370
|
+
const isClient = () => true
|
|
371
|
+
const isServer = () => false
|
|
372
|
+
|
|
340
373
|
export interface ExpandableCardSharedProps
|
|
341
374
|
extends React.ComponentProps<typeof motion.div> {
|
|
342
375
|
/** Names the pair. Use the same `part` on the card and in the panel, once each. */
|
|
@@ -403,7 +436,7 @@ ExpandableCardBody.displayName = "ExpandableCardBody"
|
|
|
403
436
|
function useModalBehaviour(
|
|
404
437
|
active: boolean,
|
|
405
438
|
ref: React.RefObject<HTMLElement | null>,
|
|
406
|
-
onClose: () => void
|
|
439
|
+
onClose: (open: false, reason: "escape-key", event: Event) => void
|
|
407
440
|
) {
|
|
408
441
|
const onCloseRef = React.useRef(onClose)
|
|
409
442
|
React.useEffect(() => {
|
|
@@ -438,7 +471,7 @@ function useModalBehaviour(
|
|
|
438
471
|
const onKeyDown = (event: KeyboardEvent) => {
|
|
439
472
|
if (event.key === "Escape") {
|
|
440
473
|
event.preventDefault()
|
|
441
|
-
onCloseRef.current()
|
|
474
|
+
onCloseRef.current(false, "escape-key", event)
|
|
442
475
|
return
|
|
443
476
|
}
|
|
444
477
|
if (event.key !== "Tab") return
|
|
@@ -490,16 +523,6 @@ function CloseIcon() {
|
|
|
490
523
|
)
|
|
491
524
|
}
|
|
492
525
|
|
|
493
|
-
/** The panel needs its own node for the focus trap and the caller may want it too. */
|
|
494
|
-
function mergeRefs<T>(...refs: (React.Ref<T> | undefined)[]): React.RefCallback<T> {
|
|
495
|
-
return (value) => {
|
|
496
|
-
for (const ref of refs) {
|
|
497
|
-
if (typeof ref === "function") ref(value)
|
|
498
|
-
else if (ref) (ref as React.RefObject<T | null>).current = value
|
|
499
|
-
}
|
|
500
|
-
}
|
|
501
|
-
}
|
|
502
|
-
|
|
503
526
|
export {
|
|
504
527
|
ExpandableCard,
|
|
505
528
|
ExpandableCardTrigger,
|
|
@@ -66,8 +66,10 @@ const FieldError = React.forwardRef<
|
|
|
66
66
|
HTMLDivElement,
|
|
67
67
|
React.ComponentProps<typeof BaseField.Error>
|
|
68
68
|
>(({ className, ...props }, ref) => (
|
|
69
|
+
// Tells the agent bridge this describes what is wrong, not what the field is for.
|
|
69
70
|
<BaseField.Error
|
|
70
71
|
ref={ref}
|
|
72
|
+
data-agent-error=""
|
|
71
73
|
className={cn(
|
|
72
74
|
"text-sm font-medium text-destructive",
|
|
73
75
|
"data-[disabled]:opacity-50",
|
|
@@ -13,6 +13,7 @@ import {
|
|
|
13
13
|
type MotionValue,
|
|
14
14
|
} from "motion/react"
|
|
15
15
|
import { cn } from "@/lib/utils"
|
|
16
|
+
import { changeAsAgent, useAgentControl } from "@/components/ui/agent"
|
|
16
17
|
|
|
17
18
|
const EASE_OUT: [number, number, number, number] = [0.23, 1, 0.32, 1]
|
|
18
19
|
|
|
@@ -34,6 +35,29 @@ function useReducedMotionConfig() {
|
|
|
34
35
|
|
|
35
36
|
export type HoldToConfirmVariant = "fill" | "ring" | "border"
|
|
36
37
|
|
|
38
|
+
/** Passed to `onConfirm`. `cancel()` unwinds the hold instead of confirming. */
|
|
39
|
+
export interface HoldToConfirmChangeDetails {
|
|
40
|
+
reason: "hold" | "imperative-action"
|
|
41
|
+
event: Event | undefined
|
|
42
|
+
isCanceled: boolean
|
|
43
|
+
cancel: () => void
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function holdComplete(): HoldToConfirmChangeDetails {
|
|
47
|
+
const details: HoldToConfirmChangeDetails = {
|
|
48
|
+
reason: "hold",
|
|
49
|
+
event: undefined,
|
|
50
|
+
isCanceled: false,
|
|
51
|
+
cancel: () => {
|
|
52
|
+
details.isCanceled = true
|
|
53
|
+
},
|
|
54
|
+
}
|
|
55
|
+
return details
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** Approval an agent's press needs by default, since a hold exists to make it deliberate. */
|
|
59
|
+
const HOLD_CONFIRM = "Hold to confirm: a person approves this."
|
|
60
|
+
|
|
37
61
|
/**
|
|
38
62
|
* The button's *padding* box, in px, with the matching inner corner radius. SVG cannot
|
|
39
63
|
* inherit a border radius, so the outline variants are drawn to measured geometry.
|
|
@@ -295,10 +319,15 @@ function useHold({
|
|
|
295
319
|
resetAfter,
|
|
296
320
|
disabled,
|
|
297
321
|
onConfirm,
|
|
322
|
+
buttonRef,
|
|
298
323
|
}: Pick<
|
|
299
324
|
HoldToConfirmProps,
|
|
300
325
|
"duration" | "resetAfter" | "disabled" | "onConfirm"
|
|
301
|
-
> & {
|
|
326
|
+
> & {
|
|
327
|
+
duration: number
|
|
328
|
+
resetAfter: number
|
|
329
|
+
buttonRef: React.RefObject<HTMLButtonElement | null>
|
|
330
|
+
}) {
|
|
302
331
|
const progress = useMotionValue(0)
|
|
303
332
|
const animation = React.useRef<ReturnType<typeof animate> | null>(null)
|
|
304
333
|
const reset = React.useRef<ReturnType<typeof setTimeout> | null>(null)
|
|
@@ -338,14 +367,24 @@ function useHold({
|
|
|
338
367
|
resetAfterRef.current = resetAfter
|
|
339
368
|
})
|
|
340
369
|
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
370
|
+
// An agent's press lands here too, with no hold run before it.
|
|
371
|
+
const complete = React.useCallback(
|
|
372
|
+
(details: HoldToConfirmChangeDetails) => {
|
|
373
|
+
holding.current = false
|
|
374
|
+
setIsHolding(false)
|
|
375
|
+
animation.current?.stop()
|
|
376
|
+
onConfirmRef.current?.(details)
|
|
377
|
+
if (details.isCanceled) {
|
|
378
|
+
animation.current = animate(progress, 0, UNWIND)
|
|
379
|
+
return
|
|
380
|
+
}
|
|
381
|
+
progress.set(1)
|
|
382
|
+
setConfirmed(true)
|
|
383
|
+
if (resetAfterRef.current > 0)
|
|
384
|
+
reset.current = setTimeout(settle, resetAfterRef.current)
|
|
385
|
+
},
|
|
386
|
+
[settle, progress]
|
|
387
|
+
)
|
|
349
388
|
|
|
350
389
|
const start = React.useCallback(() => {
|
|
351
390
|
if (disabled || confirmed || holding.current) return
|
|
@@ -357,7 +396,7 @@ function useHold({
|
|
|
357
396
|
animation.current = animate(progress, 1, {
|
|
358
397
|
duration: remaining / 1000,
|
|
359
398
|
ease: "linear",
|
|
360
|
-
onComplete: complete,
|
|
399
|
+
onComplete: () => complete(holdComplete()),
|
|
361
400
|
})
|
|
362
401
|
}, [disabled, confirmed, duration, progress, complete])
|
|
363
402
|
|
|
@@ -369,6 +408,8 @@ function useHold({
|
|
|
369
408
|
animation.current = animate(progress, 0, UNWIND)
|
|
370
409
|
}, [progress])
|
|
371
410
|
|
|
411
|
+
useHoldAgent(buttonRef, disabled, confirmed, complete)
|
|
412
|
+
|
|
372
413
|
return { progress, confirmed, isHolding, start, cancel }
|
|
373
414
|
}
|
|
374
415
|
|
|
@@ -473,8 +514,8 @@ export interface HoldToConfirmProps
|
|
|
473
514
|
variant?: HoldToConfirmVariant
|
|
474
515
|
/** Milliseconds the user must hold to confirm. */
|
|
475
516
|
duration?: number
|
|
476
|
-
/** Fired once the hold completes. */
|
|
477
|
-
onConfirm?: () => void
|
|
517
|
+
/** Fired once the hold completes, or an agent presses it. */
|
|
518
|
+
onConfirm?: (details: HoldToConfirmChangeDetails) => void
|
|
478
519
|
/** Milliseconds the confirmed state is held before resetting. Set `0` to stay confirmed. */
|
|
479
520
|
resetAfter?: number
|
|
480
521
|
/** Replaces the label while confirmed. */
|
|
@@ -488,6 +529,30 @@ export interface HoldToConfirmProps
|
|
|
488
529
|
render?: useRender.RenderProp
|
|
489
530
|
}
|
|
490
531
|
|
|
532
|
+
/** Registers the button with an agent, whose press completes the hold outright. */
|
|
533
|
+
function useHoldAgent(
|
|
534
|
+
buttonRef: React.RefObject<HTMLButtonElement | null>,
|
|
535
|
+
disabled: boolean | undefined,
|
|
536
|
+
confirmed: boolean,
|
|
537
|
+
complete: (details: HoldToConfirmChangeDetails) => void
|
|
538
|
+
) {
|
|
539
|
+
useAgentControl(() => {
|
|
540
|
+
const el = buttonRef.current
|
|
541
|
+
return {
|
|
542
|
+
kind: "button",
|
|
543
|
+
element: el,
|
|
544
|
+
disabled,
|
|
545
|
+
confirm: HOLD_CONFIRM,
|
|
546
|
+
actions: {
|
|
547
|
+
press: () =>
|
|
548
|
+
confirmed
|
|
549
|
+
? "Already confirmed."
|
|
550
|
+
: changeAsAgent((_: undefined, d: HoldToConfirmChangeDetails) => complete(d), undefined),
|
|
551
|
+
},
|
|
552
|
+
}
|
|
553
|
+
})
|
|
554
|
+
}
|
|
555
|
+
|
|
491
556
|
/**
|
|
492
557
|
* A destructive action gated behind a deliberate hold, drawn three ways:
|
|
493
558
|
*
|
|
@@ -534,12 +599,13 @@ const HoldToConfirm = React.forwardRef<HTMLButtonElement, HoldToConfirmProps>(
|
|
|
534
599
|
resetAfter,
|
|
535
600
|
disabled,
|
|
536
601
|
onConfirm,
|
|
602
|
+
buttonRef,
|
|
537
603
|
})
|
|
538
604
|
|
|
539
605
|
const text = confirmed && confirmedLabel ? confirmedLabel : children
|
|
540
606
|
const hintId = React.useId()
|
|
541
607
|
|
|
542
|
-
|
|
608
|
+
const button = useRender({
|
|
543
609
|
defaultTagName: "button",
|
|
544
610
|
render,
|
|
545
611
|
ref: [ref, buttonRef],
|
|
@@ -549,9 +615,6 @@ const HoldToConfirm = React.forwardRef<HTMLButtonElement, HoldToConfirmProps>(
|
|
|
549
615
|
// it is stated outright. `aria-hidden` keeps the description out of the name
|
|
550
616
|
// while leaving it reachable through the reference.
|
|
551
617
|
"aria-describedby": cn(props["aria-describedby"], hintId),
|
|
552
|
-
// Confirming swaps the label, and a name change on an already-focused element is
|
|
553
|
-
// not reliably announced. As a live region the swap is spoken.
|
|
554
|
-
"aria-live": "polite" as const,
|
|
555
618
|
"data-holding": isHolding || undefined,
|
|
556
619
|
"data-confirmed": confirmed || undefined,
|
|
557
620
|
className: shellClass(variant, reducedMotion, className),
|
|
@@ -573,8 +636,28 @@ const HoldToConfirm = React.forwardRef<HTMLButtonElement, HoldToConfirmProps>(
|
|
|
573
636
|
),
|
|
574
637
|
},
|
|
575
638
|
})
|
|
639
|
+
|
|
640
|
+
return (
|
|
641
|
+
<>
|
|
642
|
+
{button}
|
|
643
|
+
<Announcement confirmed={confirmed}>{confirmedLabel}</Announcement>
|
|
644
|
+
</>
|
|
645
|
+
)
|
|
576
646
|
}
|
|
577
647
|
)
|
|
578
648
|
HoldToConfirm.displayName = "HoldToConfirm"
|
|
579
649
|
|
|
650
|
+
/**
|
|
651
|
+
* Confirming swaps the label, and a name change on an already-focused element is not
|
|
652
|
+
* reliably announced, so the new label is spoken from beside the button. Inside it, the
|
|
653
|
+
* live region would keep the button reachable behind a modal dialog.
|
|
654
|
+
*/
|
|
655
|
+
function Announcement({ confirmed, children }: { confirmed: boolean; children: React.ReactNode }) {
|
|
656
|
+
return (
|
|
657
|
+
<span aria-live="polite" className="sr-only">
|
|
658
|
+
{confirmed && children}
|
|
659
|
+
</span>
|
|
660
|
+
)
|
|
661
|
+
}
|
|
662
|
+
|
|
580
663
|
export { HoldToConfirm }
|
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
export { HoldToConfirm } from './hold-to-confirm'
|
|
2
|
-
export type { HoldToConfirmProps, HoldToConfirmVariant } from './hold-to-confirm'
|
|
2
|
+
export type { HoldToConfirmProps, HoldToConfirmVariant, HoldToConfirmChangeDetails } from './hold-to-confirm'
|
|
@@ -1,11 +1,68 @@
|
|
|
1
1
|
import * as React from "react";
|
|
2
2
|
import { Input as BaseInput } from "@base-ui/react/input";
|
|
3
3
|
import { cn } from "@/lib/utils";
|
|
4
|
+
import { createAgentChangeDetails, useAgentControl, useMergedRef } from "@/components/ui/agent";
|
|
5
|
+
|
|
6
|
+
function writeValue(input: HTMLInputElement, value: string) {
|
|
7
|
+
// The prototype setter skips React's value tracker, so React does not treat the write
|
|
8
|
+
// as an edit of its own and the parent's handler is the only one that hears of it.
|
|
9
|
+
Object.getOwnPropertyDescriptor(HTMLInputElement.prototype, "value")?.set?.call(input, value);
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
function setValueAsUser(input: HTMLInputElement, value: string) {
|
|
13
|
+
writeValue(input, value);
|
|
14
|
+
input.dispatchEvent(new Event("input", { bubbles: true }));
|
|
15
|
+
}
|
|
4
16
|
|
|
5
17
|
const Input = React.forwardRef<
|
|
6
18
|
HTMLInputElement,
|
|
7
19
|
React.ComponentProps<typeof BaseInput>
|
|
8
20
|
>(({ className, ...props }, ref) => {
|
|
21
|
+
const element = React.useRef<HTMLInputElement>(null);
|
|
22
|
+
const mergedRef = useMergedRef(ref, element);
|
|
23
|
+
|
|
24
|
+
// Set while the agent's input event runs, as onValueChange has already heard of it.
|
|
25
|
+
const agentWrite = React.useRef(false);
|
|
26
|
+
const { onValueChange } = props;
|
|
27
|
+
const handleValueChange = onValueChange
|
|
28
|
+
? (value: string, details: BaseInput.ChangeEventDetails) => {
|
|
29
|
+
if (!agentWrite.current) onValueChange(value, details);
|
|
30
|
+
}
|
|
31
|
+
: undefined;
|
|
32
|
+
|
|
33
|
+
const setValue = (value: string) => {
|
|
34
|
+
const input = element.current;
|
|
35
|
+
if (!input) return "The input is not mounted.";
|
|
36
|
+
if (input.readOnly) return "The input is read-only.";
|
|
37
|
+
// The app may refuse first, so a refused change reaches nothing else. Then a real
|
|
38
|
+
// input event, as typing sends, so onChange, form libraries and the Field hear of it.
|
|
39
|
+
if (onValueChange) {
|
|
40
|
+
const details = createAgentChangeDetails<BaseInput.ChangeEventDetails>("none");
|
|
41
|
+
onValueChange(value, details);
|
|
42
|
+
if (details.isCanceled) return "The app refused the change.";
|
|
43
|
+
}
|
|
44
|
+
agentWrite.current = true;
|
|
45
|
+
try {
|
|
46
|
+
setValueAsUser(input, value);
|
|
47
|
+
} finally {
|
|
48
|
+
agentWrite.current = false;
|
|
49
|
+
}
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
useAgentControl(() => ({
|
|
53
|
+
kind: "input",
|
|
54
|
+
element: element.current,
|
|
55
|
+
value: element.current?.value,
|
|
56
|
+
// A file input takes a file from a person's picker. Nothing an agent sends can be one.
|
|
57
|
+
actions:
|
|
58
|
+
props.type === "file"
|
|
59
|
+
? ({} as Record<string, never>)
|
|
60
|
+
: {
|
|
61
|
+
setValue: (value: string) => setValue(String(value ?? "")),
|
|
62
|
+
clear: () => setValue(""),
|
|
63
|
+
},
|
|
64
|
+
}));
|
|
65
|
+
|
|
9
66
|
return (
|
|
10
67
|
<BaseInput
|
|
11
68
|
className={cn(
|
|
@@ -14,8 +71,9 @@ const Input = React.forwardRef<
|
|
|
14
71
|
"aria-invalid:ring-destructive/20 dark:aria-invalid:ring-destructive/40 aria-invalid:border-destructive",
|
|
15
72
|
className
|
|
16
73
|
)}
|
|
17
|
-
ref={
|
|
74
|
+
ref={mergedRef}
|
|
18
75
|
{...props}
|
|
76
|
+
onValueChange={handleValueChange}
|
|
19
77
|
/>
|
|
20
78
|
);
|
|
21
79
|
});
|