Thinking UI

Text Shimmer

A highlight sweeps across a label to show work in progress.

Code

components/thinking-ui/text-shimmer.tsx
"use client";

import { motion } from "motion/react";
import type { CSSProperties } from "react";
import { useMotion } from "@/lib/motion";
import { cn } from "@/lib/utils";

export type TextShimmerProps = {
  children: string;
  /** Seconds for one sweep. */
  duration?: number;
  /** Width of the highlight, in px per character. */
  spread?: number;
  className?: string;
};

export function TextShimmer({ children, duration = 1.8, spread = 2, className }: TextShimmerProps) {
  const m = useMotion();

  if (m.reduce) {
    return <span className={cn("text-fg-muted", className)}>{children}</span>;
  }

  return (
    <motion.span
      className={cn(
        "inline-block bg-[length:250%_100%,auto] bg-clip-text text-transparent [background-repeat:no-repeat,padding-box]",
        className,
      )}
      style={
        {
          "--spread": `${children.length * spread}px`,
          backgroundImage:
            "linear-gradient(90deg, transparent calc(50% - var(--spread)), var(--shimmer-highlight) 50%, transparent calc(50% + var(--spread))), linear-gradient(var(--shimmer-base), var(--shimmer-base))",
        } as CSSProperties
      }
      initial={{ backgroundPosition: "100% center" }}
      animate={{ backgroundPosition: "0% center" }}
      transition={{ duration: duration * m.scale, ease: "linear", repeat: Infinity }}
    >
      {children}
    </motion.span>
  );
}

Motion notes

  • Linear timing on purpose: a shimmer that eases looks like it's stalling.
  • The highlight width scales with text length, so short and long labels sweep at the same visual pace.
  • Pure background-position animation, so it costs almost nothing to render.
  • With reduced motion, it becomes static muted text.

Installation

  1. 1

    Install dependencies

    Terminal
    npm install motion clsx tailwind-merge
  2. 2

    Add the design tokens and motion system (once per project)

    Copy styles/tokens/*, lib/motion.tsx and lib/utils.ts from Get started.

  3. 3

    Paste the source into components/thinking-ui/text-shimmer.tsx

    Copy it from the Code tab, or use Copy prompt to let your coding agent do all of this.

Usage

Example
<TextShimmer>Searching the web…</TextShimmer>

Props

PropTypeDefaultDescription
childrenstring—The label to shimmer.
durationnumber1.8Seconds per sweep.
spreadnumber2Highlight width in px per character.
classNamestring—Classes for the text.