Scroll Float
Headline letters float up and settle one by one, scrubbed by scroll position.
pnpm dlx shadcn@latest add @leo/scroll-floatProps
| Name | Type | Default | Description |
|---|---|---|---|
| animationDuration | number | undefined | 1 | |
| children | ReactNode | Scroll float | |
| className | string | undefined | - | |
| containerClassName | string | undefined | - | |
| ease | string | undefined | back.inOut(2) | |
| scrollContainerRef | RefObject<HTMLElement | null> | undefined | - | |
| scrollEnd | string | undefined | bottom bottom-=40% | |
| scrollStart | string | undefined | center bottom+=50% | |
| stagger | number | undefined | 0.03 | |
| textClassName | string | undefined | - |
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 ScrollFloatProps {
children?: ReactNode;
scrollContainerRef?: RefObject<HTMLElement | null>;
className?: string;
containerClassName?: string;
textClassName?: string;
animationDuration?: number;
ease?: string;
scrollStart?: string;
scrollEnd?: string;
stagger?: number;
}
const ScrollFloat: React.FC<ScrollFloatProps> = ({
children = "Scroll float",
scrollContainerRef,
className,
containerClassName,
textClassName,
animationDuration = 1,
ease = "back.inOut(2)",
scrollStart = "center bottom+=50%",
scrollEnd = "bottom bottom-=40%",
stagger = 0.03,
}) => {
const containerRef = useRef<HTMLHeadingElement>(null);
const splitText = useMemo(() => {
const text = typeof children === "string" ? children : "";
return text.split("").map((char, index) => (
// biome-ignore lint/suspicious/noArrayIndexKey: characters have no stable identity
<span className="inline-block word" key={index}>
{char === " " ? "\u00A0" : char}
</span>
));
}, [children]);
useEffect(() => {
const el = containerRef.current;
if (!el) return;
const scroller = scrollContainerRef?.current ?? window;
const charElements = el.querySelectorAll(".word");
// A context reverts only this instance's tweens and triggers on cleanup.
const ctx = gsap.context(() => {
gsap.fromTo(
charElements,
{
willChange: "opacity, transform",
opacity: 0,
yPercent: 120,
scaleY: 2.3,
scaleX: 0.7,
transformOrigin: "50% 0%",
},
{
duration: animationDuration,
ease: ease,
opacity: 1,
yPercent: 0,
scaleY: 1,
scaleX: 1,
stagger: stagger,
scrollTrigger: {
trigger: el,
scroller,
start: scrollStart,
end: scrollEnd,
scrub: true,
},
},
);
}, el);
return () => {
ctx.revert();
// revert() leaves the yPercent offset cached as a px translate; without clearing it a re-run
// (React strict-mode double effect, prop change) animates from a stale y and the text stays offset.
gsap.set(charElements, { clearProps: "transform,opacity,willChange" });
};
}, [scrollContainerRef, animationDuration, ease, scrollStart, scrollEnd, stagger]);
return (
<h2 ref={containerRef} className={cn("my-5 overflow-hidden", containerClassName, className)}>
<span
className={cn("inline-block text-[clamp(1.6rem,4vw,3rem)] leading-[1.5]", textClassName)}
>
{splitText}
</span>
</h2>
);
};
export { ScrollFloat };
export default ScrollFloat;