Slide Up Reveal

Buttery smooth text reveal with slide up reveal animation.

Howwwwwwww youu doin :)
TSX
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
"use client"; import { AnimationOptions, motion } from "motion/react"; import { forwardRef, useCallback, useEffect, useImperativeHandle, useMemo, useState, } from "react"; import { cn } from "@/lib/utils"; interface SlideUpRevealProps { children: React.ReactNode; split?: "words" | "characters" | "lines"; delay?: number; stagger?: number; from?: "first" | "last" | "center"; transition?: AnimationOptions; className?: string; wordClass?: string; charClass?: string; autoStart?: boolean; onStart?: () => void; onComplete?: () => void; inView?: boolean; once?: boolean; } export interface SlideUpRevealRef { startAnimation: () => void; reset: () => void; } interface WordObject { characters: string[]; needsSpace: boolean; } const SlideUpReveal = forwardRef<SlideUpRevealRef, SlideUpRevealProps>( ( { children, split = "words", delay = 0, stagger = 0.1, from = "first", transition = { type: "tween", ease: [0.625, 0.05, 0, 1], duration: 0.5, }, className, wordClass, charClass, autoStart = true, onStart, onComplete, inView = false, once = true, ...props }, ref, ) => { const text = typeof children === "string" ? children : children?.toString() || ""; const [isAnimating, setIsAnimating] = useState(false); const splitIntoCharacters = (text: string): string[] => { if (typeof Intl !== "undefined" && "Segmenter" in Intl) { const segmenter = new Intl.Segmenter("en", { granularity: "grapheme" }); return Array.from(segmenter.segment(text), ({ segment }) => segment); } return Array.from(text); }; const elements = useMemo(() => { const words = text.split(" "); if (split === "characters") { return words.map((word, i) => ({ characters: splitIntoCharacters(word), needsSpace: i !== words.length - 1, })); } return split === "words" ? text.split(" ") : text.split(" "); }, [text, split]); const getStaggerDelay = useCallback( (index: number) => { const total = split === "characters" ? elements.reduce( (acc, word) => acc + (typeof word === "string" ? 1 : word.characters.length + (word.needsSpace ? 1 : 0)), 0, ) : elements.length; if (from === "first") return index * stagger; if (from === "last") return (total - 1 - index) * stagger; if (from === "center") { const center = Math.floor(total / 2); return Math.abs(center - index) * stagger; } return index * stagger; }, [elements, from, stagger, split], ); const startAnimation = useCallback(() => { setIsAnimating(true); onStart?.(); }, [onStart]); useImperativeHandle(ref, () => ({ startAnimation, reset: () => setIsAnimating(false), })); useEffect(() => { if (autoStart && !inView) { startAnimation(); } }, [autoStart, inView, startAnimation]); const variants = { hidden: { y: "100%" }, visible: (i: number) => ({ y: 0, transition: { ...transition, delay: delay + ((transition?.delay as number) || 0) + getStaggerDelay(i), }, }), }; return ( <motion.span className={cn( className, "flex flex-wrap whitespace-pre-wrap", split === "lines" && "flex-col", )} initial="hidden" whileInView={inView ? "visible" : undefined} animate={inView ? undefined : isAnimating ? "visible" : "hidden"} viewport={{ once }} onAnimationStart={() => { if (inView) { setIsAnimating(true); onStart?.(); } }} {...props} > <span className="sr-only">{text}</span> {(split === "characters" ? (elements as WordObject[]) : (elements as string[]).map((el, i, arr) => ({ characters: [el], needsSpace: split === "words" && i !== arr.length - 1, })) ).map((wordObj, wordIndex, array) => { const previousCharsCount = array .slice(0, wordIndex) .reduce((sum, word) => sum + word.characters.length, 0); return ( <span key={wordIndex} aria-hidden="true" className={cn("inline-flex overflow-hidden", wordClass)} > {wordObj.characters.map((char, charIndex) => ( <span className={cn( charClass, "whitespace-pre-wrap relative overflow-hidden", )} key={charIndex} > <motion.span custom={previousCharsCount + charIndex} initial="hidden" animate={isAnimating ? "visible" : "hidden"} variants={variants} onAnimationComplete={ wordIndex === array.length - 1 && charIndex === wordObj.characters.length - 1 ? onComplete : undefined } className="inline-block" > {char} </motion.span> </span> ))} {wordObj.needsSpace && ( <span className="relative overflow-hidden"> <motion.span custom={previousCharsCount + wordObj.characters.length} initial="hidden" animate={isAnimating ? "visible" : "hidden"} variants={variants} className="inline-block" > {" "} </motion.span> </span> )} </span> ); })} </motion.span> ); }, ); SlideUpReveal.displayName = "SlideUpReveal"; export default SlideUpReveal; export type { SlideUpRevealProps };

Installation

pnpm dlx shadcn@latest add @satoriui/slide-up-reveal