"use client"; import { useState, useRef, useEffect } from 'react'; import { gsap } from 'gsap'; import './BubbleMenu.css'; const DEFAULT_ITEMS = [ { label: 'home', href: '#', ariaLabel: 'Home', rotation: -8, hoverStyles: { bgColor: '#3b82f6', textColor: '#ffffff' } }, { label: 'about', href: '#', ariaLabel: 'About', rotation: 8, hoverStyles: { bgColor: '#10b981', textColor: '#ffffff' } }, { label: 'projects', href: '#', ariaLabel: 'Documentation', rotation: 8, hoverStyles: { bgColor: '#f59e0b', textColor: '#ffffff' } }, { label: 'blog', href: '#', ariaLabel: 'Blog', rotation: 8, hoverStyles: { bgColor: '#ef4444', textColor: '#ffffff' } }, { label: 'contact', href: '#', ariaLabel: 'Contact', rotation: -8, hoverStyles: { bgColor: '#8b5cf6', textColor: '#ffffff' } } ]; export default function BubbleMenu({ logo, onMenuClick, className, style, menuAriaLabel = 'Toggle menu', menuBg = '#fff', menuContentColor = '#111', useFixedPosition = false, items, animationEase = 'back.out(1.5)', animationDuration = 0.5, staggerDelay = 0.12 }) { const [isMenuOpen, setIsMenuOpen] = useState(false); const [showOverlay, setShowOverlay] = useState(false); const overlayRef = useRef(null); const bubblesRef = useRef([]); const labelRefs = useRef([]); const menuItems = items?.length ? items : DEFAULT_ITEMS; const containerClassName = ['bubble-menu', useFixedPosition ? 'fixed' : 'absolute', className] .filter(Boolean) .join(' '); const handleToggle = () => { const nextState = !isMenuOpen; if (nextState) setShowOverlay(true); setIsMenuOpen(nextState); onMenuClick?.(nextState); }; useEffect(() => { const overlay = overlayRef.current; const bubbles = bubblesRef.current.filter(Boolean); const labels = labelRefs.current.filter(Boolean); if (!overlay || !bubbles.length) return; if (isMenuOpen) { gsap.set(overlay, { display: 'flex' }); gsap.killTweensOf([...bubbles, ...labels]); gsap.set(bubbles, { scale: 0, transformOrigin: '50% 50%' }); gsap.set(labels, { y: 24, autoAlpha: 0 }); bubbles.forEach((bubble, i) => { const delay = i * staggerDelay + gsap.utils.random(-0.05, 0.05); const tl = gsap.timeline({ delay }); tl.to(bubble, { scale: 1, duration: animationDuration, ease: animationEase }); if (labels[i]) { tl.to( labels[i], { y: 0, autoAlpha: 1, duration: animationDuration, ease: 'power3.out' }, `-=${animationDuration * 0.9}` ); } }); } else if (showOverlay) { gsap.killTweensOf([...bubbles, ...labels]); gsap.to(labels, { y: 24, autoAlpha: 0, duration: 0.2, ease: 'power3.in' }); gsap.to(bubbles, { scale: 0, duration: 0.2, ease: 'power3.in', onComplete: () => { gsap.set(overlay, { display: 'none' }); setShowOverlay(false); } }); } }, [isMenuOpen, showOverlay, animationEase, animationDuration, staggerDelay]); useEffect(() => { const handleResize = () => { if (isMenuOpen) { const bubbles = bubblesRef.current.filter(Boolean); const isDesktop = window.innerWidth >= 900; bubbles.forEach((bubble, i) => { const item = menuItems[i]; if (bubble && item) { const rotation = isDesktop ? (item.rotation ?? 0) : 0; gsap.set(bubble, { rotation }); } }); } }; window.addEventListener('resize', handleResize); return () => window.removeEventListener('resize', handleResize); }, [isMenuOpen, menuItems]); return ( <> {showOverlay && (