aboutsummaryrefslogtreecommitdiff
path: root/apps/web/components/text-effect.tsx
blob: 0cbb67ce5020c7e303db030c56fa1c18af012fce (plain) (blame)
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
"use client"
import { cn } from "@lib/utils"
import { AnimatePresence, motion } from "motion/react"
import type {
	TargetAndTransition,
	Transition,
	Variant,
	Variants,
} from "motion/react"
import React from "react"

export type PresetType = "blur" | "fade-in-blur" | "scale" | "fade" | "slide"

export type PerType = "word" | "char" | "line"

export type TextEffectProps = {
	children: string
	per?: PerType
	as?: keyof React.JSX.IntrinsicElements
	variants?: {
		container?: Variants
		item?: Variants
	}
	className?: string
	preset?: PresetType
	delay?: number
	speedReveal?: number
	speedSegment?: number
	trigger?: boolean
	onAnimationComplete?: () => void
	onAnimationStart?: () => void
	segmentWrapperClassName?: string
	containerTransition?: Transition
	segmentTransition?: Transition
	style?: React.CSSProperties
}

const defaultStaggerTimes: Record<PerType, number> = {
	char: 0.03,
	word: 0.05,
	line: 0.1,
}

const defaultContainerVariants: Variants = {
	hidden: { opacity: 0 },
	visible: {
		opacity: 1,
		transition: {
			staggerChildren: 0.05,
		},
	},
	exit: {
		transition: { staggerChildren: 0.05, staggerDirection: -1 },
	},
}

const defaultItemVariants: Variants = {
	hidden: { opacity: 0 },
	visible: {
		opacity: 1,
	},
	exit: { opacity: 0 },
}

const presetVariants: Record<
	PresetType,
	{ container: Variants; item: Variants }
> = {
	blur: {
		container: defaultContainerVariants,
		item: {
			hidden: { opacity: 0, filter: "blur(12px)" },
			visible: { opacity: 1, filter: "blur(0px)" },
			exit: { opacity: 0, filter: "blur(12px)" },
		},
	},
	"fade-in-blur": {
		container: defaultContainerVariants,
		item: {
			hidden: { opacity: 0, y: 20, filter: "blur(12px)" },
			visible: { opacity: 1, y: 0, filter: "blur(0px)" },
			exit: { opacity: 0, y: 20, filter: "blur(12px)" },
		},
	},
	scale: {
		container: defaultContainerVariants,
		item: {
			hidden: { opacity: 0, scale: 0 },
			visible: { opacity: 1, scale: 1 },
			exit: { opacity: 0, scale: 0 },
		},
	},
	fade: {
		container: defaultContainerVariants,
		item: {
			hidden: { opacity: 0 },
			visible: { opacity: 1 },
			exit: { opacity: 0 },
		},
	},
	slide: {
		container: defaultContainerVariants,
		item: {
			hidden: { opacity: 0, y: 20 },
			visible: { opacity: 1, y: 0 },
			exit: { opacity: 0, y: 20 },
		},
	},
}

const AnimationComponent: React.FC<{
	segment: string
	variants: Variants
	per: "line" | "word" | "char"
	segmentWrapperClassName?: string
}> = React.memo(({ segment, variants, per, segmentWrapperClassName }) => {
	const content =
		per === "line" ? (
			<motion.span variants={variants} className="block">
				{segment}
			</motion.span>
		) : per === "word" ? (
			<motion.span
				aria-hidden="true"
				variants={variants}
				className="inline-block whitespace-pre"
			>
				{segment}
			</motion.span>
		) : (
			<motion.span className="inline-block whitespace-pre">
				{segment.split("").map((char, charIndex) => (
					<motion.span
						// biome-ignore lint/suspicious/noArrayIndexKey: Character position is stable within animation
						key={`char-${charIndex}`}
						aria-hidden="true"
						variants={variants}
						className="inline-block whitespace-pre"
					>
						{char}
					</motion.span>
				))}
			</motion.span>
		)

	if (!segmentWrapperClassName) {
		return content
	}

	const defaultWrapperClassName = per === "line" ? "block" : "inline-block"

	return (
		<span className={cn(defaultWrapperClassName, segmentWrapperClassName)}>
			{content}
		</span>
	)
})

AnimationComponent.displayName = "AnimationComponent"

const splitText = (text: string, per: PerType) => {
	if (per === "line") return text.split("\n")
	return text.split(/(\s+)/)
}

const hasTransition = (
	variant?: Variant,
): variant is TargetAndTransition & { transition?: Transition } => {
	if (!variant) return false
	return typeof variant === "object" && "transition" in variant
}

const createVariantsWithTransition = (
	baseVariants: Variants,
	transition?: Transition & { exit?: Transition },
): Variants => {
	if (!transition) return baseVariants

	const { exit: _, ...mainTransition } = transition

	return {
		...baseVariants,
		visible: {
			...baseVariants.visible,
			transition: {
				...(hasTransition(baseVariants.visible)
					? baseVariants.visible.transition
					: {}),
				...mainTransition,
			},
		},
		exit: {
			...baseVariants.exit,
			transition: {
				...(hasTransition(baseVariants.exit)
					? baseVariants.exit.transition
					: {}),
				...mainTransition,
				staggerDirection: -1,
			},
		},
	}
}

export function TextEffect({
	children,
	per = "word",
	as = "p",
	variants,
	className,
	preset = "fade",
	delay = 0,
	speedReveal = 1,
	speedSegment = 1,
	trigger = true,
	onAnimationComplete,
	onAnimationStart,
	segmentWrapperClassName,
	containerTransition,
	segmentTransition,
	style,
}: TextEffectProps) {
	const segments = splitText(children, per)
	const MotionTag = motion[as as keyof typeof motion] as typeof motion.div

	const baseVariants = preset
		? presetVariants[preset]
		: { container: defaultContainerVariants, item: defaultItemVariants }

	const stagger = defaultStaggerTimes[per] / speedReveal

	const baseDuration = 0.3 / speedSegment

	const customStagger = hasTransition(variants?.container?.visible ?? {})
		? (variants?.container?.visible as TargetAndTransition).transition
				?.staggerChildren
		: undefined

	const customDelay = hasTransition(variants?.container?.visible ?? {})
		? (variants?.container?.visible as TargetAndTransition).transition
				?.delayChildren
		: undefined

	const computedVariants = {
		container: createVariantsWithTransition(
			variants?.container || baseVariants.container,
			{
				staggerChildren: customStagger ?? stagger,
				delayChildren: customDelay ?? delay,
				...containerTransition,
				exit: {
					staggerChildren: customStagger ?? stagger,
					staggerDirection: -1,
				},
			},
		),
		item: createVariantsWithTransition(variants?.item || baseVariants.item, {
			duration: baseDuration,
			...segmentTransition,
		}),
	}

	return (
		<AnimatePresence mode="popLayout">
			{trigger && (
				<MotionTag
					initial="hidden"
					animate="visible"
					exit="exit"
					variants={computedVariants.container}
					className={className}
					onAnimationComplete={onAnimationComplete}
					onAnimationStart={onAnimationStart}
					style={style}
				>
					{per !== "line" ? <span className="sr-only">{children}</span> : null}
					{segments.map((segment, index) => (
						<AnimationComponent
							key={`${per}-${index}-${segment}`}
							segment={segment}
							variants={computedVariants.item}
							per={per}
							segmentWrapperClassName={segmentWrapperClassName}
						/>
					))}
				</MotionTag>
			)}
		</AnimatePresence>
	)
}