Components
Text Shimmer
Text Shimmer
A React component that creates an animated text shimmer effect using Motion, customizable by tag, duration, spread, delay, and repeat delay.
Loading...
Installation
1
Install the packages
npm i motion clsx tailwind-merge
2
Add util file
lib/util.ts
import { ClassValue, clsx } from "clsx";
import { twMerge } from "tailwind-merge";
export function cn(...inputs: ClassValue[]) {
return twMerge(clsx(inputs));
}
import { ClassValue, clsx } from "clsx";
import { twMerge } from "tailwind-merge";
export function cn(...inputs: ClassValue[]) {
return twMerge(clsx(inputs));
}
3
Copy and paste the following code into your project
text-shimmer.tsx
"use client";
import React, { useMemo, type JSX } from "react";
import { motion } from "motion/react";
import { cn } from "@/lib/utils";
export type TextShimmerProps = {
children: string;
as?: React.ElementType;
className?: string;
duration?: number;
spread?: number;
delay?: number;
repeatDelay?: number;
};
const TextShimmer = ({
children,
as: Component = "p",
className,
duration = 2,
spread = 2,
delay = 0,
repeatDelay = 0,
}: 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]",
"[--bg:linear-gradient(90deg,#0000_calc(50%-var(--spread)),var(--base-gradient-color),#0000_calc(50%+var(--spread)))] [background-repeat:no-repeat,padding-box]",
"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: "105% center" }}
animate={{ backgroundPosition: "-5% center" }}
transition={{
repeat: Number.POSITIVE_INFINITY,
duration,
ease: "linear",
delay,
repeatDelay,
}}
style={
{
"--spread": `${dynamicSpread}px`,
backgroundImage: `var(--bg), linear-gradient(var(--base-color), var(--base-color))`,
} as React.CSSProperties
}
>
{children}
</MotionComponent>
);
};
export default React.memo(TextShimmer);
"use client";
import React, { useMemo, type JSX } from "react";
import { motion } from "motion/react";
import { cn } from "@/lib/utils";
export type TextShimmerProps = {
children: string;
as?: React.ElementType;
className?: string;
duration?: number;
spread?: number;
delay?: number;
repeatDelay?: number;
};
const TextShimmer = ({
children,
as: Component = "p",
className,
duration = 2,
spread = 2,
delay = 0,
repeatDelay = 0,
}: 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]",
"[--bg:linear-gradient(90deg,#0000_calc(50%-var(--spread)),var(--base-gradient-color),#0000_calc(50%+var(--spread)))] [background-repeat:no-repeat,padding-box]",
"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: "105% center" }}
animate={{ backgroundPosition: "-5% center" }}
transition={{
repeat: Number.POSITIVE_INFINITY,
duration,
ease: "linear",
delay,
repeatDelay,
}}
style={
{
"--spread": `${dynamicSpread}px`,
backgroundImage: `var(--bg), linear-gradient(var(--base-color), var(--base-color))`,
} as React.CSSProperties
}
>
{children}
</MotionComponent>
);
};
export default React.memo(TextShimmer);
4
Update the import paths to match your project setup
Props
Prop | Type | Default | Description |
---|---|---|---|
children | string | - | The text content to apply the shimmer effect to. |
as | React.ElementType | "p" | The HTML tag to render (e.g., span, h1, p). |
className | string | - | Optional Tailwind class names to style the text. |
duration | number | 2 | Time (in seconds) for the shimmer animation to complete. |
spread | number | 2 | Controls the width of the shimmer highlight. |
delay | number | 0 | Time delay before the animation starts. |
repeatDelay | number | 0 | Delay before repeating the animation. |