"use client"; import { useRef, useState, type ReactNode } from "react"; import { motion, useScroll, useTransform, useMotionValueEvent, } from "motion/react"; import { useReducedMotion } from "./Preferences.tsx"; export function Parallax({ children, distance = 40, className = "", }: { children: ReactNode; distance?: number; className?: string; }) { const ref = useRef(null), reduced = useReducedMotion(), { scrollYProgress } = useScroll({ target: ref, offset: ["start end", "end start"], }); const travel = Number.isFinite(distance) ? Math.max(-120, Math.min(120, distance)) : 0, y = useTransform(scrollYProgress, [0, 1], [-travel, travel]); return (
{children}
); } export function ScrollProgress({ label = "Reading progress", className = "", }: { label?: string; className?: string; }) { const { scrollYProgress } = useScroll(), [value, setValue] = useState(0); useMotionValueEvent(scrollYProgress, "change", (v) => setValue(Math.round(Math.max(0, Math.min(1, v)) * 100)), ); return (
{value}%
); }