Shimmer effect on text.
1'use client';
2import React, { useMemo, type JSX } from 'react';
3import { motion } from 'motion/react';
4import { cn } from '@/lib/utils';
5
6export type TextShimmerProps = {
7 children: string;
8 as?: React.ElementType;
9 className?: string;
10 duration?: number;
11 spread?: number;
12};
13
14export function TextShimmer({
15 children,
16 as: Component = 'p',
17 className,
18 duration = 2,
19 spread = 2,
20}: TextShimmerProps) {
21 const MotionComponent = motion.create(
22 Component as keyof JSX.IntrinsicElements
23 );
24
25 const dynamicSpread = useMemo(() => {
26 return children.length * spread;
27 }, [children, spread]);
28
29 return (
30 <MotionComponent
31 className={cn(
32 'relative inline-block bg-[length:250%_100%,auto] bg-clip-text',
33 'text-transparent [--base-color:#a1a1aa] [--base-gradient-color:#000]',
34 '[--bg:linear-gradient(90deg,#0000_calc(50%-var(--spread)),var(--base-gradient-color),#0000_calc(50%+var(--spread)))] [background-repeat:no-repeat,padding-box]',
35 '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)))]',
36 className
37 )}
38 initial={{ backgroundPosition: '100% center' }}
39 animate={{ backgroundPosition: '0% center' }}
40 transition={{
41 repeat: Infinity,
42 duration,
43 ease: 'linear',
44 }}
45 style={
46 {
47 '--spread': `${dynamicSpread}px`,
48 backgroundImage: `var(--bg), linear-gradient(var(--base-color), var(--base-color))`,
49 } as React.CSSProperties
50 }
51 >
52 {children}
53 </MotionComponent>
54 );
55}
56