@dinachi/cli 0.8.2 → 0.10.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 +144 -0
- package/README.md +36 -5
- package/dist/index.js +336 -62
- package/package.json +17 -14
- package/templates/animated-icon/animated-icon.tsx +145 -0
- package/templates/animated-icon/index.ts +1 -0
- package/templates/animated-list/animated-list.tsx +161 -0
- package/templates/animated-list/index.ts +2 -0
- package/templates/animated-tabs/animated-tabs.tsx +244 -0
- package/templates/animated-tabs/index.ts +13 -0
- package/templates/badge/badge.tsx +4 -4
- package/templates/carousel/carousel.tsx +492 -0
- package/templates/carousel/index.ts +1 -0
- package/templates/compare-slider/compare-slider.tsx +383 -0
- package/templates/compare-slider/index.ts +2 -0
- package/templates/expandable-card/expandable-card.tsx +509 -0
- package/templates/expandable-card/index.ts +1 -0
- package/templates/hold-to-confirm/hold-to-confirm.tsx +573 -0
- package/templates/hold-to-confirm/index.ts +2 -0
- package/templates/load-transition/index.ts +1 -0
- package/templates/load-transition/load-transition.tsx +163 -0
- package/templates/marquee/index.ts +2 -0
- package/templates/marquee/marquee.tsx +256 -0
- package/templates/number-ticker/index.ts +2 -0
- package/templates/number-ticker/number-ticker.tsx +470 -0
- package/templates/otp-field/index.ts +1 -0
- package/templates/otp-field/otp-field.tsx +72 -0
- package/templates/progress-ring/index.ts +1 -0
- package/templates/progress-ring/progress-ring.tsx +268 -0
- package/templates/scroll-progress/index.ts +2 -0
- package/templates/scroll-progress/scroll-progress.tsx +147 -0
- package/templates/scroll-reveal/index.ts +2 -0
- package/templates/scroll-reveal/scroll-reveal.tsx +198 -0
- package/templates/select/select.tsx +4 -2
- package/templates/slider/slider.tsx +1 -1
- package/templates/sortable/index.ts +1 -0
- package/templates/sortable/sortable.tsx +349 -0
- package/templates/stagger-list/index.ts +2 -0
- package/templates/stagger-list/stagger-list.tsx +204 -0
- package/templates/streaming-text/index.ts +2 -0
- package/templates/streaming-text/streaming-text.tsx +220 -0
- package/templates/swipeable-row/index.ts +2 -0
- package/templates/swipeable-row/swipeable-row.tsx +368 -0
- package/templates/text-morph/index.ts +2 -0
- package/templates/text-morph/text-morph.tsx +265 -0
- package/templates/text-shimmer/index.ts +2 -0
- package/templates/text-shimmer/text-shimmer.tsx +151 -0
- package/templates/textarea/textarea.tsx +1 -2
- package/templates/toast/toast.tsx +24 -19
- package/templates/tsconfig.json +1 -0
- package/LICENSE +0 -21
- package/templates/utils/variants.ts +0 -6
|
@@ -0,0 +1,573 @@
|
|
|
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
|
+
!reducedMotion &&
|
|
404
|
+
"transition-transform duration-150 ease-[cubic-bezier(0.23,1,0.32,1)] data-[holding]:duration-100 data-[holding]:scale-[0.97]",
|
|
405
|
+
"focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2",
|
|
406
|
+
"disabled:pointer-events-none disabled:opacity-50",
|
|
407
|
+
SHAPE[variant],
|
|
408
|
+
className
|
|
409
|
+
)
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
/**
|
|
413
|
+
* The press and release wiring, composed with whatever the consumer passed rather than
|
|
414
|
+
* replacing it. A consumer handler runs first, but it cannot take over a release path:
|
|
415
|
+
* a hold whose release never fires stays armed and confirms on its own.
|
|
416
|
+
*/
|
|
417
|
+
function holdHandlers(
|
|
418
|
+
theirs: React.ButtonHTMLAttributes<HTMLButtonElement>,
|
|
419
|
+
start: () => void,
|
|
420
|
+
cancel: () => void
|
|
421
|
+
) {
|
|
422
|
+
return {
|
|
423
|
+
onPointerDown: (event: React.PointerEvent<HTMLButtonElement>) => {
|
|
424
|
+
theirs.onPointerDown?.(event)
|
|
425
|
+
// A right- or middle-press is not an intent to confirm, and a destructive hold
|
|
426
|
+
// should not start counting on one.
|
|
427
|
+
if (event.button !== 0) return
|
|
428
|
+
event.currentTarget.setPointerCapture(event.pointerId)
|
|
429
|
+
start()
|
|
430
|
+
},
|
|
431
|
+
onPointerUp: (event: React.PointerEvent<HTMLButtonElement>) => {
|
|
432
|
+
theirs.onPointerUp?.(event)
|
|
433
|
+
cancel()
|
|
434
|
+
},
|
|
435
|
+
onPointerCancel: (event: React.PointerEvent<HTMLButtonElement>) => {
|
|
436
|
+
theirs.onPointerCancel?.(event)
|
|
437
|
+
cancel()
|
|
438
|
+
},
|
|
439
|
+
onLostPointerCapture: (event: React.PointerEvent<HTMLButtonElement>) => {
|
|
440
|
+
theirs.onLostPointerCapture?.(event)
|
|
441
|
+
cancel()
|
|
442
|
+
},
|
|
443
|
+
// Keyboard users hold the same way — otherwise this control is mouse-only.
|
|
444
|
+
onKeyDown: (event: React.KeyboardEvent<HTMLButtonElement>) => {
|
|
445
|
+
theirs.onKeyDown?.(event)
|
|
446
|
+
if (event.key !== " " && event.key !== "Enter") return
|
|
447
|
+
event.preventDefault()
|
|
448
|
+
if (event.repeat) return
|
|
449
|
+
start()
|
|
450
|
+
},
|
|
451
|
+
onKeyUp: (event: React.KeyboardEvent<HTMLButtonElement>) => {
|
|
452
|
+
theirs.onKeyUp?.(event)
|
|
453
|
+
if (event.key !== " " && event.key !== "Enter") return
|
|
454
|
+
cancel()
|
|
455
|
+
},
|
|
456
|
+
onBlur: (event: React.FocusEvent<HTMLButtonElement>) => {
|
|
457
|
+
theirs.onBlur?.(event)
|
|
458
|
+
cancel()
|
|
459
|
+
},
|
|
460
|
+
}
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
export interface HoldToConfirmProps
|
|
464
|
+
extends Omit<React.ButtonHTMLAttributes<HTMLButtonElement>, "onProgress"> {
|
|
465
|
+
/** How progress is drawn. See each variant's note on the component. */
|
|
466
|
+
variant?: HoldToConfirmVariant
|
|
467
|
+
/** Milliseconds the user must hold to confirm. */
|
|
468
|
+
duration?: number
|
|
469
|
+
/** Fired once the hold completes. */
|
|
470
|
+
onConfirm?: () => void
|
|
471
|
+
/** Milliseconds the confirmed state is held before resetting. Set `0` to stay confirmed. */
|
|
472
|
+
resetAfter?: number
|
|
473
|
+
/** Replaces the label while confirmed. */
|
|
474
|
+
confirmedLabel?: React.ReactNode
|
|
475
|
+
/** The description read to assistive tech, which cannot see that this button is held. */
|
|
476
|
+
holdHint?: string
|
|
477
|
+
/** Class applied to the progress fill. */
|
|
478
|
+
fillClassName?: string
|
|
479
|
+
/** Render as a different element (e.g. the library's Button).
|
|
480
|
+
* Accepts a ReactElement or `(props, state) => ReactElement`. */
|
|
481
|
+
render?: useRender.RenderProp
|
|
482
|
+
}
|
|
483
|
+
|
|
484
|
+
/**
|
|
485
|
+
* A destructive action gated behind a deliberate hold, drawn three ways:
|
|
486
|
+
*
|
|
487
|
+
* - `fill` — a bar sweeps the button from the left, behind the label. The most
|
|
488
|
+
* legible of the three, and the only one that reads at a glance across a room.
|
|
489
|
+
* - `ring` — a stroke closes around the button. Sized for square icon-only buttons,
|
|
490
|
+
* where there is no width for a bar to travel.
|
|
491
|
+
* - `border` — the button's own outline draws itself. The quietest option: nothing
|
|
492
|
+
* moves behind the label, so it suits a hold that sits inside dense UI.
|
|
493
|
+
*
|
|
494
|
+
* The timing is intentionally asymmetric whichever way it is drawn: progress takes
|
|
495
|
+
* `duration` to complete but unwinds in 200ms. Committing should feel considered;
|
|
496
|
+
* backing out should feel free. Release always resumes from the current position
|
|
497
|
+
* rather than snapping, so repeated part-holds stay continuous.
|
|
498
|
+
*
|
|
499
|
+
* Reduced motion drops the press scale and keeps the progress. The progress is the
|
|
500
|
+
* affordance rather than decoration — a hold that counts invisibly is a control with
|
|
501
|
+
* no feedback at all, which is worse than the movement it would save.
|
|
502
|
+
*/
|
|
503
|
+
const HoldToConfirm = React.forwardRef<HTMLButtonElement, HoldToConfirmProps>(
|
|
504
|
+
(
|
|
505
|
+
{
|
|
506
|
+
variant = "fill",
|
|
507
|
+
duration = 1600,
|
|
508
|
+
onConfirm,
|
|
509
|
+
resetAfter = 1600,
|
|
510
|
+
confirmedLabel,
|
|
511
|
+
holdHint = "Press and hold to confirm",
|
|
512
|
+
fillClassName,
|
|
513
|
+
render,
|
|
514
|
+
className,
|
|
515
|
+
children,
|
|
516
|
+
disabled,
|
|
517
|
+
...props
|
|
518
|
+
},
|
|
519
|
+
ref
|
|
520
|
+
) => {
|
|
521
|
+
const buttonRef = React.useRef<HTMLButtonElement>(null)
|
|
522
|
+
const outlined = variant !== "fill"
|
|
523
|
+
const box = useBox(buttonRef, outlined)
|
|
524
|
+
const reducedMotion = useReducedMotionConfig()
|
|
525
|
+
const { progress, confirmed, isHolding, start, cancel } = useHold({
|
|
526
|
+
duration,
|
|
527
|
+
resetAfter,
|
|
528
|
+
disabled,
|
|
529
|
+
onConfirm,
|
|
530
|
+
})
|
|
531
|
+
|
|
532
|
+
const text = confirmed && confirmedLabel ? confirmedLabel : children
|
|
533
|
+
const hintId = React.useId()
|
|
534
|
+
|
|
535
|
+
return useRender({
|
|
536
|
+
defaultTagName: "button",
|
|
537
|
+
render,
|
|
538
|
+
ref: [ref, buttonRef],
|
|
539
|
+
props: {
|
|
540
|
+
...disabledAttrs(render, disabled),
|
|
541
|
+
// The hold is the whole interaction and nothing about a button announces it, so
|
|
542
|
+
// it is stated outright. `aria-hidden` keeps the description out of the name
|
|
543
|
+
// while leaving it reachable through the reference.
|
|
544
|
+
"aria-describedby": cn(props["aria-describedby"], hintId),
|
|
545
|
+
// Confirming swaps the label, and a name change on an already-focused element is
|
|
546
|
+
// not reliably announced. As a live region the swap is spoken.
|
|
547
|
+
"aria-live": "polite" as const,
|
|
548
|
+
"data-holding": isHolding || undefined,
|
|
549
|
+
"data-confirmed": confirmed || undefined,
|
|
550
|
+
className: shellClass(variant, reducedMotion, className),
|
|
551
|
+
...props,
|
|
552
|
+
...holdHandlers(props, start, cancel),
|
|
553
|
+
children: (
|
|
554
|
+
<>
|
|
555
|
+
<ProgressLayer
|
|
556
|
+
variant={variant}
|
|
557
|
+
box={box}
|
|
558
|
+
progress={progress}
|
|
559
|
+
fillClassName={fillClassName}
|
|
560
|
+
text={text}
|
|
561
|
+
/>
|
|
562
|
+
<span id={hintId} aria-hidden className="sr-only">
|
|
563
|
+
{holdHint}
|
|
564
|
+
</span>
|
|
565
|
+
</>
|
|
566
|
+
),
|
|
567
|
+
},
|
|
568
|
+
})
|
|
569
|
+
}
|
|
570
|
+
)
|
|
571
|
+
HoldToConfirm.displayName = "HoldToConfirm"
|
|
572
|
+
|
|
573
|
+
export { HoldToConfirm }
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './load-transition'
|