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)); }
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);
4

Update the import paths to match your project setup

Props

PropTypeDefaultDescription
childrenstring-The text content to apply the shimmer effect to.
asReact.ElementType"p"The HTML tag to render (e.g., span, h1, p).
classNamestring-Optional Tailwind class names to style the text.
durationnumber2Time (in seconds) for the shimmer animation to complete.
spreadnumber2Controls the width of the shimmer highlight.
delaynumber0Time delay before the animation starts.
repeatDelaynumber0Delay before repeating the animation.