@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.
Files changed (52) hide show
  1. package/CHANGELOG.md +144 -0
  2. package/README.md +36 -5
  3. package/dist/index.js +336 -62
  4. package/package.json +17 -14
  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/badge/badge.tsx +4 -4
  12. package/templates/carousel/carousel.tsx +492 -0
  13. package/templates/carousel/index.ts +1 -0
  14. package/templates/compare-slider/compare-slider.tsx +383 -0
  15. package/templates/compare-slider/index.ts +2 -0
  16. package/templates/expandable-card/expandable-card.tsx +509 -0
  17. package/templates/expandable-card/index.ts +1 -0
  18. package/templates/hold-to-confirm/hold-to-confirm.tsx +573 -0
  19. package/templates/hold-to-confirm/index.ts +2 -0
  20. package/templates/load-transition/index.ts +1 -0
  21. package/templates/load-transition/load-transition.tsx +163 -0
  22. package/templates/marquee/index.ts +2 -0
  23. package/templates/marquee/marquee.tsx +256 -0
  24. package/templates/number-ticker/index.ts +2 -0
  25. package/templates/number-ticker/number-ticker.tsx +470 -0
  26. package/templates/otp-field/index.ts +1 -0
  27. package/templates/otp-field/otp-field.tsx +72 -0
  28. package/templates/progress-ring/index.ts +1 -0
  29. package/templates/progress-ring/progress-ring.tsx +268 -0
  30. package/templates/scroll-progress/index.ts +2 -0
  31. package/templates/scroll-progress/scroll-progress.tsx +147 -0
  32. package/templates/scroll-reveal/index.ts +2 -0
  33. package/templates/scroll-reveal/scroll-reveal.tsx +198 -0
  34. package/templates/select/select.tsx +4 -2
  35. package/templates/slider/slider.tsx +1 -1
  36. package/templates/sortable/index.ts +1 -0
  37. package/templates/sortable/sortable.tsx +349 -0
  38. package/templates/stagger-list/index.ts +2 -0
  39. package/templates/stagger-list/stagger-list.tsx +204 -0
  40. package/templates/streaming-text/index.ts +2 -0
  41. package/templates/streaming-text/streaming-text.tsx +220 -0
  42. package/templates/swipeable-row/index.ts +2 -0
  43. package/templates/swipeable-row/swipeable-row.tsx +368 -0
  44. package/templates/text-morph/index.ts +2 -0
  45. package/templates/text-morph/text-morph.tsx +265 -0
  46. package/templates/text-shimmer/index.ts +2 -0
  47. package/templates/text-shimmer/text-shimmer.tsx +151 -0
  48. package/templates/textarea/textarea.tsx +1 -2
  49. package/templates/toast/toast.tsx +24 -19
  50. package/templates/tsconfig.json +1 -0
  51. package/LICENSE +0 -21
  52. package/templates/utils/variants.ts +0 -6
