Installation
Add the component
npx shadcn@latest add https://prequeluianimate.terravidhal.me/r/highlight-text.jsonFirst component? Set up your project first
Frameworks
The live preview is React. Each tab below is the file that ships for that framework, with the same durations, springs and reduced-motion rule.
"use client";
import * as React from "react";
import { motion, type HTMLMotionProps, type Transition } from "motion/react";
import { cn } from "@/lib/utils";
import {
useReduceMotion,
useSeen,
useTempoTransition,
} from "./motion";
export type HighlightTextProps = Omit<HTMLMotionProps<"span">, "children"> & {
text: string;
delay?: number;
color?: string;
inView?: boolean;
inViewOnce?: boolean;
transition?: Transition;
};
export function HighlightText({
text,
delay = 0,
color = "color-mix(in oklab, #f5c542 38%, transparent)",
inView = false,
inViewOnce = true,
transition,
className,
style,
...props
}: HighlightTextProps) {
const reduce = useReduceMotion();
const ref = React.useRef<HTMLSpanElement>(null);
const seen = useSeen(ref, inView, inViewOnce);
const tween = useTempoTransition(
transition ?? { duration: 2, ease: [0.42, 0, 0.58, 1] },
);
const shown = seen || reduce;
const final = reduce ? { duration: 0 } : { ...tween, delay: (tween.delay ?? 0) + delay / 1000 };
return (
<motion.span
ref={ref}
data-slot="highlight-text"
initial={reduce ? false : { backgroundSize: "0% 100%" }}
animate={{ backgroundSize: shown ? "100% 100%" : "0% 100%" }}
transition={shown ? final : { duration: 0 }}
className={cn("relative inline-block", className)}
style={{
backgroundImage: `linear-gradient(${color}, ${color})`,
backgroundRepeat: "no-repeat",
backgroundPosition: "left center",
...style,
}}
{...props}
>
{text}
</motion.span>
);
}Install
npx shadcn@latest add https://prequeluianimate.terravidhal.me/r/highlight-text.jsonDependencies
motion lucide-reactThe registry keeps the component source, its motion tokens and the reduced-motion branch together, so the install never pulls a different animation from the preview.
Usage
Import the component and compose it exactly like the primitive it wraps. Motion is enabled by default; the tempo prop scales every duration and softens every spring.
import { HighlightText } from "@/components/prequelui/highlight-text";
export default function Demo() {
return (
<p className="text-2xl">
Motion should feel{" "}
<HighlightText text="inevitable" delay={250} />.
</p>
);
}import { HighlightText } from "@/components/prequelui/highlight-text";
export function Variant() {
return (
<HighlightText tempo={1.2}>
…
</HighlightText>
);
}Animation
The highlight is a background block clipped to the text box. It grows from zero width to full width behind the words, from the left, in two seconds; there is no separate element to misalign.
- Background size: 0% 100% → 100% 100% over 2 s ease-in-out.
- Delay is expressed in milliseconds and added to the transition delay.
- The text never moves; only its background box changes size.
- Reduced motion shows the final highlight immediately.
API Reference
Each part accepts the usual HTML props in addition to the motion props below. No accessibility prop is hidden behind the animation layer.
HighlightText
| Prop | Type | Default |
|---|---|---|
| text | string | — |
| delay | number | 0 |
| color | string | soft amber |
| inView | boolean | false |
| inViewOnce | boolean | true |