{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "copy-button",
  "title": "CopyButton",
  "description": "A button that puts a piece of text on the clipboard: an API key, a link, a command.",
  "registryDependencies": [
    "@ouro/button",
    "@ouro/lib-announce",
    "@ouro/lib-class-name-check",
    "@ouro/lib-cn",
    "@ouro/lib-env",
    "@ouro/lib-use-dev-messages"
  ],
  "files": [
    {
      "path": "registry/ouro/components/ouro/copy-button/copy-button.tsx",
      "content": "\"use client\";\n\nimport { useEffect, useRef, useState, type ButtonHTMLAttributes, type JSX, type MouseEvent, type Ref, type SVGProps } from \"react\";\nimport { announce } from \"@/registry/ouro/lib/ouro/announce\";\nimport { useAppearanceClassNameCheck } from \"@/registry/ouro/lib/ouro/class-name-check\";\nimport { cn } from \"@/registry/ouro/lib/ouro/cn\";\nimport { useDevWarnings } from \"@/registry/ouro/lib/ouro/use-dev-messages\";\nimport { Button } from \"@/registry/ouro/components/ouro/button/button\";\n\n/** The props CopyButton adds to the `<button>` attributes it passes through. */\nexport interface CopyButtonOwnProps {\n  /** The text the button puts on the clipboard. */\n  value: string;\n  /** The button's label, and its name for screen readers when `hideLabel` is set. @default \"Copy\" */\n  label?: string | undefined;\n  /** What the button says, and screen readers hear, once the text is on the clipboard. @default \"Copied\" */\n  copiedLabel?: string | undefined;\n  /**\n   * What the button says for two seconds when the browser refused to copy: short, so the button keeps its width. @default \"Browser blocked copying\"\n   * @claim \"What the button says for two seconds\" registry/ouro/components/ouro/copy-button/copy-button.test.tsx verify:testExists(registry/ouro/components/ouro/copy-button/copy-button.test.tsx::CopyButton > by default shows Browser blocked copying, with how to copy by hand as its tooltip, and announces the whole sentence once) verify:testExists(registry/ouro/components/ouro/copy-button/copy-button.test.tsx::CopyButton > keeps a 390px-wide page from scrolling sideways when it says the browser blocked copying, at every size)\n   */\n  failedLabel?: string | undefined;\n  /**\n   * What screen readers hear, and the button's tooltip says while it shows `failedLabel`, when the\n   * browser refused to copy: what happened and what to do instead.\n   * @default \"The browser blocked copying. Select the text and copy it by hand.\", or `failedLabel` when only that is given\n   */\n  failedMessage?: string | undefined;\n  /** Shows only the icon; the label stays the button's name for screen readers. For a corner of a code block or a field. */\n  hideLabel?: boolean | undefined;\n  /** Visual weight, as Button's. @default \"outline\" */\n  variant?: \"solid\" | \"soft\" | \"outline\" | \"plain\" | (string & { 'Use \"solid\", \"soft\", \"outline\" or \"plain\"; ghost is \"plain\"': never }) | undefined;\n  /** Control size, as Button's. @default \"sm\" */\n  size?: \"sm\" | \"md\" | \"lg\" | (string & { 'Use \"sm\", \"md\" or \"lg\"': never }) | undefined;\n  /** Called with the text once it is on the clipboard. */\n  onCopy?: ((value: string) => void) | undefined;\n  /** Layout only: margins, grid placement (`ms-auto`). Appearance classes warn in development. */\n  className?: string | undefined;\n  ref?: Ref<HTMLButtonElement> | undefined;\n\n  /** Not a prop. The text to copy is `value`: `<CopyButton value={token} />`. */\n  text?: { 'Use <CopyButton value={text} />': never };\n  /** Not a prop. The text to copy is `value`: `<CopyButton value={token} />`. */\n  content?: { 'Use <CopyButton value={text} />': never };\n  /** Not a prop. The label is `label`: `<CopyButton value={url} label=\"Copy link\" />`. */\n  children?: { 'Use <CopyButton value={text} label=\"Copy link\" />': never };\n  /**\n   * Not a prop. The confirmation is `copiedLabel`, and it lasts two seconds.\n   *\n   * @claim \"The confirmation is `copiedLabel`, and it\" registry/ouro/components/ouro/copy-button/copy-button.test.tsx verify:testExists(registry/ouro/components/ouro/copy-button/copy-button.test.tsx::CopyButton > puts the value on the clipboard, says Copied once, calls onCopy and reads Copy again after two seconds)\n   */\n  timeout?: { 'Use copiedLabel=\"Copied\"; it shows for two seconds': never };\n  /** Not a prop. CopyButton copies on click itself; to react to a copy, pass `onCopy`. */\n  onClick?: { 'Use onCopy={(value) => ...}; CopyButton copies on click itself': never };\n}\n\n/** Props for {@link CopyButton}: its own props and the `<button>` attributes it passes through. */\nexport type CopyButtonProps = Omit<ButtonHTMLAttributes<HTMLButtonElement>, keyof CopyButtonOwnProps | \"type\" | \"color\"> & CopyButtonOwnProps;\n\ntype Status = \"idle\" | \"copied\" | \"failed\";\n\nconst variants = [\"solid\", \"soft\", \"outline\", \"plain\"] as const;\nconst sizes = [\"sm\", \"md\", \"lg\"] as const;\n/** How long the button says it copied before it reads its label again. */\nconst CONFIRM_MS = 2000;\n\nfunction CopyIcon(props: SVGProps<SVGSVGElement>): JSX.Element {\n  return (\n    <svg aria-hidden=\"true\" viewBox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" strokeWidth=\"2\" strokeLinecap=\"round\" strokeLinejoin=\"round\" {...props}>\n      <rect width=\"14\" height=\"14\" x=\"8\" y=\"8\" rx=\"2\" ry=\"2\" />\n      <path d=\"M4 16c-1.1 0-2-.9-2-2V4c0-1.1.9-2 2-2h10c1.1 0 2 .9 2 2\" />\n    </svg>\n  );\n}\n\nfunction CheckIcon(props: SVGProps<SVGSVGElement>): JSX.Element {\n  return (\n    <svg aria-hidden=\"true\" viewBox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" strokeWidth=\"2\" strokeLinecap=\"round\" strokeLinejoin=\"round\" {...props}>\n      <path d=\"M20 6 9 17l-5-5\" />\n    </svg>\n  );\n}\n\nfunction FailedIcon(props: SVGProps<SVGSVGElement>): JSX.Element {\n  return (\n    <svg aria-hidden=\"true\" viewBox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" strokeWidth=\"2\" strokeLinecap=\"round\" strokeLinejoin=\"round\" {...props}>\n      <path d=\"M18 6 6 18M6 6l12 12\" />\n    </svg>\n  );\n}\n\n/** Copies through a hidden text area, for pages the Clipboard API is not open to (plain http, an old browser). */\nfunction copyWithSelection(text: string): boolean {\n  const area = document.createElement(\"textarea\");\n  area.value = text;\n  area.setAttribute(\"readonly\", \"\");\n  Object.assign(area.style, { position: \"fixed\", top: \"0\", left: \"0\", opacity: \"0\", pointerEvents: \"none\" });\n  const focused = document.activeElement as HTMLElement;\n  document.body.append(area);\n  area.select();\n  let copied: boolean;\n  try {\n    copied = document.execCommand(\"copy\");\n  } catch {\n    copied = false;\n  }\n  area.remove();\n  focused.focus({ preventScroll: true });\n  return copied;\n}\n\nasync function writeClipboard(text: string): Promise<boolean> {\n  if (navigator.clipboard?.writeText) {\n    try {\n      await navigator.clipboard.writeText(text);\n      return true;\n    } catch {\n      // A browser that refuses (no permission, the page not focused) may still copy a selection.\n    }\n  }\n  return copyWithSelection(text);\n}\n\n/**\n * A button that puts a piece of text on the clipboard: an API key, a link, a command. On a\n * click it copies `value`, then for two seconds shows a check and says \"Copied\", and screen\n * readers hear the same, politely, once. When the browser refuses, it shows a cross and\n * \"Browser blocked copying\" for two seconds, with the whole of `failedMessage` (\"The browser\n * blocked copying. Select the text and copy it by hand.\") as its tooltip, and screen readers\n * hear that whole sentence once. It uses the Clipboard API, and a text selection where that is\n * not available.\n *\n * Use `CodeBlock` instead for code or a command shown with its own copy button.\n *\n * @group Actions\n * @see https://ui.ouroboros.bot/docs/copy-button\n *\n * @example\n * <CopyButton value={apiKey} label=\"Copy key\" />\n * <CopyButton value={url} label=\"Copy link\" hideLabel variant=\"plain\" />\n * @claim \"On a click it copies `value`\" registry/ouro/components/ouro/copy-button/copy-button.test.tsx verify:testExists(registry/ouro/components/ouro/copy-button/copy-button.test.tsx::CopyButton > puts the value on the clipboard, says Copied once, calls onCopy and reads Copy again after two seconds)\n * @claim \"On a click it copies `value`\" registry/ouro/components/ouro/copy-button/copy-button.test.tsx verify:fileLine(registry/ouro/components/ouro/copy-button/copy-button.test.tsx, /says\\sthat\\sthe\\sbrowser\\sblocked\\scopying,\\sonce,\\swhen\\sthe\\sbrowser\\srefuses/)\n * @claim \"When the browser refuses, it shows\" registry/ouro/components/ouro/copy-button/copy-button.test.tsx verify:testExists(registry/ouro/components/ouro/copy-button/copy-button.test.tsx::CopyButton > by default shows Browser blocked copying, with how to copy by hand as its tooltip, and announces the whole sentence once)\n */\nexport function CopyButton(props: CopyButtonProps): JSX.Element {\n  const {\n    value,\n    label = \"Copy\",\n    copiedLabel = \"Copied\",\n    failedLabel = \"Browser blocked copying\",\n    failedMessage = props.failedLabel ?? \"The browser blocked copying. Select the text and copy it by hand.\",\n    hideLabel,\n    variant: variantProp = \"outline\",\n    size: sizeProp = \"sm\",\n    onCopy,\n    className,\n    ref,\n    disabled,\n    // Guard props (hint types), dropped here so they never reach the DOM.\n    text,\n    content,\n    children,\n    timeout,\n    onClick,\n    ...rest\n  } = props;\n  const variant = (variants as readonly string[]).includes(variantProp) ? (variantProp as (typeof variants)[number]) : \"outline\";\n  const size = (sizes as readonly string[]).includes(sizeProp) ? (sizeProp as (typeof sizes)[number]) : \"sm\";\n  useAppearanceClassNameCheck(\n    \"CopyButton\",\n    className,\n    process.env.NODE_ENV !== \"production\"\n      ? (token: string) => `remove \"${token}\"; use variant=\"plain\" or size=\"md\"`\n      : /* istanbul ignore next @preserve -- production only */ \"\",\n  );\n  useDevWarnings(\n    \"CopyButton\",\n    process.env.NODE_ENV !== \"production\"\n      ? () => [\n          variant !== variantProp && [`variant=\"${String(variantProp)}\" is not a variant; use \"solid\", \"soft\", \"outline\" or \"plain\"`, '<CopyButton value={text} variant=\"plain\" />'],\n          size !== sizeProp && [`size=\"${String(sizeProp)}\" is not a size; use \"sm\", \"md\" or \"lg\"`, '<CopyButton value={text} size=\"sm\" />'],\n          text !== undefined && [\"text is not a prop\", \"<CopyButton value={text} />\"],\n          content !== undefined && [\"content is not a prop\", \"<CopyButton value={text} />\"],\n          children !== undefined && [\"CopyButton takes no children; its label is label\", `<CopyButton value={text} label=\"${typeof children === \"string\" ? children : \"Copy\"}\" />`],\n          timeout !== undefined && [\"timeout is not a prop; the confirmation shows for two seconds\", '<CopyButton value={text} copiedLabel=\"Copied\" />'],\n          onClick !== undefined && [\"onClick is not a prop; CopyButton copies on click itself\", \"<CopyButton value={text} onCopy={(value) => {}} />\"],\n        ]\n      : /* istanbul ignore next @preserve -- production only */ undefined,\n  );\n  const [status, setStatus] = useState<Status>(\"idle\");\n  const timer = useRef<ReturnType<typeof setTimeout> | undefined>(undefined);\n  const mounted = useRef(false);\n  useEffect(() => {\n    mounted.current = true;\n    return () => {\n      mounted.current = false;\n      clearTimeout(timer.current);\n    };\n  }, []);\n\n  const copy = async (event: MouseEvent<HTMLButtonElement>) => {\n    event.preventDefault();\n    const copied = await writeClipboard(value);\n    if (copied) onCopy?.(value);\n    // Gone before the clipboard answered: nothing is left to show or say it.\n    if (!mounted.current) return;\n    clearTimeout(timer.current);\n    setStatus(copied ? \"copied\" : \"failed\");\n    announce(copied ? copiedLabel : failedMessage);\n    timer.current = setTimeout(() => setStatus(\"idle\"), CONFIRM_MS);\n  };\n\n  const shown = status === \"copied\" ? copiedLabel : status === \"failed\" ? failedLabel : label;\n  const Icon = status === \"copied\" ? CheckIcon : status === \"failed\" ? FailedIcon : CopyIcon;\n  return (\n    <span data-ouro=\"copy-button\" data-status={status} className={cn(\"inline-flex\", className)}>\n      <Button\n        data-ouro-control=\"copy-button\"\n        data-ouro-part=\"trigger\"\n        {...rest}\n        ref={ref}\n        disabled={disabled}\n        variant={variant}\n        size={size}\n        tone=\"secondary\"\n        onClick={copy}\n        {...(status === \"failed\" ? { title: failedMessage } : {})}\n        {...(hideLabel ? { \"aria-label\": shown, className: \"w-(--ouro-button-h) px-0\" } : { startIcon: Icon })}\n      >\n        {hideLabel ? <Icon className=\"size-[1.25em] shrink-0\" /> : shown}\n      </Button>\n    </span>\n  );\n}\n",
      "type": "registry:ui",
      "target": "@components/ouro/copy-button/copy-button.tsx"
    }
  ],
  "docs": "A button that puts a piece of text on the clipboard: an API key, a link, a command. Import: import { CopyButton } from \"@/components/ouro/copy-button/copy-button\", with @/components replaced by your components alias (aliases.components in components.json, such as ~/components). Also installed: button, the helpers they import in lib/ouro. Needs ouro-base in your stylesheet: add that item once per app, from the same place as this one, if you have not. Docs: https://ui.ouroboros.bot/docs/copy-button.md Ouro UI v1.3.257.",
  "type": "registry:ui"
}