{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "copy-button",
  "title": "Copy Button",
  "description": "Accessible clipboard button wrapping Flag's Button with transient CheckIcon confirmation.",
  "dependencies": [
    "lucide-react@^1.49.0"
  ],
  "registryDependencies": [
    "@flag/utils",
    "@flag/button"
  ],
  "files": [
    {
      "path": "registry/flag/ui/copy-button.tsx",
      "content": "\"use client\"\n\nimport * as React from \"react\"\nimport { CheckIcon, CopyIcon } from \"lucide-react\"\n\nimport { cn } from \"@/lib/utils\"\nimport { Button } from \"@/registry/flag/ui/button\"\n\n// Clipboard action button built on Flag's Button: swaps CopyIcon -> CheckIcon\n// (in --color-success) for `copiedDurationMs`, keeps an accessible label, and\n// announces completion politely. Tier A: .btn.btn-copy[data-copied].\nfunction CopyButton({\n  value,\n  label = \"Copy\",\n  copiedLabel = \"Copied\",\n  copiedDurationMs = 1800,\n  variant = \"ghost\",\n  size = \"icon-sm\",\n  type = \"button\",\n  className,\n  children,\n  onClick,\n  onCopySuccess,\n  \"aria-label\": ariaLabel,\n  ...props\n}: Omit<React.ComponentProps<typeof Button>, \"asChild\"> & {\n  value: string\n  label?: string\n  copiedLabel?: string\n  copiedDurationMs?: number\n  onCopySuccess?: (value: string) => void\n}) {\n  const [copiedValue, setCopiedValue] = React.useState<string | null>(null)\n  const mountedRef = React.useRef(true)\n  const copyIdRef = React.useRef(0)\n  const prevValueRef = React.useRef(value)\n  const timeoutRef = React.useRef<ReturnType<typeof setTimeout> | null>(null)\n  const copied = copiedValue !== null && copiedValue === value\n\n  React.useEffect(() => {\n    mountedRef.current = true\n    return () => {\n      mountedRef.current = false\n      copyIdRef.current += 1\n      if (timeoutRef.current !== null) {\n        clearTimeout(timeoutRef.current)\n        timeoutRef.current = null\n      }\n    }\n  }, [])\n\n  React.useEffect(() => {\n    if (prevValueRef.current === value) return\n    prevValueRef.current = value\n    copyIdRef.current += 1\n    if (timeoutRef.current !== null) {\n      clearTimeout(timeoutRef.current)\n      timeoutRef.current = setTimeout(() => {\n        if (!mountedRef.current) return\n        setCopiedValue(null)\n        timeoutRef.current = null\n      }, 0)\n    }\n  }, [value])\n\n  const resolvedAriaLabel =\n    ariaLabel ?? (children ? undefined : copied ? copiedLabel : label)\n\n  return (\n    <Button\n      type={type}\n      variant={variant}\n      size={size}\n      aria-label={resolvedAriaLabel}\n      className={cn(className)}\n      onClick={(event) => {\n        onClick?.(event)\n        if (event.defaultPrevented || !navigator.clipboard?.writeText) return\n        const targetValue = value\n        const copyId = ++copyIdRef.current\n        if (timeoutRef.current !== null) {\n          clearTimeout(timeoutRef.current)\n          timeoutRef.current = null\n        }\n        void (async () => {\n          try {\n            await navigator.clipboard.writeText(targetValue)\n            if (!mountedRef.current || copyId !== copyIdRef.current) return\n            setCopiedValue(targetValue)\n            onCopySuccess?.(targetValue)\n            timeoutRef.current = setTimeout(() => {\n              if (!mountedRef.current || copyId !== copyIdRef.current) return\n              setCopiedValue(null)\n              timeoutRef.current = null\n            }, copiedDurationMs)\n          } catch {\n            if (!mountedRef.current || copyId !== copyIdRef.current) return\n            /* clipboard write may fail in non-secure contexts */\n          }\n        })()\n      }}\n      {...props}\n      data-slot=\"copy-button\"\n      data-copied={copied ? \"true\" : undefined}\n    >\n      {copied ? (\n        <CheckIcon aria-hidden=\"true\" className=\"text-success\" />\n      ) : (\n        <CopyIcon aria-hidden=\"true\" />\n      )}\n      {children}\n      <span className=\"sr-only\" aria-live=\"polite\">\n        {copied ? copiedLabel : \"\"}\n      </span>\n    </Button>\n  )\n}\n\nexport { CopyButton }\n",
      "type": "registry:ui"
    }
  ],
  "type": "registry:ui"
}