@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.
- package/CHANGELOG.md +144 -0
- package/README.md +36 -5
- package/dist/index.js +336 -62
- package/package.json +17 -14
- 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/badge/badge.tsx +4 -4
- 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/index.ts +1 -0
- package/templates/otp-field/otp-field.tsx +72 -0
- 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/select/select.tsx +4 -2
- package/templates/slider/slider.tsx +1 -1
- 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/LICENSE +0 -21
- package/templates/utils/variants.ts +0 -6
|
@@ -0,0 +1,349 @@
|
|
|
1
|
+
"use client"
|
|
2
|
+
|
|
3
|
+
import * as React from "react"
|
|
4
|
+
import {
|
|
5
|
+
motion,
|
|
6
|
+
MotionConfigContext,
|
|
7
|
+
Reorder,
|
|
8
|
+
useDragControls,
|
|
9
|
+
useReducedMotion,
|
|
10
|
+
type HTMLMotionProps,
|
|
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 EASE_OUT: [number, number, number, number] = [0.23, 1, 0.32, 1]
|
|
26
|
+
|
|
27
|
+
interface SortableContextValue {
|
|
28
|
+
value: string[]
|
|
29
|
+
grabbed: string | null
|
|
30
|
+
grab: (id: string, label: string) => void
|
|
31
|
+
drop: (id: string, label: string) => void
|
|
32
|
+
cancel: (id: string, label: string) => void
|
|
33
|
+
move: (id: string, delta: number, label: string) => void
|
|
34
|
+
reducedMotion: boolean
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const SortableContext = React.createContext<SortableContextValue | null>(null)
|
|
38
|
+
|
|
39
|
+
interface SortableItemContextValue {
|
|
40
|
+
id: string
|
|
41
|
+
label: string
|
|
42
|
+
grabbed: boolean
|
|
43
|
+
dragControls: ReturnType<typeof useDragControls>
|
|
44
|
+
setDragging: (dragging: boolean) => void
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const SortableItemContext = React.createContext<SortableItemContextValue | null>(null)
|
|
48
|
+
|
|
49
|
+
export interface SortableProps
|
|
50
|
+
extends Omit<HTMLMotionProps<"ul">, "values" | "onReorder" | "children"> {
|
|
51
|
+
/** The current order, as stable ids. Ids rather than objects because identity is what a
|
|
52
|
+
* reorder is about, and it is what React's keys need anyway. */
|
|
53
|
+
value: string[]
|
|
54
|
+
onValueChange: (next: string[]) => void
|
|
55
|
+
children?: React.ReactNode
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* A list the reader can reorder.
|
|
60
|
+
*
|
|
61
|
+
* The drag is the easy half. What makes this a component rather than a snippet is that
|
|
62
|
+
* reordering by pointer and reordering by keyboard have to end at the same place, and only
|
|
63
|
+
* one of them can be expressed as a gesture. The handle is a real button: Space picks the
|
|
64
|
+
* row up, the arrows move it, Space drops it, Escape puts it back where it started — and
|
|
65
|
+
* every one of those goes through the same reorder the drag does.
|
|
66
|
+
*
|
|
67
|
+
* Rows that move because another row passed them animate with FLIP, so a displaced row
|
|
68
|
+
* reads as having been pushed rather than repainted somewhere else.
|
|
69
|
+
*/
|
|
70
|
+
const Sortable = React.forwardRef<HTMLUListElement, SortableProps>(
|
|
71
|
+
({ value, onValueChange, className, children, ...props }, ref) => {
|
|
72
|
+
const reducedMotion = useReducedMotionConfig()
|
|
73
|
+
const [grabbed, setGrabbed] = React.useState<string | null>(null)
|
|
74
|
+
const [announcement, setAnnouncement] = React.useState("")
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* The order at the moment the row was picked up.
|
|
78
|
+
*
|
|
79
|
+
* Escape has one meaning everywhere else in a UI — undo what this interaction did — and
|
|
80
|
+
* a sortable list that drops the row wherever it currently sits is the one place people
|
|
81
|
+
* expect it to hold. Keeping the pre-grab order is what lets Escape mean the same thing
|
|
82
|
+
* here.
|
|
83
|
+
*/
|
|
84
|
+
const snapshot = React.useRef<string[] | null>(null)
|
|
85
|
+
|
|
86
|
+
const positionOf = (list: string[], id: string) => list.indexOf(id) + 1
|
|
87
|
+
|
|
88
|
+
const grab = React.useCallback(
|
|
89
|
+
(id: string, label: string) => {
|
|
90
|
+
snapshot.current = value
|
|
91
|
+
setGrabbed(id)
|
|
92
|
+
setAnnouncement(
|
|
93
|
+
`${label} grabbed, position ${positionOf(value, id)} of ${value.length}. Use the arrow keys to move, Escape to cancel.`
|
|
94
|
+
)
|
|
95
|
+
},
|
|
96
|
+
[value]
|
|
97
|
+
)
|
|
98
|
+
|
|
99
|
+
const drop = React.useCallback(
|
|
100
|
+
(id: string, label: string) => {
|
|
101
|
+
snapshot.current = null
|
|
102
|
+
setGrabbed(null)
|
|
103
|
+
setAnnouncement(
|
|
104
|
+
`${label} dropped at position ${positionOf(value, id)} of ${value.length}.`
|
|
105
|
+
)
|
|
106
|
+
},
|
|
107
|
+
[value]
|
|
108
|
+
)
|
|
109
|
+
|
|
110
|
+
const cancel = React.useCallback(
|
|
111
|
+
(id: string, label: string) => {
|
|
112
|
+
const original = snapshot.current
|
|
113
|
+
snapshot.current = null
|
|
114
|
+
setGrabbed(null)
|
|
115
|
+
if (original) onValueChange(original)
|
|
116
|
+
setAnnouncement(
|
|
117
|
+
`Cancelled. ${label} back at position ${positionOf(original ?? value, id)} of ${value.length}.`
|
|
118
|
+
)
|
|
119
|
+
},
|
|
120
|
+
[value, onValueChange]
|
|
121
|
+
)
|
|
122
|
+
|
|
123
|
+
const move = React.useCallback(
|
|
124
|
+
(id: string, delta: number, label: string) => {
|
|
125
|
+
const from = value.indexOf(id)
|
|
126
|
+
const to = from + delta
|
|
127
|
+
if (from < 0 || to < 0 || to >= value.length) return
|
|
128
|
+
|
|
129
|
+
const next = [...value]
|
|
130
|
+
;[next[from], next[to]] = [next[to], next[from]]
|
|
131
|
+
onValueChange(next)
|
|
132
|
+
setAnnouncement(`${label}, position ${to + 1} of ${value.length}`)
|
|
133
|
+
},
|
|
134
|
+
[value, onValueChange]
|
|
135
|
+
)
|
|
136
|
+
|
|
137
|
+
const context = React.useMemo(
|
|
138
|
+
() => ({ value, grabbed, grab, drop, cancel, move, reducedMotion }),
|
|
139
|
+
[value, grabbed, grab, drop, cancel, move, reducedMotion]
|
|
140
|
+
)
|
|
141
|
+
|
|
142
|
+
return (
|
|
143
|
+
<SortableContext.Provider value={context}>
|
|
144
|
+
<Reorder.Group
|
|
145
|
+
ref={ref}
|
|
146
|
+
as="ul"
|
|
147
|
+
axis="y"
|
|
148
|
+
values={value}
|
|
149
|
+
onReorder={onValueChange}
|
|
150
|
+
className={cn("list-none space-y-2", className)}
|
|
151
|
+
{...props}
|
|
152
|
+
>
|
|
153
|
+
{children}
|
|
154
|
+
</Reorder.Group>
|
|
155
|
+
|
|
156
|
+
{/* A reorder produces no DOM event a screen reader reports on its own. Assertive
|
|
157
|
+
rather than polite: the reader is mid-interaction and the position is the only
|
|
158
|
+
thing telling them where the row went. */}
|
|
159
|
+
<span aria-live="assertive" className="sr-only">
|
|
160
|
+
{announcement}
|
|
161
|
+
</span>
|
|
162
|
+
</SortableContext.Provider>
|
|
163
|
+
)
|
|
164
|
+
}
|
|
165
|
+
)
|
|
166
|
+
Sortable.displayName = "Sortable"
|
|
167
|
+
|
|
168
|
+
export interface SortableItemProps
|
|
169
|
+
extends Omit<React.ComponentProps<typeof Reorder.Item>, "value" | "children"> {
|
|
170
|
+
/** The id this row carries in `value`. Give the same string to React's `key`. */
|
|
171
|
+
id: string
|
|
172
|
+
/** Names the row in the reorder announcements, and in the handle's accessible name. */
|
|
173
|
+
label: string
|
|
174
|
+
children?: React.ReactNode
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/**
|
|
178
|
+
* One reorderable row. Its children are yours; put a `<SortableHandle />` among them.
|
|
179
|
+
*
|
|
180
|
+
* A row being dragged is above the page rather than in it, so it scales up a hair and takes
|
|
181
|
+
* a shadow. Without that, a row in flight and a row at rest are the same object in two
|
|
182
|
+
* places.
|
|
183
|
+
*/
|
|
184
|
+
const SortableItem = React.forwardRef<HTMLLIElement, SortableItemProps>(
|
|
185
|
+
({ id, label, className, children, ...props }, ref) => {
|
|
186
|
+
const list = React.useContext(SortableContext)
|
|
187
|
+
|
|
188
|
+
if (!list) {
|
|
189
|
+
throw new Error("SortableItem must be used within a Sortable")
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
const dragControls = useDragControls()
|
|
193
|
+
const [dragging, setDragging] = React.useState(false)
|
|
194
|
+
const grabbed = list.grabbed === id
|
|
195
|
+
const lifted = dragging || grabbed
|
|
196
|
+
|
|
197
|
+
const item = React.useMemo(
|
|
198
|
+
() => ({ id, label, grabbed, dragControls, setDragging }),
|
|
199
|
+
[id, label, grabbed, dragControls]
|
|
200
|
+
)
|
|
201
|
+
|
|
202
|
+
return (
|
|
203
|
+
<SortableItemContext.Provider value={item}>
|
|
204
|
+
<Reorder.Item
|
|
205
|
+
ref={ref}
|
|
206
|
+
as="li"
|
|
207
|
+
value={id}
|
|
208
|
+
// The row only moves from the handle. A whole-row drag target fights text
|
|
209
|
+
// selection and swallows every click inside it.
|
|
210
|
+
dragListener={false}
|
|
211
|
+
dragControls={dragControls}
|
|
212
|
+
onDragStart={() => setDragging(true)}
|
|
213
|
+
onDragEnd={() => setDragging(false)}
|
|
214
|
+
animate={{ scale: lifted && !list.reducedMotion ? 1.02 : 1 }}
|
|
215
|
+
transition={{ type: "spring", bounce: 0.2, duration: 0.3 }}
|
|
216
|
+
className={cn(
|
|
217
|
+
"relative flex items-center gap-3 rounded-xl border bg-background px-3 py-3",
|
|
218
|
+
lifted ? "z-10 border-primary/50" : "border-border",
|
|
219
|
+
className
|
|
220
|
+
)}
|
|
221
|
+
{...props}
|
|
222
|
+
>
|
|
223
|
+
{/* The lift is a shadow fading in, not a `boxShadow` being interpolated. A
|
|
224
|
+
shadow keyframe repaints the blur on every frame; an opacity change on a
|
|
225
|
+
layer that already has one does not. */}
|
|
226
|
+
<motion.span
|
|
227
|
+
aria-hidden
|
|
228
|
+
className="pointer-events-none absolute inset-0 rounded-xl shadow-[0_8px_24px_rgb(0_0_0/0.14)]"
|
|
229
|
+
initial={{ opacity: 0 }}
|
|
230
|
+
animate={{ opacity: lifted ? 1 : 0 }}
|
|
231
|
+
transition={{
|
|
232
|
+
duration: list.reducedMotion ? 0 : 0.2,
|
|
233
|
+
ease: EASE_OUT,
|
|
234
|
+
}}
|
|
235
|
+
/>
|
|
236
|
+
{children}
|
|
237
|
+
</Reorder.Item>
|
|
238
|
+
</SortableItemContext.Provider>
|
|
239
|
+
)
|
|
240
|
+
}
|
|
241
|
+
)
|
|
242
|
+
SortableItem.displayName = "SortableItem"
|
|
243
|
+
|
|
244
|
+
export type SortableHandleProps = React.ComponentProps<"button">
|
|
245
|
+
|
|
246
|
+
|
|
247
|
+
/**
|
|
248
|
+
* The grip. A real button, so the reorder has a keyboard path at all.
|
|
249
|
+
*
|
|
250
|
+
* Space or Enter picks the row up, the arrows move it, Space drops it, Escape restores the
|
|
251
|
+
* order from before the grab. Tabbing away drops the row where it is rather than
|
|
252
|
+
* cancelling: focus left, but the moves already made were deliberate.
|
|
253
|
+
*/
|
|
254
|
+
const SortableHandle = React.forwardRef<HTMLButtonElement, SortableHandleProps>(
|
|
255
|
+
({ className, children, onKeyDown, onPointerDown, onBlur, ...props }, ref) => {
|
|
256
|
+
const list = React.useContext(SortableContext)
|
|
257
|
+
const item = React.useContext(SortableItemContext)
|
|
258
|
+
|
|
259
|
+
if (!list || !item) {
|
|
260
|
+
throw new Error("SortableHandle must be used within a SortableItem")
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
const { id, label, grabbed, dragControls } = item
|
|
264
|
+
|
|
265
|
+
// Flagged on CRAP, which scores complexity against coverage that is estimated here
|
|
266
|
+
// rather than measured — so the keyboard tests below it cannot move the number.
|
|
267
|
+
// fallow-ignore-next-line complexity
|
|
268
|
+
const handleKeyDown = (event: React.KeyboardEvent<HTMLButtonElement>) => {
|
|
269
|
+
onKeyDown?.(event)
|
|
270
|
+
if (event.defaultPrevented) return
|
|
271
|
+
|
|
272
|
+
if (event.key === " " || event.key === "Enter") {
|
|
273
|
+
event.preventDefault()
|
|
274
|
+
if (grabbed) list.drop(id, label)
|
|
275
|
+
else list.grab(id, label)
|
|
276
|
+
return
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
if (event.key === "Escape" && grabbed) {
|
|
280
|
+
// Stopped here: Escape inside a dialog or a popover would otherwise close the
|
|
281
|
+
// container out from under a reorder the reader was in the middle of cancelling.
|
|
282
|
+
event.preventDefault()
|
|
283
|
+
event.stopPropagation()
|
|
284
|
+
list.cancel(id, label)
|
|
285
|
+
return
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
if (!grabbed) return
|
|
289
|
+
|
|
290
|
+
if (event.key === "ArrowUp") {
|
|
291
|
+
event.preventDefault()
|
|
292
|
+
list.move(id, -1, label)
|
|
293
|
+
}
|
|
294
|
+
if (event.key === "ArrowDown") {
|
|
295
|
+
event.preventDefault()
|
|
296
|
+
list.move(id, 1, label)
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
return (
|
|
301
|
+
<button
|
|
302
|
+
ref={ref}
|
|
303
|
+
type="button"
|
|
304
|
+
aria-label={`Reorder ${label}`}
|
|
305
|
+
aria-pressed={grabbed}
|
|
306
|
+
onPointerDown={(event) => {
|
|
307
|
+
onPointerDown?.(event)
|
|
308
|
+
if (event.defaultPrevented) return
|
|
309
|
+
dragControls.start(event)
|
|
310
|
+
}}
|
|
311
|
+
onKeyDown={handleKeyDown}
|
|
312
|
+
onBlur={(event) => {
|
|
313
|
+
onBlur?.(event)
|
|
314
|
+
if (grabbed) list.drop(id, label)
|
|
315
|
+
}}
|
|
316
|
+
className={cn(
|
|
317
|
+
"relative cursor-grab touch-none rounded-md p-1 text-muted-foreground transition-colors hover:text-foreground focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring active:cursor-grabbing",
|
|
318
|
+
className
|
|
319
|
+
)}
|
|
320
|
+
{...props}
|
|
321
|
+
>
|
|
322
|
+
{children ?? <GripIcon />}
|
|
323
|
+
</button>
|
|
324
|
+
)
|
|
325
|
+
}
|
|
326
|
+
)
|
|
327
|
+
SortableHandle.displayName = "SortableHandle"
|
|
328
|
+
|
|
329
|
+
/** The default grip. Inlined rather than pulled from an icon package, so the component
|
|
330
|
+
* installs with nothing but `motion`. */
|
|
331
|
+
function GripIcon() {
|
|
332
|
+
return (
|
|
333
|
+
<svg
|
|
334
|
+
aria-hidden
|
|
335
|
+
viewBox="0 0 24 24"
|
|
336
|
+
fill="currentColor"
|
|
337
|
+
className="h-4 w-4"
|
|
338
|
+
>
|
|
339
|
+
<circle cx="9" cy="6" r="1.6" />
|
|
340
|
+
<circle cx="15" cy="6" r="1.6" />
|
|
341
|
+
<circle cx="9" cy="12" r="1.6" />
|
|
342
|
+
<circle cx="15" cy="12" r="1.6" />
|
|
343
|
+
<circle cx="9" cy="18" r="1.6" />
|
|
344
|
+
<circle cx="15" cy="18" r="1.6" />
|
|
345
|
+
</svg>
|
|
346
|
+
)
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
export { Sortable, SortableItem, SortableHandle }
|
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
"use client"
|
|
2
|
+
|
|
3
|
+
import * as React from "react"
|
|
4
|
+
import { motion, MotionConfigContext, useReducedMotion, type Variants } 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
|
+
const EASE_OUT: [number, number, number, number] = [0.23, 1, 0.32, 1]
|
|
19
|
+
|
|
20
|
+
export type StaggerListVariant = "rise" | "scale" | "blur"
|
|
21
|
+
|
|
22
|
+
/** One duration for every variant. A list entrance is UI, and UI motion stays under
|
|
23
|
+
* 300ms whichever channel it animates — a variant that needs longer to read is a
|
|
24
|
+
* variant that is doing too much. */
|
|
25
|
+
const DEFAULT_DURATION = 0.3
|
|
26
|
+
|
|
27
|
+
/** Reduced motion drops to a bare fade, which has less to say and so needs less time. */
|
|
28
|
+
const REDUCED_DURATION = 0.2
|
|
29
|
+
|
|
30
|
+
/** Blur is the one channel here the compositor cannot do for free, and its cost scales
|
|
31
|
+
* with the radius rather than with the fact of blurring. Small enough to stay cheap on
|
|
32
|
+
* a long list, large enough to still read as focus arriving. */
|
|
33
|
+
const BLUR_RADIUS = 2
|
|
34
|
+
|
|
35
|
+
interface StaggerContextValue {
|
|
36
|
+
itemVariants: Variants
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
const StaggerContext = React.createContext<StaggerContextValue | null>(null)
|
|
40
|
+
|
|
41
|
+
export interface StaggerListProps
|
|
42
|
+
extends React.ComponentProps<typeof motion.ul> {
|
|
43
|
+
/** How each item enters. See each variant's note on the component. */
|
|
44
|
+
variant?: StaggerListVariant
|
|
45
|
+
/** Seconds between each item. Keep it in the 0.03–0.08 range — beyond that the
|
|
46
|
+
* last item is late enough to feel like a wait. */
|
|
47
|
+
stagger?: number
|
|
48
|
+
/** Seconds before the first item. */
|
|
49
|
+
delay?: number
|
|
50
|
+
/** Seconds for each item's own animation. */
|
|
51
|
+
duration?: number
|
|
52
|
+
/** Item travel distance in px. Ignored by `scale`, which does not travel. */
|
|
53
|
+
distance?: number
|
|
54
|
+
/** Start when scrolled into view rather than on mount. */
|
|
55
|
+
startOnView?: boolean
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* A list whose items arrive one after another, drawn three ways:
|
|
60
|
+
*
|
|
61
|
+
* - `rise` — items lift into place from below. The default, and the right one for
|
|
62
|
+
* anything read top to bottom, because the motion runs along the reading direction.
|
|
63
|
+
* - `scale` — items settle in place from slightly small. Use it for grids and
|
|
64
|
+
* card walls, where there is no single reading direction for a rise to follow.
|
|
65
|
+
* - `blur` — items pull into focus as they rise. The only variant that animates a
|
|
66
|
+
* filter, so it is the only one whose cost grows with the item count. Two things keep
|
|
67
|
+
* that in hand: the radius is small, and the item travels, which puts it on its own
|
|
68
|
+
* compositing layer so the filter is a layer operation rather than a repaint. The
|
|
69
|
+
* residue is a `filter: blur(0px)` left on each item after it lands, which makes that
|
|
70
|
+
* item a containing block for any fixed-position descendant.
|
|
71
|
+
*
|
|
72
|
+
* Every variant fades in, so a reduced-motion preference drops the second channel and
|
|
73
|
+
* keeps the fade and the stagger — sequencing is not movement, and the sequence is the
|
|
74
|
+
* information this component carries.
|
|
75
|
+
*/
|
|
76
|
+
const StaggerList = React.forwardRef<HTMLUListElement, StaggerListProps>(
|
|
77
|
+
(
|
|
78
|
+
{
|
|
79
|
+
variant = "rise",
|
|
80
|
+
stagger = 0.05,
|
|
81
|
+
delay = 0,
|
|
82
|
+
duration,
|
|
83
|
+
distance = 8,
|
|
84
|
+
startOnView = true,
|
|
85
|
+
className,
|
|
86
|
+
children,
|
|
87
|
+
...props
|
|
88
|
+
},
|
|
89
|
+
ref
|
|
90
|
+
) => {
|
|
91
|
+
const reducedMotion = useReducedMotionConfig()
|
|
92
|
+
const seconds = duration ?? DEFAULT_DURATION
|
|
93
|
+
|
|
94
|
+
// The stagger survives a reduced-motion preference. What that preference asks for is
|
|
95
|
+
// less movement, and a sequence of fades has none — collapsing it would delete the
|
|
96
|
+
// component's whole point rather than soften it.
|
|
97
|
+
const containerVariants: Variants = React.useMemo(
|
|
98
|
+
() => ({
|
|
99
|
+
hidden: {},
|
|
100
|
+
show: {
|
|
101
|
+
transition: {
|
|
102
|
+
staggerChildren: stagger,
|
|
103
|
+
delayChildren: delay,
|
|
104
|
+
},
|
|
105
|
+
},
|
|
106
|
+
}),
|
|
107
|
+
[stagger, delay]
|
|
108
|
+
)
|
|
109
|
+
|
|
110
|
+
const itemVariants: Variants = React.useMemo(() => {
|
|
111
|
+
const transition = {
|
|
112
|
+
duration: reducedMotion ? REDUCED_DURATION : seconds,
|
|
113
|
+
ease: EASE_OUT,
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
if (reducedMotion) {
|
|
117
|
+
return { hidden: { opacity: 0 }, show: { opacity: 1, transition } }
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
// Full transform strings rather than motion's `x`/`y`/`scale` shorthands. The
|
|
121
|
+
// shorthands are driven from the main thread, which is exactly where the contention
|
|
122
|
+
// is when a stagger runs: during a scroll, or while the page is still painting the
|
|
123
|
+
// content the list is made of.
|
|
124
|
+
switch (variant) {
|
|
125
|
+
case "scale":
|
|
126
|
+
return {
|
|
127
|
+
hidden: { opacity: 0, transform: "scale(0.96)" },
|
|
128
|
+
show: { opacity: 1, transform: "scale(1)", transition },
|
|
129
|
+
}
|
|
130
|
+
case "blur":
|
|
131
|
+
return {
|
|
132
|
+
hidden: {
|
|
133
|
+
opacity: 0,
|
|
134
|
+
filter: `blur(${BLUR_RADIUS}px)`,
|
|
135
|
+
transform: `translateY(${distance}px)`,
|
|
136
|
+
},
|
|
137
|
+
show: {
|
|
138
|
+
opacity: 1,
|
|
139
|
+
filter: "blur(0px)",
|
|
140
|
+
transform: "translateY(0px)",
|
|
141
|
+
transition,
|
|
142
|
+
},
|
|
143
|
+
}
|
|
144
|
+
default:
|
|
145
|
+
return {
|
|
146
|
+
hidden: { opacity: 0, transform: `translateY(${distance}px)` },
|
|
147
|
+
show: { opacity: 1, transform: "translateY(0px)", transition },
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
}, [variant, distance, seconds, reducedMotion])
|
|
151
|
+
|
|
152
|
+
const contextValue = React.useMemo(() => ({ itemVariants }), [itemVariants])
|
|
153
|
+
|
|
154
|
+
return (
|
|
155
|
+
<StaggerContext.Provider value={contextValue}>
|
|
156
|
+
<motion.ul
|
|
157
|
+
ref={ref}
|
|
158
|
+
className={cn("list-none", className)}
|
|
159
|
+
variants={containerVariants}
|
|
160
|
+
initial="hidden"
|
|
161
|
+
{...(
|
|
162
|
+
// A margin rather than a visible fraction: a fraction of a tall list is a lot
|
|
163
|
+
// of scrolling, so a grid would sit at zero opacity while its first rows were
|
|
164
|
+
// already on screen. Insetting from the edge starts the sequence once the list
|
|
165
|
+
// has properly arrived, rather than the instant it clips the fold.
|
|
166
|
+
startOnView
|
|
167
|
+
? { whileInView: "show", viewport: { once: true, margin: "-100px" } }
|
|
168
|
+
: { animate: "show" }
|
|
169
|
+
)}
|
|
170
|
+
{...props}
|
|
171
|
+
>
|
|
172
|
+
{children}
|
|
173
|
+
</motion.ul>
|
|
174
|
+
</StaggerContext.Provider>
|
|
175
|
+
)
|
|
176
|
+
}
|
|
177
|
+
)
|
|
178
|
+
StaggerList.displayName = "StaggerList"
|
|
179
|
+
|
|
180
|
+
export type StaggerListItemProps = React.ComponentProps<typeof motion.li>
|
|
181
|
+
|
|
182
|
+
const StaggerListItem = React.forwardRef<HTMLLIElement, StaggerListItemProps>(
|
|
183
|
+
({ className, children, ...props }, ref) => {
|
|
184
|
+
const context = React.useContext(StaggerContext)
|
|
185
|
+
|
|
186
|
+
if (!context) {
|
|
187
|
+
throw new Error("StaggerListItem must be used within a StaggerList")
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
return (
|
|
191
|
+
<motion.li
|
|
192
|
+
ref={ref}
|
|
193
|
+
className={cn(className)}
|
|
194
|
+
variants={context.itemVariants}
|
|
195
|
+
{...props}
|
|
196
|
+
>
|
|
197
|
+
{children}
|
|
198
|
+
</motion.li>
|
|
199
|
+
)
|
|
200
|
+
}
|
|
201
|
+
)
|
|
202
|
+
StaggerListItem.displayName = "StaggerListItem"
|
|
203
|
+
|
|
204
|
+
export { StaggerList, StaggerListItem }
|