activity.tsx82 lines · main
1'use client';
2
3import { motion, useAnimation } from 'motion/react';
4import type { HTMLAttributes } from 'react';
5import { forwardRef, useCallback, useImperativeHandle, useRef } from 'react';
6
7import { cn } from '@/lib/utils';
8
9export interface ActivityIconHandle {
10 startAnimation: () => void;
11 stopAnimation: () => void;
12}
13
14interface ActivityIconProps extends HTMLAttributes<HTMLDivElement> {
15 size?: number;
16}
17
18const ActivityIcon = forwardRef<ActivityIconHandle, ActivityIconProps>(
19 ({ onMouseEnter, onMouseLeave, className, size = 18, ...props }, ref) => {
20 const controls = useAnimation();
21 const isControlledRef = useRef(false);
22
23 useImperativeHandle(ref, () => {
24 isControlledRef.current = true;
25 return {
26 startAnimation: () => controls.start('animate'),
27 stopAnimation: () => controls.start('normal'),
28 };
29 });
30
31 const handleMouseEnter = useCallback(
32 (e: React.MouseEvent<HTMLDivElement>) => {
33 if (isControlledRef.current) onMouseEnter?.(e);
34 else controls.start('animate');
35 },
36 [controls, onMouseEnter],
37 );
38
39 const handleMouseLeave = useCallback(
40 (e: React.MouseEvent<HTMLDivElement>) => {
41 if (isControlledRef.current) onMouseLeave?.(e);
42 else controls.start('normal');
43 },
44 [controls, onMouseLeave],
45 );
46
47 return (
48 <div
49 className={cn(className)}
50 onMouseEnter={handleMouseEnter}
51 onMouseLeave={handleMouseLeave}
52 {...props}
53 >
54 <svg
55 fill="none"
56 height={size}
57 stroke="currentColor"
58 strokeLinecap="round"
59 strokeLinejoin="round"
60 strokeWidth="2"
61 viewBox="0 0 24 24"
62 width={size}
63 xmlns="http://www.w3.org/2000/svg"
64 >
65 <motion.path
66 animate={controls}
67 d="M22 12h-4l-3 9L9 3l-3 9H2"
68 transition={{ duration: 0.6, ease: 'easeInOut' }}
69 variants={{
70 normal: { pathLength: 1, opacity: 1 },
71 animate: { pathLength: [0, 1], opacity: [0.3, 1] },
72 }}
73 />
74 </svg>
75 </div>
76 );
77 },
78);
79
80ActivityIcon.displayName = 'ActivityIcon';
81
82export { ActivityIcon };