@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,509 @@
1
+ "use client"
2
+
3
+ import * as React from "react"
4
+ import { createPortal } from "react-dom"
5
+ import {
6
+ AnimatePresence,
7
+ motion,
8
+ MotionConfigContext,
9
+ useReducedMotion,
10
+ type Transition,
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 ?? false
23
+ }
24
+
25
+ const FOCUSABLE =
26
+ 'a[href], button:not([disabled]), textarea, input, select, [tabindex]:not([tabindex="-1"])'
27
+
28
+ interface ExpandableCardContextValue {
29
+ open: boolean
30
+ setOpen: (open: boolean) => void
31
+ /** Pairs an element in the card with its counterpart in the panel. */
32
+ layoutId: (part: string) => string
33
+ transition: Transition
34
+ reducedMotion: boolean
35
+ }
36
+
37
+ const ExpandableCardContext =
38
+ React.createContext<ExpandableCardContextValue | null>(null)
39
+
40
+ function useExpandableCard(component: string) {
41
+ const context = React.useContext(ExpandableCardContext)
42
+ if (!context) {
43
+ throw new Error(`${component} must be used within an ExpandableCard`)
44
+ }
45
+ return context
46
+ }
47
+
48
+ export interface ExpandableCardProps extends React.ComponentProps<"div"> {
49
+ open?: boolean
50
+ defaultOpen?: boolean
51
+ onOpenChange?: (open: boolean) => void
52
+ }
53
+
54
+ /**
55
+ * A card that opens into its own detail view.
56
+ *
57
+ * This is the one interaction in the tier where the motion does something a cut cannot: it
58
+ * establishes that the panel you are now reading *is* the card you tapped. Two elements
59
+ * sharing a `layoutId` are one object to the layout engine, so the card does not fade out
60
+ * while a dialog fades in — it travels, and the reader never loses track of what they are
61
+ * looking at.
62
+ *
63
+ * It is also the easiest thing here to overuse. A shared transition on a grid of twelve
64
+ * cards is twelve chances to sit through half a second of animation to read a paragraph. It
65
+ * earns its place on a hero item, or on a list short enough that opening one is a decision
66
+ * rather than a habit.
67
+ *
68
+ * The panel is a real modal, which is a larger commitment than it looks: `aria-modal` is a
69
+ * promise that the rest of the page is unreachable, and a dialog that says it and does not
70
+ * honour it is worse than one that never claimed to be modal. So the whole set is here —
71
+ * Escape, a focus trap, focus restored to the card that opened it, the card made inert, and
72
+ * the page held still underneath.
73
+ */
74
+ const ExpandableCard = React.forwardRef<HTMLDivElement, ExpandableCardProps>(
75
+ ({ open: openProp, defaultOpen = false, onOpenChange, className, ...props }, ref) => {
76
+ const reducedMotion = useReducedMotionConfig()
77
+ const [uncontrolled, setUncontrolled] = React.useState(defaultOpen)
78
+ const open = openProp ?? uncontrolled
79
+
80
+ const setOpen = React.useCallback(
81
+ (next: boolean) => {
82
+ if (openProp === undefined) setUncontrolled(next)
83
+ onOpenChange?.(next)
84
+ },
85
+ [openProp, onOpenChange]
86
+ )
87
+
88
+ // Scopes the shared layout ids to this card, so a grid of them does not have every
89
+ // card trying to travel to the same panel.
90
+ const scope = React.useId()
91
+ const layoutId = React.useCallback(
92
+ (part: string) => `${scope}-${part}`,
93
+ [scope]
94
+ )
95
+
96
+ // Longer than the tier's 300ms rule, deliberately: that rule is about elements moving a
97
+ // short distance, and this object crosses most of the viewport. Land it too fast and
98
+ // the travel reads as a cut.
99
+ const transition = React.useMemo<Transition>(
100
+ () =>
101
+ reducedMotion
102
+ ? { duration: 0 }
103
+ : { type: "spring", bounce: 0.12, duration: 0.42 },
104
+ [reducedMotion]
105
+ )
106
+
107
+ const context = React.useMemo(
108
+ () => ({ open, setOpen, layoutId, transition, reducedMotion }),
109
+ [open, setOpen, layoutId, transition, reducedMotion]
110
+ )
111
+
112
+ return (
113
+ <ExpandableCardContext.Provider value={context}>
114
+ <div ref={ref} className={cn("relative", className)} {...props} />
115
+ </ExpandableCardContext.Provider>
116
+ )
117
+ }
118
+ )
119
+ ExpandableCard.displayName = "ExpandableCard"
120
+
121
+ export type ExpandableCardTriggerProps = React.ComponentProps<typeof motion.button>
122
+
123
+ /** The card itself. It is the thing that travels, so it is a button, not a wrapper. */
124
+ const ExpandableCardTrigger = React.forwardRef<
125
+ HTMLButtonElement,
126
+ ExpandableCardTriggerProps
127
+ >(({ className, onClick, ...props }, ref) => {
128
+ const { open, setOpen, layoutId, transition } =
129
+ useExpandableCard("ExpandableCardTrigger")
130
+
131
+ return (
132
+ <motion.button
133
+ ref={ref}
134
+ type="button"
135
+ layoutId={layoutId("card")}
136
+ transition={transition}
137
+ aria-haspopup="dialog"
138
+ aria-expanded={open}
139
+ // Inert rather than merely covered. A dialog the reader can still tab into is not
140
+ // modal, whatever the attribute says.
141
+ inert={open || undefined}
142
+ onClick={(event) => {
143
+ onClick?.(event)
144
+ if (event.defaultPrevented) return
145
+ setOpen(true)
146
+ }}
147
+ className={cn(
148
+ "block w-full overflow-hidden rounded-2xl border border-border bg-background text-left focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring",
149
+ className
150
+ )}
151
+ {...props}
152
+ />
153
+ )
154
+ })
155
+ ExpandableCardTrigger.displayName = "ExpandableCardTrigger"
156
+
157
+ export interface ExpandableCardPanelProps
158
+ extends Omit<React.ComponentProps<typeof motion.div>, "children"> {
159
+ /** Accessible name for the dialog. It is the card's title, said once. */
160
+ title: string
161
+ /** Set false to supply your own close control inside the panel. */
162
+ showClose?: boolean
163
+ /**
164
+ * Fill the viewport rather than sit in the middle of it.
165
+ *
166
+ * The card travels the same way; it just lands on all four edges. Content taller than the
167
+ * screen scrolls, which a centred panel never has to do, since that one is sized by what
168
+ * is in it.
169
+ */
170
+ fullscreen?: boolean
171
+ closeLabel?: string
172
+ backdropClassName?: string
173
+ /** Where the overlay is portalled. Defaults to `document.body`. */
174
+ container?: Element | null
175
+ children?: React.ReactNode
176
+ }
177
+
178
+ /**
179
+ * A centred panel is as tall as its contents, so it never scrolls. A full-screen one is as
180
+ * tall as the screen, and the page underneath is locked, so without a scroller here
181
+ * anything past the fold is unreachable.
182
+ */
183
+ function PanelBody({
184
+ fullscreen,
185
+ children,
186
+ }: {
187
+ fullscreen: boolean
188
+ children: React.ReactNode
189
+ }) {
190
+ if (!fullscreen) return <>{children}</>
191
+
192
+ return (
193
+ <div className="min-h-0 flex-1 overflow-y-auto overscroll-contain">{children}</div>
194
+ )
195
+ }
196
+
197
+ /** Outside the scroller, so it holds its corner while the body moves under it. */
198
+ function PanelClose({
199
+ fullscreen,
200
+ label,
201
+ onClick,
202
+ }: {
203
+ fullscreen: boolean
204
+ label: string
205
+ onClick: () => void
206
+ }) {
207
+ return (
208
+ <button
209
+ type="button"
210
+ onClick={onClick}
211
+ aria-label={label}
212
+ className={cn(
213
+ "absolute rounded-full bg-background/70 p-1.5 text-foreground backdrop-blur transition-colors hover:bg-background focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring",
214
+ fullscreen ? "right-4 top-4" : "right-3 top-3"
215
+ )}
216
+ >
217
+ <CloseIcon />
218
+ </button>
219
+ )
220
+ }
221
+
222
+ /**
223
+ * No radius, no border and no shadow when full screen: all three describe an edge, and a
224
+ * panel on all four edges of the screen has none to describe.
225
+ */
226
+ function panelSurface(fullscreen: boolean): string {
227
+ return fullscreen
228
+ ? "flex h-full w-full flex-col"
229
+ : "w-full max-w-md rounded-2xl border border-border shadow-2xl"
230
+ }
231
+
232
+ /** The opened card. Mounted only while open, so the shared layout has somewhere to land. */
233
+ const ExpandableCardPanel = React.forwardRef<HTMLDivElement, ExpandableCardPanelProps>(
234
+ (
235
+ {
236
+ title,
237
+ showClose = true,
238
+ fullscreen = false,
239
+ closeLabel = "Close",
240
+ backdropClassName,
241
+ container,
242
+ className,
243
+ children,
244
+ ...props
245
+ },
246
+ ref
247
+ ) => {
248
+ const { open, setOpen, layoutId, transition } =
249
+ useExpandableCard("ExpandableCardPanel")
250
+
251
+ const panel = React.useRef<HTMLDivElement>(null)
252
+ const close = React.useCallback(() => setOpen(false), [setOpen])
253
+
254
+ useModalBehaviour(open, panel, close)
255
+
256
+ const target = usePortalTarget(container)
257
+ if (!target) return null
258
+
259
+ return createPortal(
260
+ <AnimatePresence>
261
+ {open ? (
262
+ <>
263
+ <motion.div
264
+ aria-hidden
265
+ initial={{ opacity: 0 }}
266
+ animate={{ opacity: 1 }}
267
+ exit={{ opacity: 0 }}
268
+ transition={{ duration: 0.2 }}
269
+ onClick={close}
270
+ className={cn(
271
+ "fixed inset-0 z-40 bg-background/80 backdrop-blur-sm",
272
+ backdropClassName
273
+ )}
274
+ />
275
+ {/* The centring frame does not intercept the backdrop's clicks; only the panel
276
+ inside it takes pointer events. Its padding is what keeps a centred panel
277
+ off the edges, so a full-screen one cannot have it. */}
278
+ <div
279
+ className={cn(
280
+ "pointer-events-none fixed inset-0 z-50 flex items-center justify-center",
281
+ !fullscreen && "p-4"
282
+ )}
283
+ >
284
+ <motion.div
285
+ ref={mergeRefs(ref, panel)}
286
+ layoutId={layoutId("card")}
287
+ transition={transition}
288
+ role="dialog"
289
+ aria-modal="true"
290
+ aria-label={title}
291
+ className={cn(
292
+ "pointer-events-auto relative overflow-hidden bg-background",
293
+ panelSurface(fullscreen),
294
+ className
295
+ )}
296
+ {...props}
297
+ >
298
+ <PanelBody fullscreen={fullscreen}>{children}</PanelBody>
299
+ {showClose ? (
300
+ <PanelClose
301
+ fullscreen={fullscreen}
302
+ label={closeLabel}
303
+ onClick={close}
304
+ />
305
+ ) : null}
306
+ </motion.div>
307
+ </div>
308
+ </>
309
+ ) : null}
310
+ </AnimatePresence>,
311
+ target
312
+ )
313
+ }
314
+ )
315
+ ExpandableCardPanel.displayName = "ExpandableCardPanel"
316
+
317
+ /**
318
+ * Where the overlay is rendered, resolved on the client.
319
+ *
320
+ * The panel has to leave the card's subtree. `position: fixed` is only relative to the
321
+ * viewport while no ancestor has a transform, a filter, a `backdrop-filter`, `perspective`
322
+ * or paint containment — any one of those makes that ancestor the containing block instead,
323
+ * and the overlay silently centres itself inside a page-height wrapper somewhere below the
324
+ * fold. A consumer cannot be asked to audit their layout for that, so the panel does not
325
+ * rely on the layout at all.
326
+ *
327
+ * Resolved in an effect rather than at render, because `document` does not exist on the
328
+ * server and a portal cannot be part of the first paint.
329
+ */
330
+ function usePortalTarget(container?: Element | null) {
331
+ const [target, setTarget] = React.useState<Element | null>(null)
332
+
333
+ React.useEffect(() => {
334
+ setTarget(container ?? document.body)
335
+ }, [container])
336
+
337
+ return target
338
+ }
339
+
340
+ export interface ExpandableCardSharedProps
341
+ extends React.ComponentProps<typeof motion.div> {
342
+ /** Names the pair. Use the same `part` on the card and in the panel, once each. */
343
+ part: string
344
+ }
345
+
346
+ /**
347
+ * An element that exists on both sides of the transition and should travel between them.
348
+ *
349
+ * Artwork, a title, a kicker. Pair them by `part`; a `part` used twice on the same side has
350
+ * two elements claiming to be one object, and the layout engine picks one.
351
+ */
352
+ const ExpandableCardShared = React.forwardRef<HTMLDivElement, ExpandableCardSharedProps>(
353
+ ({ part, ...props }, ref) => {
354
+ const { layoutId, transition } = useExpandableCard("ExpandableCardShared")
355
+
356
+ return (
357
+ <motion.div
358
+ ref={ref}
359
+ layoutId={layoutId(part)}
360
+ transition={transition}
361
+ {...props}
362
+ />
363
+ )
364
+ }
365
+ )
366
+ ExpandableCardShared.displayName = "ExpandableCardShared"
367
+
368
+ export type ExpandableCardBodyProps = React.ComponentProps<typeof motion.div>
369
+
370
+ /**
371
+ * Content that only exists in the panel.
372
+ *
373
+ * It has no counterpart on the card, so it arrives after the travel rather than trying to
374
+ * share it. Anything that exists on only one side of a transition should wait for the
375
+ * object to land.
376
+ */
377
+ const ExpandableCardBody = React.forwardRef<HTMLDivElement, ExpandableCardBodyProps>(
378
+ ({ className, ...props }, ref) => {
379
+ const { reducedMotion } = useExpandableCard("ExpandableCardBody")
380
+
381
+ return (
382
+ <motion.div
383
+ ref={ref}
384
+ initial={{ opacity: 0, y: 6 }}
385
+ animate={{ opacity: 1, y: 0 }}
386
+ transition={{ delay: reducedMotion ? 0 : 0.16, duration: 0.24 }}
387
+ className={cn("text-sm leading-relaxed text-muted-foreground", className)}
388
+ {...props}
389
+ />
390
+ )
391
+ }
392
+ )
393
+ ExpandableCardBody.displayName = "ExpandableCardBody"
394
+
395
+ /**
396
+ * Everything `aria-modal="true"` is claiming.
397
+ *
398
+ * Escape closes, focus moves in on open and returns to whatever opened it on close, Tab
399
+ * cycles inside, and the page underneath cannot scroll. The scroll lock compensates for the
400
+ * width of the scrollbar it removes, because otherwise the whole layout shifts sideways at
401
+ * the exact moment the card is mid-flight, and the shared-layout animation gets the blame.
402
+ */
403
+ function useModalBehaviour(
404
+ active: boolean,
405
+ ref: React.RefObject<HTMLElement | null>,
406
+ onClose: () => void
407
+ ) {
408
+ const onCloseRef = React.useRef(onClose)
409
+ React.useEffect(() => {
410
+ onCloseRef.current = onClose
411
+ })
412
+
413
+ React.useEffect(() => {
414
+ if (!active) return
415
+
416
+ const opener = document.activeElement as HTMLElement | null
417
+ const body = document.body
418
+ const previousOverflow = body.style.overflow
419
+ const previousPadding = body.style.paddingRight
420
+ const scrollbar = window.innerWidth - document.documentElement.clientWidth
421
+
422
+ body.style.overflow = "hidden"
423
+ if (scrollbar > 0) body.style.paddingRight = `${scrollbar}px`
424
+
425
+ const focusable = () =>
426
+ Array.from(ref.current?.querySelectorAll<HTMLElement>(FOCUSABLE) ?? []).filter(
427
+ (node) => node.offsetParent !== null
428
+ )
429
+
430
+ // Focus the panel's own controls, not the page. `preventScroll` matters: the dialog is
431
+ // mid-travel, and letting the browser scroll to the element it is focusing would move
432
+ // the ground under the animation.
433
+ focusable()[0]?.focus({ preventScroll: true })
434
+
435
+ // Flagged on CRAP, which scores complexity against coverage that is estimated here
436
+ // rather than measured — so the focus-trap tests cannot move the number.
437
+ // fallow-ignore-next-line complexity
438
+ const onKeyDown = (event: KeyboardEvent) => {
439
+ if (event.key === "Escape") {
440
+ event.preventDefault()
441
+ onCloseRef.current()
442
+ return
443
+ }
444
+ if (event.key !== "Tab") return
445
+
446
+ const nodes = focusable()
447
+ if (nodes.length === 0) {
448
+ event.preventDefault()
449
+ return
450
+ }
451
+
452
+ const first = nodes[0]
453
+ const last = nodes[nodes.length - 1]
454
+ const inside = ref.current?.contains(document.activeElement)
455
+
456
+ if (event.shiftKey && (!inside || document.activeElement === first)) {
457
+ event.preventDefault()
458
+ last.focus()
459
+ } else if (!event.shiftKey && (!inside || document.activeElement === last)) {
460
+ event.preventDefault()
461
+ first.focus()
462
+ }
463
+ }
464
+
465
+ document.addEventListener("keydown", onKeyDown)
466
+
467
+ return () => {
468
+ document.removeEventListener("keydown", onKeyDown)
469
+ body.style.overflow = previousOverflow
470
+ body.style.paddingRight = previousPadding
471
+ // By the time this runs the card is no longer inert, so it can take focus back.
472
+ opener?.focus({ preventScroll: true })
473
+ }
474
+ }, [active, ref])
475
+ }
476
+
477
+ function CloseIcon() {
478
+ return (
479
+ <svg
480
+ aria-hidden
481
+ viewBox="0 0 24 24"
482
+ fill="none"
483
+ stroke="currentColor"
484
+ strokeWidth={2}
485
+ strokeLinecap="round"
486
+ className="h-4 w-4"
487
+ >
488
+ <path d="M6 6l12 12M18 6L6 18" />
489
+ </svg>
490
+ )
491
+ }
492
+
493
+ /** The panel needs its own node for the focus trap and the caller may want it too. */
494
+ function mergeRefs<T>(...refs: (React.Ref<T> | undefined)[]): React.RefCallback<T> {
495
+ return (value) => {
496
+ for (const ref of refs) {
497
+ if (typeof ref === "function") ref(value)
498
+ else if (ref) (ref as React.RefObject<T | null>).current = value
499
+ }
500
+ }
501
+ }
502
+
503
+ export {
504
+ ExpandableCard,
505
+ ExpandableCardTrigger,
506
+ ExpandableCardPanel,
507
+ ExpandableCardShared,
508
+ ExpandableCardBody,
509
+ }
@@ -0,0 +1 @@
1
+ export * from './expandable-card'