{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "copy-button",
  "title": "Copy Button",
  "description": "A standalone copy-to-clipboard button with ghost/outline/solid variants, icon-only or labeled modes, a floating tooltip, and animated copied/error states.",
  "dependencies": [
    "motion",
    "clsx",
    "tailwind-merge"
  ],
  "files": [
    {
      "path": "registry/new-york/copy-button/copy-button.tsx",
      "content": "\"use client\";\n\nimport * as React from \"react\";\nimport { createPortal } from \"react-dom\";\nimport { motion, AnimatePresence } from \"motion/react\";\nimport { clsx, type ClassValue } from \"clsx\";\nimport { twMerge } from \"tailwind-merge\";\n\nfunction cn(...inputs: ClassValue[]) {\n  return twMerge(clsx(inputs));\n}\n\nexport type CopyButtonVariant = \"ghost\" | \"outline\" | \"solid\";\nexport type CopyButtonSize = \"sm\" | \"md\" | \"lg\";\ntype CopyState = \"idle\" | \"copied\" | \"error\";\n\nexport interface CopyButtonProps {\n  /** The text to copy, or a function (sync or async) that produces it lazily, evaluated on click. */\n  value: string | (() => string | Promise<string>);\n  /** Shown next to the icon; omit for an icon-only button. */\n  label?: string;\n  copiedLabel?: string;\n  errorLabel?: string;\n  variant?: CopyButtonVariant;\n  size?: CopyButtonSize;\n  theme?: \"dark\" | \"light\";\n  accentColor?: string;\n  /** How long the copied/error state is shown before reverting, in ms. */\n  resetDelay?: number;\n  /** Icon-only buttons show a small floating tooltip on hover/focus by default. */\n  showTooltip?: boolean;\n  onCopy?: (value: string) => void;\n  onError?: (error: unknown) => void;\n  disabled?: boolean;\n  className?: string;\n}\n\nconst PALETTES = {\n  dark: { text: \"#F5F4F1\", muted: \"rgba(245,244,241,0.6)\", border: \"rgba(255,255,255,0.14)\", hover: \"rgba(255,255,255,0.08)\", solidBg: \"#F5F4F1\", solidText: \"#0A0A0A\", tooltipBg: \"#161616\", tooltipBorder: \"rgba(255,255,255,0.12)\", error: \"#FF7A6B\", success: \"#87FFE3\" },\n  light: { text: \"#0A0A0A\", muted: \"rgba(10,10,10,0.6)\", border: \"rgba(10,10,10,0.16)\", hover: \"rgba(10,10,10,0.06)\", solidBg: \"#0A0A0A\", solidText: \"#FFFFFF\", tooltipBg: \"#FFFFFF\", tooltipBorder: \"rgba(10,10,10,0.1)\", error: \"#E5484D\", success: \"#0F9F7F\" },\n};\n\nconst SIZES: Record<CopyButtonSize, { height: number; padX: number; font: number; icon: number; radius: number; gap: number }> = {\n  sm: { height: 30, padX: 10, font: 12.5, icon: 14, radius: 9, gap: 6 },\n  md: { height: 36, padX: 12, font: 13.5, icon: 15, radius: 10, gap: 7 },\n  lg: { height: 42, padX: 15, font: 14.5, icon: 17, radius: 11, gap: 8 },\n};\n\ntype Palette = (typeof PALETTES)[\"dark\"];\n\nfunction StateIcon({ state, size }: { state: CopyState; size: number }) {\n  return (\n    <svg width={size} height={size} viewBox=\"0 0 16 16\" fill=\"none\" stroke=\"currentColor\" strokeWidth=\"1.7\" strokeLinecap=\"round\" strokeLinejoin=\"round\" aria-hidden=\"true\">\n      {state === \"copied\" ? (\n        <path d=\"M3 8.4L6.2 11.5L13 4.3\" />\n      ) : state === \"error\" ? (\n        <path d=\"M4 4L12 12M12 4L4 12\" />\n      ) : (\n        <>\n          <rect x=\"5.7\" y=\"5.7\" width=\"8.3\" height=\"8.8\" rx=\"1.6\" />\n          <path d=\"M3.7 10.8A1.5 1.5 0 0 1 2.2 9.3V3.7A1.5 1.5 0 0 1 3.7 2.2h5.6a1.5 1.5 0 0 1 1.5 1.5\" />\n        </>\n      )}\n    </svg>\n  );\n}\n\nfunction Tooltip({ label, anchorRef, p }: { label: string; anchorRef: React.RefObject<HTMLElement | null>; p: Palette }) {\n  const [pos, setPos] = React.useState<{ x: number; bottom: number } | null>(null);\n  React.useEffect(() => {\n    const r = anchorRef.current?.getBoundingClientRect();\n    // Anchored with `bottom` (from the viewport's bottom edge) rather than\n    // `top` + a -100% transform: this component also animates `y` for its\n    // little entrance slide, and Motion's animated y/scale/x replace\n    // whatever `y` the style prop provides rather than composing with it —\n    // so a static y:\"-100%\" anchor would just get clobbered by animate's\n    // y:0 the moment the entrance finishes. `bottom` needs no y at all.\n    if (r) setPos({ x: r.left + r.width / 2, bottom: window.innerHeight - r.top + 8 });\n  }, [anchorRef]);\n  if (typeof document === \"undefined\" || !pos) return null;\n  return createPortal(\n    <motion.span\n      role=\"tooltip\"\n      initial={{ opacity: 0, y: 4, scale: 0.94 }}\n      animate={{ opacity: 1, y: 0, scale: 1 }}\n      exit={{ opacity: 0, y: 4, scale: 0.94 }}\n      transition={{ duration: 0.12 }}\n      className=\"pointer-events-none fixed z-[9999] font-medium whitespace-nowrap\"\n      style={{ left: pos.x, bottom: pos.bottom, x: \"-50%\", padding: \"4px 8px\", borderRadius: 7, background: p.tooltipBg, border: `1px solid ${p.tooltipBorder}`, color: p.text, fontSize: 12 }}\n    >\n      {label}\n    </motion.span>,\n    document.body,\n  );\n}\n\nexport function CopyButton({\n  value,\n  label,\n  copiedLabel = \"Copied\",\n  errorLabel = \"Failed to copy\",\n  variant = \"ghost\",\n  size = \"md\",\n  theme = \"dark\",\n  accentColor,\n  resetDelay = 1600,\n  showTooltip = true,\n  onCopy,\n  onError,\n  disabled = false,\n  className,\n}: CopyButtonProps) {\n  const p = PALETTES[theme];\n  const s = SIZES[size];\n  const iconOnly = !label;\n  const [state, setState] = React.useState<CopyState>(\"idle\");\n  const [hovered, setHovered] = React.useState(false);\n  const timer = React.useRef<ReturnType<typeof setTimeout> | null>(null);\n  const btnRef = React.useRef<HTMLButtonElement>(null);\n\n  React.useEffect(() => () => {\n    if (timer.current) clearTimeout(timer.current);\n  }, []);\n\n  const settle = (next: CopyState) => {\n    setState(next);\n    if (timer.current) clearTimeout(timer.current);\n    timer.current = setTimeout(() => setState(\"idle\"), resetDelay);\n  };\n\n  const handleClick = async () => {\n    if (disabled || state !== \"idle\") return;\n    try {\n      const resolved = typeof value === \"function\" ? await value() : value;\n      const cb = navigator.clipboard;\n      if (!cb || typeof cb.writeText !== \"function\") throw new Error(\"Clipboard API unavailable\");\n      await cb.writeText(resolved);\n      settle(\"copied\");\n      onCopy?.(resolved);\n    } catch (err) {\n      // clipboard can be blocked inside an embedded frame, or the value\n      // function itself can throw/reject\n      settle(\"error\");\n      onError?.(err);\n    }\n  };\n\n  const stateColor = state === \"copied\" ? p.success : state === \"error\" ? p.error : undefined;\n  const text = state === \"copied\" ? copiedLabel : state === \"error\" ? errorLabel : label;\n\n  const variantStyle: React.CSSProperties =\n    variant === \"solid\"\n      ? { background: p.solidBg, color: p.solidText, border: \"1px solid transparent\" }\n      : variant === \"outline\"\n        ? { background: \"transparent\", color: p.text, border: `1px solid ${p.border}` }\n        : { background: \"transparent\", color: p.text, border: \"1px solid transparent\" };\n\n  const tooltipLabel = state === \"copied\" ? copiedLabel : state === \"error\" ? errorLabel : \"Copy\";\n\n  return (\n    <span className=\"relative inline-flex\">\n      <motion.button\n        ref={btnRef}\n        type=\"button\"\n        disabled={disabled}\n        onClick={handleClick}\n        aria-label={iconOnly ? tooltipLabel : undefined}\n        onMouseEnter={() => setHovered(true)}\n        onMouseLeave={() => setHovered(false)}\n        onFocus={() => setHovered(true)}\n        onBlur={() => setHovered(false)}\n        whileTap={disabled ? undefined : { scale: 0.94 }}\n        className={cn(\"inline-flex cursor-pointer items-center justify-center outline-none focus-visible:ring-2 disabled:cursor-not-allowed disabled:opacity-45\", className)}\n        style={{\n          height: s.height,\n          width: iconOnly ? s.height : undefined,\n          padding: iconOnly ? 0 : `0 ${s.padX}px`,\n          borderRadius: s.radius,\n          gap: s.gap,\n          fontSize: s.font,\n          fontWeight: 500,\n          fontFamily: \"Inter, sans-serif\",\n          color: stateColor ?? variantStyle.color,\n          background: variant === \"ghost\" && hovered && state === \"idle\" ? p.hover : variantStyle.background,\n          border: variantStyle.border,\n          borderColor: variant === \"outline\" ? (stateColor ?? p.border) : undefined,\n          transition: \"background 0.14s ease, color 0.14s ease, border-color 0.14s ease\",\n          [\"--tw-ring-color\" as string]: accentColor ?? p.text,\n        }}\n      >\n        <AnimatePresence mode=\"popLayout\" initial={false}>\n          <motion.span key={state} initial={{ opacity: 0, scale: 0.6, rotate: -20 }} animate={{ opacity: 1, scale: 1, rotate: 0 }} exit={{ opacity: 0, scale: 0.6 }} transition={{ type: \"spring\", stiffness: 500, damping: 28 }} className=\"flex shrink-0 items-center justify-center\">\n            <StateIcon state={state} size={s.icon} />\n          </motion.span>\n        </AnimatePresence>\n        {!iconOnly && (\n          <span className=\"whitespace-nowrap\" style={{ minWidth: 0 }}>\n            {text}\n          </span>\n        )}\n      </motion.button>\n      {iconOnly && showTooltip && (\n        <AnimatePresence>{hovered && !disabled && <Tooltip label={tooltipLabel} anchorRef={btnRef} p={p} />}</AnimatePresence>\n      )}\n    </span>\n  );\n}\n",
      "type": "registry:component"
    }
  ],
  "type": "registry:component"
}