|
1 | 1 | 'use client'; |
2 | 2 |
|
3 | | -interface ButtonProps { |
| 3 | +import { motion, useMotionValue, useSpring, HTMLMotionProps } from 'framer-motion'; |
| 4 | +import { useRef } from 'react'; |
| 5 | + |
| 6 | +interface ButtonProps extends HTMLMotionProps<"button"> { |
4 | 7 | children: React.ReactNode; |
5 | | - onClick?: () => void; |
6 | 8 | variant?: 'primary' | 'secondary' | 'danger'; |
7 | | - disabled?: boolean; |
8 | | - type?: 'button' | 'submit'; |
9 | | - className?: string; |
10 | 9 | } |
11 | 10 |
|
12 | | -import { motion } from 'framer-motion'; |
13 | | - |
14 | 11 | export function Button({ |
15 | 12 | children, |
16 | | - onClick, |
17 | 13 | variant = 'primary', |
18 | | - disabled = false, |
19 | | - type = 'button', |
20 | 14 | className = '', |
| 15 | + ...props |
21 | 16 | }: ButtonProps) { |
22 | | - // Base classes provided by globals.css .cyber-button |
23 | | - // We can add variant-specific overrides if needed, but the main style is consistent |
| 17 | + const ref = useRef<HTMLButtonElement>(null); |
| 18 | + |
| 19 | + const x = useMotionValue(0); |
| 20 | + const y = useMotionValue(0); |
| 21 | + |
| 22 | + // Smooth springs for magnetic effect - weak strength |
| 23 | + const springConfig = { damping: 15, stiffness: 150, mass: 0.1 }; |
| 24 | + const springX = useSpring(x, springConfig); |
| 25 | + const springY = useSpring(y, springConfig); |
| 26 | + |
| 27 | + /* Cache rect on mouse enter to avoid layout thrashing */ |
| 28 | + const rectRef = useRef<DOMRect | null>(null); |
| 29 | + |
| 30 | + const handleMouseEnter = (e: React.MouseEvent<HTMLButtonElement>) => { |
| 31 | + rectRef.current = e.currentTarget.getBoundingClientRect(); |
| 32 | + }; |
| 33 | + |
| 34 | + const handleMouseMove = (e: React.MouseEvent<HTMLButtonElement>) => { |
| 35 | + if (props.disabled) return; |
| 36 | + const { clientX, clientY } = e; |
| 37 | + |
| 38 | + // Use cached rect if available, fallback to getBoundingClientRect if not |
| 39 | + const rect = rectRef.current || e.currentTarget.getBoundingClientRect(); |
| 40 | + const { left, top, width, height } = rect; |
| 41 | + const centerX = left + width / 2; |
| 42 | + const centerY = top + height / 2; |
| 43 | + |
| 44 | + // Weak magnetic pull |
| 45 | + // Divide distance by 8 to limit movement max to ~10px typically |
| 46 | + const distanceX = clientX - centerX; |
| 47 | + const distanceY = clientY - centerY; |
| 48 | + |
| 49 | + x.set(distanceX / 8); |
| 50 | + y.set(distanceY / 8); |
| 51 | + }; |
| 52 | + |
| 53 | + const handleMouseLeave = () => { |
| 54 | + x.set(0); |
| 55 | + y.set(0); |
| 56 | + }; |
24 | 57 |
|
25 | 58 | const variantStyles = { |
26 | | - primary: '', // Standard cyber-button |
27 | | - secondary: 'bg-transparent border-neon-green/30 hover:bg-neon-green/10', // Override for secondary |
| 59 | + primary: '', |
| 60 | + secondary: 'bg-transparent border-neon-green/30 hover:bg-neon-green/10', |
28 | 61 | danger: 'border-red-500 text-red-500 hover:bg-red-500/10 hover:text-red-500 hover:shadow-[0_0_20px_rgba(239,68,68,0.4)]', |
29 | 62 | }; |
30 | 63 |
|
31 | 64 | return ( |
32 | 65 | <motion.button |
33 | | - type={type} |
34 | | - onClick={onClick} |
35 | | - disabled={disabled} |
36 | | - whileHover={!disabled ? { scale: 1.02 } : {}} |
37 | | - whileTap={!disabled ? { scale: 0.98 } : {}} |
| 66 | + ref={ref} |
| 67 | + onMouseMove={handleMouseMove} |
| 68 | + onMouseEnter={handleMouseEnter} |
| 69 | + onMouseLeave={handleMouseLeave} |
| 70 | + style={{ x: springX, y: springY }} |
| 71 | + whileHover={!props.disabled ? { |
| 72 | + scale: 1.02, |
| 73 | + boxShadow: '0 0 15px rgba(0, 255, 65, 0.3)' |
| 74 | + } : {}} |
| 75 | + whileTap={!props.disabled ? { scale: 0.98 } : {}} |
38 | 76 | className={`cyber-button ${variantStyles[variant]} ${className}`} |
| 77 | + {...props} |
39 | 78 | > |
40 | 79 | {children} |
41 | 80 | </motion.button> |
|
0 commit comments