aboutsummaryrefslogtreecommitdiff
path: root/apps/web/components/text-shimmer.tsx
blob: a9abd87022ebf580f4f8d32b97ae0fe84fed58b1 (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
"use client"
import { cn } from "@lib/utils"
import { motion } from "motion/react"
import React, { type JSX, useMemo } from "react"

export type TextShimmerProps = {
	children: string
	as?: React.ElementType
	className?: string
	duration?: number
	spread?: number
}

function TextShimmerComponent({
	children,
	as: Component = "p",
	className,
	duration = 2,
	spread = 2,
}: TextShimmerProps) {
	const MotionComponent = motion.create(
		Component as keyof JSX.IntrinsicElements,
	)

	const dynamicSpread = useMemo(() => {
		return children.length * spread
	}, [children, spread])

	return (
		<MotionComponent
			className={cn(
				"relative inline-block bg-[length:250%_100%,auto] bg-clip-text",
				"text-transparent [--base-color:#a1a1aa] [--base-gradient-color:#000]",
				"[background-repeat:no-repeat,padding-box] [--bg:linear-gradient(90deg,#0000_calc(50%-var(--spread)),var(--base-gradient-color),#0000_calc(50%+var(--spread)))]",
				"dark:[--base-color:#71717a] dark:[--base-gradient-color:#ffffff] dark:[--bg:linear-gradient(90deg,#0000_calc(50%-var(--spread)),var(--base-gradient-color),#0000_calc(50%+var(--spread)))]",
				className,
			)}
			initial={{ backgroundPosition: "100% center" }}
			animate={{ backgroundPosition: "0% center" }}
			transition={{
				repeat: Number.POSITIVE_INFINITY,
				duration,
				ease: "linear",
			}}
			style={
				{
					"--spread": `${dynamicSpread}px`,
					backgroundImage:
						"var(--bg), linear-gradient(var(--base-color), var(--base-color))",
				} as React.CSSProperties
			}
		>
			{children}
		</MotionComponent>
	)
}

export const TextShimmer = React.memo(TextShimmerComponent)