@dinachi/cli 0.9.0 → 0.11.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (69) hide show
  1. package/CHANGELOG.md +101 -0
  2. package/README.md +34 -5
  3. package/dist/index.js +294 -63
  4. package/package.json +8 -6
  5. package/templates/accordion/accordion.tsx +4 -5
  6. package/templates/alert-dialog/alert-dialog.tsx +9 -2
  7. package/templates/animated-icon/animated-icon.tsx +145 -0
  8. package/templates/animated-icon/index.ts +1 -0
  9. package/templates/animated-list/animated-list.tsx +161 -0
  10. package/templates/animated-list/index.ts +2 -0
  11. package/templates/animated-tabs/animated-tabs.tsx +244 -0
  12. package/templates/animated-tabs/index.ts +13 -0
  13. package/templates/autocomplete/autocomplete.tsx +6 -4
  14. package/templates/button/button.tsx +19 -2
  15. package/templates/carousel/carousel.tsx +492 -0
  16. package/templates/carousel/index.ts +1 -0
  17. package/templates/collapsible/collapsible.tsx +3 -7
  18. package/templates/combobox/combobox.tsx +6 -4
  19. package/templates/compare-slider/compare-slider.tsx +383 -0
  20. package/templates/compare-slider/index.ts +2 -0
  21. package/templates/context-menu/context-menu.tsx +10 -6
  22. package/templates/dialog/dialog.tsx +9 -2
  23. package/templates/drawer/drawer.tsx +24 -2
  24. package/templates/expandable-card/expandable-card.tsx +509 -0
  25. package/templates/expandable-card/index.ts +1 -0
  26. package/templates/hold-to-confirm/hold-to-confirm.tsx +580 -0
  27. package/templates/hold-to-confirm/index.ts +2 -0
  28. package/templates/load-transition/index.ts +1 -0
  29. package/templates/load-transition/load-transition.tsx +163 -0
  30. package/templates/marquee/index.ts +2 -0
  31. package/templates/marquee/marquee.tsx +256 -0
  32. package/templates/menu/menu.tsx +10 -6
  33. package/templates/menubar/menubar.tsx +10 -6
  34. package/templates/meter/meter.tsx +1 -1
  35. package/templates/navigation-menu/navigation-menu.tsx +9 -6
  36. package/templates/number-ticker/index.ts +2 -0
  37. package/templates/number-ticker/number-ticker.tsx +470 -0
  38. package/templates/otp-field/otp-field.tsx +3 -2
  39. package/templates/popover/popover.tsx +7 -6
  40. package/templates/preview-card/preview-card.tsx +2 -1
  41. package/templates/progress/progress.tsx +1 -1
  42. package/templates/progress-ring/index.ts +1 -0
  43. package/templates/progress-ring/progress-ring.tsx +268 -0
  44. package/templates/scroll-progress/index.ts +2 -0
  45. package/templates/scroll-progress/scroll-progress.tsx +147 -0
  46. package/templates/scroll-reveal/index.ts +2 -0
  47. package/templates/scroll-reveal/scroll-reveal.tsx +198 -0
  48. package/templates/select/select.tsx +18 -4
  49. package/templates/slider/slider.tsx +3 -2
  50. package/templates/sortable/index.ts +1 -0
  51. package/templates/sortable/sortable.tsx +349 -0
  52. package/templates/stagger-list/index.ts +2 -0
  53. package/templates/stagger-list/stagger-list.tsx +204 -0
  54. package/templates/streaming-text/index.ts +2 -0
  55. package/templates/streaming-text/streaming-text.tsx +220 -0
  56. package/templates/swipeable-row/index.ts +2 -0
  57. package/templates/swipeable-row/swipeable-row.tsx +368 -0
  58. package/templates/switch/switch.tsx +4 -2
  59. package/templates/tabs/tabs.tsx +10 -2
  60. package/templates/text-morph/index.ts +2 -0
  61. package/templates/text-morph/text-morph.tsx +265 -0
  62. package/templates/text-shimmer/index.ts +2 -0
  63. package/templates/text-shimmer/text-shimmer.tsx +151 -0
  64. package/templates/textarea/textarea.tsx +1 -2
  65. package/templates/toast/toast.tsx +24 -19
  66. package/templates/toggle/toggle.tsx +1 -1
  67. package/templates/tooltip/tooltip.tsx +10 -4
  68. package/templates/tsconfig.json +1 -0
  69. package/templates/utils/variants.ts +0 -6
