"use client"; import { motion, MotionValue, useScroll, useTransform } from "motion/react"; import { ComponentPropsWithoutRef, FC, ReactNode, useRef } from "react"; import { cn } from "@/lib/utils"; export interface TextRevealProps extends ComponentPropsWithoutRef<"div"> { children: string; } export const TextReveal: FC = ({ children, className }) => { const targetRef = useRef(null); const { scrollYProgress } = useScroll({ target: targetRef, }); if (typeof children !== "string") { throw new Error("TextReveal: children must be a string"); } const words = children.split(" "); return (
{words.map((word, i) => { const start = i / words.length; const end = start + 1 / words.length; return ( {word} ); })}
); }; interface WordProps { children: ReactNode; progress: MotionValue; range: [number, number]; } const Word: FC = ({ children, progress, range }) => { const opacity = useTransform(progress, range, [0, 1]); return ( {children} {children} ); };