"use client"; import { useRef, useEffect, useCallback, useState } from 'react'; import { gsap } from 'gsap'; import './MagicBento.css'; const DEFAULT_PARTICLE_COUNT = 12; const DEFAULT_SPOTLIGHT_RADIUS = 300; const DEFAULT_GLOW_COLOR = '132, 0, 255'; const MOBILE_BREAKPOINT = 768; const cardData = [ { color: '#060010', title: 'Analytics', description: 'Track user behavior', label: 'Insights' }, { color: '#060010', title: 'Dashboard', description: 'Centralized data view', label: 'Overview' }, { color: '#060010', title: 'Collaboration', description: 'Work together seamlessly', label: 'Teamwork' }, { color: '#060010', title: 'Automation', description: 'Streamline workflows', label: 'Efficiency' }, { color: '#060010', title: 'Integration', description: 'Connect favorite tools', label: 'Connectivity' }, { color: '#060010', title: 'Security', description: 'Enterprise-grade protection', label: 'Protection' } ]; const createParticleElement = (x, y, color = DEFAULT_GLOW_COLOR) => { const el = document.createElement('div'); el.className = 'particle'; el.style.cssText = ` position: absolute; width: 4px; height: 4px; border-radius: 50%; background: rgba(${color}, 1); box-shadow: 0 0 6px rgba(${color}, 0.6); pointer-events: none; z-index: 100; left: ${x}px; top: ${y}px; `; return el; }; const calculateSpotlightValues = radius => ({ proximity: radius * 0.5, fadeDistance: radius * 0.75 }); const updateCardGlowProperties = (card, mouseX, mouseY, glow, radius) => { const rect = card.getBoundingClientRect(); const relativeX = ((mouseX - rect.left) / rect.width) * 100; const relativeY = ((mouseY - rect.top) / rect.height) * 100; card.style.setProperty('--glow-x', `${relativeX}%`); card.style.setProperty('--glow-y', `${relativeY}%`); card.style.setProperty('--glow-intensity', glow.toString()); card.style.setProperty('--glow-radius', `${radius}px`); }; const ParticleCard = ({ children, className = '', disableAnimations = false, style, particleCount = DEFAULT_PARTICLE_COUNT, glowColor = DEFAULT_GLOW_COLOR, enableTilt = true, clickEffect = false, enableMagnetism = false }) => { const cardRef = useRef(null); const particlesRef = useRef([]); const timeoutsRef = useRef([]); const isHoveredRef = useRef(false); const memoizedParticles = useRef([]); const particlesInitialized = useRef(false); const magnetismAnimationRef = useRef(null); const initializeParticles = useCallback(() => { if (particlesInitialized.current || !cardRef.current) return; const { width, height } = cardRef.current.getBoundingClientRect(); memoizedParticles.current = Array.from({ length: particleCount }, () => createParticleElement(Math.random() * width, Math.random() * height, glowColor) ); particlesInitialized.current = true; }, [particleCount, glowColor]); const clearAllParticles = useCallback(() => { timeoutsRef.current.forEach(clearTimeout); timeoutsRef.current = []; magnetismAnimationRef.current?.kill(); particlesRef.current.forEach(particle => { gsap.to(particle, { scale: 0, opacity: 0, duration: 0.3, ease: 'back.in(1.7)', onComplete: () => { particle.parentNode?.removeChild(particle); } }); }); particlesRef.current = []; }, []); const animateParticles = useCallback(() => { if (!cardRef.current || !isHoveredRef.current) return; if (!particlesInitialized.current) { initializeParticles(); } memoizedParticles.current.forEach((particle, index) => { const timeoutId = setTimeout(() => { if (!isHoveredRef.current || !cardRef.current) return; const clone = particle.cloneNode(true); cardRef.current.appendChild(clone); particlesRef.current.push(clone); gsap.fromTo(clone, { scale: 0, opacity: 0 }, { scale: 1, opacity: 1, duration: 0.3, ease: 'back.out(1.7)' }); gsap.to(clone, { x: (Math.random() - 0.5) * 100, y: (Math.random() - 0.5) * 100, rotation: Math.random() * 360, duration: 2 + Math.random() * 2, ease: 'none', repeat: -1, yoyo: true }); gsap.to(clone, { opacity: 0.3, duration: 1.5, ease: 'power2.inOut', repeat: -1, yoyo: true }); }, index * 100); timeoutsRef.current.push(timeoutId); }); }, [initializeParticles]); useEffect(() => { if (disableAnimations || !cardRef.current) return; const element = cardRef.current; const handleMouseEnter = () => { isHoveredRef.current = true; animateParticles(); if (enableTilt) { gsap.to(element, { rotateX: 5, rotateY: 5, duration: 0.3, ease: 'power2.out', transformPerspective: 1000 }); } }; const handleMouseLeave = () => { isHoveredRef.current = false; clearAllParticles(); if (enableTilt) { gsap.to(element, { rotateX: 0, rotateY: 0, duration: 0.3, ease: 'power2.out' }); } if (enableMagnetism) { gsap.to(element, { x: 0, y: 0, duration: 0.3, ease: 'power2.out' }); } }; const handleMouseMove = e => { if (!enableTilt && !enableMagnetism) return; const rect = element.getBoundingClientRect(); const x = e.clientX - rect.left; const y = e.clientY - rect.top; const centerX = rect.width / 2; const centerY = rect.height / 2; if (enableTilt) { const rotateX = ((y - centerY) / centerY) * -10; const rotateY = ((x - centerX) / centerX) * 10; gsap.to(element, { rotateX, rotateY, duration: 0.1, ease: 'power2.out', transformPerspective: 1000 }); } if (enableMagnetism) { const magnetX = (x - centerX) * 0.05; const magnetY = (y - centerY) * 0.05; magnetismAnimationRef.current = gsap.to(element, { x: magnetX, y: magnetY, duration: 0.3, ease: 'power2.out' }); } }; const handleClick = e => { if (!clickEffect) return; const rect = element.getBoundingClientRect(); const x = e.clientX - rect.left; const y = e.clientY - rect.top; const maxDistance = Math.max( Math.hypot(x, y), Math.hypot(x - rect.width, y), Math.hypot(x, y - rect.height), Math.hypot(x - rect.width, y - rect.height) ); const ripple = document.createElement('div'); ripple.style.cssText = ` position: absolute; width: ${maxDistance * 2}px; height: ${maxDistance * 2}px; border-radius: 50%; background: radial-gradient(circle, rgba(${glowColor}, 0.4) 0%, rgba(${glowColor}, 0.2) 30%, transparent 70%); left: ${x - maxDistance}px; top: ${y - maxDistance}px; pointer-events: none; z-index: 1000; `; element.appendChild(ripple); gsap.fromTo( ripple, { scale: 0, opacity: 1 }, { scale: 1, opacity: 0, duration: 0.8, ease: 'power2.out', onComplete: () => ripple.remove() } ); }; element.addEventListener('mouseenter', handleMouseEnter); element.addEventListener('mouseleave', handleMouseLeave); element.addEventListener('mousemove', handleMouseMove); element.addEventListener('click', handleClick); return () => { isHoveredRef.current = false; element.removeEventListener('mouseenter', handleMouseEnter); element.removeEventListener('mouseleave', handleMouseLeave); element.removeEventListener('mousemove', handleMouseMove); element.removeEventListener('click', handleClick); clearAllParticles(); }; }, [animateParticles, clearAllParticles, disableAnimations, enableTilt, enableMagnetism, clickEffect, glowColor]); return (
{card.description}
{card.description}