lightswind
Version:
A collection of beautifully crafted React Components, Blocks & Templates for Modern Developers. Create stunning web applications effortlessly by using our 160+ professional and animated react components.
37 lines (36 loc) • 2.35 kB
JavaScript
// @ts-nocheck
"use client";
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { useState } from "react";
import { motion, AnimatePresence } from "framer-motion";
import { Check, Copy } from "lucide-react";
import { cn } from "../../lib/utils";
export function AnimatedCopyButton({ textToCopy, className, size = "md", onCopy, }) {
const [isCopied, setIsCopied] = useState(false);
const handleCopy = async () => {
try {
await navigator.clipboard.writeText(textToCopy);
setIsCopied(true);
if (onCopy)
onCopy();
// Reset after 2 seconds
setTimeout(() => {
setIsCopied(false);
}, 2000);
}
catch (err) {
console.error("Failed to copy text: ", err);
}
};
const sizes = {
sm: "h-8 w-8",
md: "h-10 w-10",
lg: "h-12 w-12",
};
const iconSizes = {
sm: "h-4 w-4",
md: "h-5 w-5",
lg: "h-6 w-6",
};
return (_jsxs("button", { onClick: handleCopy, className: cn("relative flex items-center justify-center rounded-md border bg-background transition-colors hover:bg-muted focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2", sizes[size], className), "aria-label": "Copy to clipboard", children: [_jsx(AnimatePresence, { mode: "wait", initial: false, children: isCopied ? (_jsx(motion.div, { initial: { opacity: 0, scale: 0.5, rotate: -45 }, animate: { opacity: 1, scale: 1, rotate: 0 }, exit: { opacity: 0, scale: 0.5, rotate: 45 }, transition: { type: "spring", stiffness: 300, damping: 20 }, className: "absolute flex items-center justify-center text-green-500", children: _jsx(Check, { className: iconSizes[size] }) }, "check")) : (_jsx(motion.div, { initial: { opacity: 0, scale: 0.5 }, animate: { opacity: 1, scale: 1 }, exit: { opacity: 0, scale: 0.5 }, transition: { type: "spring", stiffness: 300, damping: 20 }, className: "absolute flex items-center justify-center text-foreground/70", children: _jsx(Copy, { className: iconSizes[size] }) }, "copy")) }), isCopied && (_jsx(motion.div, { initial: { opacity: 0.5, scale: 1 }, animate: { opacity: 0, scale: 2 }, transition: { duration: 0.5 }, className: "absolute inset-0 rounded-md bg-green-500/20" }))] }));
}