'use client';
import type { Transition, Variants } from 'motion/react';
import type { HTMLAttributes } from 'react';
import { forwardRef, useCallback, useImperativeHandle, useRef } from 'react';
import { motion, useAnimation } from 'motion/react';
import { cn } from '@/lib/utils';
export interface FolderClockIconHandle {
startAnimation: () => void;
stopAnimation: () => void;
}
interface FolderClockIconProps extends HTMLAttributes<HTMLDivElement> {
size?: number;
}
const HAND_TRANSITION: Transition = {
duration: 0.6,
ease: [0.4, 0, 0.2, 1],
};
const HAND_VARIANTS: Variants = {
normal: {
rotate: 0,
originX: '0%',
originY: '100%',
},
animate: {
rotate: 360,
originX: '0%',
originY: '100%',
},
};
const MINUTE_HAND_TRANSITION: Transition = {
duration: 0.5,
ease: 'easeInOut',
};
const MINUTE_HAND_VARIANTS: Variants = {
normal: {
rotate: 0,
originX: '0%',
originY: '100%',
},
animate: {
rotate: 45,
originX: '0%',
originY: '100%',
},
};
const FolderClockIcon = forwardRef<FolderClockIconHandle, FolderClockIconProps>(
({ onMouseEnter, onMouseLeave, className, size = 28, ...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<HTMLDivElement>) => {
if (!isControlledRef.current) {
controls.start('animate');
} else {
onMouseEnter?.(e);
}
},
[controls, onMouseEnter]
);
const handleMouseLeave = useCallback(
(e: React.MouseEvent<HTMLDivElement>) => {
if (!isControlledRef.current) {
controls.start('normal');
} else {
onMouseLeave?.(e);
}
},
[controls, onMouseLeave]
);
return (
<div
className={cn(className)}
onMouseEnter={handleMouseEnter}
onMouseLeave={handleMouseLeave}
{...props}
>
<svg
xmlns="http://www.w3.org/2000/svg"
width={size}
height={size}
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
strokeWidth="2"
strokeLinecap="round"
strokeLinejoin="round"
>
<path d="M7 20H4a2 2 0 0 1-2-2V5a2 2 0 0 1 2-2h3.9a2 2 0 0 1 1.69.9l.81 1.2a2 2 0 0 0 1.67.9H20a2 2 0 0 1 2 2" />
<circle cx="16" cy="16" r="6" />
<motion.line
x1="16"
y1="16"
x2="16"
y2="13"
variants={HAND_VARIANTS}
animate={controls}
initial="normal"
transition={HAND_TRANSITION}
/>
<motion.line
x1="16"
y1="16"
x2="18.5"
y2="16"
variants={MINUTE_HAND_VARIANTS}
animate={controls}
initial="normal"
transition={MINUTE_HAND_TRANSITION}
/>
</svg>
</div>
);
}
);
FolderClockIcon.displayName = 'FolderClockIcon';
export { FolderClockIcon };