@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,220 @@
|
|
|
1
|
+
"use client"
|
|
2
|
+
|
|
3
|
+
import * as React from "react"
|
|
4
|
+
import { motion, 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 ?? false
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
const EASE_OUT: [number, number, number, number] = [0.23, 1, 0.32, 1]
|
|
19
|
+
|
|
20
|
+
/** Fast enough to stay ahead of a reader, slow enough that the arrival is visible.
|
|
21
|
+
* Around the pace a mid-sized model emits at. */
|
|
22
|
+
const DEFAULT_WORDS_PER_SECOND = 14
|
|
23
|
+
|
|
24
|
+
export interface StreamingTextProps
|
|
25
|
+
extends Omit<React.ComponentProps<"div">, "children"> {
|
|
26
|
+
/** The text so far. May grow between renders; appending resumes rather than restarts. */
|
|
27
|
+
text: string
|
|
28
|
+
/**
|
|
29
|
+
* Whether the stream has ended.
|
|
30
|
+
*
|
|
31
|
+
* The component cannot infer this. Catching up to the current string is not the same
|
|
32
|
+
* event as the stream finishing, and between two chunks the two are indistinguishable
|
|
33
|
+
* from in here. Everything that means "finished" hangs off this: the blinking caret,
|
|
34
|
+
* `onDone`, and the single announcement to assistive technology.
|
|
35
|
+
*/
|
|
36
|
+
complete?: boolean
|
|
37
|
+
/** Change it to rewind to the first word. */
|
|
38
|
+
runKey?: number
|
|
39
|
+
/** Holds the reveal where it is. The clock stops with it, so there is no catch-up
|
|
40
|
+
* burst on resume. */
|
|
41
|
+
paused?: boolean
|
|
42
|
+
/** Reveal pace. Changing it mid-stream changes the pace without restarting. */
|
|
43
|
+
wordsPerSecond?: number
|
|
44
|
+
/** Fired once, when the reveal has caught up and `complete` is true. */
|
|
45
|
+
onDone?: () => void
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Text arriving a word at a time, the way a model emits it.
|
|
50
|
+
*
|
|
51
|
+
* Three things separate this from a typewriter effect.
|
|
52
|
+
*
|
|
53
|
+
* The pace is driven by elapsed time rather than by a per-character timer, so a dropped
|
|
54
|
+
* frame costs smoothness rather than sending the paragraph out of sync with the stream.
|
|
55
|
+
*
|
|
56
|
+
* The unit is a word, not a character. Revealing per character reflows the line on nearly
|
|
57
|
+
* every frame, which is both expensive and unreadable — the text jitters as words break
|
|
58
|
+
* and rejoin. A word appears whole, in place, and only its opacity and blur change, so
|
|
59
|
+
* nothing after it moves.
|
|
60
|
+
*
|
|
61
|
+
* And the reveal is append-aware. Real streaming hands you a string that grows, not a
|
|
62
|
+
* finished paragraph to play back, so progress is carried in a ref and only ever moves
|
|
63
|
+
* forward. Text arriving late raises the ceiling; it does not rewind the reader to the
|
|
64
|
+
* first word.
|
|
65
|
+
*/
|
|
66
|
+
const StreamingText = React.forwardRef<HTMLDivElement, StreamingTextProps>(
|
|
67
|
+
(
|
|
68
|
+
{
|
|
69
|
+
text,
|
|
70
|
+
complete = true,
|
|
71
|
+
runKey = 0,
|
|
72
|
+
paused = false,
|
|
73
|
+
wordsPerSecond = DEFAULT_WORDS_PER_SECOND,
|
|
74
|
+
onDone,
|
|
75
|
+
className,
|
|
76
|
+
...props
|
|
77
|
+
},
|
|
78
|
+
ref
|
|
79
|
+
) => {
|
|
80
|
+
const reducedMotion = useReducedMotionConfig()
|
|
81
|
+
|
|
82
|
+
// Split on whitespace *keeping* it, so the gaps survive the reveal as literal text
|
|
83
|
+
// rather than having to be reconstructed.
|
|
84
|
+
const words = React.useMemo(() => text.split(/(\s+)/), [text])
|
|
85
|
+
|
|
86
|
+
const [visible, setVisible] = React.useState(0)
|
|
87
|
+
/** Every word we have has been shown. More may yet arrive. */
|
|
88
|
+
const caughtUp = visible >= words.length
|
|
89
|
+
const done = caughtUp && complete
|
|
90
|
+
|
|
91
|
+
/** Fractional word position. Integer state is what renders; this is what accumulates. */
|
|
92
|
+
const progress = React.useRef(0)
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Read inside the frame loop instead of closed over it. A pace change mid-stream is a
|
|
96
|
+
* change of pace, not a change of content — as a dependency of the effect it would
|
|
97
|
+
* tear the loop down and start the paragraph again.
|
|
98
|
+
*/
|
|
99
|
+
const paceRef = React.useRef(wordsPerSecond)
|
|
100
|
+
React.useEffect(() => {
|
|
101
|
+
paceRef.current = wordsPerSecond
|
|
102
|
+
}, [wordsPerSecond])
|
|
103
|
+
|
|
104
|
+
const doneRef = React.useRef(onDone)
|
|
105
|
+
React.useEffect(() => {
|
|
106
|
+
doneRef.current = onDone
|
|
107
|
+
}, [onDone])
|
|
108
|
+
|
|
109
|
+
React.useEffect(() => {
|
|
110
|
+
progress.current = 0
|
|
111
|
+
setVisible(0)
|
|
112
|
+
}, [runKey])
|
|
113
|
+
|
|
114
|
+
React.useEffect(() => {
|
|
115
|
+
if (paused || caughtUp) return
|
|
116
|
+
|
|
117
|
+
let last = performance.now()
|
|
118
|
+
let frame = requestAnimationFrame(function tick(now) {
|
|
119
|
+
// Delta time, not time-since-start: the loop stops and restarts — for a pause, or
|
|
120
|
+
// for text that has caught up with the stream — and picks up where it left off.
|
|
121
|
+
const delta = (now - last) / 1000
|
|
122
|
+
last = now
|
|
123
|
+
|
|
124
|
+
progress.current = Math.min(
|
|
125
|
+
progress.current + delta * paceRef.current,
|
|
126
|
+
words.length
|
|
127
|
+
)
|
|
128
|
+
setVisible(Math.floor(progress.current))
|
|
129
|
+
|
|
130
|
+
if (progress.current < words.length) frame = requestAnimationFrame(tick)
|
|
131
|
+
})
|
|
132
|
+
|
|
133
|
+
return () => cancelAnimationFrame(frame)
|
|
134
|
+
// `caughtUp` flips once per chunk, not once per frame, so the loop is rebuilt only
|
|
135
|
+
// when there is genuinely nothing left to reveal.
|
|
136
|
+
}, [paused, caughtUp, words.length])
|
|
137
|
+
|
|
138
|
+
React.useEffect(() => {
|
|
139
|
+
if (done) doneRef.current?.()
|
|
140
|
+
}, [done])
|
|
141
|
+
|
|
142
|
+
return (
|
|
143
|
+
<div
|
|
144
|
+
ref={ref}
|
|
145
|
+
className={cn("text-sm leading-relaxed text-foreground", className)}
|
|
146
|
+
{...props}
|
|
147
|
+
>
|
|
148
|
+
<p
|
|
149
|
+
// The visible text is decoration as far as assistive technology is concerned. A
|
|
150
|
+
// live region over a growing paragraph re-announces the whole thing on every
|
|
151
|
+
// word; the finished answer goes out once, below.
|
|
152
|
+
aria-hidden
|
|
153
|
+
>
|
|
154
|
+
{words.slice(0, visible).map((word, index) =>
|
|
155
|
+
// Whitespace is emitted as-is. Animating it would put a fade on a gap.
|
|
156
|
+
/^\s+$/.test(word) ? (
|
|
157
|
+
<React.Fragment key={index}>{word}</React.Fragment>
|
|
158
|
+
) : (
|
|
159
|
+
<motion.span
|
|
160
|
+
key={index}
|
|
161
|
+
// Under reduced motion the word still arrives — the streaming is the
|
|
162
|
+
// data, not the decoration. It just arrives without the blur-in.
|
|
163
|
+
initial={
|
|
164
|
+
reducedMotion
|
|
165
|
+
? { opacity: 1, filter: "blur(0px)", transform: "translateY(0px)" }
|
|
166
|
+
: { opacity: 0, filter: "blur(4px)", transform: "translateY(2px)" }
|
|
167
|
+
}
|
|
168
|
+
animate={{
|
|
169
|
+
opacity: 1,
|
|
170
|
+
filter: "blur(0px)",
|
|
171
|
+
transform: "translateY(0px)",
|
|
172
|
+
}}
|
|
173
|
+
transition={{ duration: 0.22, ease: EASE_OUT }}
|
|
174
|
+
className="inline-block"
|
|
175
|
+
>
|
|
176
|
+
{word}
|
|
177
|
+
</motion.span>
|
|
178
|
+
)
|
|
179
|
+
)}
|
|
180
|
+
<Caret idle={done} reducedMotion={reducedMotion} />
|
|
181
|
+
</p>
|
|
182
|
+
|
|
183
|
+
<p aria-live="polite" className="sr-only">
|
|
184
|
+
{done ? text : ""}
|
|
185
|
+
</p>
|
|
186
|
+
</div>
|
|
187
|
+
)
|
|
188
|
+
}
|
|
189
|
+
)
|
|
190
|
+
StreamingText.displayName = "StreamingText"
|
|
191
|
+
|
|
192
|
+
/**
|
|
193
|
+
* The caret stays solid while tokens are landing: a blink during output competes with the
|
|
194
|
+
* words for attention. It only starts blinking once the stream stops, where it means
|
|
195
|
+
* "your turn" rather than "still working".
|
|
196
|
+
*/
|
|
197
|
+
function Caret({
|
|
198
|
+
idle,
|
|
199
|
+
reducedMotion,
|
|
200
|
+
}: {
|
|
201
|
+
idle: boolean
|
|
202
|
+
reducedMotion: boolean
|
|
203
|
+
}) {
|
|
204
|
+
const blinking = idle && !reducedMotion
|
|
205
|
+
|
|
206
|
+
return (
|
|
207
|
+
<motion.span
|
|
208
|
+
aria-hidden
|
|
209
|
+
className="ml-0.5 inline-block h-[1em] w-[2px] translate-y-[0.15em] rounded-full bg-primary align-baseline"
|
|
210
|
+
animate={blinking ? { opacity: [1, 1, 0, 0] } : { opacity: 1 }}
|
|
211
|
+
transition={
|
|
212
|
+
blinking
|
|
213
|
+
? { duration: 1.1, repeat: Infinity, times: [0, 0.5, 0.5, 1] }
|
|
214
|
+
: { duration: 0 }
|
|
215
|
+
}
|
|
216
|
+
/>
|
|
217
|
+
)
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
export { StreamingText }
|
|
@@ -0,0 +1,368 @@
|
|
|
1
|
+
"use client"
|
|
2
|
+
|
|
3
|
+
import * as React from "react"
|
|
4
|
+
import {
|
|
5
|
+
animate,
|
|
6
|
+
motion,
|
|
7
|
+
MotionConfigContext,
|
|
8
|
+
useMotionValue,
|
|
9
|
+
useMotionValueEvent,
|
|
10
|
+
useReducedMotion,
|
|
11
|
+
useTransform,
|
|
12
|
+
type MotionValue,
|
|
13
|
+
type PanInfo,
|
|
14
|
+
} from "motion/react"
|
|
15
|
+
import { cn } from "@/lib/utils"
|
|
16
|
+
|
|
17
|
+
/** `useReducedMotion` reads the OS setting only, and only at mount. Also honour an
|
|
18
|
+
* app-level `<MotionConfig reducedMotion>`, so a user preference in the host app
|
|
19
|
+
* reaches this component. */
|
|
20
|
+
function useReducedMotionConfig() {
|
|
21
|
+
const config = React.useContext(MotionConfigContext)
|
|
22
|
+
const prefersReduced = useReducedMotion()
|
|
23
|
+
if (config.reducedMotion === "always") return true
|
|
24
|
+
if (config.reducedMotion === "never") return false
|
|
25
|
+
return prefersReduced ?? false
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const EASE_OUT: [number, number, number, number] = [0.23, 1, 0.32, 1]
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Seconds of deceleration to project forward before deciding where the row belongs.
|
|
32
|
+
*
|
|
33
|
+
* UIScrollView's decay works out near this for a flick. Below about 0.1 the projection is
|
|
34
|
+
* too weak to feel; above about 0.3 a light flick overshoots the whole row.
|
|
35
|
+
*/
|
|
36
|
+
const PROJECTION = 0.18
|
|
37
|
+
|
|
38
|
+
const SETTLE = { type: "spring", stiffness: 420, damping: 42, mass: 0.9 } as const
|
|
39
|
+
|
|
40
|
+
/** Width of one action button, in px. Wide enough for a 44px touch target with room. */
|
|
41
|
+
const DEFAULT_ACTION_WIDTH = 68
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Holds the close callback of whichever row is currently open.
|
|
45
|
+
*
|
|
46
|
+
* Two open rows is a state the reader never asked for: they swiped a second row, so they
|
|
47
|
+
* are done with the first. Kept in a ref rather than state because closing a row is a side
|
|
48
|
+
* effect on a motion value, and routing it through a render would put a frame between the
|
|
49
|
+
* new row opening and the old one giving way.
|
|
50
|
+
*/
|
|
51
|
+
const SwipeableRowGroupContext =
|
|
52
|
+
React.createContext<React.RefObject<(() => void) | null> | null>(null)
|
|
53
|
+
|
|
54
|
+
/** Wrap a list in this to keep at most one row open at a time. Optional; a row outside a
|
|
55
|
+
* group works, it just does not know about its neighbours. */
|
|
56
|
+
function SwipeableRowGroup({ children }: { children: React.ReactNode }) {
|
|
57
|
+
const openRow = React.useRef<(() => void) | null>(null)
|
|
58
|
+
return (
|
|
59
|
+
<SwipeableRowGroupContext.Provider value={openRow}>
|
|
60
|
+
{children}
|
|
61
|
+
</SwipeableRowGroupContext.Provider>
|
|
62
|
+
)
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export interface SwipeableRowAction {
|
|
66
|
+
/** Accessible name for the button. It has only an icon, so this is the only name. */
|
|
67
|
+
label: string
|
|
68
|
+
icon: React.ReactNode
|
|
69
|
+
onSelect: () => void
|
|
70
|
+
/** Styles the button as destructive, and lends its icon to the full-swipe layer. */
|
|
71
|
+
destructive?: boolean
|
|
72
|
+
className?: string
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export interface SwipeableRowProps
|
|
76
|
+
extends Omit<React.ComponentProps<"div">, "onDragEnd"> {
|
|
77
|
+
/** Revealed under the row, right to left. Also the keyboard path: they are real
|
|
78
|
+
* buttons, and focusing one opens the row. */
|
|
79
|
+
actions: SwipeableRowAction[]
|
|
80
|
+
/**
|
|
81
|
+
* Committing a full swipe. Omit it and the row only ever opens — there is no
|
|
82
|
+
* threshold, and no destructive layer.
|
|
83
|
+
*/
|
|
84
|
+
onDismiss?: () => void
|
|
85
|
+
/** Fraction of the row's width past which releasing commits `onDismiss`. */
|
|
86
|
+
dismissAt?: number
|
|
87
|
+
/** Width of each action button in px. */
|
|
88
|
+
actionWidth?: number
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* A row that reveals actions under a swipe.
|
|
93
|
+
*
|
|
94
|
+
* The whole quality of this interaction lives in the release. Snapping to the nearest
|
|
95
|
+
* position ignores that the finger was still moving, and the row stops dead under a hand
|
|
96
|
+
* that was clearly throwing it. What makes it feel native is *projection*: where would
|
|
97
|
+
* this row end up if it kept decelerating the way a flicked object does? That projected
|
|
98
|
+
* point is what gets compared against the thresholds, so a short fast flick opens the row
|
|
99
|
+
* and a long slow drag that stops short falls back — which is what the hand meant in both
|
|
100
|
+
* cases.
|
|
101
|
+
*
|
|
102
|
+
* The spring that takes over is handed the pointer's exit velocity, so there is no seam
|
|
103
|
+
* between the finger driving the row and the animation driving it.
|
|
104
|
+
*
|
|
105
|
+
* The projection is measured from where the row *is*, not from how far the finger moved.
|
|
106
|
+
* Under drag elasticity those two disagree, and the eye is following the row.
|
|
107
|
+
*/
|
|
108
|
+
const SwipeableRow = React.forwardRef<HTMLDivElement, SwipeableRowProps>(
|
|
109
|
+
// The gesture is already out in `useSwipeGesture`. What is left is four layers that
|
|
110
|
+
// each exist only under some condition — actions, the committed wash, the armed icon,
|
|
111
|
+
// the row itself — and reduced motion changes three of them.
|
|
112
|
+
// fallow-ignore-next-line complexity
|
|
113
|
+
(
|
|
114
|
+
{
|
|
115
|
+
actions,
|
|
116
|
+
onDismiss,
|
|
117
|
+
dismissAt = 0.5,
|
|
118
|
+
actionWidth = DEFAULT_ACTION_WIDTH,
|
|
119
|
+
className,
|
|
120
|
+
children,
|
|
121
|
+
...props
|
|
122
|
+
},
|
|
123
|
+
ref
|
|
124
|
+
) => {
|
|
125
|
+
const reducedMotion = useReducedMotionConfig()
|
|
126
|
+
const x = useMotionValue(0)
|
|
127
|
+
const rowRef = React.useRef<HTMLDivElement>(null)
|
|
128
|
+
|
|
129
|
+
/** Where the row rests with its actions showing. Derived, so adding an action moves
|
|
130
|
+
* the resting point instead of leaving a button off the edge. */
|
|
131
|
+
const openAt = -actionWidth * actions.length
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* Measured, not assumed. The dismiss threshold is a fraction of the row, and the drag
|
|
135
|
+
* has to be free to travel the full width or the threshold sits somewhere the finger
|
|
136
|
+
* cannot reach.
|
|
137
|
+
*/
|
|
138
|
+
const [width, setWidth] = React.useState(0)
|
|
139
|
+
React.useLayoutEffect(() => {
|
|
140
|
+
const node = rowRef.current
|
|
141
|
+
if (!node || typeof ResizeObserver === "undefined") return
|
|
142
|
+
const observer = new ResizeObserver(([entry]) =>
|
|
143
|
+
setWidth(entry.contentRect.width)
|
|
144
|
+
)
|
|
145
|
+
observer.observe(node)
|
|
146
|
+
return () => observer.disconnect()
|
|
147
|
+
}, [])
|
|
148
|
+
|
|
149
|
+
// The actions are already in the DOM behind the row; they only need to look like they
|
|
150
|
+
// are being uncovered rather than sitting there waiting. Scaling them off the row's
|
|
151
|
+
// own position ties the two together exactly, with no second clock to drift.
|
|
152
|
+
const actionScale = useTransform(x, [openAt, 0], [1, 0.8])
|
|
153
|
+
const actionOpacity = useTransform(x, [openAt, openAt / 3, 0], [1, 0.6, 0])
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* Whether releasing now would commit.
|
|
157
|
+
*
|
|
158
|
+
* The reader cannot be asked to estimate a threshold from how far their own finger has
|
|
159
|
+
* travelled, so the row states it: past the point of no return the destructive action
|
|
160
|
+
* takes the whole width. It is a binary fact and it snaps rather than fading in
|
|
161
|
+
* proportionally — a half-committed delete is not a thing.
|
|
162
|
+
*/
|
|
163
|
+
const [armed, setArmed] = React.useState(false)
|
|
164
|
+
useMotionValueEvent(x, "change", (value) => {
|
|
165
|
+
if (!width || !onDismiss) return
|
|
166
|
+
setArmed(value < -width * dismissAt)
|
|
167
|
+
})
|
|
168
|
+
|
|
169
|
+
const { settle, onDragEnd } = useSwipeGesture({
|
|
170
|
+
x,
|
|
171
|
+
openAt,
|
|
172
|
+
width,
|
|
173
|
+
dismissAt,
|
|
174
|
+
onDismiss,
|
|
175
|
+
})
|
|
176
|
+
|
|
177
|
+
const destructive = actions.find((action) => action.destructive)
|
|
178
|
+
const snap = reducedMotion
|
|
179
|
+
? { duration: 0 }
|
|
180
|
+
: { duration: 0.16, ease: EASE_OUT }
|
|
181
|
+
|
|
182
|
+
return (
|
|
183
|
+
<div
|
|
184
|
+
ref={mergeRefs(ref, rowRef)}
|
|
185
|
+
className={cn("relative overflow-hidden rounded-xl", className)}
|
|
186
|
+
{...props}
|
|
187
|
+
>
|
|
188
|
+
<div className="absolute inset-y-0 right-0 flex items-stretch">
|
|
189
|
+
{actions.map((action) => (
|
|
190
|
+
// Real buttons, always focusable. A swipe is not an accessible affordance on
|
|
191
|
+
// its own, so the keyboard path is the same set of controls — focusing one
|
|
192
|
+
// opens the row so the reader can see what they are on.
|
|
193
|
+
<motion.button
|
|
194
|
+
key={action.label}
|
|
195
|
+
type="button"
|
|
196
|
+
aria-label={action.label}
|
|
197
|
+
onFocus={() => settle(openAt)}
|
|
198
|
+
onBlur={() => settle(0)}
|
|
199
|
+
onClick={action.onSelect}
|
|
200
|
+
// Width inline rather than as a class, so `actionWidth` cannot drift out of
|
|
201
|
+
// step with a Tailwind arbitrary value.
|
|
202
|
+
style={{
|
|
203
|
+
width: actionWidth,
|
|
204
|
+
...(reducedMotion
|
|
205
|
+
? null
|
|
206
|
+
: { scale: actionScale, opacity: actionOpacity }),
|
|
207
|
+
}}
|
|
208
|
+
className={cn(
|
|
209
|
+
"flex shrink-0 items-center justify-center focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-inset focus-visible:ring-ring",
|
|
210
|
+
action.destructive
|
|
211
|
+
? "bg-destructive text-destructive-foreground"
|
|
212
|
+
: "bg-muted text-foreground",
|
|
213
|
+
action.className
|
|
214
|
+
)}
|
|
215
|
+
>
|
|
216
|
+
{action.icon}
|
|
217
|
+
</motion.button>
|
|
218
|
+
))}
|
|
219
|
+
</div>
|
|
220
|
+
|
|
221
|
+
{onDismiss ? (
|
|
222
|
+
<>
|
|
223
|
+
{/* The committed layer, painted over the actions so it swallows them instead
|
|
224
|
+
of having to fade each one out. Scaled from its right edge rather than
|
|
225
|
+
widened, so the whole thing stays one composited transform. */}
|
|
226
|
+
<motion.div
|
|
227
|
+
aria-hidden
|
|
228
|
+
className="pointer-events-none absolute inset-0 origin-right bg-destructive"
|
|
229
|
+
initial={{ scaleX: 0 }}
|
|
230
|
+
animate={{ scaleX: armed ? 1 : 0 }}
|
|
231
|
+
transition={snap}
|
|
232
|
+
/>
|
|
233
|
+
<motion.div
|
|
234
|
+
aria-hidden
|
|
235
|
+
className="pointer-events-none absolute inset-y-0 right-0 flex items-center justify-center text-destructive-foreground"
|
|
236
|
+
style={{ width: actionWidth }}
|
|
237
|
+
initial={{ opacity: 0, scale: 1 }}
|
|
238
|
+
animate={{ opacity: armed ? 1 : 0, scale: armed ? 1.15 : 1 }}
|
|
239
|
+
transition={snap}
|
|
240
|
+
>
|
|
241
|
+
{destructive?.icon}
|
|
242
|
+
</motion.div>
|
|
243
|
+
</>
|
|
244
|
+
) : null}
|
|
245
|
+
|
|
246
|
+
<motion.div
|
|
247
|
+
drag={reducedMotion ? false : "x"}
|
|
248
|
+
style={{ x }}
|
|
249
|
+
// Free to travel the whole width. The open position is a resting point the
|
|
250
|
+
// release decides on, not a wall the finger has to fight through — anything the
|
|
251
|
+
// gesture must overcome to reach the dismiss threshold makes the threshold feel
|
|
252
|
+
// like a bug.
|
|
253
|
+
dragConstraints={{ left: width ? -width : openAt * 2, right: 0 }}
|
|
254
|
+
// Past the constraints the row still moves, but under resistance. A hard wall
|
|
255
|
+
// reads as a broken gesture; the give says there is nothing further left.
|
|
256
|
+
dragElastic={{ left: 0.06, right: 0.08 }}
|
|
257
|
+
// The projection here is ours. Motion's own momentum would fight it.
|
|
258
|
+
dragMomentum={false}
|
|
259
|
+
onDragEnd={onDragEnd}
|
|
260
|
+
className={cn(
|
|
261
|
+
// `rounded-[inherit]`, not a radius of its own: the container clips, and a
|
|
262
|
+
// square border inside a rounded clip loses its four corners to it. Inheriting
|
|
263
|
+
// means the border still follows whatever radius the caller set.
|
|
264
|
+
"relative flex touch-pan-y items-center rounded-[inherit] border border-border bg-background",
|
|
265
|
+
// The row is only draggable when it is allowed to move, so it only claims the
|
|
266
|
+
// cursor then. `grabbing` on :active rather than on a drag state, so the change
|
|
267
|
+
// lands on the press instead of on the first pixel of travel.
|
|
268
|
+
!reducedMotion && "cursor-grab active:cursor-grabbing"
|
|
269
|
+
)}
|
|
270
|
+
>
|
|
271
|
+
<div
|
|
272
|
+
className="min-w-0 flex-1 px-4 py-3.5"
|
|
273
|
+
// Under reduced motion the row never moves, so the actions have to be given
|
|
274
|
+
// room rather than revealed.
|
|
275
|
+
style={reducedMotion ? { paddingRight: -openAt + 16 } : undefined}
|
|
276
|
+
>
|
|
277
|
+
{children}
|
|
278
|
+
</div>
|
|
279
|
+
</motion.div>
|
|
280
|
+
</div>
|
|
281
|
+
)
|
|
282
|
+
}
|
|
283
|
+
)
|
|
284
|
+
SwipeableRow.displayName = "SwipeableRow"
|
|
285
|
+
|
|
286
|
+
/**
|
|
287
|
+
* Everything the release decides: where the row lands, and whether it lands at all.
|
|
288
|
+
*
|
|
289
|
+
* Kept apart from the render because it is the only part with a memory — of which row in
|
|
290
|
+
* the group is currently open, and of the velocity the finger left behind.
|
|
291
|
+
*/
|
|
292
|
+
function useSwipeGesture({
|
|
293
|
+
x,
|
|
294
|
+
openAt,
|
|
295
|
+
width,
|
|
296
|
+
dismissAt,
|
|
297
|
+
onDismiss,
|
|
298
|
+
}: {
|
|
299
|
+
x: MotionValue<number>
|
|
300
|
+
openAt: number
|
|
301
|
+
width: number
|
|
302
|
+
dismissAt: number
|
|
303
|
+
onDismiss?: () => void
|
|
304
|
+
}) {
|
|
305
|
+
const groupRef = React.useContext(SwipeableRowGroupContext)
|
|
306
|
+
|
|
307
|
+
const close = React.useCallback(() => {
|
|
308
|
+
animate(x, 0, SETTLE)
|
|
309
|
+
}, [x])
|
|
310
|
+
|
|
311
|
+
const claim = React.useCallback(() => {
|
|
312
|
+
if (!groupRef) return
|
|
313
|
+
if (groupRef.current && groupRef.current !== close) groupRef.current()
|
|
314
|
+
groupRef.current = close
|
|
315
|
+
}, [groupRef, close])
|
|
316
|
+
|
|
317
|
+
const release = React.useCallback(() => {
|
|
318
|
+
if (groupRef?.current === close) groupRef.current = null
|
|
319
|
+
}, [groupRef, close])
|
|
320
|
+
|
|
321
|
+
const settle = React.useCallback(
|
|
322
|
+
(to: number, velocity = 0) => {
|
|
323
|
+
if (to === 0) release()
|
|
324
|
+
else claim()
|
|
325
|
+
// Handing the pointer's velocity to the spring is the entire trick. Without it the
|
|
326
|
+
// row restarts from rest and the gesture visibly ends twice.
|
|
327
|
+
animate(x, to, { ...SETTLE, velocity })
|
|
328
|
+
},
|
|
329
|
+
[x, claim, release]
|
|
330
|
+
)
|
|
331
|
+
|
|
332
|
+
const onDragEnd = React.useCallback(
|
|
333
|
+
(_: unknown, info: PanInfo) => {
|
|
334
|
+
const projected = x.get() + info.velocity.x * PROJECTION
|
|
335
|
+
|
|
336
|
+
if (onDismiss && width && projected < -width * dismissAt) {
|
|
337
|
+
release()
|
|
338
|
+
// Thrown far enough to mean "gone". Finish the travel rather than snapping back
|
|
339
|
+
// and asking the reader to confirm what they already did.
|
|
340
|
+
animate(x, -width, {
|
|
341
|
+
type: "spring",
|
|
342
|
+
velocity: info.velocity.x,
|
|
343
|
+
stiffness: 420,
|
|
344
|
+
damping: 46,
|
|
345
|
+
onComplete: onDismiss,
|
|
346
|
+
})
|
|
347
|
+
return
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
settle(projected < openAt / 2 ? openAt : 0, info.velocity.x)
|
|
351
|
+
},
|
|
352
|
+
[x, openAt, width, dismissAt, onDismiss, release, settle]
|
|
353
|
+
)
|
|
354
|
+
|
|
355
|
+
return { settle, onDragEnd }
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
/** The row needs its own measurement and the caller may want the node too. */
|
|
359
|
+
function mergeRefs<T>(...refs: (React.Ref<T> | undefined)[]): React.RefCallback<T> {
|
|
360
|
+
return (value) => {
|
|
361
|
+
for (const ref of refs) {
|
|
362
|
+
if (typeof ref === "function") ref(value)
|
|
363
|
+
else if (ref) (ref as React.RefObject<T | null>).current = value
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
export { SwipeableRow, SwipeableRowGroup }
|