@dinachi/cli 0.9.0 → 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 +57 -0
- package/README.md +34 -5
- package/dist/index.js +283 -61
- package/package.json +8 -6
- 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/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/otp-field.tsx +1 -1
- 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/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/templates/utils/variants.ts +0 -6
|
@@ -0,0 +1,383 @@
|
|
|
1
|
+
"use client"
|
|
2
|
+
|
|
3
|
+
import * as React from "react"
|
|
4
|
+
import {
|
|
5
|
+
animate,
|
|
6
|
+
motion,
|
|
7
|
+
MotionConfigContext,
|
|
8
|
+
useMotionTemplate,
|
|
9
|
+
useMotionValue,
|
|
10
|
+
useReducedMotion,
|
|
11
|
+
useTransform,
|
|
12
|
+
} from "motion/react"
|
|
13
|
+
import { cn } from "@/lib/utils"
|
|
14
|
+
|
|
15
|
+
/** `useReducedMotion` reads the OS setting only, and only at mount. Also honour an
|
|
16
|
+
* app-level `<MotionConfig reducedMotion>`, so a user preference in the host app
|
|
17
|
+
* reaches this component. */
|
|
18
|
+
function useReducedMotionConfig() {
|
|
19
|
+
const config = React.useContext(MotionConfigContext)
|
|
20
|
+
const prefersReduced = useReducedMotion()
|
|
21
|
+
if (config.reducedMotion === "always") return true
|
|
22
|
+
if (config.reducedMotion === "never") return false
|
|
23
|
+
return prefersReduced
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const EASE_OUT: [number, number, number, number] = [0.23, 1, 0.32, 1]
|
|
27
|
+
|
|
28
|
+
/** Runs a consumer's handler ahead of the component's own, so passing one adds
|
|
29
|
+
* behaviour instead of replacing it. */
|
|
30
|
+
function compose<E>(
|
|
31
|
+
theirs: ((event: E) => void) | undefined,
|
|
32
|
+
ours: (event: E) => void
|
|
33
|
+
) {
|
|
34
|
+
return (event: E) => {
|
|
35
|
+
theirs?.(event)
|
|
36
|
+
ours(event)
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
const clamp = (n: number) => Math.min(100, Math.max(0, n))
|
|
41
|
+
|
|
42
|
+
export interface CompareSliderProps
|
|
43
|
+
extends Omit<React.ComponentProps<"div">, "children" | "onChange"> {
|
|
44
|
+
/** Content shown on the left of the divider. */
|
|
45
|
+
before: React.ReactNode
|
|
46
|
+
/** Content shown on the right of the divider. */
|
|
47
|
+
after: React.ReactNode
|
|
48
|
+
/** Starting divider position, 0–100. */
|
|
49
|
+
defaultPosition?: number
|
|
50
|
+
/** Percentage points moved per arrow key press. */
|
|
51
|
+
step?: number
|
|
52
|
+
/** What starts a drag.
|
|
53
|
+
*
|
|
54
|
+
* `"handle"` — the default — limits dragging to the divider, leaving both layers
|
|
55
|
+
* live: buttons take clicks, text takes selection, links take focus.
|
|
56
|
+
*
|
|
57
|
+
* `"panel"` treats the whole surface as the control, so pressing anywhere jumps
|
|
58
|
+
* the divider there. Reach for it when the panes are images, where there is
|
|
59
|
+
* nothing to click and a thin handle is a needlessly small target — but note that
|
|
60
|
+
* it makes both layers inert, which reads as a bug on anything interactive. */
|
|
61
|
+
drag?: "panel" | "handle"
|
|
62
|
+
/** Accessible name for the divider handle. */
|
|
63
|
+
label?: string
|
|
64
|
+
/** Keep the knob at the vertical middle of the viewport instead of the middle of
|
|
65
|
+
* the panel. Set this when the panel is taller than the screen, or the knob spends
|
|
66
|
+
* most of the page off-screen. */
|
|
67
|
+
stickyHandle?: boolean
|
|
68
|
+
/** Fired on release and on each keyboard step. */
|
|
69
|
+
onPositionChange?: (position: number) => void
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Drag a divider to compare two layers.
|
|
74
|
+
*
|
|
75
|
+
* A pointer drag tracks 1:1 with no easing — that is direct manipulation, and any
|
|
76
|
+
* smoothing there reads as lag. A keyboard step is the opposite case: one press moves
|
|
77
|
+
* the divider a fixed slice of the panel in a single frame, which strobes rather than
|
|
78
|
+
* moves, so each step is carried over 100ms instead. The tween retargets from wherever
|
|
79
|
+
* the divider currently is, so a held arrow key composes into one continuous travel
|
|
80
|
+
* rather than a queue of restarts.
|
|
81
|
+
*
|
|
82
|
+
* Position lives in a motion value, so dragging never triggers a React render.
|
|
83
|
+
* Component state is synced on release and on each keyboard step, where it feeds
|
|
84
|
+
* `aria-valuenow` — that sync is deliberately immediate on a key press, so the
|
|
85
|
+
* announced value is never waiting on the pixels to arrive. A drag writes the same two
|
|
86
|
+
* attributes straight to the node, which keeps the reported value current without
|
|
87
|
+
* putting a render in the middle of a gesture.
|
|
88
|
+
*/
|
|
89
|
+
const CompareSlider = React.forwardRef<HTMLDivElement, CompareSliderProps>(
|
|
90
|
+
(
|
|
91
|
+
{
|
|
92
|
+
before,
|
|
93
|
+
after,
|
|
94
|
+
defaultPosition = 50,
|
|
95
|
+
step = 2,
|
|
96
|
+
drag = "handle",
|
|
97
|
+
label = "Compare position",
|
|
98
|
+
stickyHandle = false,
|
|
99
|
+
onPositionChange,
|
|
100
|
+
className,
|
|
101
|
+
...props
|
|
102
|
+
},
|
|
103
|
+
ref
|
|
104
|
+
) => {
|
|
105
|
+
const containerRef = React.useRef<HTMLDivElement>(null)
|
|
106
|
+
React.useImperativeHandle(ref, () => containerRef.current as HTMLDivElement)
|
|
107
|
+
|
|
108
|
+
const reducedMotion = useReducedMotionConfig()
|
|
109
|
+
const position = useMotionValue(clamp(defaultPosition))
|
|
110
|
+
const [announced, setAnnounced] = React.useState(clamp(defaultPosition))
|
|
111
|
+
// Where the divider is headed. Stepping off the motion value instead would sample
|
|
112
|
+
// a step still in flight, so each key repeat would advance by less than a full step.
|
|
113
|
+
const settled = React.useRef(clamp(defaultPosition))
|
|
114
|
+
const stepAnimation = React.useRef<ReturnType<typeof animate> | null>(null)
|
|
115
|
+
// The pointer that owns the gesture. A second finger arriving mid-drag would
|
|
116
|
+
// measure its own grab offset and jump the divider under the first one, so every
|
|
117
|
+
// other pointer is ignored until this one lifts.
|
|
118
|
+
const activePointer = React.useRef<number | null>(null)
|
|
119
|
+
// How far the pointer sat from the divider when the drag began. Grabbing a handle
|
|
120
|
+
// should not teleport it under your finger; pressing the panel should.
|
|
121
|
+
const grabOffset = React.useRef(0)
|
|
122
|
+
|
|
123
|
+
React.useEffect(() => () => stepAnimation.current?.stop(), [])
|
|
124
|
+
|
|
125
|
+
const remainder = useTransform(position, (p) => 100 - p)
|
|
126
|
+
const clipPath = useMotionTemplate`inset(0% ${remainder}% 0% 0%)`
|
|
127
|
+
// A percentage translate resolves against the element's own width, so a track the
|
|
128
|
+
// full width of the panel converts the position straight into a transform. `left`
|
|
129
|
+
// would say the same thing as a layout property recomputed on every frame of a
|
|
130
|
+
// drag, where this is composited.
|
|
131
|
+
const track = useMotionTemplate`translateX(${position}%)`
|
|
132
|
+
|
|
133
|
+
const handleRef = React.useRef<HTMLDivElement>(null)
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* The two attributes a slider is read through, written to the node rather than
|
|
137
|
+
* through state. React owns them on every render; between renders a drag keeps them
|
|
138
|
+
* current itself, so what assistive tech reports mid-gesture is where the divider
|
|
139
|
+
* actually is rather than where it was when the finger landed.
|
|
140
|
+
*/
|
|
141
|
+
const announceNow = React.useCallback((value: number) => {
|
|
142
|
+
const node = handleRef.current
|
|
143
|
+
if (!node) return
|
|
144
|
+
node.setAttribute("aria-valuenow", String(value))
|
|
145
|
+
node.setAttribute("aria-valuetext", `${value}%`)
|
|
146
|
+
}, [])
|
|
147
|
+
|
|
148
|
+
const setFromClientX = React.useCallback(
|
|
149
|
+
(clientX: number) => {
|
|
150
|
+
const rect = containerRef.current?.getBoundingClientRect()
|
|
151
|
+
if (!rect || rect.width === 0) return
|
|
152
|
+
const x = clientX - grabOffset.current - rect.left
|
|
153
|
+
const next = clamp((x / rect.width) * 100)
|
|
154
|
+
settled.current = next
|
|
155
|
+
position.set(next)
|
|
156
|
+
announceNow(Math.round(next))
|
|
157
|
+
},
|
|
158
|
+
[position, announceNow]
|
|
159
|
+
)
|
|
160
|
+
|
|
161
|
+
// The last value handed to `onPositionChange`. Plenty of gestures end where they
|
|
162
|
+
// began: clicking a handle without dragging it, or stepping into either end. A
|
|
163
|
+
// change event for an unchanged value is noise the caller has to filter.
|
|
164
|
+
const emitted = React.useRef(clamp(defaultPosition))
|
|
165
|
+
|
|
166
|
+
const emit = React.useCallback(
|
|
167
|
+
(next: number) => {
|
|
168
|
+
setAnnounced(next)
|
|
169
|
+
if (next === emitted.current) return
|
|
170
|
+
emitted.current = next
|
|
171
|
+
onPositionChange?.(next)
|
|
172
|
+
},
|
|
173
|
+
[onPositionChange]
|
|
174
|
+
)
|
|
175
|
+
|
|
176
|
+
const commit = React.useCallback(() => {
|
|
177
|
+
emit(Math.round(position.get()))
|
|
178
|
+
}, [position, emit])
|
|
179
|
+
|
|
180
|
+
const stepBy = React.useCallback(
|
|
181
|
+
(delta: number) => {
|
|
182
|
+
stepAnimation.current?.stop()
|
|
183
|
+
const next = clamp(settled.current + delta)
|
|
184
|
+
settled.current = next
|
|
185
|
+
const rounded = Math.round(next)
|
|
186
|
+
emit(rounded)
|
|
187
|
+
if (reducedMotion) {
|
|
188
|
+
position.set(next)
|
|
189
|
+
return
|
|
190
|
+
}
|
|
191
|
+
stepAnimation.current = animate(position, next, {
|
|
192
|
+
duration: 0.1,
|
|
193
|
+
ease: EASE_OUT,
|
|
194
|
+
})
|
|
195
|
+
},
|
|
196
|
+
[position, emit, reducedMotion]
|
|
197
|
+
)
|
|
198
|
+
|
|
199
|
+
const handleKeyDown = (event: React.KeyboardEvent<HTMLDivElement>) => {
|
|
200
|
+
const large = event.shiftKey ? 5 : 1
|
|
201
|
+
switch (event.key) {
|
|
202
|
+
case "ArrowLeft":
|
|
203
|
+
case "ArrowDown":
|
|
204
|
+
event.preventDefault()
|
|
205
|
+
stepBy(-step * large)
|
|
206
|
+
break
|
|
207
|
+
case "ArrowRight":
|
|
208
|
+
case "ArrowUp":
|
|
209
|
+
event.preventDefault()
|
|
210
|
+
stepBy(step * large)
|
|
211
|
+
break
|
|
212
|
+
case "Home":
|
|
213
|
+
event.preventDefault()
|
|
214
|
+
stepBy(-100)
|
|
215
|
+
break
|
|
216
|
+
case "End":
|
|
217
|
+
event.preventDefault()
|
|
218
|
+
stepBy(100)
|
|
219
|
+
break
|
|
220
|
+
default:
|
|
221
|
+
break
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
const onHandle = drag === "handle"
|
|
226
|
+
// A panel taller than the screen still has to scroll vertically, so only the
|
|
227
|
+
// horizontal axis is claimed there.
|
|
228
|
+
const touch = stickyHandle ? "touch-pan-y" : "touch-none"
|
|
229
|
+
|
|
230
|
+
const endDrag = (event: React.PointerEvent<HTMLDivElement>) => {
|
|
231
|
+
if (event.pointerId !== activePointer.current) return
|
|
232
|
+
activePointer.current = null
|
|
233
|
+
grabOffset.current = 0
|
|
234
|
+
commit()
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
const dragHandlers = {
|
|
238
|
+
onPointerDown: (event: React.PointerEvent<HTMLDivElement>) => {
|
|
239
|
+
if (activePointer.current !== null) return
|
|
240
|
+
const rect = containerRef.current?.getBoundingClientRect()
|
|
241
|
+
if (!rect || rect.width === 0) return
|
|
242
|
+
// A step still in flight has to be dropped here rather than left to finish,
|
|
243
|
+
// or it would keep writing over the pointer for the rest of its 100ms. The
|
|
244
|
+
// grab offset is then measured against where the divider actually is on
|
|
245
|
+
// screen, not where that step was headed.
|
|
246
|
+
stepAnimation.current?.stop()
|
|
247
|
+
grabOffset.current = onHandle
|
|
248
|
+
? event.clientX - (rect.left + (rect.width * position.get()) / 100)
|
|
249
|
+
: 0
|
|
250
|
+
activePointer.current = event.pointerId
|
|
251
|
+
event.currentTarget.setPointerCapture(event.pointerId)
|
|
252
|
+
setFromClientX(event.clientX)
|
|
253
|
+
},
|
|
254
|
+
onPointerMove: (event: React.PointerEvent<HTMLDivElement>) => {
|
|
255
|
+
if (event.pointerId === activePointer.current) setFromClientX(event.clientX)
|
|
256
|
+
},
|
|
257
|
+
onPointerUp: endDrag,
|
|
258
|
+
onPointerCancel: endDrag,
|
|
259
|
+
// Capture can be lost without a `pointerup` — the drag has to end there too, or
|
|
260
|
+
// the divider stays bound to a pointer that is no longer down.
|
|
261
|
+
onLostPointerCapture: endDrag,
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
// Spread after the consumer's props, and composed rather than assigned: their
|
|
265
|
+
// handler still runs, but it cannot take the drag's place. A slider whose pointer
|
|
266
|
+
// handlers were quietly overwritten still looks interactive and is not.
|
|
267
|
+
const panelHandlers = onHandle
|
|
268
|
+
? {}
|
|
269
|
+
: {
|
|
270
|
+
onPointerDown: compose(props.onPointerDown, dragHandlers.onPointerDown),
|
|
271
|
+
onPointerMove: compose(props.onPointerMove, dragHandlers.onPointerMove),
|
|
272
|
+
onPointerUp: compose(props.onPointerUp, dragHandlers.onPointerUp),
|
|
273
|
+
onPointerCancel: compose(
|
|
274
|
+
props.onPointerCancel,
|
|
275
|
+
dragHandlers.onPointerCancel
|
|
276
|
+
),
|
|
277
|
+
onLostPointerCapture: compose(
|
|
278
|
+
props.onLostPointerCapture,
|
|
279
|
+
dragHandlers.onLostPointerCapture
|
|
280
|
+
),
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
return (
|
|
284
|
+
<div
|
|
285
|
+
ref={containerRef}
|
|
286
|
+
className={cn(
|
|
287
|
+
"relative isolate w-full rounded-lg",
|
|
288
|
+
// Only claim selection and touch gestures where the drag actually starts.
|
|
289
|
+
// In handle mode the layers stay live, so taking either off the container
|
|
290
|
+
// would break selecting text and scrolling the page under it.
|
|
291
|
+
onHandle ? "select-auto" : cn("select-none", touch),
|
|
292
|
+
// `overflow: clip` still trims to the radius but does not open a scrollport,
|
|
293
|
+
// which `overflow: hidden` does — and a sticky knob resolves against the
|
|
294
|
+
// nearest scrollport, so `hidden` would pin it to a box that never scrolls.
|
|
295
|
+
stickyHandle ? "overflow-clip" : "overflow-hidden",
|
|
296
|
+
className
|
|
297
|
+
)}
|
|
298
|
+
{...props}
|
|
299
|
+
{...panelHandlers}
|
|
300
|
+
>
|
|
301
|
+
{/* `after` is the base layer and `before` is clipped over it, because the clip
|
|
302
|
+
runs from the left edge to the divider — so whichever layer is clipped is
|
|
303
|
+
the one on the left.
|
|
304
|
+
|
|
305
|
+
Both layers isolate. Without it neither one owns a stacking context, so a
|
|
306
|
+
`z-10` anywhere inside the base layer competes directly with the clipped
|
|
307
|
+
layer above it and paints straight through the clip — invisible with two
|
|
308
|
+
images, immediately obvious the moment either side is real UI. */}
|
|
309
|
+
<div className="relative isolate">{after}</div>
|
|
310
|
+
|
|
311
|
+
<motion.div style={{ clipPath }} className="absolute inset-0 isolate">
|
|
312
|
+
{before}
|
|
313
|
+
</motion.div>
|
|
314
|
+
|
|
315
|
+
{/* The track spans the panel and carries the transform; it stays inert so the
|
|
316
|
+
layers underneath keep their clicks, and only the strip inside it takes the
|
|
317
|
+
pointer. */}
|
|
318
|
+
<motion.div
|
|
319
|
+
style={{ transform: track }}
|
|
320
|
+
className="pointer-events-none absolute inset-0 z-10"
|
|
321
|
+
>
|
|
322
|
+
<div
|
|
323
|
+
ref={handleRef}
|
|
324
|
+
role="slider"
|
|
325
|
+
tabIndex={0}
|
|
326
|
+
aria-label={label}
|
|
327
|
+
aria-valuemin={0}
|
|
328
|
+
aria-valuemax={100}
|
|
329
|
+
aria-valuenow={announced}
|
|
330
|
+
// Without this a reader announces a bare "62". `aria-label` says what is
|
|
331
|
+
// being compared; this supplies the unit.
|
|
332
|
+
aria-valuetext={`${announced}%`}
|
|
333
|
+
aria-orientation="horizontal"
|
|
334
|
+
onKeyDown={handleKeyDown}
|
|
335
|
+
{...(onHandle ? dragHandlers : {})}
|
|
336
|
+
className={cn(
|
|
337
|
+
"pointer-events-auto absolute inset-y-0 left-0 w-0.5 -translate-x-1/2 cursor-ew-resize bg-background",
|
|
338
|
+
"focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring",
|
|
339
|
+
// A 2px line is not a grab target. The pseudo-element widens the hit area
|
|
340
|
+
// to a comfortable strip without widening the line you can see.
|
|
341
|
+
onHandle &&
|
|
342
|
+
cn(
|
|
343
|
+
touch,
|
|
344
|
+
"select-none before:absolute before:inset-y-0 before:-left-2.5 before:-right-2.5 before:content-['']"
|
|
345
|
+
)
|
|
346
|
+
)}
|
|
347
|
+
>
|
|
348
|
+
<span
|
|
349
|
+
aria-hidden
|
|
350
|
+
className={cn(
|
|
351
|
+
"flex h-7 w-7 items-center justify-center rounded-full bg-background text-foreground shadow-md",
|
|
352
|
+
stickyHandle
|
|
353
|
+
? // The divider is 2px wide, so the knob's flow position starts 1px
|
|
354
|
+
// left of centre and has to be pulled back by the rest of its half.
|
|
355
|
+
// The top margin only decides where it rests before anything has
|
|
356
|
+
// scrolled — without it the knob would start pinned to the very top
|
|
357
|
+
// edge of the panel, which reads as broken.
|
|
358
|
+
"sticky top-[50vh] -ml-[13px] mt-[30vh]"
|
|
359
|
+
: "absolute left-1/2 top-1/2 -translate-x-1/2 -translate-y-1/2"
|
|
360
|
+
)}
|
|
361
|
+
>
|
|
362
|
+
<svg
|
|
363
|
+
className="h-3.5 w-3.5"
|
|
364
|
+
viewBox="0 0 24 24"
|
|
365
|
+
fill="none"
|
|
366
|
+
stroke="currentColor"
|
|
367
|
+
strokeWidth={2}
|
|
368
|
+
strokeLinecap="round"
|
|
369
|
+
strokeLinejoin="round"
|
|
370
|
+
>
|
|
371
|
+
<polyline points="15 18 9 12 15 6" transform="translate(-3 0)" />
|
|
372
|
+
<polyline points="9 18 15 12 9 6" transform="translate(3 0)" />
|
|
373
|
+
</svg>
|
|
374
|
+
</span>
|
|
375
|
+
</div>
|
|
376
|
+
</motion.div>
|
|
377
|
+
</div>
|
|
378
|
+
)
|
|
379
|
+
}
|
|
380
|
+
)
|
|
381
|
+
CompareSlider.displayName = "CompareSlider"
|
|
382
|
+
|
|
383
|
+
export { CompareSlider }
|