"use client"; import { useState } from "react"; import { motion, AnimatePresence, useScroll, useMotionValueEvent, } from "motion/react"; import { Link } from "react-router-dom"; import { cn } from "@/lib/utils"; import { Button } from "../Button"; export const FloatingNav = ({ navItems, className, }: { navItems: { name: string; link: string; icon?: JSX.Element; }[]; className?: string; }) => { const { scrollYProgress } = useScroll(); const [visible, setVisible] = useState(true); useMotionValueEvent(scrollYProgress, "change", (current) => { if (typeof current === "number") { const previous = scrollYProgress.getPrevious(); // Check if previous is a number to avoid errors if (typeof previous === "number" && current > previous) { setVisible(false); // Scrolling down } else { setVisible(true); // Scrolling up or at the top } } }); return ( {navItems.map((navItem, idx: number) => ( {navItem.icon} {navItem.name} ))}
Docs
); };