Scroll Reveal
Paragraph words sharpen, fade in and straighten as the text scrolls into view.
pnpm dlx shadcn@latest add @leo/scroll-revealProps
| Name | Type | Default | Description |
|---|---|---|---|
| baseOpacity | number | undefined | 0.1 | |
| baseRotation | number | undefined | 3 | |
| blurStrength | number | undefined | 4 | |
| children | ReactNode | Words fade in and sharpen as you scroll. | |
| className | string | undefined | - | |
| containerClassName | string | undefined | - | |
| enableBlur | boolean | undefined | true | |
| rotationEnd | string | undefined | bottom bottom | |
| scrollContainerRef | RefObject<HTMLElement | null> | undefined | - | |
| textClassName | string | undefined | - | |
| wordAnimationEnd | string | undefined | bottom bottom |
Dependencies
- gsap@^3.13.0
Source
"use client";
import { gsap } from "gsap";
import { ScrollTrigger } from "gsap/ScrollTrigger";
import type React from "react";
import { type ReactNode, type RefObject, useEffect, useMemo, useRef } from "react";
import { cn } from "@/lib/utils";
gsap.registerPlugin(ScrollTrigger);
export interface ScrollRevealProps {
children?: ReactNode;
scrollContainerRef?: RefObject<HTMLElement | null>;
enableBlur?: boolean;
baseOpacity?: number;
baseRotation?: number;
blurStrength?: number;
className?: string;
containerClassName?: string;
textClassName?: string;
rotationEnd?: string;
wordAnimationEnd?: string;
}
const ScrollReveal: React.FC<ScrollRevealProps> = ({
children = "Words fade in and sharpen as you scroll.",
scrollContainerRef,
enableBlur = true,
baseOpacity = 0.1,
baseRotation = 3,
blurStrength = 4,
className,
containerClassName,
textClassName,
rotationEnd = "bottom bottom",
wordAnimationEnd = "bottom bottom",
}) => {
const containerRef = useRef<HTMLHeadingElement>(null);
const splitText = useMemo(() => {
const text = typeof children === "string" ? children : "";
return text.split(/(\s+)/).map((word, index) => {
if (word.match(/^\s+$/)) return word;
return (
// biome-ignore lint/suspicious/noArrayIndexKey: words have no stable identity
<span className="inline-block word" key={index}>
{word}
</span>
);
});
}, [children]);
useEffect(() => {
const el = containerRef.current;
if (!el) return;
const scroller = scrollContainerRef?.current ?? window;
// A context reverts only this instance's tweens and triggers on cleanup.
const ctx = gsap.context(() => {
gsap.fromTo(
el,
{ transformOrigin: "0% 50%", rotate: baseRotation },
{
ease: "none",
rotate: 0,
scrollTrigger: {
trigger: el,
scroller,
start: "top bottom",
end: rotationEnd,
scrub: true,
},
},
);
const wordElements = el.querySelectorAll<HTMLElement>(".word");
gsap.fromTo(
wordElements,
{ opacity: baseOpacity, willChange: "opacity" },
{
ease: "none",
opacity: 1,
stagger: 0.05,
scrollTrigger: {
trigger: el,
scroller,
start: "top bottom-=20%",
end: wordAnimationEnd,
scrub: true,
},
},
);
if (enableBlur) {
gsap.fromTo(
wordElements,
{ filter: `blur(${blurStrength}px)` },
{
ease: "none",
filter: "blur(0px)",
stagger: 0.05,
scrollTrigger: {
trigger: el,
scroller,
start: "top bottom-=20%",
end: wordAnimationEnd,
scrub: true,
},
},
);
}
}, el);
return () => ctx.revert();
}, [
scrollContainerRef,
enableBlur,
baseRotation,
baseOpacity,
rotationEnd,
wordAnimationEnd,
blurStrength,
]);
return (
<h2 ref={containerRef} className={cn("my-5", containerClassName, className)}>
<p className={cn("text-[clamp(1.6rem,4vw,3rem)] leading-[1.5] font-semibold", textClassName)}>
{splitText}
</p>
</h2>
);
};
export { ScrollReveal };
export default ScrollReveal;