@dinachi/cli 0.12.0 → 1.1.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 +131 -0
- package/dist/index.js +73 -7
- package/package.json +1 -1
- package/templates/accordion/accordion.tsx +70 -12
- package/templates/agent/agent.tsx +1865 -0
- package/templates/agent/index.ts +52 -0
- package/templates/alert-dialog/alert-dialog.tsx +59 -19
- package/templates/alert-dialog/index.ts +2 -0
- package/templates/animated-icon/animated-icon.tsx +1 -0
- package/templates/animated-list/animated-list.tsx +2 -1
- package/templates/animated-tabs/animated-tabs.tsx +69 -5
- package/templates/autocomplete/autocomplete.tsx +188 -21
- package/templates/avatar/avatar.tsx +3 -0
- package/templates/badge/badge.tsx +1 -0
- package/templates/button/button.tsx +1 -0
- package/templates/card/card.tsx +6 -1
- package/templates/carousel/carousel.tsx +109 -24
- package/templates/checkbox/checkbox.tsx +61 -4
- package/templates/checkbox-group/checkbox-group.tsx +83 -8
- package/templates/collapsible/collapsible.tsx +15 -4
- package/templates/combobox/combobox.tsx +312 -26
- package/templates/compare-slider/compare-slider.tsx +104 -28
- package/templates/compare-slider/index.ts +1 -1
- package/templates/context-menu/context-menu.tsx +93 -24
- package/templates/dialog/dialog.tsx +53 -14
- package/templates/drawer/drawer.tsx +36 -8
- package/templates/expandable-card/expandable-card.tsx +70 -43
- package/templates/field/field.tsx +7 -1
- package/templates/fieldset/fieldset.tsx +2 -0
- package/templates/form/form.tsx +1 -0
- package/templates/hold-to-confirm/hold-to-confirm.tsx +100 -16
- package/templates/hold-to-confirm/index.ts +1 -1
- package/templates/input/input.tsx +60 -1
- package/templates/label/label.tsx +1 -0
- package/templates/link/link.tsx +1 -0
- package/templates/load-transition/load-transition.tsx +1 -0
- package/templates/marquee/marquee.tsx +1 -0
- package/templates/menu/menu.tsx +45 -12
- package/templates/menubar/menubar.tsx +41 -11
- package/templates/meter/meter.tsx +5 -1
- package/templates/navigation-menu/navigation-menu.tsx +79 -24
- package/templates/number-field/index.ts +1 -0
- package/templates/number-field/number-field.tsx +77 -2
- package/templates/number-ticker/number-ticker.tsx +3 -0
- package/templates/otp-field/otp-field.tsx +61 -11
- package/templates/popover/popover.tsx +36 -7
- package/templates/preview-card/preview-card.tsx +27 -5
- package/templates/progress/progress.tsx +5 -1
- package/templates/progress-ring/progress-ring.tsx +1 -0
- package/templates/radio/radio.tsx +74 -6
- package/templates/scroll-area/scroll-area.tsx +14 -4
- package/templates/scroll-progress/scroll-progress.tsx +1 -0
- package/templates/scroll-reveal/scroll-reveal.tsx +1 -0
- package/templates/select/select.tsx +241 -12
- package/templates/separator/separator.tsx +1 -0
- package/templates/skeleton/skeleton.tsx +1 -0
- package/templates/slider/slider.tsx +84 -8
- package/templates/sortable/sortable.tsx +79 -9
- package/templates/stagger-list/stagger-list.tsx +2 -0
- package/templates/streaming-text/streaming-text.tsx +1 -0
- package/templates/swipeable-row/index.ts +1 -1
- package/templates/swipeable-row/swipeable-row.tsx +126 -41
- package/templates/switch/switch.tsx +43 -4
- package/templates/tabs/tabs.tsx +79 -10
- package/templates/text/text.tsx +1 -0
- package/templates/text-morph/text-morph.tsx +2 -0
- package/templates/text-shimmer/text-shimmer.tsx +1 -0
- package/templates/textarea/index.ts +1 -1
- package/templates/textarea/textarea.tsx +71 -4
- package/templates/toast/toast.tsx +13 -2
- package/templates/toggle/toggle.tsx +40 -7
- package/templates/toggle-group/toggle-group.tsx +98 -18
- package/templates/toolbar/toolbar.tsx +6 -0
- package/templates/tooltip/tooltip.tsx +9 -3
|
@@ -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,13 +128,13 @@ 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 (
|
|
113
136
|
<ExpandableCardContext.Provider value={context}>
|
|
114
|
-
<div ref={ref} className={cn("relative", className)} {...props} />
|
|
137
|
+
<div ref={ref} className={cn("relative", className)} data-slot="expandable-card" {...props} />
|
|
115
138
|
</ExpandableCardContext.Provider>
|
|
116
139
|
)
|
|
117
140
|
}
|
|
@@ -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,12 +165,13 @@ 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",
|
|
149
172
|
className
|
|
150
173
|
)}
|
|
174
|
+
data-slot="expandable-card-trigger"
|
|
151
175
|
{...props}
|
|
152
176
|
/>
|
|
153
177
|
)
|
|
@@ -202,7 +226,7 @@ function PanelClose({
|
|
|
202
226
|
}: {
|
|
203
227
|
fullscreen: boolean
|
|
204
228
|
label: string
|
|
205
|
-
onClick: () => void
|
|
229
|
+
onClick: (event: React.MouseEvent) => void
|
|
206
230
|
}) {
|
|
207
231
|
return (
|
|
208
232
|
<button
|
|
@@ -230,6 +254,15 @@ function panelSurface(fullscreen: boolean): string {
|
|
|
230
254
|
}
|
|
231
255
|
|
|
232
256
|
/** The opened card. Mounted only while open, so the shared layout has somewhere to land. */
|
|
257
|
+
/** Marks the panel while it plays its exit, so it stops counting as an open modal. */
|
|
258
|
+
function ExitMark({ panel }: { panel: React.RefObject<HTMLDivElement | null> }) {
|
|
259
|
+
const present = useIsPresent()
|
|
260
|
+
React.useLayoutEffect(() => {
|
|
261
|
+
panel.current?.toggleAttribute("data-ending-style", !present)
|
|
262
|
+
}, [present, panel])
|
|
263
|
+
return null
|
|
264
|
+
}
|
|
265
|
+
|
|
233
266
|
const ExpandableCardPanel = React.forwardRef<HTMLDivElement, ExpandableCardPanelProps>(
|
|
234
267
|
(
|
|
235
268
|
{
|
|
@@ -245,13 +278,14 @@ const ExpandableCardPanel = React.forwardRef<HTMLDivElement, ExpandableCardPanel
|
|
|
245
278
|
},
|
|
246
279
|
ref
|
|
247
280
|
) => {
|
|
248
|
-
const { open, setOpen, layoutId, transition } =
|
|
281
|
+
const { open, setOpen, agent, layoutId, transition } =
|
|
249
282
|
useExpandableCard("ExpandableCardPanel")
|
|
250
283
|
|
|
251
284
|
const panel = React.useRef<HTMLDivElement>(null)
|
|
252
|
-
|
|
285
|
+
// The card goes inert while open, so the agent finds the control at the panel then.
|
|
286
|
+
const panelRef = useMergedRef(ref, panel, agent?.popup)
|
|
253
287
|
|
|
254
|
-
useModalBehaviour(open, panel,
|
|
288
|
+
useModalBehaviour(open, panel, setOpen)
|
|
255
289
|
|
|
256
290
|
const target = usePortalTarget(container)
|
|
257
291
|
if (!target) return null
|
|
@@ -266,7 +300,7 @@ const ExpandableCardPanel = React.forwardRef<HTMLDivElement, ExpandableCardPanel
|
|
|
266
300
|
animate={{ opacity: 1 }}
|
|
267
301
|
exit={{ opacity: 0 }}
|
|
268
302
|
transition={{ duration: 0.2 }}
|
|
269
|
-
onClick={
|
|
303
|
+
onClick={(event) => setOpen(false, "outside-press", event.nativeEvent)}
|
|
270
304
|
className={cn(
|
|
271
305
|
"fixed inset-0 z-40 bg-background/80 backdrop-blur-sm",
|
|
272
306
|
backdropClassName
|
|
@@ -282,7 +316,7 @@ const ExpandableCardPanel = React.forwardRef<HTMLDivElement, ExpandableCardPanel
|
|
|
282
316
|
)}
|
|
283
317
|
>
|
|
284
318
|
<motion.div
|
|
285
|
-
ref={
|
|
319
|
+
ref={panelRef}
|
|
286
320
|
layoutId={layoutId("card")}
|
|
287
321
|
transition={transition}
|
|
288
322
|
role="dialog"
|
|
@@ -293,14 +327,16 @@ const ExpandableCardPanel = React.forwardRef<HTMLDivElement, ExpandableCardPanel
|
|
|
293
327
|
panelSurface(fullscreen),
|
|
294
328
|
className
|
|
295
329
|
)}
|
|
330
|
+
data-slot="expandable-card-panel"
|
|
296
331
|
{...props}
|
|
297
332
|
>
|
|
333
|
+
<ExitMark panel={panel} />
|
|
298
334
|
<PanelBody fullscreen={fullscreen}>{children}</PanelBody>
|
|
299
335
|
{showClose ? (
|
|
300
336
|
<PanelClose
|
|
301
337
|
fullscreen={fullscreen}
|
|
302
338
|
label={closeLabel}
|
|
303
|
-
onClick={close}
|
|
339
|
+
onClick={(event) => setOpen(false, "close-press", event.nativeEvent)}
|
|
304
340
|
/>
|
|
305
341
|
) : null}
|
|
306
342
|
</motion.div>
|
|
@@ -324,19 +360,18 @@ ExpandableCardPanel.displayName = "ExpandableCardPanel"
|
|
|
324
360
|
* fold. A consumer cannot be asked to audit their layout for that, so the panel does not
|
|
325
361
|
* rely on the layout at all.
|
|
326
362
|
*
|
|
327
|
-
*
|
|
328
|
-
*
|
|
363
|
+
* Null on the server and while hydrating, because `document` does not exist on the server
|
|
364
|
+
* and a portal cannot be part of the first paint.
|
|
329
365
|
*/
|
|
330
366
|
function usePortalTarget(container?: Element | null) {
|
|
331
|
-
const
|
|
332
|
-
|
|
333
|
-
React.useEffect(() => {
|
|
334
|
-
setTarget(container ?? document.body)
|
|
335
|
-
}, [container])
|
|
336
|
-
|
|
337
|
-
return target
|
|
367
|
+
const client = React.useSyncExternalStore(subscribeNever, isClient, isServer)
|
|
368
|
+
return client ? (container ?? document.body) : null
|
|
338
369
|
}
|
|
339
370
|
|
|
371
|
+
const subscribeNever = () => () => {}
|
|
372
|
+
const isClient = () => true
|
|
373
|
+
const isServer = () => false
|
|
374
|
+
|
|
340
375
|
export interface ExpandableCardSharedProps
|
|
341
376
|
extends React.ComponentProps<typeof motion.div> {
|
|
342
377
|
/** Names the pair. Use the same `part` on the card and in the panel, once each. */
|
|
@@ -358,6 +393,7 @@ const ExpandableCardShared = React.forwardRef<HTMLDivElement, ExpandableCardShar
|
|
|
358
393
|
ref={ref}
|
|
359
394
|
layoutId={layoutId(part)}
|
|
360
395
|
transition={transition}
|
|
396
|
+
data-slot="expandable-card-shared"
|
|
361
397
|
{...props}
|
|
362
398
|
/>
|
|
363
399
|
)
|
|
@@ -385,6 +421,7 @@ const ExpandableCardBody = React.forwardRef<HTMLDivElement, ExpandableCardBodyPr
|
|
|
385
421
|
animate={{ opacity: 1, y: 0 }}
|
|
386
422
|
transition={{ delay: reducedMotion ? 0 : 0.16, duration: 0.24 }}
|
|
387
423
|
className={cn("text-sm leading-relaxed text-muted-foreground", className)}
|
|
424
|
+
data-slot="expandable-card-body"
|
|
388
425
|
{...props}
|
|
389
426
|
/>
|
|
390
427
|
)
|
|
@@ -403,7 +440,7 @@ ExpandableCardBody.displayName = "ExpandableCardBody"
|
|
|
403
440
|
function useModalBehaviour(
|
|
404
441
|
active: boolean,
|
|
405
442
|
ref: React.RefObject<HTMLElement | null>,
|
|
406
|
-
onClose: () => void
|
|
443
|
+
onClose: (open: false, reason: "escape-key", event: Event) => void
|
|
407
444
|
) {
|
|
408
445
|
const onCloseRef = React.useRef(onClose)
|
|
409
446
|
React.useEffect(() => {
|
|
@@ -438,7 +475,7 @@ function useModalBehaviour(
|
|
|
438
475
|
const onKeyDown = (event: KeyboardEvent) => {
|
|
439
476
|
if (event.key === "Escape") {
|
|
440
477
|
event.preventDefault()
|
|
441
|
-
onCloseRef.current()
|
|
478
|
+
onCloseRef.current(false, "escape-key", event)
|
|
442
479
|
return
|
|
443
480
|
}
|
|
444
481
|
if (event.key !== "Tab") return
|
|
@@ -490,16 +527,6 @@ function CloseIcon() {
|
|
|
490
527
|
)
|
|
491
528
|
}
|
|
492
529
|
|
|
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
530
|
export {
|
|
504
531
|
ExpandableCard,
|
|
505
532
|
ExpandableCardTrigger,
|
|
@@ -6,7 +6,7 @@ const Field = React.forwardRef<
|
|
|
6
6
|
HTMLDivElement,
|
|
7
7
|
React.ComponentProps<typeof BaseField.Root>
|
|
8
8
|
>(({ className, ...props }, ref) => (
|
|
9
|
-
<BaseField.Root ref={ref} className={cn("space-y-2", className)} {...props} />
|
|
9
|
+
<BaseField.Root ref={ref} data-slot="field" className={cn("space-y-2", className)} {...props} />
|
|
10
10
|
));
|
|
11
11
|
Field.displayName = "Field";
|
|
12
12
|
|
|
@@ -16,6 +16,7 @@ const FieldLabel = React.forwardRef<
|
|
|
16
16
|
>(({ className, ...props }, ref) => (
|
|
17
17
|
<BaseField.Label
|
|
18
18
|
ref={ref}
|
|
19
|
+
data-slot="field-label"
|
|
19
20
|
className={cn(
|
|
20
21
|
"text-sm font-medium leading-none data-[disabled]:cursor-not-allowed data-[disabled]:opacity-70",
|
|
21
22
|
"data-[invalid]:text-destructive",
|
|
@@ -32,6 +33,7 @@ const FieldControl = React.forwardRef<
|
|
|
32
33
|
>(({ className, ...props }, ref) => (
|
|
33
34
|
<BaseField.Control
|
|
34
35
|
ref={ref}
|
|
36
|
+
data-slot="field-control"
|
|
35
37
|
className={cn(
|
|
36
38
|
"flex h-10 w-full rounded-md border border-input bg-background px-3 py-2 text-sm ring-offset-background",
|
|
37
39
|
"file:border-0 file:bg-transparent file:text-sm file:font-medium",
|
|
@@ -52,6 +54,7 @@ const FieldDescription = React.forwardRef<
|
|
|
52
54
|
>(({ className, ...props }, ref) => (
|
|
53
55
|
<BaseField.Description
|
|
54
56
|
ref={ref}
|
|
57
|
+
data-slot="field-description"
|
|
55
58
|
className={cn(
|
|
56
59
|
"text-sm text-muted-foreground",
|
|
57
60
|
"data-[disabled]:opacity-50",
|
|
@@ -66,8 +69,11 @@ const FieldError = React.forwardRef<
|
|
|
66
69
|
HTMLDivElement,
|
|
67
70
|
React.ComponentProps<typeof BaseField.Error>
|
|
68
71
|
>(({ className, ...props }, ref) => (
|
|
72
|
+
// Tells the agent bridge this describes what is wrong, not what the field is for.
|
|
69
73
|
<BaseField.Error
|
|
70
74
|
ref={ref}
|
|
75
|
+
data-slot="field-error"
|
|
76
|
+
data-agent-error=""
|
|
71
77
|
className={cn(
|
|
72
78
|
"text-sm font-medium text-destructive",
|
|
73
79
|
"data-[disabled]:opacity-50",
|
|
@@ -10,6 +10,7 @@ const Fieldset = React.forwardRef<
|
|
|
10
10
|
>(({ className, ...props }, ref) => (
|
|
11
11
|
<FieldsetPrimitive.Root
|
|
12
12
|
ref={ref}
|
|
13
|
+
data-slot="fieldset"
|
|
13
14
|
className={cn("space-y-3 rounded-lg border p-4", className)}
|
|
14
15
|
{...props}
|
|
15
16
|
/>
|
|
@@ -22,6 +23,7 @@ const FieldsetLegend = React.forwardRef<
|
|
|
22
23
|
>(({ className, ...props }, ref) => (
|
|
23
24
|
<FieldsetPrimitive.Legend
|
|
24
25
|
ref={ref}
|
|
26
|
+
data-slot="fieldset-legend"
|
|
25
27
|
className={cn("px-1 text-sm font-medium", className)}
|
|
26
28
|
{...props}
|
|
27
29
|
/>
|
package/templates/form/form.tsx
CHANGED
|
@@ -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,12 +615,10 @@ 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),
|
|
621
|
+
"data-slot": "hold-to-confirm",
|
|
558
622
|
...props,
|
|
559
623
|
...holdHandlers(props, start, cancel),
|
|
560
624
|
children: (
|
|
@@ -573,8 +637,28 @@ const HoldToConfirm = React.forwardRef<HTMLButtonElement, HoldToConfirmProps>(
|
|
|
573
637
|
),
|
|
574
638
|
},
|
|
575
639
|
})
|
|
640
|
+
|
|
641
|
+
return (
|
|
642
|
+
<>
|
|
643
|
+
{button}
|
|
644
|
+
<Announcement confirmed={confirmed}>{confirmedLabel}</Announcement>
|
|
645
|
+
</>
|
|
646
|
+
)
|
|
576
647
|
}
|
|
577
648
|
)
|
|
578
649
|
HoldToConfirm.displayName = "HoldToConfirm"
|
|
579
650
|
|
|
651
|
+
/**
|
|
652
|
+
* Confirming swaps the label, and a name change on an already-focused element is not
|
|
653
|
+
* reliably announced, so the new label is spoken from beside the button. Inside it, the
|
|
654
|
+
* live region would keep the button reachable behind a modal dialog.
|
|
655
|
+
*/
|
|
656
|
+
function Announcement({ confirmed, children }: { confirmed: boolean; children: React.ReactNode }) {
|
|
657
|
+
return (
|
|
658
|
+
<span aria-live="polite" className="sr-only">
|
|
659
|
+
{confirmed && children}
|
|
660
|
+
</span>
|
|
661
|
+
)
|
|
662
|
+
}
|
|
663
|
+
|
|
580
664
|
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,21 +1,80 @@
|
|
|
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
|
|
68
|
+
data-slot="input"
|
|
11
69
|
className={cn(
|
|
12
70
|
"file:text-foreground placeholder:text-muted-foreground selection:bg-primary selection:text-primary-foreground dark:bg-input/30 border-input flex h-9 w-full min-w-0 rounded-md border bg-transparent px-3 py-1 text-base shadow-xs transition-[color,box-shadow] outline-none file:inline-flex file:h-7 file:border-0 file:bg-transparent file:text-sm file:font-medium disabled:pointer-events-none disabled:cursor-not-allowed disabled:opacity-50 md:text-sm",
|
|
13
71
|
"focus-visible:border-ring focus-visible:ring-ring/50 focus-visible:ring-[3px]",
|
|
14
72
|
"aria-invalid:ring-destructive/20 dark:aria-invalid:ring-destructive/40 aria-invalid:border-destructive",
|
|
15
73
|
className
|
|
16
74
|
)}
|
|
17
|
-
ref={
|
|
75
|
+
ref={mergedRef}
|
|
18
76
|
{...props}
|
|
77
|
+
onValueChange={handleValueChange}
|
|
19
78
|
/>
|
|
20
79
|
);
|
|
21
80
|
});
|
package/templates/link/link.tsx
CHANGED
|
@@ -85,6 +85,7 @@ const LoadTransition = React.forwardRef<HTMLDivElement, LoadTransitionProps>(
|
|
|
85
85
|
"relative overflow-hidden border border-border bg-background p-4",
|
|
86
86
|
className
|
|
87
87
|
)}
|
|
88
|
+
data-slot="load-transition"
|
|
88
89
|
{...props}
|
|
89
90
|
>
|
|
90
91
|
<AnimatePresence mode="popLayout" initial={false}>
|