Scroll Velocity
Marquee text rows that drift in alternating directions and speed up with scroll velocity.
pnpm dlx shadcn@latest add @leo/scroll-velocityProps
| Name | Type | Default | Description |
|---|---|---|---|
| className | string | undefined | - | Applied to the root element. |
| damping | number | undefined | 50 | |
| numCopies | number | undefined | 6 | |
| parallaxClassName | string | undefined | parallax | |
| parallaxStyle | CSSProperties | undefined | - | |
| scrollContainerRef | RefObject<HTMLElement> | undefined | - | |
| scrollerClassName | string | undefined | scroller | |
| scrollerStyle | CSSProperties | undefined | - | |
| stiffness | number | undefined | 400 | |
| textClassName | string | undefined | Applied to every repeated text span (upstream's `className`). | |
| texts | ReactNode[] | undefined | ["Scroll Velocity", "Keep Moving"] | |
| velocity | number | undefined | 100 | |
| velocityMapping | VelocityMapping | undefined | { input: [0, 1000], output: [0, 5] } |
Dependencies
- motion@^12.23.12
Source
"use client";
import {
motion,
useAnimationFrame,
useMotionValue,
useScroll,
useSpring,
useTransform,
useVelocity,
} from "motion/react";
import type React from "react";
import { useLayoutEffect, useRef, useState } from "react";
import { cn } from "@/lib/utils";
interface VelocityMapping {
input: [number, number];
output: [number, number];
}
interface VelocityTextProps {
children: React.ReactNode;
baseVelocity: number;
scrollContainerRef?: React.RefObject<HTMLElement>;
className?: string;
damping?: number;
stiffness?: number;
numCopies?: number;
velocityMapping?: VelocityMapping;
parallaxClassName?: string;
scrollerClassName?: string;
parallaxStyle?: React.CSSProperties;
scrollerStyle?: React.CSSProperties;
}
export interface ScrollVelocityProps {
scrollContainerRef?: React.RefObject<HTMLElement>;
texts?: React.ReactNode[];
velocity?: number;
/** Applied to the root element. */
className?: string;
/** Applied to every repeated text span (upstream's `className`). */
textClassName?: string;
damping?: number;
stiffness?: number;
numCopies?: number;
velocityMapping?: VelocityMapping;
parallaxClassName?: string;
scrollerClassName?: string;
parallaxStyle?: React.CSSProperties;
scrollerStyle?: React.CSSProperties;
}
const DEFAULT_TEXTS = ["Scroll Velocity", "Keep Moving"];
function useElementWidth<T extends HTMLElement>(ref: React.RefObject<T | null>): number {
const [width, setWidth] = useState(0);
useLayoutEffect(() => {
function updateWidth() {
if (ref.current) {
setWidth(ref.current.offsetWidth);
}
}
updateWidth();
window.addEventListener("resize", updateWidth);
return () => window.removeEventListener("resize", updateWidth);
}, [ref]);
return width;
}
export const ScrollVelocity: React.FC<ScrollVelocityProps> = ({
scrollContainerRef,
texts = DEFAULT_TEXTS,
velocity = 100,
className,
textClassName = "",
damping = 50,
stiffness = 400,
numCopies = 6,
velocityMapping = { input: [0, 1000], output: [0, 5] },
parallaxClassName = "parallax",
scrollerClassName = "scroller",
parallaxStyle,
scrollerStyle,
}) => {
function VelocityText({
children,
baseVelocity = velocity,
scrollContainerRef,
className = "",
damping,
stiffness,
numCopies,
velocityMapping,
parallaxClassName,
scrollerClassName,
parallaxStyle,
scrollerStyle,
}: VelocityTextProps) {
const baseX = useMotionValue(0);
const scrollOptions = scrollContainerRef ? { container: scrollContainerRef } : {};
const { scrollY } = useScroll(scrollOptions);
const scrollVelocity = useVelocity(scrollY);
const smoothVelocity = useSpring(scrollVelocity, {
damping: damping ?? 50,
stiffness: stiffness ?? 400,
});
const velocityFactor = useTransform(
smoothVelocity,
velocityMapping?.input || [0, 1000],
velocityMapping?.output || [0, 5],
{ clamp: false },
);
const copyRef = useRef<HTMLSpanElement>(null);
const copyWidth = useElementWidth(copyRef);
function wrap(min: number, max: number, v: number): number {
const range = max - min;
const mod = (((v - min) % range) + range) % range;
return mod + min;
}
const x = useTransform(baseX, (v) => {
if (copyWidth === 0) return "0px";
return `${wrap(-copyWidth, 0, v)}px`;
});
const directionFactor = useRef<number>(1);
useAnimationFrame((_t, delta) => {
let moveBy = directionFactor.current * baseVelocity * (delta / 1000);
if (velocityFactor.get() < 0) {
directionFactor.current = -1;
} else if (velocityFactor.get() > 0) {
directionFactor.current = 1;
}
moveBy += directionFactor.current * moveBy * velocityFactor.get();
baseX.set(baseX.get() + moveBy);
});
const spans = [];
for (let i = 0; i < (numCopies ?? 6); i++) {
spans.push(
<span className={`flex-shrink-0 ${className}`} key={i} ref={i === 0 ? copyRef : null}>
{children}
</span>,
);
}
return (
<div className={`${parallaxClassName} relative overflow-hidden`} style={parallaxStyle}>
<motion.div
className={`${scrollerClassName} flex whitespace-nowrap text-center font-sans text-4xl font-bold tracking-[-0.02em] drop-shadow md:text-[5rem] md:leading-[5rem]`}
style={{ x, ...scrollerStyle }}
>
{spans}
</motion.div>
</div>
);
}
return (
<section className={cn(className)}>
{texts.map((text, index) => (
<VelocityText
// biome-ignore lint/suspicious/noArrayIndexKey: rows are positional and never reordered
key={index}
className={textClassName}
baseVelocity={index % 2 !== 0 ? -velocity : velocity}
scrollContainerRef={scrollContainerRef}
damping={damping}
stiffness={stiffness}
numCopies={numCopies}
velocityMapping={velocityMapping}
parallaxClassName={parallaxClassName}
scrollerClassName={scrollerClassName}
parallaxStyle={parallaxStyle}
scrollerStyle={scrollerStyle}
>
{text}
</VelocityText>
))}
</section>
);
};
export default ScrollVelocity;