Highlight Text
A marker stroke that sweeps across text as it scrolls into view.
gsap@gsap/reactMIT · free
Good design is mostly about knowing what to leave out, and great motion is about knowing when to stop.
Usage
import { HighlightText } from "@/components/highlight-text";
<p>
Good design is about <HighlightText>knowing what to leave out</HighlightText>.
</p>
// follow the scroll position instead of playing once
<HighlightText scrub color="rgba(94, 240, 200, 0.45)">…</HighlightText>Installation
npx shadcn@latest add https://ombakui.com/r/highlight-text.jsonInstall the dependencies:
npm install gsap @gsap/reactCopy this into components/highlight-text.tsx. It uses cn() from @/lib/utils.
components/highlight-text.tsx
"use client";
import { useRef, type ElementType, type ReactNode } from "react";
import { gsap } from "gsap";
import { ScrollTrigger } from "gsap/ScrollTrigger";
import { useGSAP } from "@gsap/react";
import { cn } from "@/lib/utils";
if (typeof window !== "undefined") gsap.registerPlugin(ScrollTrigger, useGSAP);
export type HighlightTextProps = {
children: ReactNode;
/** HTML tag to render. Default "span" */
as?: ElementType;
/** Marker color. Any CSS color. Default "rgba(250, 204, 21, 0.4)" */
color?: string;
/** Tie the sweep to the scroll position instead of playing once. Default false */
scrub?: boolean;
/** Seconds for the sweep when not scrubbing. Default 1.1 */
duration?: number;
/** Seconds before starting. Default 0 */
delay?: number;
/** ScrollTrigger start position. Default "top 80%" */
start?: string;
/** How much of the line height the marker covers, 0–1. Default 0.4 */
thickness?: number;
className?: string;
};
/**
* A marker stroke that sweeps across the text as it scrolls into view. Wraps nicely across
* multiple lines. The text is fully highlighted for reduced-motion users.
*/
export function HighlightText({
children,
as: Tag = "span",
color = "rgba(250, 204, 21, 0.4)",
scrub = false,
duration = 1.1,
delay = 0,
start = "top 80%",
thickness = 0.4,
className,
}: HighlightTextProps) {
const ref = useRef<HTMLElement>(null);
useGSAP(
() => {
const el = ref.current;
if (!el || window.matchMedia("(prefers-reduced-motion: reduce)").matches) return;
gsap.fromTo(
el,
{ backgroundSize: `0% ${thickness * 100}%` },
{
backgroundSize: `100% ${thickness * 100}%`,
ease: scrub ? "none" : "power2.inOut",
duration,
delay,
scrollTrigger: scrub ? { trigger: el, start, end: "bottom 45%", scrub: true } : { trigger: el, start, once: true },
},
);
},
{ scope: ref, dependencies: [scrub, duration, delay, start, thickness] },
);
return (
<Tag
ref={ref}
className={cn("[box-decoration-break:clone] [-webkit-box-decoration-break:clone]", className)}
style={{
backgroundImage: `linear-gradient(${color}, ${color})`,
backgroundRepeat: "no-repeat",
backgroundPosition: "0 88%",
backgroundSize: `100% ${thickness * 100}%`,
}}
>
{children}
</Tag>
);
}Usage
import { HighlightText } from "@/components/highlight-text";
<p>
Good design is about <HighlightText>knowing what to leave out</HighlightText>.
</p>
// follow the scroll position instead of playing once
<HighlightText scrub color="rgba(94, 240, 200, 0.45)">…</HighlightText>Props
| Prop | Type | Default | Description |
|---|---|---|---|
| children* | ReactNode | – | |
| as | ElementType | "span" | HTML tag to render. |
| color | string | "rgba(250, 204, 21, 0.4)" | Marker color. Any CSS color. |
| scrub | boolean | false | Tie the sweep to the scroll position instead of playing once. |
| duration | number | 1.1 | Seconds for the sweep when not scrubbing. |
| delay | number | 0 | Seconds before starting. |
| start | string | "top 80%" | ScrollTrigger start position. |
| thickness | number | 0.4 | How much of the line height the marker covers, 0–1. |
| className | string | – |