@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,163 @@
1
+ "use client"
2
+
3
+ import * as React from "react"
4
+ import {
5
+ AnimatePresence,
6
+ motion,
7
+ MotionConfigContext,
8
+ useReducedMotion,
9
+ type Transition,
10
+ } from "motion/react"
11
+ import { cn } from "@/lib/utils"
12
+
13
+ /**
14
+ * The handover from skeleton to content.
15
+ *
16
+ * Almost nobody treats this as an animation, which is why it is the jankiest moment in most
17
+ * apps: the skeleton is removed, the real content is inserted at a different height, and
18
+ * everything below it jumps. The skeleton was supposed to be preventing exactly that.
19
+ *
20
+ * Two things fix it. The container animates its own height across the swap, so the page
21
+ * below settles instead of snapping — and because that is motion's layout animation, the
22
+ * height change is applied as a transform with a scale correction on the children rather
23
+ * than as a real height tween, so it stays off the main thread. The skeleton then leaves on
24
+ * a shorter clock than the content arrives on, so there is never a frame with both at full
25
+ * strength and never one with neither.
26
+ *
27
+ * The third thing is not motion at all: a skeleton that appears and disappears inside 150ms
28
+ * is a flash, and a flash is worse than the wait it was hiding. See `useSkeletonVisibility`.
29
+ */
30
+
31
+ /** `useReducedMotion` reads the OS setting only, and only at mount. Also honour an
32
+ * app-level `<MotionConfig reducedMotion>`, so a user preference in the host app
33
+ * reaches this component. */
34
+ function useReducedMotionConfig() {
35
+ const config = React.useContext(MotionConfigContext)
36
+ const prefersReduced = useReducedMotion()
37
+ if (config.reducedMotion === "always") return true
38
+ if (config.reducedMotion === "never") return false
39
+ return prefersReduced ?? false
40
+ }
41
+
42
+ const RESIZE: Transition = { type: "spring", bounce: 0, duration: 0.4 }
43
+
44
+ export interface LoadTransitionProps
45
+ extends Omit<React.ComponentProps<typeof motion.div>, "children"> {
46
+ loading: boolean
47
+ skeleton: React.ReactNode
48
+ /** Milliseconds of loading before the skeleton appears at all. */
49
+ delay?: number
50
+ /** Milliseconds the skeleton is held once shown, so it cannot flash. */
51
+ minimum?: number
52
+ /** Corner radius in px. An inline value, not a class — see below. */
53
+ radius?: number
54
+ children?: React.ReactNode
55
+ }
56
+
57
+ const LoadTransition = React.forwardRef<HTMLDivElement, LoadTransitionProps>(
58
+ (
59
+ {
60
+ loading,
61
+ skeleton,
62
+ delay = 180,
63
+ minimum = 420,
64
+ radius = 12,
65
+ className,
66
+ style,
67
+ children,
68
+ ...props
69
+ },
70
+ ref
71
+ ) => {
72
+ const reducedMotion = useReducedMotionConfig()
73
+ const showSkeleton = useSkeletonVisibility(loading, delay, minimum)
74
+
75
+ return (
76
+ <motion.div
77
+ ref={ref}
78
+ layout
79
+ transition={reducedMotion ? { duration: 0 } : RESIZE}
80
+ // Radius as an inline style, not a class: motion can only counteract the distortion
81
+ // its own scale introduces on a value it is animating, and a class is invisible to
82
+ // it. As a class the corners visibly stretch during the resize.
83
+ style={{ borderRadius: radius, ...style }}
84
+ className={cn(
85
+ "relative overflow-hidden border border-border bg-background p-4",
86
+ className
87
+ )}
88
+ {...props}
89
+ >
90
+ <AnimatePresence mode="popLayout" initial={false}>
91
+ {showSkeleton ? (
92
+ <motion.div
93
+ key="skeleton"
94
+ // `layout` on the child too, so the parent's scale is undone here rather than
95
+ // squashing the rows inside it.
96
+ layout
97
+ initial={{ opacity: 1 }}
98
+ animate={{ opacity: 1 }}
99
+ // Out fast. A skeleton lingering over the real text is the one thing worse
100
+ // than a hard swap.
101
+ exit={{ opacity: 0 }}
102
+ transition={{ duration: reducedMotion ? 0 : 0.12 }}
103
+ >
104
+ {skeleton}
105
+ </motion.div>
106
+ ) : (
107
+ <motion.div
108
+ key="content"
109
+ layout
110
+ // `y` is present in both branches so reduced motion drops the travel rather
111
+ // than animating it from `undefined`.
112
+ initial={reducedMotion ? { opacity: 0, y: 0 } : { opacity: 0, y: 6 }}
113
+ animate={{ opacity: 1, y: 0 }}
114
+ exit={{ opacity: 0, y: 0 }}
115
+ transition={{
116
+ duration: reducedMotion ? 0 : 0.26,
117
+ ease: [0.23, 1, 0.32, 1],
118
+ }}
119
+ >
120
+ {children}
121
+ </motion.div>
122
+ )}
123
+ </AnimatePresence>
124
+ </motion.div>
125
+ )
126
+ }
127
+ )
128
+ LoadTransition.displayName = "LoadTransition"
129
+
130
+ /**
131
+ * When the skeleton is actually on screen, which is not the same as when the data is
132
+ * loading.
133
+ *
134
+ * Two thresholds, and they solve opposite problems. Nothing shows for the first `delay`,
135
+ * because a request that returns in 120ms should go straight to content — a placeholder
136
+ * shown for two frames reads as a glitch, not as feedback. Once shown it stays for at least
137
+ * `minimum`, because a skeleton that appears and vanishes immediately is the same flash
138
+ * arriving from the other direction.
139
+ */
140
+ function useSkeletonVisibility(loading: boolean, delay = 180, minimum = 420) {
141
+ const [visible, setVisible] = React.useState(false)
142
+ const shownAt = React.useRef(0)
143
+
144
+ React.useEffect(() => {
145
+ if (loading) {
146
+ if (visible) return
147
+ const timer = setTimeout(() => {
148
+ shownAt.current = Date.now()
149
+ setVisible(true)
150
+ }, delay)
151
+ return () => clearTimeout(timer)
152
+ }
153
+
154
+ if (!visible) return
155
+ const held = Date.now() - shownAt.current
156
+ const timer = setTimeout(() => setVisible(false), Math.max(0, minimum - held))
157
+ return () => clearTimeout(timer)
158
+ }, [loading, visible, delay, minimum])
159
+
160
+ return visible
161
+ }
162
+
163
+ export { LoadTransition, useSkeletonVisibility }
@@ -0,0 +1,2 @@
1
+ export { Marquee } from './marquee'
2
+ export type { MarqueeProps } from './marquee'
@@ -0,0 +1,256 @@
1
+ "use client"
2
+
3
+ import * as React from "react"
4
+ import { MotionConfigContext, useReducedMotion } from "motion/react"
5
+ import { cn } from "@/lib/utils"
6
+
7
+ /** `useReducedMotion` reads the OS setting only, and only at mount. Also honour an
8
+ * app-level `<MotionConfig reducedMotion>`, so a user preference in the host app
9
+ * reaches this component. */
10
+ function useReducedMotionConfig() {
11
+ const config = React.useContext(MotionConfigContext)
12
+ const prefersReduced = useReducedMotion()
13
+ if (config.reducedMotion === "always") return true
14
+ if (config.reducedMotion === "never") return false
15
+ return prefersReduced
16
+ }
17
+
18
+ /** Hovering is a request to read, so the strip clears out of the way at press-feedback
19
+ * speed. Resuming answers nothing the reader is waiting on and takes the slower half of
20
+ * the asymmetry, so the strip does not snap back into their eye. */
21
+ const PAUSE_MS = 160
22
+ const RESUME_MS = 240
23
+
24
+ const EDGE_MASK =
25
+ "linear-gradient(90deg, transparent, #000 12%, #000 88%, transparent)"
26
+
27
+ export interface MarqueeProps extends React.ComponentProps<"div"> {
28
+ /** Seconds for one full pass of the content. */
29
+ duration?: number
30
+ /** Travel direction. */
31
+ direction?: "left" | "right"
32
+ /** Pause while hovered. Focus pauses regardless, so a keyboard user never loses the
33
+ * element they are on. */
34
+ pauseOnHover?: boolean
35
+ /** Fade the leading and trailing edges. Applies only while the strip is moving. */
36
+ fade?: boolean
37
+ /** Names the strip once reduced motion turns it into a focusable scroller. */
38
+ label?: string
39
+ }
40
+
41
+ /**
42
+ * A strip that scrolls its content on a seamless loop.
43
+ *
44
+ * The loop runs only when it has something to say. Content narrower than the container
45
+ * is already fully readable, so it sits still instead of sliding a gap past the reader,
46
+ * and the strip stops whenever it is scrolled out of view. Under a reduced-motion
47
+ * preference it becomes an ordinary horizontal scroller: the overflow stays reachable by
48
+ * pointer and by keyboard, it just no longer moves on its own.
49
+ *
50
+ * Travel is handed to the compositor through the Web Animations API rather than driven
51
+ * per frame from React, because this is ambient motion that runs for the life of the
52
+ * page — a busy main thread must not be able to make it stutter.
53
+ */
54
+ const Marquee = React.forwardRef<HTMLDivElement, MarqueeProps>(
55
+ (
56
+ {
57
+ duration = 24,
58
+ direction = "left",
59
+ label = "Scrolling content",
60
+ pauseOnHover = true,
61
+ fade = true,
62
+ className,
63
+ children,
64
+ ...props
65
+ },
66
+ ref
67
+ ) => {
68
+ const reducedMotion = useReducedMotionConfig()
69
+
70
+ const containerRef = React.useRef<HTMLDivElement>(null)
71
+ const laneRef = React.useRef<HTMLDivElement>(null)
72
+ const trackRef = React.useRef<HTMLDivElement>(null)
73
+ React.useImperativeHandle(ref, () => containerRef.current!, [])
74
+
75
+ /** One track's width, or 0 while the content still fits. The loop distance and the
76
+ * "is there anything left to reveal" test are the same measurement. */
77
+ const [distance, setDistance] = React.useState(0)
78
+
79
+ React.useEffect(() => {
80
+ const container = containerRef.current
81
+ const track = trackRef.current
82
+ if (!container || !track || typeof ResizeObserver === "undefined") return
83
+
84
+ const observer = new ResizeObserver(() => {
85
+ const width = track.getBoundingClientRect().width
86
+ setDistance(width > container.clientWidth ? width : 0)
87
+ })
88
+ observer.observe(container)
89
+ observer.observe(track)
90
+ return () => observer.disconnect()
91
+ }, [])
92
+
93
+ const running = distance > 0 && !reducedMotion
94
+
95
+ const animationRef = React.useRef<Animation | null>(null)
96
+ const progressRef = React.useRef(0)
97
+ const rateRef = React.useRef(1)
98
+ const visibleRef = React.useRef(true)
99
+ const rampRef = React.useRef(0)
100
+
101
+ React.useEffect(() => {
102
+ const lane = laneRef.current
103
+ if (!lane || !running) return
104
+
105
+ const total = duration * 1000
106
+ const from = { transform: "translateX(0px)" }
107
+ const to = { transform: `translateX(${-distance}px)` }
108
+ const animation = lane.animate(
109
+ direction === "left" ? [from, to] : [to, from],
110
+ { duration: total, iterations: Infinity, easing: "linear" }
111
+ )
112
+ // A resize or a prop change rebuilds the animation; carrying the progress across
113
+ // keeps the strip from snapping back to the top of the loop mid-pass.
114
+ animation.currentTime = progressRef.current * total
115
+ animation.playbackRate = rateRef.current
116
+ if (!visibleRef.current) animation.pause()
117
+ animationRef.current = animation
118
+
119
+ return () => {
120
+ const elapsed = animation.currentTime
121
+ progressRef.current =
122
+ typeof elapsed === "number" ? (elapsed / total) % 1 : 0
123
+ // A ramp in flight is still writing `playbackRate` to this animation frame by
124
+ // frame. Left running it would spend the rest of its span walking a cancelled
125
+ // animation's rate while the replacement above sat at whatever it was built
126
+ // with — a strip braked mid-hover and then resized would never finish stopping.
127
+ cancelAnimationFrame(rampRef.current)
128
+ animation.cancel()
129
+ animationRef.current = null
130
+ }
131
+ }, [running, distance, duration, direction])
132
+
133
+ React.useEffect(() => {
134
+ const container = containerRef.current
135
+ if (!container || !running || typeof IntersectionObserver === "undefined")
136
+ return
137
+
138
+ // Off-screen the loop is spending battery on something nobody can see.
139
+ const observer = new IntersectionObserver(([entry]) => {
140
+ visibleRef.current = entry.isIntersecting
141
+ if (entry.isIntersecting) animationRef.current?.play()
142
+ else animationRef.current?.pause()
143
+ })
144
+ observer.observe(container)
145
+ return () => observer.disconnect()
146
+ }, [running])
147
+
148
+ const rampTo = React.useCallback((target: number) => {
149
+ rateRef.current = target
150
+ const animation = animationRef.current
151
+ if (!animation) return
152
+
153
+ cancelAnimationFrame(rampRef.current)
154
+ const start = animation.playbackRate
155
+ if (start === target) return
156
+
157
+ // A ticker that stops dead reads as a dropped frame rather than as a pause, so the
158
+ // playback rate is walked down instead. Linear, like the travel it is braking.
159
+ const startedAt = performance.now()
160
+ const span = target === 0 ? PAUSE_MS : RESUME_MS
161
+ const step = (now: number) => {
162
+ const progress = Math.min((now - startedAt) / span, 1)
163
+ animation.playbackRate = start + (target - start) * progress
164
+ if (progress < 1) rampRef.current = requestAnimationFrame(step)
165
+ }
166
+ rampRef.current = requestAnimationFrame(step)
167
+ }, [])
168
+
169
+ React.useEffect(() => () => cancelAnimationFrame(rampRef.current), [])
170
+
171
+ const hoveredRef = React.useRef(false)
172
+ const focusedRef = React.useRef(false)
173
+ const syncRate = () =>
174
+ rampTo(hoveredRef.current || focusedRef.current ? 0 : 1)
175
+
176
+ const setHovered = (
177
+ hovered: boolean,
178
+ event: React.PointerEvent<HTMLDivElement>
179
+ ) => {
180
+ // A tap fires pointerenter with no matching pointerleave, which would strand the
181
+ // strip until the next tap somewhere else.
182
+ if (!pauseOnHover || event.pointerType === "touch") return
183
+ hoveredRef.current = hovered
184
+ syncRate()
185
+ }
186
+
187
+ const setFocused = (focused: boolean) => {
188
+ focusedRef.current = focused
189
+ syncRate()
190
+ }
191
+
192
+ const scrollable = reducedMotion && distance > 0
193
+
194
+ return (
195
+ <div
196
+ ref={containerRef}
197
+ {...props}
198
+ className={cn(
199
+ "group flex w-full",
200
+ scrollable ? "overflow-x-auto" : "overflow-hidden",
201
+ className
202
+ )}
203
+ style={
204
+ running && fade
205
+ ? { maskImage: EDGE_MASK, WebkitMaskImage: EDGE_MASK, ...props.style }
206
+ : props.style
207
+ }
208
+ // Nothing carries the overflow into view on its own here, so the reader has to be
209
+ // able to pan it themselves — including from the keyboard. A focusable element
210
+ // needs a name and a role to be one, so the scroller is announced as a region.
211
+ tabIndex={scrollable ? 0 : props.tabIndex}
212
+ role={scrollable ? (props.role ?? "region") : props.role}
213
+ aria-label={scrollable ? (props['aria-label'] ?? label) : props['aria-label']}
214
+ // Declared after the spread and composed rather than replaced: a consumer that
215
+ // wants to know about hover must not be able to switch off the pause that keeps
216
+ // the strip still while a keyboard user is reading it.
217
+ onPointerEnter={(event) => {
218
+ props.onPointerEnter?.(event)
219
+ setHovered(true, event)
220
+ }}
221
+ onPointerLeave={(event) => {
222
+ props.onPointerLeave?.(event)
223
+ setHovered(false, event)
224
+ }}
225
+ onFocusCapture={(event) => {
226
+ props.onFocusCapture?.(event)
227
+ setFocused(true)
228
+ }}
229
+ onBlurCapture={(event) => {
230
+ props.onBlurCapture?.(event)
231
+ setFocused(false)
232
+ }}
233
+ >
234
+ <div ref={laneRef} className="flex min-w-full shrink-0">
235
+ <div ref={trackRef} className="flex shrink-0 items-center gap-4 pr-4">
236
+ {children}
237
+ </div>
238
+ {running && (
239
+ /* The duplicate makes the loop seamless. `inert` keeps its copies of any
240
+ links out of the tab order as well as out of the accessibility tree. */
241
+ <div
242
+ aria-hidden
243
+ inert
244
+ className="flex shrink-0 items-center gap-4 pr-4"
245
+ >
246
+ {children}
247
+ </div>
248
+ )}
249
+ </div>
250
+ </div>
251
+ )
252
+ }
253
+ )
254
+ Marquee.displayName = "Marquee"
255
+
256
+ export { Marquee }
@@ -0,0 +1,2 @@
1
+ export { NumberTicker } from './number-ticker'
2
+ export type { NumberTickerProps, NumberTickerVariant } from './number-ticker'