@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.
Files changed (47) hide show
  1. package/CHANGELOG.md +57 -0
  2. package/README.md +34 -5
  3. package/dist/index.js +283 -61
  4. package/package.json +8 -6
  5. package/templates/animated-icon/animated-icon.tsx +145 -0
  6. package/templates/animated-icon/index.ts +1 -0
  7. package/templates/animated-list/animated-list.tsx +161 -0
  8. package/templates/animated-list/index.ts +2 -0
  9. package/templates/animated-tabs/animated-tabs.tsx +244 -0
  10. package/templates/animated-tabs/index.ts +13 -0
  11. package/templates/carousel/carousel.tsx +492 -0
  12. package/templates/carousel/index.ts +1 -0
  13. package/templates/compare-slider/compare-slider.tsx +383 -0
  14. package/templates/compare-slider/index.ts +2 -0
  15. package/templates/expandable-card/expandable-card.tsx +509 -0
  16. package/templates/expandable-card/index.ts +1 -0
  17. package/templates/hold-to-confirm/hold-to-confirm.tsx +573 -0
  18. package/templates/hold-to-confirm/index.ts +2 -0
  19. package/templates/load-transition/index.ts +1 -0
  20. package/templates/load-transition/load-transition.tsx +163 -0
  21. package/templates/marquee/index.ts +2 -0
  22. package/templates/marquee/marquee.tsx +256 -0
  23. package/templates/number-ticker/index.ts +2 -0
  24. package/templates/number-ticker/number-ticker.tsx +470 -0
  25. package/templates/otp-field/otp-field.tsx +1 -1
  26. package/templates/progress-ring/index.ts +1 -0
  27. package/templates/progress-ring/progress-ring.tsx +268 -0
  28. package/templates/scroll-progress/index.ts +2 -0
  29. package/templates/scroll-progress/scroll-progress.tsx +147 -0
  30. package/templates/scroll-reveal/index.ts +2 -0
  31. package/templates/scroll-reveal/scroll-reveal.tsx +198 -0
  32. package/templates/sortable/index.ts +1 -0
  33. package/templates/sortable/sortable.tsx +349 -0
  34. package/templates/stagger-list/index.ts +2 -0
  35. package/templates/stagger-list/stagger-list.tsx +204 -0
  36. package/templates/streaming-text/index.ts +2 -0
  37. package/templates/streaming-text/streaming-text.tsx +220 -0
  38. package/templates/swipeable-row/index.ts +2 -0
  39. package/templates/swipeable-row/swipeable-row.tsx +368 -0
  40. package/templates/text-morph/index.ts +2 -0
  41. package/templates/text-morph/text-morph.tsx +265 -0
  42. package/templates/text-shimmer/index.ts +2 -0
  43. package/templates/text-shimmer/text-shimmer.tsx +151 -0
  44. package/templates/textarea/textarea.tsx +1 -2
  45. package/templates/toast/toast.tsx +24 -19
  46. package/templates/tsconfig.json +1 -0
  47. package/templates/utils/variants.ts +0 -6