@@ -0,0 +1,580 @@
1
+ "use client"
2
+
3
+ import * as React from "react"
4
+ import { useRender } from "@base-ui/react/use-render"
5
+ import {
6
+ animate,
7
+ motion,
8
+ MotionConfigContext,
9
+ useMotionTemplate,
10
+ useMotionValue,
11
+ useReducedMotion,
12
+ useTransform,
13
+ type MotionValue,
14
+ } from "motion/react"
15
+ import { cn } from "@/lib/utils"
16
+
17
+ const EASE_OUT: [number, number, number, number] = [0.23, 1, 0.32, 1]
18
+
19
+ /** Every path back to zero — released, cancelled, or reset after a confirm. */
20
+ const UNWIND = { duration: 0.2, ease: EASE_OUT }
21
+
22
+ const STROKE = 2
23
+
24
+ /** `useReducedMotion` reads the OS setting only, and only at mount. Also honour an
25
+ * app-level `<MotionConfig reducedMotion>`, so a user preference in the host app
26
+ * reaches this component. */
27
+ function useReducedMotionConfig() {
28
+ const config = React.useContext(MotionConfigContext)
29
+ const prefersReduced = useReducedMotion()
30
+ if (config.reducedMotion === "always") return true
31
+ if (config.reducedMotion === "never") return false
32
+ return prefersReduced
33
+ }
34
+
35
+ export type HoldToConfirmVariant = "fill" | "ring" | "border"
36
+
37
+ /**
38
+ * The button's *padding* box, in px, with the matching inner corner radius. SVG cannot
39
+ * inherit a border radius, so the outline variants are drawn to measured geometry.
40
+ *
41
+ * Padding box because that is what an absolutely positioned `inset-0` child resolves
42
+ * against, and the SVG is one. The radius is clamped against the measured size:
43
+ * `rounded-full` computes to 9999px, which the element is far too small to honour.
44
+ */
45
+ function useBox(ref: React.RefObject<HTMLElement | null>, enabled: boolean) {
46
+ const [box, setBox] = React.useState({ w: 0, h: 0, r: 0 })
47
+
48
+ React.useLayoutEffect(() => {
49
+ const node = ref.current
50
+ if (!node || !enabled) return
51
+
52
+ const measure = (borderBoxWidth: number, borderBoxHeight: number) => {
53
+ const style = getComputedStyle(node)
54
+ const border = parseFloat(style.borderTopWidth) || 0
55
+ const outer = parseFloat(style.borderTopLeftRadius) || 0
56
+ const width = borderBoxWidth - border * 2
57
+ const height = borderBoxHeight - border * 2
58
+ const next = {
59
+ w: width,
60
+ h: height,
61
+ // The specified radius is the outer one; a border's inner curve is tighter
62
+ // by its own width.
63
+ r: Math.max(0, Math.min(outer - border, width / 2, height / 2)),
64
+ }
65
+ // The observer fires on every layout pass; only re-render when it matters.
66
+ setBox((prev) =>
67
+ prev.w === next.w && prev.h === next.h && prev.r === next.r ? prev : next
68
+ )
69
+ }
70
+
71
+ measure(node.offsetWidth, node.offsetHeight)
72
+ const observer = new ResizeObserver(([entry]) => {
73
+ // The laid-out size, not `getBoundingClientRect` — that folds in the press
74
+ // transform, so any re-measure while the button is held would redraw the
75
+ // outline 3% small and then snap it back on release.
76
+ // `borderBoxSize` was a bare object before Firefox 92 and absent from early
77
+ // Safari, either of which throws on the array read. `offsetWidth` reports the
78
+ // same border box, and is what the first measure above already uses.
79
+ const size = entry.borderBoxSize?.[0]
80
+ if (size) measure(size.inlineSize, size.blockSize)
81
+ else measure(node.offsetWidth, node.offsetHeight)
82
+ })
83
+ observer.observe(node)
84
+ return () => observer.disconnect()
85
+ }, [ref, enabled])
86
+
87
+ return box
88
+ }
89
+
90
+ type Box = ReturnType<typeof useBox>
91
+
92
+ interface ProgressLayerProps {
93
+ /** 0–1, the whole state each layer draws itself from. */
94
+ progress: MotionValue<number>
95
+ fillClassName?: string
96
+ /** The button's label. Sits above the progress, so each layer places it itself. */
97
+ text: React.ReactNode
98
+ }
99
+
100
+ /** Above the progress in every variant, and the only thing that has to be. */
101
+ function Label({ children }: { children: React.ReactNode }) {
102
+ return <span className="relative z-10">{children}</span>
103
+ }
104
+
105
+ /**
106
+ * The stroke `ring` and `border` are both drawn with.
107
+ *
108
+ * `pathLength` is normalised to 1 by Motion, so the same 0–1 progress value that scales
109
+ * the `fill` bar also closes a stroke — the length never has to be measured, only the
110
+ * shape it is drawn on.
111
+ */
112
+ function useStroke(progress: MotionValue<number>, fillClassName?: string) {
113
+ // A round cap still paints a dot at zero length, which on an untouched control reads
114
+ // as an indicator stuck at the starting line.
115
+ const visible = useTransform(progress, (p) => (p > 0 ? 1 : 0))
116
+
117
+ return {
118
+ fill: "none",
119
+ strokeWidth: STROKE,
120
+ strokeLinecap: "round" as const,
121
+ pathLength: 1,
122
+ style: { pathLength: progress, opacity: visible },
123
+ className: cn("stroke-destructive", fillClassName),
124
+ }
125
+ }
126
+
127
+ /** A bar sweeping the button from the left, behind the label. */
128
+ function FillProgress({ progress, fillClassName, text }: ProgressLayerProps) {
129
+ // The sweep, as a clip in the button's own coordinates.
130
+ const unswept = useTransform(progress, (p) => (1 - p) * 100)
131
+ const swept = useMotionTemplate`inset(0% ${unswept}% 0% 0%)`
132
+
133
+ return (
134
+ <>
135
+ {/* Covers the border box and takes no radius of its own: the button's
136
+ `overflow-hidden` is what shapes it. A radius here would be the outer one
137
+ applied a border-width inside, which does not nest and leaves a hairline gap. */}
138
+ <motion.span
139
+ aria-hidden
140
+ style={{ scaleX: progress }}
141
+ className={cn(
142
+ "absolute -inset-px origin-left bg-destructive",
143
+ fillClassName
144
+ )}
145
+ />
146
+ <Label>{text}</Label>
147
+ {/* A second copy of the label, inverted and clipped to the bar's edge. No colour
148
+ transition can be timed to a moving boundary; a clip shares it exactly. Boxed
149
+ like the bar so the clip resolves against the width the bar is scaling. */}
150
+ <motion.span
151
+ aria-hidden
152
+ style={{ clipPath: swept }}
153
+ className="absolute -inset-px z-20 flex items-center justify-center text-destructive-foreground"
154
+ >
155
+ {text}
156
+ </motion.span>
157
+ </>
158
+ )
159
+ }
160
+
161
+ /** A stroke closing around a square button. */
162
+ function RingProgress({
163
+ progress,
164
+ fillClassName,
165
+ text,
166
+ box,
167
+ }: ProgressLayerProps & { box: Box }) {
168
+ const stroke = useStroke(progress, fillClassName)
169
+ // Inset by half the stroke so the whole width stays inside both the SVG viewport and
170
+ // the button's own `overflow: hidden` — otherwise the outer half is trimmed.
171
+ const inset = STROKE / 2
172
+ const ring = {
173
+ cx: box.w / 2,
174
+ cy: box.h / 2,
175
+ r: Math.max(0, Math.min(box.w, box.h) / 2 - inset),
176
+ }
177
+
178
+ return (
179
+ <>
180
+ <svg
181
+ aria-hidden
182
+ // Rotated so the stroke closes from the top rather than from 3 o'clock.
183
+ className="pointer-events-none absolute inset-0 h-full w-full -rotate-90"
184
+ >
185
+ <circle
186
+ {...ring}
187
+ fill="none"
188
+ strokeWidth={STROKE}
189
+ className="stroke-destructive/20"
190
+ />
191
+ <motion.circle {...ring} {...stroke} />
192
+ </svg>
193
+ <Label>{text}</Label>
194
+ </>
195
+ )
196
+ }
197
+
198
+ /** The button's own outline, drawing itself. */
199
+ function BorderProgress({
200
+ progress,
201
+ fillClassName,
202
+ text,
203
+ box,
204
+ }: ProgressLayerProps & { box: Box }) {
205
+ const stroke = useStroke(progress, fillClassName)
206
+ const { w, h, r } = box
207
+ // Half a stroke in, for the same reason as the ring — and here the corners disappear
208
+ // entirely without it.
209
+ const inset = STROKE / 2
210
+ const right = w - inset
211
+ const bottom = h - inset
212
+ // The measured radius is the button's outer one; the trace sits half a stroke inside
213
+ // it, so its own corners are that much tighter.
214
+ const corner = Math.max(0, r - inset)
215
+
216
+ // Two halves leaving the top centre in opposite directions and meeting at the bottom.
217
+ // A single lap would spend its first third crawling along the top edge — which is
218
+ // just the `fill` bar again — and would start at wherever the top-left corner happens
219
+ // to end. Mirrored halves read as the outline closing in, and the moment they meet is
220
+ // the moment the hold completes.
221
+ const outline = [
222
+ // Clockwise: right along the top, down the right side, back to the bottom centre.
223
+ `M ${w / 2} ${inset} H ${right - corner} A ${corner} ${corner} 0 0 1 ${right} ${inset + corner} V ${bottom - corner} A ${corner} ${corner} 0 0 1 ${right - corner} ${bottom} H ${w / 2}`,
224
+ // Anticlockwise: the same path mirrored down the left side.
225
+ `M ${w / 2} ${inset} H ${inset + corner} A ${corner} ${corner} 0 0 0 ${inset} ${inset + corner} V ${bottom - corner} A ${corner} ${corner} 0 0 0 ${inset + corner} ${bottom} H ${w / 2}`,
226
+ ]
227
+
228
+ return (
229
+ <>
230
+ {w > 0 && h > 0 && (
231
+ <svg
232
+ aria-hidden
233
+ className="pointer-events-none absolute inset-0 h-full w-full"
234
+ >
235
+ {/* The rail is the same two paths, so it cannot drift from the trace.
236
+ Drawing it here rather than as a CSS border also keeps the two exactly
237
+ coincident — a real border sits a padding box further out. */}
238
+ {outline.map((d, index) => (
239
+ <path
240
+ key={`rail-${index}`}
241
+ d={d}
242
+ fill="none"
243
+ strokeWidth={STROKE}
244
+ className="stroke-destructive/20"
245
+ />
246
+ ))}
247
+ {outline.map((d, index) => (
248
+ <motion.path key={index} d={d} {...stroke} />
249
+ ))}
250
+ </svg>
251
+ )}
252
+ <Label>{text}</Label>
253
+ </>
254
+ )
255
+ }
256
+
257
+ /** Picks the layer a variant is drawn with. */
258
+ function ProgressLayer({
259
+ variant,
260
+ box,
261
+ ...layer
262
+ }: ProgressLayerProps & { variant: HoldToConfirmVariant; box: Box }) {
263
+ if (variant === "ring") return <RingProgress {...layer} box={box} />
264
+ if (variant === "border") return <BorderProgress {...layer} box={box} />
265
+ return <FillProgress {...layer} />
266
+ }
267
+
268
+ /**
269
+ * The shape of the button itself, per variant.
270
+ *
271
+ * `ring` and `border` draw their own outline, so a visible static border under either
272
+ * would read as a second indicator, stuck. Theirs is transparent rather than absent:
273
+ * the width still has to be reserved, so all three sit the same size.
274
+ *
275
+ * `ring`'s padding is the room its circle is drawn in. With none, the button is only as
276
+ * big as its icon and the stroke lands on the glyph. 10px clears a 16px icon at 38px,
277
+ * the height of the other two.
278
+ */
279
+ const SHAPE: Record<HoldToConfirmVariant, string> = {
280
+ fill: "rounded-md border border-destructive",
281
+ ring: "aspect-square rounded-full border border-transparent p-2.5",
282
+ border: "rounded-md border border-transparent",
283
+ }
284
+
285
+ /**
286
+ * The hold itself: how far through it is, whether it is running, and the two ways it
287
+ * can end.
288
+ *
289
+ * Everything here is a ref rather than state, because a hold is driven by pointer and
290
+ * key events that have to read the current value synchronously. `isHolding` and
291
+ * `confirmed` are the only parts the render tree needs, so they are the only state.
292
+ */
293
+ function useHold({
294
+ duration,
295
+ resetAfter,
296
+ disabled,
297
+ onConfirm,
298
+ }: Pick<
299
+ HoldToConfirmProps,
300
+ "duration" | "resetAfter" | "disabled" | "onConfirm"
301
+ > & { duration: number; resetAfter: number }) {
302
+ const progress = useMotionValue(0)
303
+ const animation = React.useRef<ReturnType<typeof animate> | null>(null)
304
+ const reset = React.useRef<ReturnType<typeof setTimeout> | null>(null)
305
+ const holding = React.useRef(false)
306
+ const [confirmed, setConfirmed] = React.useState(false)
307
+ const [isHolding, setIsHolding] = React.useState(false)
308
+
309
+ React.useEffect(
310
+ () => () => {
311
+ animation.current?.stop()
312
+ if (reset.current) clearTimeout(reset.current)
313
+ },
314
+ []
315
+ )
316
+
317
+ // Read when the hold completes, not captured when it starts. The running animation
318
+ // keeps whichever `onComplete` it was handed at the press, so a handler closing over
319
+ // a selected row would otherwise fire against the row selected 1600ms earlier.
320
+ const onConfirmRef = React.useRef(onConfirm)
321
+ React.useEffect(() => {
322
+ onConfirmRef.current = onConfirm
323
+ })
324
+
325
+ const settle = React.useCallback(() => {
326
+ setConfirmed(false)
327
+ // Tracked like every other run. A press landing inside this window has to be able
328
+ // to stop it, or two animations drive `progress` at once and the new hold gets
329
+ // dragged back to zero under the user's finger.
330
+ animation.current = animate(progress, 0, UNWIND)
331
+ }, [progress])
332
+
333
+ // Read late for the same reason `onConfirm` is: the running animation keeps whichever
334
+ // `onComplete` it was handed at the press, so a `resetAfter` raised during the hold
335
+ // would be ignored for the hold the user is actually watching.
336
+ const resetAfterRef = React.useRef(resetAfter)
337
+ React.useEffect(() => {
338
+ resetAfterRef.current = resetAfter
339
+ })
340
+
341
+ const complete = React.useCallback(() => {
342
+ holding.current = false
343
+ setIsHolding(false)
344
+ setConfirmed(true)
345
+ onConfirmRef.current?.()
346
+ if (resetAfterRef.current > 0)
347
+ reset.current = setTimeout(settle, resetAfterRef.current)
348
+ }, [settle])
349
+
350
+ const start = React.useCallback(() => {
351
+ if (disabled || confirmed || holding.current) return
352
+ holding.current = true
353
+ setIsHolding(true)
354
+ animation.current?.stop()
355
+ // Resume from where the fill actually is, not from zero.
356
+ const remaining = (1 - progress.get()) * duration
357
+ animation.current = animate(progress, 1, {
358
+ duration: remaining / 1000,
359
+ ease: "linear",
360
+ onComplete: complete,
361
+ })
362
+ }, [disabled, confirmed, duration, progress, complete])
363
+
364
+ const cancel = React.useCallback(() => {
365
+ if (!holding.current) return
366
+ holding.current = false
367
+ setIsHolding(false)
368
+ animation.current?.stop()
369
+ animation.current = animate(progress, 0, UNWIND)
370
+ }, [progress])
371
+
372
+ return { progress, confirmed, isHolding, start, cancel }
373
+ }
374
+
375
+ /**
376
+ * `type` and `disabled` mean nothing on an anchor, and `disabled` on one is invalid HTML
377
+ * that leaves the element still clickable. Only a native button gets them; anything else
378
+ * is told the same thing in the way its own role understands.
379
+ */
380
+ function disabledAttrs(render: useRender.RenderProp | undefined, disabled?: boolean) {
381
+ const nativeButton =
382
+ render === undefined ||
383
+ (React.isValidElement(render) && render.type === "button")
384
+
385
+ return nativeButton
386
+ ? { type: "button" as const, disabled }
387
+ : { "aria-disabled": disabled || undefined }
388
+ }
389
+
390
+ /** The button's own surface, which is the same whichever way progress is drawn. */
391
+ function shellClass(
392
+ variant: HoldToConfirmVariant,
393
+ reducedMotion: boolean | null,
394
+ className: string | undefined
395
+ ) {
396
+ return cn(
397
+ "relative isolate inline-flex select-none items-center justify-center overflow-hidden",
398
+ "px-4 py-2 text-sm font-medium text-destructive",
399
+ // Keyed to the held state rather than `:active`, so a keyboard hold gets the same
400
+ // press feedback as a pointer one, and so the button lifts on the frame the hold
401
+ // completes. Quicker in than out: the press is the interface answering the user,
402
+ // the release is only it relaxing.
403
+ // `useReducedMotion` samples the OS setting at mount, so the class is dropped here
404
+ // only for a preference that was already set. `motion-reduce` catches the same
405
+ // preference turned on while the page is open, which the hook would not see.
406
+ !reducedMotion &&
407
+ cn(
408
+ "transition-transform duration-[var(--motion-duration-fast,150ms)] ease-[var(--motion-ease-out,cubic-bezier(0.23,1,0.32,1))]",
409
+ "data-[holding]:duration-100 data-[holding]:scale-[0.97]",
410
+ "motion-reduce:data-[holding]:scale-100"
411
+ ),
412
+ "focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2",
413
+ "disabled:pointer-events-none disabled:opacity-50",
414
+ SHAPE[variant],
415
+ className
416
+ )
417
+ }
418
+
419
+ /**
420
+ * The press and release wiring, composed with whatever the consumer passed rather than
421
+ * replacing it. A consumer handler runs first, but it cannot take over a release path:
422
+ * a hold whose release never fires stays armed and confirms on its own.
423
+ */
424
+ function holdHandlers(
425
+ theirs: React.ButtonHTMLAttributes<HTMLButtonElement>,
426
+ start: () => void,
427
+ cancel: () => void
428
+ ) {
429
+ return {
430
+ onPointerDown: (event: React.PointerEvent<HTMLButtonElement>) => {
431
+ theirs.onPointerDown?.(event)
432
+ // A right- or middle-press is not an intent to confirm, and a destructive hold
433
+ // should not start counting on one.
434
+ if (event.button !== 0) return
435
+ event.currentTarget.setPointerCapture(event.pointerId)
436
+ start()
437
+ },
438
+ onPointerUp: (event: React.PointerEvent<HTMLButtonElement>) => {
439
+ theirs.onPointerUp?.(event)
440
+ cancel()
441
+ },
442
+ onPointerCancel: (event: React.PointerEvent<HTMLButtonElement>) => {
443
+ theirs.onPointerCancel?.(event)
444
+ cancel()
445
+ },
446
+ onLostPointerCapture: (event: React.PointerEvent<HTMLButtonElement>) => {
447
+ theirs.onLostPointerCapture?.(event)
448
+ cancel()
449
+ },
450
+ // Keyboard users hold the same way — otherwise this control is mouse-only.
451
+ onKeyDown: (event: React.KeyboardEvent<HTMLButtonElement>) => {
452
+ theirs.onKeyDown?.(event)
453
+ if (event.key !== " " && event.key !== "Enter") return
454
+ event.preventDefault()
455
+ if (event.repeat) return
456
+ start()
457
+ },
458
+ onKeyUp: (event: React.KeyboardEvent<HTMLButtonElement>) => {
459
+ theirs.onKeyUp?.(event)
460
+ if (event.key !== " " && event.key !== "Enter") return
461
+ cancel()
462
+ },
463
+ onBlur: (event: React.FocusEvent<HTMLButtonElement>) => {
464
+ theirs.onBlur?.(event)
465
+ cancel()
466
+ },
467
+ }
468
+ }
469
+
470
+ export interface HoldToConfirmProps
471
+ extends Omit<React.ButtonHTMLAttributes<HTMLButtonElement>, "onProgress"> {
472
+ /** How progress is drawn. See each variant's note on the component. */
473
+ variant?: HoldToConfirmVariant
474
+ /** Milliseconds the user must hold to confirm. */
475
+ duration?: number
476
+ /** Fired once the hold completes. */
477
+ onConfirm?: () => void
478
+ /** Milliseconds the confirmed state is held before resetting. Set `0` to stay confirmed. */
479
+ resetAfter?: number
480
+ /** Replaces the label while confirmed. */
481
+ confirmedLabel?: React.ReactNode
482
+ /** The description read to assistive tech, which cannot see that this button is held. */
483
+ holdHint?: string
484
+ /** Class applied to the progress fill. */
485
+ fillClassName?: string
486
+ /** Render as a different element (e.g. the library's Button).
487
+ * Accepts a ReactElement or `(props, state) => ReactElement`. */
488
+ render?: useRender.RenderProp
489
+ }
490
+
491
+ /**
492
+ * A destructive action gated behind a deliberate hold, drawn three ways:
493
+ *
494
+ * - `fill` — a bar sweeps the button from the left, behind the label. The most
495
+ * legible of the three, and the only one that reads at a glance across a room.
496
+ * - `ring` — a stroke closes around the button. Sized for square icon-only buttons,
497
+ * where there is no width for a bar to travel.
498
+ * - `border` — the button's own outline draws itself. The quietest option: nothing
499
+ * moves behind the label, so it suits a hold that sits inside dense UI.
500
+ *
501
+ * The timing is intentionally asymmetric whichever way it is drawn: progress takes
502
+ * `duration` to complete but unwinds in 200ms. Committing should feel considered;
503
+ * backing out should feel free. Release always resumes from the current position
504
+ * rather than snapping, so repeated part-holds stay continuous.
505
+ *
506
+ * Reduced motion drops the press scale and keeps the progress. The progress is the
507
+ * affordance rather than decoration — a hold that counts invisibly is a control with
508
+ * no feedback at all, which is worse than the movement it would save.
509
+ */
510
+ const HoldToConfirm = React.forwardRef<HTMLButtonElement, HoldToConfirmProps>(
511
+ (
512
+ {
513
+ variant = "fill",
514
+ duration = 1600,
515
+ onConfirm,
516
+ resetAfter = 1600,
517
+ confirmedLabel,
518
+ holdHint = "Press and hold to confirm",
519
+ fillClassName,
520
+ render,
521
+ className,
522
+ children,
523
+ disabled,
524
+ ...props
525
+ },
526
+ ref
527
+ ) => {
528
+ const buttonRef = React.useRef<HTMLButtonElement>(null)
529
+ const outlined = variant !== "fill"
530
+ const box = useBox(buttonRef, outlined)
531
+ const reducedMotion = useReducedMotionConfig()
532
+ const { progress, confirmed, isHolding, start, cancel } = useHold({
533
+ duration,
534
+ resetAfter,
535
+ disabled,
536
+ onConfirm,
537
+ })
538
+
539
+ const text = confirmed && confirmedLabel ? confirmedLabel : children
540
+ const hintId = React.useId()
541
+
542
+ return useRender({
543
+ defaultTagName: "button",
544
+ render,
545
+ ref: [ref, buttonRef],
546
+ props: {
547
+ ...disabledAttrs(render, disabled),
548
+ // The hold is the whole interaction and nothing about a button announces it, so
549
+ // it is stated outright. `aria-hidden` keeps the description out of the name
550
+ // while leaving it reachable through the reference.
551
+ "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
+ "data-holding": isHolding || undefined,
556
+ "data-confirmed": confirmed || undefined,
557
+ className: shellClass(variant, reducedMotion, className),
558
+ ...props,
559
+ ...holdHandlers(props, start, cancel),
560
+ children: (
561
+ <>
562
+ <ProgressLayer
563
+ variant={variant}
564
+ box={box}
565
+ progress={progress}
566
+ fillClassName={fillClassName}
567
+ text={text}
568
+ />
569
+ <span id={hintId} aria-hidden className="sr-only">
570
+ {holdHint}
571
+ </span>
572
+ </>
573
+ ),
574
+ },
575
+ })
576
+ }
577
+ )
578
+ HoldToConfirm.displayName = "HoldToConfirm"
579
+
580
+ export { HoldToConfirm }
@@ -0,0 +1,2 @@
1
+ export { HoldToConfirm } from './hold-to-confirm'
2
+ export type { HoldToConfirmProps, HoldToConfirmVariant } from './hold-to-confirm'
@@ -0,0 +1 @@
1
+ export * from './load-transition'