Shiny Text
A shimmering text effect for React and Tailwind CSS: a band of light sweeps across the letters using a gradient clipped to the text. Pick the colors, speed, angle and direction, render any heading level with the as prop, and keep the text real and selectable for SEO.
- SEO-friendly real text
- background-clip: text
- Heading levels via as
- Loop or hover
- prefers-reduced-motion
Introducing Dusk Pro
import { ShinyText } from "@/components/dusk/text/shiny-text";
export default function ShinyTextDemo() {
return (
<ShinyText as="h2" className="text-center text-5xl font-bold tracking-tight sm:text-6xl">
Introducing Dusk Pro
</ShinyText>
);
}Installation
$ npx shadcn@latest add https://duskui.com/r/shiny-text.json1. Install the dependencies.
npm install motion2. Copy the source into
components/dusk/text/shiny-text.tsx.shiny-text.tsx "use client"; import * as React from "react"; import { animate, useReducedMotion, type AnimationPlaybackControls } from "motion/react"; import { cn } from "@/lib/utils"; type ShinyTextTag = "span" | "p" | "div" | "strong" | "em" | "h1" | "h2" | "h3" | "h4" | "h5" | "h6"; export interface ShinyTextProps extends Omit<React.HTMLAttributes<HTMLElement>, "color"> { /** Element to render. Use a heading level for titles. */ as?: ShinyTextTag; /** Base text color (any CSS color). */ color?: string; /** Color of the light band. */ shineColor?: string; /** Seconds for one sweep across the text. */ duration?: number; /** Seconds between sweeps. */ repeatDelay?: number; /** Width of the light band as a fraction of the text width (0.1–1). */ spread?: number; /** Angle of the band in degrees. */ angle?: number; /** Sweep direction. */ direction?: "ltr" | "rtl"; /** `"loop"` sweeps continuously; `"hover"` sweeps once each time the pointer enters. */ trigger?: "loop" | "hover"; } // The gradient is 3x the text width; these are the positions where the band sits just outside each edge. const OFF_LEFT = "100% 0"; const OFF_RIGHT = "0% 0"; /** * Text with a band of light sweeping across it: a gradient clipped to the * glyphs (`background-clip: text`) whose position Motion animates outside React. * * It's one real text element, so headings stay selectable, translatable and * indexable. The server renders the plain base color; the shine is purely * decorative and is switched off under reduced motion. */ export function ShinyText({ as = "span", color = "color-mix(in oklab, var(--color-foreground) 55%, transparent)", shineColor = "var(--color-foreground)", duration = 1.8, repeatDelay = 2.5, spread = 0.35, angle = 110, direction = "ltr", trigger = "loop", className, style, children, onPointerEnter, ...props }: ShinyTextProps) { const Comp = as as React.ElementType; const ref = React.useRef<HTMLElement>(null); const sweeping = React.useRef<AnimationPlaybackControls | null>(null); const reduceMotion = useReducedMotion(); const band = (Math.min(1, Math.max(0.1, spread)) * (100 / 3)) / 2; const background = `linear-gradient(${angle}deg, ${color} calc(50% - ${band}%), ${shineColor} 50%, ${color} calc(50% + ${band}%))`; const [start, end] = direction === "ltr" ? [OFF_LEFT, OFF_RIGHT] : [OFF_RIGHT, OFF_LEFT]; const sweep = React.useCallback( (repeat: boolean) => { const el = ref.current; if (!el) return null; return animate( el, { backgroundPosition: [start, end] }, { duration, ease: [0.45, 0, 0.25, 1], ...(repeat ? { repeat: Infinity, repeatDelay } : null), } ); }, [start, end, duration, repeatDelay] ); React.useEffect(() => { const el = ref.current; if (!el || reduceMotion || trigger !== "loop") return; const controls = sweep(true); return () => { controls?.stop(); el.style.backgroundPosition = start; }; }, [reduceMotion, trigger, sweep, start]); React.useEffect(() => () => sweeping.current?.stop(), []); const onEnter = (event: React.PointerEvent<HTMLElement>) => { onPointerEnter?.(event); if (reduceMotion || trigger !== "hover" || sweeping.current) return; const controls = sweep(false); sweeping.current = controls; controls?.finished.then(() => { sweeping.current = null; }); }; return ( <Comp ref={ref} data-slot="shiny-text" onPointerEnter={onEnter} className={cn( "bg-clip-text text-transparent [-webkit-background-clip:text] [background-image:var(--shiny-gradient)] [background-repeat:no-repeat] [background-size:300%_100%]", // Keep selected text legible: the glyph fill is transparent otherwise. "selection:text-foreground", // Forced-colors modes drop backgrounds; fall back to the system text color. "forced-colors:[background-image:none] forced-colors:text-[CanvasText]", className )} style={ { "--shiny-gradient": background, backgroundPosition: start, ...style, } as React.CSSProperties } {...props} > {children} </Comp> ); }
Usage
import { ShinyText } from "@/components/dusk/text/shiny-text"
export function Announcement() {
return (
<ShinyText as="h2" className="text-4xl font-bold">
Now in public beta
</ShinyText>
)
}Examples
Colors, speed & direction
color is the base text color and shineColor the band. spread sets the band width, angle its slant, direction the sweep direction, and duration / repeatDelay the rhythm.
Mint sweep
Slow gold
Right to left
import { ShinyText } from "@/components/dusk/text/shiny-text";
export default function ShinyTextColorsDemo() {
return (
<div className="grid gap-5 text-center text-3xl font-semibold tracking-tight">
<ShinyText
as="p"
color="color-mix(in oklab, var(--color-primary) 45%, transparent)"
shineColor="var(--color-primary)"
>
Mint sweep
</ShinyText>
<ShinyText as="p" color="#a16207" shineColor="#fde68a" spread={0.5} duration={2.6}>
Slow gold
</ShinyText>
<ShinyText as="p" color="#6d28d9" shineColor="#f0abfc" direction="rtl" angle={70} repeatDelay={0.6}>
Right to left
</ShinyText>
</div>
);
}Loading text
A fast, narrow sweep makes a calm loading label. Pass role="status" so the message is announced.
import { ShinyText } from "@/components/dusk/text/shiny-text";
export default function ShinyTextLoadingDemo() {
return (
<div className="flex w-full max-w-sm items-center gap-3 rounded-xl border border-white/10 bg-white/[0.03] px-4 py-3">
<span className="size-2 rounded-full bg-white/40" aria-hidden />
<ShinyText role="status" duration={1.2} repeatDelay={0.2} spread={0.25} className="text-sm font-medium">
Generating your component…
</ShinyText>
</div>
);
}Sweep on hover
trigger="hover" plays a single sweep each time the pointer enters, good for links and announcement pills.
import { ArrowRight } from "lucide-react";
import { ShinyText } from "@/components/dusk/text/shiny-text";
export default function ShinyTextHoverDemo() {
return (
<a
href="#hover"
className="group inline-flex items-center gap-2 rounded-full border border-white/10 bg-white/[0.04] px-4 py-1.5 text-sm outline-none transition-colors hover:border-white/20 focus-visible:ring-2 focus-visible:ring-ring/60"
>
<span className="rounded-full bg-primary/15 px-2 py-0.5 text-xs font-medium text-primary">New</span>
<ShinyText trigger="hover" duration={0.9} color="rgb(255 255 255 / 0.7)" shineColor="rgb(255 255 255)">
Text effects just landed
</ShinyText>
<ArrowRight className="size-3.5 text-white/50 transition-transform group-hover:translate-x-0.5" aria-hidden />
</a>
);
}API Reference
Also accepts any HTML attribute for the rendered element.
| Prop | Type | Default | Description |
|---|---|---|---|
| as | "span" | "p" | "div" | "strong" | "em" | "h1" … "h6" | "span" | Element to render. Use a heading level for titles. |
| color | string | foreground at 55% | Base text color (any CSS color). |
| shineColor | string | var(--color-foreground) | Color of the light band. |
| duration | number | 1.8 | Seconds for one sweep. |
| repeatDelay | number | 2.5 | Seconds between sweeps in loop mode. |
| spread | number | 0.35 | Band width as a fraction of the text width (0.1–1). |
| angle | number | 110 | Band angle in degrees. |
| direction | "ltr" | "rtl" | "ltr" | Sweep direction. |
| trigger | "loop" | "hover" | "loop" | Sweep continuously or once per hover. |
Accessibility
- The text is a single real element with no duplicated copies, so it is selectable, translatable and indexed like any heading.
- Use
asto keep the correct heading level in the document outline. - Selected text switches to a solid color so it stays legible, and forced-colors (high contrast) modes fall back to the system text color.
- The shine moves outside React, and only the background position animates.
- With prefers-reduced-motion the text is static in its base color. Pick a base color with enough contrast on its own.