git-pull-request-closed

PreviousNext
Docs
lucide-animatedui

Preview

Loading preview…
git-pull-request-closed.tsx
'use client';

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 GitPullRequestClosedIconHandle {
  startAnimation: () => void;
  stopAnimation: () => void;
}

interface GitPullRequestClosedIconProps extends HTMLAttributes<HTMLDivElement> {
  size?: number;
}

const DURATION = 0.3;

const CALCULATE_DELAY = (i: number) => {
  if (i === 0) return 0.1;

  return i * DURATION + 0.1;
};

const GitPullRequestClosedIcon = forwardRef<
  GitPullRequestClosedIconHandle,
  GitPullRequestClosedIconProps
>(({ 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"
      >
        <motion.circle
          cx="6"
          cy="6"
          r="3"
          transition={{
            duration: DURATION,
            delay: CALCULATE_DELAY(0),
            opacity: { delay: CALCULATE_DELAY(0) },
          }}
          variants={{
            normal: { pathLength: 1, opacity: 1, transition: { delay: 0 } },
            animate: {
              pathLength: [0, 1],
              opacity: [0, 1],
            },
          }}
          animate={controls}
        />

        <motion.path
          d="M6 9v12"
          transition={{
            duration: DURATION,
            delay: CALCULATE_DELAY(1),
            opacity: { delay: CALCULATE_DELAY(1) },
          }}
          variants={{
            normal: {
              pathLength: 1,
              pathOffset: 0,
              opacity: 1,
              transition: { delay: 0 },
            },
            animate: {
              pathLength: [0, 1],
              opacity: [0, 1],
              pathOffset: [1, 0],
            },
          }}
          animate={controls}
        />

        <motion.path
          d="m21 3-6 6"
          transition={{
            duration: DURATION,
            delay: CALCULATE_DELAY(2),
            opacity: { delay: CALCULATE_DELAY(2) },
          }}
          variants={{
            normal: { opacity: 1, transition: { delay: 0 } },
            animate: { opacity: [0, 1] },
          }}
          animate={controls}
        />

        <motion.path
          d="m21 9-6-6"
          transition={{
            duration: DURATION,
            delay: CALCULATE_DELAY(2),
            opacity: { delay: CALCULATE_DELAY(2) },
          }}
          variants={{
            normal: { opacity: 1, transition: { delay: 0 } },
            animate: { opacity: [0, 1] },
          }}
          animate={controls}
        />

        <motion.path
          d="M18 11.5V15"
          transition={{
            duration: DURATION,
            delay: CALCULATE_DELAY(3),
            opacity: { delay: CALCULATE_DELAY(3) },
          }}
          variants={{
            normal: {
              pathLength: 1,
              pathOffset: 0,
              opacity: 1,
              transition: { delay: 0 },
            },
            animate: {
              pathLength: [0, 1],
              opacity: [0, 1],
              pathOffset: [1, 0],
            },
          }}
          animate={controls}
        />

        <motion.circle
          cx="18"
          cy="18"
          r="3"
          transition={{
            duration: DURATION,
            delay: CALCULATE_DELAY(4),
            opacity: { delay: CALCULATE_DELAY(4) },
          }}
          variants={{
            normal: { pathLength: 1, opacity: 1, transition: { delay: 0 } },
            animate: {
              pathLength: [0, 1],
              opacity: [0, 1],
            },
          }}
          animate={controls}
        />
      </svg>
    </div>
  );
});

GitPullRequestClosedIcon.displayName = 'GitPullRequestClosedIcon';

export { GitPullRequestClosedIcon };

Installation

npx shadcn@latest add @lucide-animated/git-pull-request-closed

Usage

import { GitPullRequestClosed } from "@/components/ui/git-pull-request-closed"
<GitPullRequestClosed />