bookmark-check

PreviousNext
Docs
lucide-animatedui

Preview

Loading preview…
bookmark-check.tsx
'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 BookmarkCheckIconHandle {
  startAnimation: () => void;
  stopAnimation: () => void;
}

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

const BOOKMARK_VARIANTS: Variants = {
  normal: { scaleY: 1, scaleX: 1 },
  animate: {
    scaleY: [1, 1.3, 0.9, 1.05, 1],
    scaleX: [1, 0.9, 1.1, 0.95, 1],
    transition: {
      duration: 0.6,
      ease: 'easeOut',
    },
  },
};

const CHECK_VARIANTS: Variants = {
  normal: { opacity: 1, strokeDashoffset: 0 },
  animate: {
    strokeDashoffset: [1, 0],
    opacity: [0, 1],
    transition: {
      duration: 0.3,
      ease: 'easeOut',
    },
  },
};

const BookmarkCheckIcon = forwardRef<
  BookmarkCheckIconHandle,
  BookmarkCheckIconProps
>(({ className, size = 28, onMouseEnter, onMouseLeave, ...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.path
          d="m19 21-7-4-7 4V5a2 2 0 0 1 2-2h10a2 2 0 0 1 2 2Z"
          animate={controls}
          variants={BOOKMARK_VARIANTS}
          style={{ originX: 0.5, originY: 0.5 }}
        />

        <motion.path
          d="m9 10 2 2 4-4"
          animate={controls}
          variants={CHECK_VARIANTS}
          initial="normal"
          strokeDasharray="1 1"
          pathLength="1"
        />
      </svg>
    </div>
  );
});

BookmarkCheckIcon.displayName = 'BookmarkCheckIcon';

export { BookmarkCheckIcon };

Installation

npx shadcn@latest add @lucide-animated/bookmark-check

Usage

import { BookmarkCheck } from "@/components/ui/bookmark-check"
<BookmarkCheck />