|
| 1 | +import type { Variants } from 'framer-motion' |
| 2 | +import { motion, useAnimation } from 'framer-motion' |
| 3 | +import type { HTMLAttributes } from 'react' |
| 4 | +import React, { forwardRef, useCallback, useImperativeHandle, useRef } from 'react' |
| 5 | + |
| 6 | +export interface MenuIconHandle { |
| 7 | + startAnimation: () => void |
| 8 | + stopAnimation: () => void |
| 9 | +} |
| 10 | + |
| 11 | +interface MenuIconProps extends HTMLAttributes<HTMLDivElement> { |
| 12 | + size?: number |
| 13 | +} |
| 14 | + |
| 15 | +const lineVariants: Variants = { |
| 16 | + normal: { |
| 17 | + rotate: 0, |
| 18 | + y: 0, |
| 19 | + opacity: 1 |
| 20 | + }, |
| 21 | + animate: (custom: number) => ({ |
| 22 | + rotate: custom === 1 ? 45 : custom === 3 ? -45 : 0, |
| 23 | + y: custom === 1 ? 6 : custom === 3 ? -6 : 0, |
| 24 | + opacity: custom === 2 ? 0 : 1, |
| 25 | + transition: { |
| 26 | + type: 'spring', |
| 27 | + stiffness: 260, |
| 28 | + damping: 20 |
| 29 | + } |
| 30 | + }) |
| 31 | +} |
| 32 | + |
| 33 | +const MenuIcon = forwardRef<MenuIconHandle, MenuIconProps>( |
| 34 | + ({ onMouseEnter, onMouseLeave, className, size = 28, ...props }, ref) => { |
| 35 | + const controls = useAnimation() |
| 36 | + const isControlledRef = useRef(false) |
| 37 | + |
| 38 | + useImperativeHandle(ref, () => { |
| 39 | + isControlledRef.current = true |
| 40 | + |
| 41 | + return { |
| 42 | + startAnimation: () => controls.start('animate'), |
| 43 | + stopAnimation: () => controls.start('normal') |
| 44 | + } |
| 45 | + }) |
| 46 | + |
| 47 | + const handleMouseEnter = useCallback( |
| 48 | + (e: React.MouseEvent<HTMLDivElement>) => { |
| 49 | + if (!isControlledRef.current) { |
| 50 | + controls.start('animate') |
| 51 | + } else { |
| 52 | + onMouseEnter?.(e) |
| 53 | + } |
| 54 | + }, |
| 55 | + [controls, onMouseEnter] |
| 56 | + ) |
| 57 | + |
| 58 | + const handleMouseLeave = useCallback( |
| 59 | + (e: React.MouseEvent<HTMLDivElement>) => { |
| 60 | + if (!isControlledRef.current) { |
| 61 | + controls.start('normal') |
| 62 | + } else { |
| 63 | + onMouseLeave?.(e) |
| 64 | + } |
| 65 | + }, |
| 66 | + [controls, onMouseLeave] |
| 67 | + ) |
| 68 | + return ( |
| 69 | + <div |
| 70 | + className={` select-none p-2 hover:bg-accent rounded-md transition-colors duration-200 flex items-center justify-center ${className}`} |
| 71 | + onMouseEnter={handleMouseEnter} |
| 72 | + onMouseLeave={handleMouseLeave} |
| 73 | + {...props} |
| 74 | + > |
| 75 | + <svg |
| 76 | + xmlns="http://www.w3.org/2000/svg" |
| 77 | + width={size} |
| 78 | + height={size} |
| 79 | + viewBox="0 0 24 24" |
| 80 | + fill="none" |
| 81 | + stroke="currentColor" |
| 82 | + strokeWidth="2" |
| 83 | + strokeLinecap="round" |
| 84 | + strokeLinejoin="round" |
| 85 | + > |
| 86 | + <motion.line |
| 87 | + x1="4" |
| 88 | + y1="6" |
| 89 | + x2="20" |
| 90 | + y2="6" |
| 91 | + variants={lineVariants} |
| 92 | + animate={controls} |
| 93 | + custom={1} |
| 94 | + /> |
| 95 | + <motion.line |
| 96 | + x1="4" |
| 97 | + y1="12" |
| 98 | + x2="20" |
| 99 | + y2="12" |
| 100 | + variants={lineVariants} |
| 101 | + animate={controls} |
| 102 | + custom={2} |
| 103 | + /> |
| 104 | + <motion.line |
| 105 | + x1="4" |
| 106 | + y1="18" |
| 107 | + x2="20" |
| 108 | + y2="18" |
| 109 | + variants={lineVariants} |
| 110 | + animate={controls} |
| 111 | + custom={3} |
| 112 | + /> |
| 113 | + </svg> |
| 114 | + </div> |
| 115 | + ) |
| 116 | + } |
| 117 | +) |
| 118 | + |
| 119 | +MenuIcon.displayName = 'MenuIcon' |
| 120 | + |
| 121 | +export { MenuIcon } |
0 commit comments