{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "copy-button",
  "title": "Copy Button",
  "description": "An icon button that copies text to the clipboard, morphs its icon to reflect the result, and anchors a confirmation toast to itself.",
  "dependencies": [
    "lucide",
    "morphicons"
  ],
  "registryDependencies": [
    "utils",
    "@persianlabsui/button",
    "@persianlabsui/tooltip",
    "@persianlabsui/toast"
  ],
  "files": [
    {
      "path": "registry/base/ui/copy-button.tsx",
      "content": "\"use client\"\n\nimport { Check, Copy } from \"lucide\"\nimport { MorphIcon } from \"morphicons/react\"\nimport * as React from \"react\"\n\nimport { Button } from \"@/components/ui/button\"\nimport {\n  Tooltip,\n  TooltipContent,\n  TooltipTrigger,\n} from \"@/components/ui/tooltip\"\nimport { AnchoredToastProvider, ToastPrimitive } from \"@/components/ui/toast\"\nimport { useCopyToClipboard } from \"@/hooks/use-copy-to-clipboard\"\nimport { cn } from \"@/lib/utils\"\n\nexport interface CopyStateIconProps {\n  copied: boolean\n  idleIcon?: React.ReactNode\n  doneIcon?: React.ReactNode\n}\n\nexport function CopyStateIcon({\n  copied,\n  idleIcon,\n  doneIcon,\n}: CopyStateIconProps) {\n  if (!copied && idleIcon) return idleIcon\n  if (copied && doneIcon) return doneIcon\n\n  return (\n    <MorphIcon\n      icon={copied ? Check : Copy}\n      spring=\"snappy\"\n      className=\"size-4\"\n    />\n  )\n}\n\nexport interface CopyButtonProps extends React.ComponentProps<typeof Button> {\n  text: string | (() => string)\n  label?: string\n  /**\n   * Toast manager the \"copied\" feedback anchors to. By default CopyButton\n   * brings its own manager and provider, so it works with zero setup. Pass\n   * your app's own manager (alongside a matching ancestor\n   * `AnchoredToastProvider`) only if you want this button's confirmation to\n   * share state with other anchored toasts elsewhere on the page.\n   */\n  toastManager?: ReturnType<typeof ToastPrimitive.createToastManager>\n  /** Which side of the button the confirmation toast anchors to. @default \"top\" */\n  side?: NonNullable<ToastPrimitive.Positioner.Props[\"side\"]>\n  onCopySuccess?: (text: string) => void\n  idleIcon?: React.ReactNode\n  doneIcon?: React.ReactNode\n}\n\n/**\n * An icon button that copies `text` to the clipboard, morphs its icon to\n * reflect the result, and anchors a toast to itself confirming the copy —\n * self-contained, no `AnchoredToastProvider` wrapper needed.\n */\nexport function CopyButton({\n  className,\n  size = \"icon\",\n  children,\n  text,\n  label = \"کپی\",\n  toastManager,\n  side = \"top\",\n  idleIcon,\n  doneIcon,\n  onClick,\n  onCopySuccess,\n  ...props\n}: CopyButtonProps) {\n  const ref = React.useRef<HTMLButtonElement>(null)\n  const pendingValue = React.useRef(\"\")\n  const timeout = 2000\n  const ownManager = React.useMemo(\n    () => ToastPrimitive.createToastManager(),\n    []\n  )\n  const manager = toastManager ?? ownManager\n\n  const { copyToClipboard, isCopied } = useCopyToClipboard({\n    timeout,\n    onCopy: () => {\n      if (ref.current) {\n        manager.add({\n          title: \"کپی شد!\",\n          timeout,\n          positionerProps: { anchor: ref.current, side },\n        })\n      }\n      onCopySuccess?.(pendingValue.current)\n    },\n  })\n\n  const button = (\n    <Tooltip>\n      <TooltipTrigger\n        render={\n          <Button\n            ref={ref}\n            className={cn(\"will-change-transform\", className)}\n            size={size}\n            aria-label={label}\n            onClick={(event) => {\n              const value = typeof text === \"function\" ? text() : text\n              pendingValue.current = value\n              copyToClipboard(value)\n              onClick?.(event)\n            }}\n            {...props}\n          >\n            <CopyStateIcon\n              copied={isCopied}\n              idleIcon={idleIcon}\n              doneIcon={doneIcon}\n            />\n            {children}\n          </Button>\n        }\n      />\n      <TooltipContent sideOffset={8}>{label}</TooltipContent>\n    </Tooltip>\n  )\n\n  if (toastManager) {\n    return button\n  }\n\n  return (\n    <AnchoredToastProvider toastManager={ownManager}>\n      {button}\n    </AnchoredToastProvider>\n  )\n}\n",
      "type": "registry:ui"
    },
    {
      "path": "registry/base/hooks/use-copy-to-clipboard.ts",
      "content": "\"use client\"\n\nimport * as React from \"react\"\n\nexport function useCopyToClipboard({\n  timeout = 2000,\n  onCopy,\n}: {\n  timeout?: number\n  onCopy?: () => void\n} = {}): { copyToClipboard: (value: string) => void; isCopied: boolean } {\n  const [isCopied, setIsCopied] = React.useState(false)\n  const timeoutIdRef = React.useRef<NodeJS.Timeout | null>(null)\n\n  const copyToClipboard = (value: string): void => {\n    if (typeof window === \"undefined\" || !navigator.clipboard?.writeText) {\n      return\n    }\n\n    if (!value) return\n\n    navigator.clipboard.writeText(value).then(() => {\n      if (timeoutIdRef.current) {\n        clearTimeout(timeoutIdRef.current)\n      }\n\n      setIsCopied(true)\n\n      if (onCopy) {\n        onCopy()\n      }\n\n      if (timeout !== 0) {\n        timeoutIdRef.current = setTimeout(() => {\n          setIsCopied(false)\n          timeoutIdRef.current = null\n        }, timeout)\n      }\n    }, console.error)\n  }\n\n  // Cleanup timeout on unmount\n  React.useEffect(() => {\n    return (): void => {\n      if (timeoutIdRef.current) {\n        clearTimeout(timeoutIdRef.current)\n      }\n    }\n  }, [])\n\n  return { copyToClipboard, isCopied }\n}\n",
      "type": "registry:hook"
    }
  ],
  "type": "registry:ui"
}