@@ -0,0 +1,492 @@
1
+ "use client"
2
+
3
+ import * as React from "react"
4
+ import {
5
+ animate,
6
+ motion,
7
+ useMotionValue,
8
+ MotionConfigContext,
9
+ useReducedMotion,
10
+ type PanInfo,
11
+ } from "motion/react"
12
+ import { cn } from "@/lib/utils"
13
+
14
+ /**
15
+ * A carousel that lands where the flick was aimed.
16
+ *
17
+ * Snap-to-nearest is the wrong rule. It measures where the finger stopped, which on a quick
18
+ * flick is barely past the slide it started on, so a decisive gesture bounces back and the
19
+ * carousel feels stuck. Projecting the release velocity forward and snapping to whichever
20
+ * slide *that* lands in gets the opposite behaviour: a flick advances, a slow drag that
21
+ * stops short does not, and both match what the hand meant.
22
+ *
23
+ * Under reduced motion the drag is switched off and this becomes an ordinary scroll-snap
24
+ * strip. That is not a downgrade — a native scroller is a better carousel than a JS one for
25
+ * anyone who did not want the momentum in the first place. The dots and the arrows keep
26
+ * working either way; they just jump instead of animating.
27
+ */
28
+
29
+ /** `useReducedMotion` reads the OS setting only, and only at mount. Also honour an
30
+ * app-level `<MotionConfig reducedMotion>`, so a user preference in the host app
31
+ * reaches this component. */
32
+ function useReducedMotionConfig() {
33
+ const config = React.useContext(MotionConfigContext)
34
+ const prefersReduced = useReducedMotion()
35
+ if (config.reducedMotion === "always") return true
36
+ if (config.reducedMotion === "never") return false
37
+ return prefersReduced ?? false
38
+ }
39
+
40
+ /** Seconds of deceleration to project forward before choosing a slide. */
41
+ const PROJECTION = 0.2
42
+
43
+ const SNAP = { type: "spring", stiffness: 380, damping: 40, mass: 0.9 } as const
44
+
45
+ interface CarouselContextValue {
46
+ index: number
47
+ count: number
48
+ goTo: (next: number, velocity?: number) => void
49
+ reducedMotion: boolean
50
+ x: ReturnType<typeof useMotionValue<number>>
51
+ offsets: number[]
52
+ setOffsets: (offsets: number[]) => void
53
+ scroller: React.RefObject<HTMLDivElement | null>
54
+ registerSlide: (position: number, node: HTMLDivElement | null) => void
55
+ /** Reads every slide's position out of the layout. Trims to `count` first, because a
56
+ * shortened list leaves stale nodes behind and a stale node still reports an offset. */
57
+ measureSlides: (count: number) => number[]
58
+ currentIndex: () => number
59
+ setIndex: (next: number) => void
60
+ }
61
+
62
+ const CarouselContext = React.createContext<CarouselContextValue | null>(null)
63
+
64
+ function useCarousel(component: string) {
65
+ const context = React.useContext(CarouselContext)
66
+ if (!context) {
67
+ throw new Error(`${component} must be used within a Carousel`)
68
+ }
69
+ return context
70
+ }
71
+
72
+ /** Position within the strip, handed down so a slide does not have to count itself. */
73
+ const CarouselSlotContext = React.createContext<number | null>(null)
74
+
75
+ export interface CarouselProps extends React.ComponentProps<"div"> {
76
+ /** Names the carousel. Screen readers read it before the slide. */
77
+ label?: string
78
+ index?: number
79
+ defaultIndex?: number
80
+ onIndexChange?: (index: number) => void
81
+ }
82
+
83
+ const Carousel = React.forwardRef<HTMLDivElement, CarouselProps>(
84
+ (
85
+ {
86
+ label = "Carousel",
87
+ index: indexProp,
88
+ defaultIndex = 0,
89
+ onIndexChange,
90
+ className,
91
+ children,
92
+ ...props
93
+ },
94
+ ref
95
+ ) => {
96
+ const reducedMotion = useReducedMotionConfig()
97
+ const [uncontrolled, setUncontrolled] = React.useState(defaultIndex)
98
+ const index = indexProp ?? uncontrolled
99
+
100
+ const x = useMotionValue(0)
101
+ const scroller = React.useRef<HTMLDivElement>(null)
102
+ const slides = React.useRef<(HTMLDivElement | null)[]>([])
103
+ const [offsets, setOffsets] = React.useState<number[]>([])
104
+ // Slides are counted by measuring them, so the dots cannot get out of step with the
105
+ // strip they control.
106
+ const count = offsets.length
107
+
108
+ // The resize and scroll handlers need to know where we are without being rebuilt
109
+ // every time we move.
110
+ const indexRef = React.useRef(index)
111
+ React.useEffect(() => {
112
+ indexRef.current = index
113
+ }, [index])
114
+
115
+ const setIndex = React.useCallback(
116
+ (next: number) => {
117
+ // Scrolling reports the same slide many times over. Only a change is news.
118
+ if (next === indexRef.current) return
119
+ indexRef.current = next
120
+ if (indexProp === undefined) setUncontrolled(next)
121
+ onIndexChange?.(next)
122
+ },
123
+ [indexProp, onIndexChange]
124
+ )
125
+
126
+ const currentIndex = React.useCallback(() => indexRef.current, [])
127
+
128
+ const registerSlide = React.useCallback(
129
+ (position: number, node: HTMLDivElement | null) => {
130
+ slides.current[position] = node
131
+ },
132
+ []
133
+ )
134
+
135
+ const measureSlides = React.useCallback((slideCount: number) => {
136
+ slides.current.length = slideCount
137
+ return slides.current.map((slide) => slide?.offsetLeft ?? 0)
138
+ }, [])
139
+
140
+ const goTo = React.useCallback(
141
+ (next: number, velocity = 0) => {
142
+ const last = offsets.length - 1
143
+ if (last < 0) return
144
+ const clamped = Math.min(Math.max(next, 0), last)
145
+ setIndex(clamped)
146
+
147
+ const to = offsets[clamped]
148
+ if (to === undefined) return
149
+
150
+ if (reducedMotion) {
151
+ scroller.current?.scrollTo({ left: to, behavior: "auto" })
152
+ return
153
+ }
154
+ animate(x, -to, { ...SNAP, velocity })
155
+ },
156
+ [offsets, setIndex, reducedMotion, x]
157
+ )
158
+
159
+ const context = React.useMemo(
160
+ () => ({
161
+ index,
162
+ count,
163
+ goTo,
164
+ reducedMotion,
165
+ x,
166
+ offsets,
167
+ setOffsets,
168
+ scroller,
169
+ registerSlide,
170
+ measureSlides,
171
+ currentIndex,
172
+ setIndex,
173
+ }),
174
+ [
175
+ index,
176
+ count,
177
+ goTo,
178
+ reducedMotion,
179
+ x,
180
+ offsets,
181
+ registerSlide,
182
+ measureSlides,
183
+ currentIndex,
184
+ setIndex,
185
+ ]
186
+ )
187
+
188
+ return (
189
+ <CarouselContext.Provider value={context}>
190
+ <div
191
+ ref={ref}
192
+ role="group"
193
+ aria-roledescription="carousel"
194
+ aria-label={label}
195
+ className={cn("space-y-4", className)}
196
+ {...props}
197
+ >
198
+ {children}
199
+ </div>
200
+ </CarouselContext.Provider>
201
+ )
202
+ }
203
+ )
204
+ Carousel.displayName = "Carousel"
205
+
206
+ export type CarouselViewportProps = React.ComponentProps<"div">
207
+
208
+ /** The window the strip moves inside. Everything measured is measured from here. */
209
+ const CarouselViewport = React.forwardRef<HTMLDivElement, CarouselViewportProps>(
210
+ ({ className, children, ...props }, ref) => {
211
+ const {
212
+ x,
213
+ offsets,
214
+ setOffsets,
215
+ goTo,
216
+ reducedMotion,
217
+ scroller,
218
+ measureSlides,
219
+ currentIndex,
220
+ setIndex,
221
+ } = useCarousel("CarouselViewport")
222
+
223
+ /**
224
+ * Where each slide actually starts, read from the DOM.
225
+ *
226
+ * The obvious shortcut is `viewport.width * 0.72 + gap`, and it is wrong the moment the
227
+ * gap comes from a class rather than a constant, or a slide is a different width, or the
228
+ * container has padding. Asking the layout where the slides are costs one measurement
229
+ * per resize and cannot drift.
230
+ */
231
+ const slideCount = React.Children.count(children)
232
+
233
+ React.useEffect(() => {
234
+ const node = scroller.current
235
+ if (!node || typeof ResizeObserver === "undefined") return
236
+
237
+ const measure = () => {
238
+ const next = measureSlides(slideCount)
239
+ setOffsets(next)
240
+
241
+ // A resize moves every slide. Without this the strip keeps its old translation and
242
+ // the carousel silently ends up sitting between two of them.
243
+ const to = next[currentIndex()]
244
+ if (to === undefined) return
245
+ if (reducedMotion) node.scrollTo({ left: to, behavior: "auto" })
246
+ else x.set(-to)
247
+ }
248
+
249
+ const observer = new ResizeObserver(measure)
250
+ observer.observe(node)
251
+ return () => observer.disconnect()
252
+ }, [
253
+ x,
254
+ reducedMotion,
255
+ scroller,
256
+ setOffsets,
257
+ measureSlides,
258
+ currentIndex,
259
+ slideCount,
260
+ ])
261
+
262
+ const positioned = React.Children.map(children, (child, i) => (
263
+ <CarouselSlotContext.Provider value={i}>{child}</CarouselSlotContext.Provider>
264
+ ))
265
+
266
+ if (reducedMotion) {
267
+ return (
268
+ <div
269
+ ref={mergeRefs(ref, scroller)}
270
+ // Under reduced motion the browser's own scroller does the work, so the only
271
+ // thing left to do is keep the dots honest about where it stopped.
272
+ onScroll={(event) => {
273
+ if (offsets.length === 0) return
274
+ setIndex(nearest(offsets, event.currentTarget.scrollLeft))
275
+ }}
276
+ className={cn(
277
+ "relative flex snap-x snap-mandatory gap-3 overflow-x-auto pb-2",
278
+ className
279
+ )}
280
+ {...props}
281
+ >
282
+ {positioned}
283
+ </div>
284
+ )
285
+ }
286
+
287
+ const onDragEnd = (_: unknown, info: PanInfo) => {
288
+ if (offsets.length === 0) return
289
+
290
+ const projected = -(x.get() + info.velocity.x * PROJECTION)
291
+ // Nearest by measured position rather than by dividing through a nominal slide width,
292
+ // so uneven slides land as accurately as even ones.
293
+ goTo(nearest(offsets, projected), info.velocity.x)
294
+ }
295
+
296
+ return (
297
+ <div
298
+ ref={mergeRefs(ref, scroller)}
299
+ className={cn("overflow-hidden", className)}
300
+ {...props}
301
+ >
302
+ <motion.div
303
+ drag="x"
304
+ style={{ x }}
305
+ dragConstraints={{ left: -(offsets[offsets.length - 1] ?? 0), right: 0 }}
306
+ // Give at both ends rather than a wall, so the edge of the set is something you
307
+ // feel instead of something you hit.
308
+ dragElastic={0.16}
309
+ dragMomentum={false}
310
+ onDragEnd={onDragEnd}
311
+ // `relative` makes the strip the offset parent, so each slide's `offsetLeft` is
312
+ // its position within the strip rather than within the page.
313
+ className="relative flex cursor-grab touch-pan-y gap-3 active:cursor-grabbing"
314
+ >
315
+ {positioned}
316
+ </motion.div>
317
+ </div>
318
+ )
319
+ }
320
+ )
321
+ CarouselViewport.displayName = "CarouselViewport"
322
+
323
+ export interface CarouselSlideProps extends React.ComponentProps<"div"> {
324
+ /** Names this slide. Falls back to its position in the set. */
325
+ label?: string
326
+ }
327
+
328
+ const CarouselSlide = React.forwardRef<HTMLDivElement, CarouselSlideProps>(
329
+ ({ label, className, ...props }, ref) => {
330
+ const { count, registerSlide } = useCarousel("CarouselSlide")
331
+ const position = React.useContext(CarouselSlotContext)
332
+ if (position === null) {
333
+ throw new Error("CarouselSlide must be used within a CarouselViewport")
334
+ }
335
+
336
+ const counted = `${position + 1} of ${count || position + 1}`
337
+
338
+ return (
339
+ <div
340
+ ref={mergeRefs(ref, (node: HTMLDivElement | null) => {
341
+ registerSlide(position, node)
342
+ })}
343
+ role="group"
344
+ aria-roledescription="slide"
345
+ aria-label={label ? `${counted}: ${label}` : counted}
346
+ className={cn("w-[72%] shrink-0 snap-start", className)}
347
+ {...props}
348
+ />
349
+ )
350
+ }
351
+ )
352
+ CarouselSlide.displayName = "CarouselSlide"
353
+
354
+ export interface CarouselDotsProps extends React.ComponentProps<"div"> {
355
+ /** Slide names, in order. Used in each dot's accessible name. */
356
+ labels?: string[]
357
+ dotClassName?: string
358
+ }
359
+
360
+ /**
361
+ * Not tabs.
362
+ *
363
+ * A tab controls a panel that appears in its place; these move a strip that is already
364
+ * fully present, and calling them tabs promises a `tabpanel` relationship that does not
365
+ * exist. They are buttons that jump.
366
+ */
367
+ const CarouselDots = React.forwardRef<HTMLDivElement, CarouselDotsProps>(
368
+ ({ labels, dotClassName, className, ...props }, ref) => {
369
+ const { index, count, goTo } = useCarousel("CarouselDots")
370
+
371
+ return (
372
+ <div
373
+ ref={ref}
374
+ role="group"
375
+ aria-label="Choose a slide"
376
+ className={cn("flex gap-1.5", className)}
377
+ {...props}
378
+ >
379
+ {Array.from({ length: count }, (_, i) => (
380
+ <button
381
+ key={i}
382
+ type="button"
383
+ aria-label={
384
+ labels?.[i] ? `Go to slide ${i + 1}: ${labels[i]}` : `Go to slide ${i + 1}`
385
+ }
386
+ aria-current={i === index ? "true" : undefined}
387
+ onClick={() => goTo(i)}
388
+ className={cn(
389
+ "h-1.5 rounded-full transition-all duration-200 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 focus-visible:ring-offset-background",
390
+ i === index ? "w-6 bg-primary" : "w-1.5 bg-border hover:bg-muted-foreground",
391
+ dotClassName
392
+ )}
393
+ />
394
+ ))}
395
+ </div>
396
+ )
397
+ }
398
+ )
399
+ CarouselDots.displayName = "CarouselDots"
400
+
401
+ export type CarouselArrowProps = React.ComponentProps<"button">
402
+
403
+ const CarouselPrevious = React.forwardRef<HTMLButtonElement, CarouselArrowProps>(
404
+ ({ className, children, ...props }, ref) => {
405
+ const { index, goTo } = useCarousel("CarouselPrevious")
406
+
407
+ return (
408
+ <button
409
+ ref={ref}
410
+ type="button"
411
+ aria-label="Previous slide"
412
+ disabled={index === 0}
413
+ onClick={() => goTo(index - 1)}
414
+ className={cn(arrowClass, className)}
415
+ {...props}
416
+ >
417
+ {children ?? <ChevronIcon direction="left" />}
418
+ </button>
419
+ )
420
+ }
421
+ )
422
+ CarouselPrevious.displayName = "CarouselPrevious"
423
+
424
+ const CarouselNext = React.forwardRef<HTMLButtonElement, CarouselArrowProps>(
425
+ ({ className, children, ...props }, ref) => {
426
+ const { index, count, goTo } = useCarousel("CarouselNext")
427
+
428
+ return (
429
+ <button
430
+ ref={ref}
431
+ type="button"
432
+ aria-label="Next slide"
433
+ disabled={count === 0 || index === count - 1}
434
+ onClick={() => goTo(index + 1)}
435
+ className={cn(arrowClass, className)}
436
+ {...props}
437
+ >
438
+ {children ?? <ChevronIcon direction="right" />}
439
+ </button>
440
+ )
441
+ }
442
+ )
443
+ CarouselNext.displayName = "CarouselNext"
444
+
445
+ const arrowClass =
446
+ "rounded-lg border border-border p-1.5 text-foreground transition-opacity focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring disabled:opacity-35"
447
+
448
+ /** Inlined so the carousel installs with nothing but `motion`. */
449
+ function ChevronIcon({ direction }: { direction: "left" | "right" }) {
450
+ return (
451
+ <svg
452
+ aria-hidden
453
+ viewBox="0 0 24 24"
454
+ fill="none"
455
+ stroke="currentColor"
456
+ strokeWidth={2}
457
+ strokeLinecap="round"
458
+ strokeLinejoin="round"
459
+ className="h-4 w-4"
460
+ >
461
+ <path d={direction === "left" ? "M15 18l-6-6 6-6" : "M9 18l6-6-6-6"} />
462
+ </svg>
463
+ )
464
+ }
465
+
466
+ function nearest(offsets: number[], target: number) {
467
+ let closest = 0
468
+ for (let i = 1; i < offsets.length; i += 1) {
469
+ if (Math.abs(offsets[i] - target) < Math.abs(offsets[closest] - target)) {
470
+ closest = i
471
+ }
472
+ }
473
+ return closest
474
+ }
475
+
476
+ function mergeRefs<T>(...refs: (React.Ref<T> | undefined)[]): React.RefCallback<T> {
477
+ return (value) => {
478
+ for (const ref of refs) {
479
+ if (typeof ref === "function") ref(value)
480
+ else if (ref) (ref as React.RefObject<T | null>).current = value
481
+ }
482
+ }
483
+ }
484
+
485
+ export {
486
+ Carousel,
487
+ CarouselViewport,
488
+ CarouselSlide,
489
+ CarouselDots,
490
+ CarouselPrevious,
491
+ CarouselNext,
492
+ }
@@ -0,0 +1 @@
1
+ export * from './carousel'