import { useRef, useState, useEffect } from "react"; import { motion, useMotionValue, useMotionTemplate } from "motion/react"; import { cls } from "@/lib/utils"; const CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; const randomChars = () => Array.from({ length: 1500 }, () => CHARS[Math.floor(Math.random() * 62)]).join(""); interface HoverPatternProps { children: React.ReactNode; className?: string; } const HoverPattern = ({ children, className = "" }: HoverPatternProps) => { const ref = useRef(null); const x = useMotionValue(0); const y = useMotionValue(0); const [chars, setChars] = useState(randomChars); const [isMobile, setIsMobile] = useState(false); useEffect(() => { const checkMobile = () => setIsMobile(window.innerWidth < 768); checkMobile(); window.addEventListener("resize", checkMobile); return () => window.removeEventListener("resize", checkMobile); }, []); useEffect(() => { if (isMobile && ref.current) { x.set(ref.current.offsetWidth / 2); y.set(ref.current.offsetHeight / 2); } }, [isMobile, x, y]); const mask = useMotionTemplate`radial-gradient(${isMobile ? 110 : 250}px at ${x}px ${y}px, white, transparent)`; const base = "absolute inset-0 rounded-[inherit] transition-opacity duration-300"; return (
{ if (!ref.current) return; const rect = ref.current.getBoundingClientRect(); x.set(e.clientX - rect.left); y.set(e.clientY - rect.top); setChars(randomChars()); }} > {children}

{chars}

); }; export default HoverPattern;