'use client';
import type { 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 ChartBarDecreasingIconHandle {
startAnimation: () => void;
stopAnimation: () => void;
}
interface ChartBarDecreasingIconProps extends HTMLAttributes<HTMLDivElement> {
size?: number;
}
const LINE_VARIANTS: Variants = {
visible: { pathLength: 1, opacity: 1 },
hidden: { pathLength: 0, opacity: 0 },
};
const ChartBarDecreasingIcon = forwardRef<
ChartBarDecreasingIconHandle,
ChartBarDecreasingIconProps
>(({ onMouseEnter, onMouseLeave, className, size = 28, ...props }, ref) => {
const controls = useAnimation();
const isControlledRef = useRef(false);
useImperativeHandle(ref, () => {
isControlledRef.current = true;
return {
startAnimation: async () => {
await controls.start((i) => ({
pathLength: 0,
opacity: 0,
transition: { delay: i * 0.1, duration: 0.3 },
}));
await controls.start((i) => ({
pathLength: 1,
opacity: 1,
transition: { delay: i * 0.1, duration: 0.3 },
}));
},
stopAnimation: () => controls.start('visible'),
};
});
const handleMouseEnter = useCallback(
async (e: React.MouseEvent<HTMLDivElement>) => {
if (!isControlledRef.current) {
await controls.start((i) => ({
pathLength: 0,
opacity: 0,
transition: { delay: i * 0.1, duration: 0.3 },
}));
await controls.start((i) => ({
pathLength: 1,
opacity: 1,
transition: { delay: i * 0.1, duration: 0.3 },
}));
} else {
onMouseEnter?.(e);
}
},
[controls, onMouseEnter]
);
const handleMouseLeave = useCallback(
(e: React.MouseEvent<HTMLDivElement>) => {
if (!isControlledRef.current) {
controls.start('visible');
} 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="M3 3v16a2 2 0 0 0 2 2h16" />
<motion.path
variants={LINE_VARIANTS}
initial="visible"
animate={controls}
custom={1}
d="M7 11h8"
/>
<motion.path
variants={LINE_VARIANTS}
initial="visible"
animate={controls}
custom={2}
d="M7 16h3"
/>
<motion.path
variants={LINE_VARIANTS}
initial="visible"
animate={controls}
custom={0}
d="M7 6h12"
/>
</svg>
</div>
);
});
ChartBarDecreasingIcon.displayName = 'ChartBarDecreasingIcon';
export { ChartBarDecreasingIcon };