@@ -0,0 +1,470 @@
1
+ "use client"
2
+
3
+ import * as React from "react"
4
+ import {
5
+ MotionConfigContext,
6
+ useInView,
7
+ useMotionValueEvent,
8
+ useReducedMotion,
9
+ useSpring,
10
+ type MotionValue,
11
+ } from "motion/react"
12
+ import { cn } from "@/lib/utils"
13
+
14
+ /** `useReducedMotion` reads the OS setting only, and only at mount. Also honour an
15
+ * app-level `<MotionConfig reducedMotion>`, so a user preference in the host app
16
+ * reaches this component. */
17
+ function useReducedMotionConfig() {
18
+ const config = React.useContext(MotionConfigContext)
19
+ const prefersReduced = useReducedMotion()
20
+ if (config.reducedMotion === "always") return true
21
+ if (config.reducedMotion === "never") return false
22
+ return prefersReduced
23
+ }
24
+
25
+ /** The columns are written by hand rather than by React, and a layout effect is the
26
+ * only place a DOM write lands before the browser paints. There is no paint to beat
27
+ * on the server, and React warns if you ask for one. */
28
+ const useIsomorphicLayoutEffect =
29
+ typeof window === "undefined" ? React.useEffect : React.useLayoutEffect
30
+
31
+ /** Apple's move-and-reposition spring: 0.4s response, critically damped. Nothing here
32
+ * was thrown by a finger, so there is no momentum for an overshoot to stand for. */
33
+ const DURATION = 0.4
34
+ const BOUNCE = 0
35
+
36
+ /** One full revolution. A column crossing more ground than that still settles in the
37
+ * same 0.4s, so the surplus digits arrive faster than the eye resolves them. Starting
38
+ * nearer keeps the landing digit and the direction of travel and drops the strobe. */
39
+ const REVOLUTION = 10
40
+
41
+ const CELL = "flex h-[1.15em] items-center justify-center leading-[1.15em]"
42
+
43
+ const digitOf = (position: number) => ((Math.trunc(position) % 10) + 10) % 10
44
+
45
+ /**
46
+ * Where a column should start from to reach `to` within one revolution. The result is
47
+ * always congruent to `from` modulo ten and on the same side of the target, so the
48
+ * glyph and the sub-digit offset on screen are unchanged. Only the lap count drops.
49
+ */
50
+ function trimTravel(from: number, to: number) {
51
+ const span = to - from
52
+ if (Math.abs(span) <= REVOLUTION) return from
53
+ const laps = Math.ceil(Math.abs(span) / REVOLUTION) - 1
54
+ return from + Math.sign(span) * laps * REVOLUTION
55
+ }
56
+
57
+ export type NumberTickerVariant = "odometer" | "counter" | "flip"
58
+
59
+ /** A column's start and target, both in unwrapped digit space. See the note on
60
+ * `cells` in the component for what that space is. */
61
+ interface ColumnProps {
62
+ to: number
63
+ from: number
64
+ duration: number
65
+ bounce: number
66
+ delay: number
67
+ active: boolean
68
+ }
69
+
70
+ /**
71
+ * Sends a column to its target once it is allowed to move. A spring re-targets from
72
+ * wherever the value currently sits and carries its velocity across, so a number that
73
+ * changes mid-roll bends towards the new one rather than starting again from zero.
74
+ */
75
+ function useSettle(
76
+ position: MotionValue<number>,
77
+ to: number,
78
+ delay: number,
79
+ active: boolean
80
+ ) {
81
+ React.useEffect(() => {
82
+ if (!active) return
83
+
84
+ const settle = () => {
85
+ const trimmed = trimTravel(position.get(), to)
86
+ if (trimmed !== position.get()) position.jump(trimmed)
87
+ position.set(to)
88
+ }
89
+
90
+ if (delay <= 0) {
91
+ settle()
92
+ return
93
+ }
94
+ const timer = setTimeout(settle, delay * 1000)
95
+ return () => clearTimeout(timer)
96
+ }, [position, to, delay, active])
97
+ }
98
+
99
+ /**
100
+ * One wheel of the odometer, drawn as a two-cell strip: the digit in the window and
101
+ * the one after it. Rolling is a fractional offset of that strip, and the pair is
102
+ * rewritten in the same write as the offset resets, so the wheel is seamless without
103
+ * carrying ten cells per column.
104
+ */
105
+ function TickerDigit({ to, from, duration, bounce, delay, active }: ColumnProps) {
106
+ const stripRef = React.useRef<HTMLSpanElement>(null)
107
+ const nearRef = React.useRef<HTMLSpanElement>(null)
108
+ const farRef = React.useRef<HTMLSpanElement>(null)
109
+
110
+ // `useSpring` takes raw spring options, where `duration` is milliseconds, unlike a
111
+ // `Transition`, which is seconds. The public prop is seconds to match the rest of the
112
+ // tier, so convert here.
113
+ const position = useSpring(from, { duration: duration * 1000, bounce })
114
+
115
+ const paint = React.useCallback((at: number) => {
116
+ const settled = Math.floor(at)
117
+ if (nearRef.current) nearRef.current.textContent = String(digitOf(settled))
118
+ if (farRef.current) farRef.current.textContent = String(digitOf(settled + 1))
119
+ // The strip is two cells tall, so half of it is exactly one digit.
120
+ if (stripRef.current) {
121
+ stripRef.current.style.transform = `translateY(${(settled - at) * 50}%)`
122
+ }
123
+ }, [])
124
+
125
+ useMotionValueEvent(position, "change", paint)
126
+ // React owns the glyphs on the first render and after every prop change; this pulls
127
+ // them straight back to the live spring value before any of that reaches the screen.
128
+ useIsomorphicLayoutEffect(() => paint(position.get()))
129
+ useSettle(position, to, delay, active)
130
+
131
+ return (
132
+ <span className="relative inline-block h-[1.15em] overflow-hidden align-bottom">
133
+ {/* An invisible glyph sizes the window. The strip is absolutely placed so it can
134
+ be taller than the line box without stretching it. `aria-hidden` is explicit so
135
+ the guarantee does not rest on the `invisible` class surviving a restyle. */}
136
+ <span aria-hidden className="invisible block h-[1.15em] leading-[1.15em]">
137
+ 0
138
+ </span>
139
+ <span
140
+ ref={stripRef}
141
+ className="absolute inset-x-0 top-0 flex flex-col"
142
+ aria-hidden
143
+ >
144
+ <span ref={nearRef} className={CELL}>
145
+ {digitOf(from)}
146
+ </span>
147
+ <span ref={farRef} className={CELL}>
148
+ {digitOf(from + 1)}
149
+ </span>
150
+ </span>
151
+ </span>
152
+ )
153
+ }
154
+
155
+ /**
156
+ * One flap of a split-flap board, driven by the same spring as the odometer wheel and
157
+ * read as a rotation instead of an offset. The face turns a half circle between one
158
+ * digit and the next, so it is edge-on, and therefore invisible, at the exact moment the
159
+ * glyph behind it is swapped. Counting up tips the flap towards you and counting down
160
+ * tips it away, both of which fall out of the direction the spring is travelling.
161
+ */
162
+ function FlipDigit({ to, from, duration, bounce, delay, active }: ColumnProps) {
163
+ const faceRef = React.useRef<HTMLSpanElement>(null)
164
+ const position = useSpring(from, { duration: duration * 1000, bounce })
165
+
166
+ const paint = React.useCallback((at: number) => {
167
+ const face = faceRef.current
168
+ if (!face) return
169
+ // Rounding rather than flooring puts the swap at the half-way point, which is the
170
+ // one place in the turn where the face has no height to show it happening.
171
+ const settled = Math.round(at)
172
+ face.textContent = String(digitOf(settled))
173
+ face.style.transform = `rotateX(${(at - settled) * 180}deg)`
174
+ }, [])
175
+
176
+ useMotionValueEvent(position, "change", paint)
177
+ useIsomorphicLayoutEffect(() => paint(position.get()))
178
+ useSettle(position, to, delay, active)
179
+
180
+ return (
181
+ <span
182
+ className="relative inline-block h-[1.15em] overflow-hidden align-bottom"
183
+ // Without perspective the rotation is orthographic: the flap squashes vertically
184
+ // instead of turning away from you.
185
+ style={{ perspective: "260px" }}
186
+ >
187
+ <span aria-hidden className="invisible block h-[1.15em] leading-[1.15em]">
188
+ 0
189
+ </span>
190
+ <span
191
+ ref={faceRef}
192
+ aria-hidden
193
+ className="absolute inset-0 flex items-center justify-center leading-[1.15em]"
194
+ >
195
+ {digitOf(from)}
196
+ </span>
197
+ </span>
198
+ )
199
+ }
200
+
201
+ interface CounterValueProps {
202
+ value: number
203
+ from: number
204
+ duration: number
205
+ bounce: number
206
+ active: boolean
207
+ formatter: Intl.NumberFormat
208
+ }
209
+
210
+ /**
211
+ * The plain count-up: one spring on the value itself, so every frame shows a real
212
+ * number the user could read off. The wheel variants roll through digits that never
213
+ * meant anything; this one is the right choice when the intermediate values matter,
214
+ * or when the format is too involved to split into columns.
215
+ */
216
+ function CounterValue({
217
+ value,
218
+ from,
219
+ duration,
220
+ bounce,
221
+ active,
222
+ formatter,
223
+ }: CounterValueProps) {
224
+ const nodeRef = React.useRef<HTMLSpanElement>(null)
225
+ const position = useSpring(from, { duration: duration * 1000, bounce })
226
+
227
+ // Written straight to the DOM. Routing sixty frames a second through state would
228
+ // re-render the whole subtree for a string only this node reads.
229
+ const paint = React.useCallback(
230
+ (at: number) => {
231
+ if (nodeRef.current) nodeRef.current.textContent = formatter.format(at)
232
+ },
233
+ [formatter]
234
+ )
235
+
236
+ useMotionValueEvent(position, "change", paint)
237
+ useIsomorphicLayoutEffect(() => paint(position.get()))
238
+ useSettle(position, value, 0, active)
239
+
240
+ return (
241
+ <span ref={nodeRef} aria-hidden>
242
+ {formatter.format(from)}
243
+ </span>
244
+ )
245
+ }
246
+
247
+ interface Cell {
248
+ char: string
249
+ /** Null for anything that is not a digit: the grouping mark, the decimal separator,
250
+ * a minus sign. */
251
+ place: number | null
252
+ to: number
253
+ order: number
254
+ }
255
+
256
+ export interface NumberTickerProps
257
+ extends Omit<React.ComponentProps<"span">, "children"> {
258
+ /** The value to count to. */
259
+ value: number
260
+ /** Which effect to run. See each variant's note on the component. */
261
+ variant?: NumberTickerVariant
262
+ /** The value each column starts from. */
263
+ from?: number
264
+ /** Decimal places to render. */
265
+ decimals?: number
266
+ /**
267
+ * Locale passed to Intl.NumberFormat. Defaults to the user's locale.
268
+ *
269
+ * A locale that formats with non-Latin digits (`ar-EG`, `hi-IN-u-nu-deva`) renders
270
+ * and announces correctly but does not roll — the columns are found by matching
271
+ * Latin `0-9`, so nothing is recognised as a digit and the value simply updates.
272
+ */
273
+ locale?: string
274
+ /** Extra Intl.NumberFormat options, e.g. `{ style: "currency", currency: "USD" }`. */
275
+ format?: Intl.NumberFormatOptions
276
+ /** Set this when `value` is bound to something that keeps moving, like a dashboard
277
+ * metric or a cart total. The number renders instantly, with no roll. A figure that
278
+ * is mid-animation whenever you look at it cannot be read. */
279
+ live?: boolean
280
+ /** Seconds added per column, left to right. The cascade is what makes it read as a
281
+ * board settling rather than every column stopping at once. Keep it in the
282
+ * 0.03–0.08 range. Ignored by `counter`, which has no columns. */
283
+ stagger?: number
284
+ /** Seconds for a column to settle. */
285
+ duration?: number
286
+ /** Spring overshoot, 0–1. Off by default: no gesture precedes this motion, so an
287
+ * overshoot has no momentum to express and reads as the figure being briefly wrong. */
288
+ bounce?: number
289
+ /** Count when scrolled into view rather than on mount. */
290
+ startOnView?: boolean
291
+ /** Only arm the reveal the first time it enters view. Later changes to `value` still
292
+ * animate; `live` is the prop that stops that. */
293
+ once?: boolean
294
+ }
295
+
296
+ /**
297
+ * A number that animates to its value, in three flavours:
298
+ *
299
+ * - `odometer`: digit columns roll like a mechanical counter, spring driven, each column
300
+ * settling a beat after the one to its left.
301
+ * - `counter`: the value itself is interpolated and reformatted every frame, so the
302
+ * intermediate numbers are real. No per-digit DOM.
303
+ * - `flip`: a split-flap board. Each column turns over to its new digit, towards you
304
+ * when the value rises and away when it falls.
305
+ *
306
+ * The effect is a reveal, so it is budgeted as one: it earns its 0.4s the first time a
307
+ * figure arrives. A value that keeps changing only needs to be correct, so pass `live`
308
+ * and nothing moves.
309
+ *
310
+ * All three announce only the final formatted value to assistive technology. The moving
311
+ * parts are `aria-hidden`, since a number changing sixty times a second is noise in a
312
+ * screen reader.
313
+ */
314
+ const NumberTicker = React.forwardRef<HTMLSpanElement, NumberTickerProps>(
315
+ (
316
+ {
317
+ value,
318
+ variant = "odometer",
319
+ from = 0,
320
+ decimals = 0,
321
+ locale,
322
+ format,
323
+ live = false,
324
+ stagger = 0.03,
325
+ duration = DURATION,
326
+ bounce = BOUNCE,
327
+ startOnView = true,
328
+ once = true,
329
+ className,
330
+ ...props
331
+ },
332
+ ref
333
+ ) => {
334
+ const innerRef = React.useRef<HTMLSpanElement>(null)
335
+ React.useImperativeHandle(ref, () => innerRef.current as HTMLSpanElement)
336
+
337
+ const inView = useInView(innerRef, { once, margin: "0px 0px -48px 0px" })
338
+ const reducedMotion = useReducedMotionConfig()
339
+ // `isStatic` is how Motion is told to render a tree without animating it. A number
340
+ // is content, so that pass has to hold the real one, not the starting value.
341
+ const { isStatic } = React.useContext(MotionConfigContext)
342
+
343
+ const formatter = React.useMemo(
344
+ () =>
345
+ new Intl.NumberFormat(locale, {
346
+ minimumFractionDigits: decimals,
347
+ maximumFractionDigits: decimals,
348
+ ...format,
349
+ }),
350
+ [locale, decimals, format]
351
+ )
352
+ const formatted = React.useMemo(
353
+ () => formatter.format(value),
354
+ [formatter, value]
355
+ )
356
+
357
+ // Walk right to left so each digit knows its place value, then left to right to
358
+ // accumulate the digits above it. That accumulation is the column's position in an
359
+ // unwrapped space: the ones column of 1,199 sits at 1199 and of 1,200 at 1200. A
360
+ // carry is therefore one step forward rather than nine steps back, which is what a
361
+ // real wheel does and what springing the bare 0–9 digit can never express.
362
+ const cells = React.useMemo(() => {
363
+ let place = 0
364
+ const byPlace = Array.from(formatted)
365
+ .reverse()
366
+ .map<Cell>((char) => ({
367
+ char,
368
+ place: /\d/.test(char) ? place++ - decimals : null,
369
+ to: 0,
370
+ order: 0,
371
+ }))
372
+ .reverse()
373
+
374
+ let above = 0
375
+ let order = 0
376
+ for (const cell of byPlace) {
377
+ if (cell.place === null) continue
378
+ above = above * 10 + Number(cell.char)
379
+ cell.to = above
380
+ cell.order = order++
381
+ }
382
+ return byPlace
383
+ }, [formatted, decimals])
384
+
385
+ // `from` in that same unwrapped space, as an integer so no column inherits a
386
+ // rounding error from the one below it.
387
+ const fromScaled = React.useMemo(
388
+ () => Math.round(Math.abs(from) * 10 ** decimals),
389
+ [from, decimals]
390
+ )
391
+
392
+ const active = !startOnView || inView
393
+
394
+ if (live || reducedMotion || isStatic) {
395
+ return (
396
+ <span
397
+ ref={innerRef}
398
+ className={cn("inline-block tabular-nums tracking-tight", className)}
399
+ {...props}
400
+ >
401
+ {formatted}
402
+ </span>
403
+ )
404
+ }
405
+
406
+ if (variant === "counter") {
407
+ return (
408
+ <span
409
+ ref={innerRef}
410
+ className={cn("inline-block tabular-nums tracking-tight", className)}
411
+ {...props}
412
+ >
413
+ <CounterValue
414
+ value={value}
415
+ from={from}
416
+ duration={duration}
417
+ bounce={bounce}
418
+ active={active}
419
+ formatter={formatter}
420
+ />
421
+ <span className="sr-only">{formatted}</span>
422
+ </span>
423
+ )
424
+ }
425
+
426
+ return (
427
+ <span
428
+ ref={innerRef}
429
+ className={cn(
430
+ "inline-flex items-baseline tabular-nums tracking-tight",
431
+ className
432
+ )}
433
+ {...props}
434
+ >
435
+ {cells.map((cell, i) => {
436
+ if (cell.place === null) {
437
+ return (
438
+ <span key={`sep-${i}`} aria-hidden>
439
+ {cell.char}
440
+ </span>
441
+ )
442
+ }
443
+ const shared = {
444
+ to: cell.to,
445
+ from: Math.floor(fromScaled / 10 ** (cell.place + decimals)),
446
+ duration,
447
+ bounce,
448
+ // Left-most column starts first; the ones column settles last.
449
+ delay: cell.order * stagger,
450
+ active,
451
+ }
452
+ // Keyed by place so a column keeps its identity when the number gains or
453
+ // loses digits. Keyed by position, 1,200 dropping to 900 would hand the
454
+ // thousands column's spring to the hundreds and roll a settled wheel.
455
+ const key = `place-${cell.place}`
456
+ return variant === "flip" ? (
457
+ <FlipDigit key={key} {...shared} />
458
+ ) : (
459
+ <TickerDigit key={key} {...shared} />
460
+ )
461
+ })}
462
+ {/* The columns are decorative; this is what a screen reader announces. */}
463
+ <span className="sr-only">{formatted}</span>
464
+ </span>
465
+ )
466
+ }
467
+ )
468
+ NumberTicker.displayName = "NumberTicker"
469
+
470
+ export { NumberTicker }
@@ -1,7 +1,7 @@
1
1
  "use client"
2
2
 
3
3
  import * as React from "react"
4
- import { OTPFieldPreview as OTPFieldPrimitive } from "@base-ui/react/otp-field"
4
+ import { OTPField as OTPFieldPrimitive } from "@base-ui/react/otp-field"
5
5
  import { cn } from "@/lib/utils"
6
6
 
7
7
  const OTPField = React.forwardRef<
@@ -0,0 +1 @@
1
+ export * from './progress-ring'