Dia Text Reveal
magicui/dia-text-revealFreeComponent
A horizontal color band sweeps across text, revealing a gradient shine before settling on the base color.
Live preview
live · rendered by the registry
Rendered by magicui on their own page — this is the component running, not a screenshot of it.Install it
npx shadcn@latest add https://magicui.design/r/dia-text-reveal.jsonSource
1"use client"23import { useEffect, useRef, useState } from "react"4import {5 animate,6 motion,7 useInView,8 useMotionValue,9 useReducedMotion,10 useTransform,11 type HTMLMotionProps,12} from "motion/react"1314import { cn } from "@/lib/utils"1516const DEFAULT_COLORS = ["#c679c4", "#fa3d1d", "#ffb005", "#e1e1fe", "#0358f7"]17const BAND_HALF = 1718const SWEEP_START = -BAND_HALF19const SWEEP_END = 100 + BAND_HALF2021const sweepEase = (t: number) =>22 t < 0.5 ? 4 * t ** 3 : 1 - (-2 * t + 2) ** 3 / 22324function buildGradient(pos: number, colors: string[], textColor: string) {25 const bandStart = pos - BAND_HALF26 const bandEnd = pos + BAND_HALF2728 if (bandStart >= 100) {29 return `linear-gradient(90deg, ${textColor}, ${textColor})`30 }31 const n = colors.length32 const parts: string[] = []3334 if (bandStart > 0)35 parts.push(`${textColor} 0%`, `${textColor} ${bandStart.toFixed(2)}%`)3637 colors.forEach((c, i) => {38 const pct = n === 1 ? pos : bandStart + (i / (n - 1)) * BAND_HALF * 239 parts.push(`${c} ${pct.toFixed(2)}%`)40 })4142 if (bandEnd < 100)43 parts.push(`transparent ${bandEnd.toFixed(2)}%`, `transparent 100%`)4445 return `linear-gradient(90deg, ${parts.join(", ")})`46}4748function measureWidths(el: HTMLElement, texts: string[]) {49 const ghost = el.cloneNode() as HTMLElement50 Object.assign(ghost.style, {51 position: "absolute",52 visibility: "hidden",53 pointerEvents: "none",54 width: "auto",55 whiteSpace: "nowrap",56 })57 el.parentElement!.appendChild(ghost)58 const widths = texts.map((t) => {59 ghost.textContent = t60 return ghost.getBoundingClientRect().width61 })62 ghost.remove()63 return widths64}6566/**67 * Props for {@link DiaTextReveal}.68 */69export interface DiaTextRevealProps extends Omit<70 HTMLMotionProps<"span">,71 "ref" | "children" | "style" | "animate" | "transition" | "color"72> {73 /**74 * Text to reveal. Pass multiple strings to rotate when {@link DiaTextRevealProps.repeat} is `true`.75 */76 text: string | string[]77 /**78 * Colors sampled across the moving gradient band. Defaults to a built-in palette.79 */80 colors?: string[]81 /**82 * CSS color for revealed text after the sweep and for leading/trailing regions during the animation.83 * @defaultValue `"var(--foreground)"`84 */85 textColor?: string86 /**87 * Duration of one sweep pass, in seconds.88 * @defaultValue `1.5`89 */90 duration?: number91 /**92 * Delay before the sweep starts, in seconds.93 * @defaultValue `0`94 */95 delay?: number96 /**97// … truncated
What it pulls in
npm packages
Files it writes
More from magicui
All 247 items| Component | Registry | Kind | Access | Installs | Command |
|---|---|---|---|---|---|
| Androidandroid | magicui | Components | Free | no deps | |
| Animated Beamanimated-beam | magicui | Components | Free | 1 dep | |
| Animated Circular Progress Baranimated-circular-progress-bar | magicui | Components | Free | no deps | |
| Animated Gradient Textanimated-gradient-text | magicui | Components | Free | no deps | |
| Animated Grid Patternanimated-grid-pattern | magicui | Components | Free | 1 dep | |
| Animated Listanimated-list | magicui | Components | Free | 1 dep | |
| Animated Shiny Textanimated-shiny-text | magicui | Components | Free | no deps | |
| Theme Toggleranimated-theme-toggler | magicui | Components | Free | 1 dep |
