rocket.tsx83 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 RocketIconHandle {
10 startAnimation: () => void;
11 stopAnimation: () => void;
12}
13
14interface RocketIconProps extends HTMLAttributes<HTMLDivElement> {
15 size?: number;
16}
17
18const RocketIcon = forwardRef<RocketIconHandle, RocketIconProps>(
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 <motion.svg
55 animate={controls}
56 fill="none"
57 height={size}
58 stroke="currentColor"
59 strokeLinecap="round"
60 strokeLinejoin="round"
61 strokeWidth="2"
62 viewBox="0 0 24 24"
63 width={size}
64 xmlns="http://www.w3.org/2000/svg"
65 transition={{ duration: 0.5, ease: 'easeInOut' }}
66 variants={{
67 normal: { x: 0, y: 0 },
68 animate: { x: [0, 2, 0], y: [0, -3, 0] },
69 }}
70 >
71 <path d="M4.5 16.5c-1.5 1.26-2 5-2 5s3.74-.5 5-2c.71-.84.7-2.13-.09-2.91a2.18 2.18 0 0 0-2.91-.09z" />
72 <path d="m12 15-3-3a22 22 0 0 1 2-3.95A12.88 12.88 0 0 1 22 2c0 2.72-.78 7.5-6 11a22.35 22.35 0 0 1-4 2z" />
73 <path d="M9 12H4s.55-3.03 2-4c1.62-1.08 5 0 5 0" />
74 <path d="M12 15v5s3.03-.55 4-2c1.08-1.62 0-5 0-5" />
75 </motion.svg>
76 </div>
77 );
78 },
79);
80
81RocketIcon.displayName = 'RocketIcon';
82
83export { RocketIcon };