'use client'; import { cn } from '@/lib/utils'; import type { Variants } from 'motion/react'; import { motion, useAnimation } from 'motion/react'; import type { HTMLAttributes } from 'react'; import { forwardRef, useCallback, useImperativeHandle, useRef } from 'react'; export interface ActivityIconHandle { startAnimation: () => void; stopAnimation: () => void; } const variants: Variants = { normal: { opacity: 1, pathLength: 1, pathOffset: 0, transition: { duration: 0.4, opacity: { duration: 0.1 }, }, }, animate: { opacity: [0, 1], pathLength: [0, 1], pathOffset: [1, 0], transition: { duration: 0.6, ease: 'linear', opacity: { duration: 0.1 }, }, }, }; const ActivityIcon = forwardRef< ActivityIconHandle, HTMLAttributes >(({ onMouseEnter, onMouseLeave, className, ...props }, ref) => { const controls = useAnimation(); const isControlledRef = useRef(false); useImperativeHandle(ref, () => { isControlledRef.current = true; return { startAnimation: () => controls.start('animate'), stopAnimation: () => controls.start('normal'), }; }); const handleMouseEnter = useCallback( (e: React.MouseEvent) => { if (!isControlledRef.current) { controls.start('animate'); } else { onMouseEnter?.(e); } }, [controls, onMouseEnter] ); const handleMouseLeave = useCallback( (e: React.MouseEvent) => { if (!isControlledRef.current) { controls.start('normal'); } else { onMouseLeave?.(e); } }, [controls, onMouseLeave] ); return (
); }); ActivityIcon.displayName = 'ActivityIcon'; export { ActivityIcon };