{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"copy-button","type":"registry:ui","title":"Copy button","description":"Copies a value and confirms what was copied, with a fallback when the clipboard is blocked.","dependencies":["@radix-ui/react-slot","clsx","lucide-react","tailwind-merge"],"registryDependencies":["https://caliperui.site/r/styles.json"],"files":[{"path":"ui/button.tsx","type":"registry:ui","target":"components/ui/button.tsx","content":"\"use client\";\n\nimport { Slot, Slottable } from \"@radix-ui/react-slot\";\nimport { LoaderCircle } from \"lucide-react\";\nimport {\n  useLayoutEffect,\n  useRef,\n  useState,\n  type ComponentProps,\n  type ReactNode,\n} from \"react\";\nimport { cn } from \"./lib/cn\";\nimport \"./shared/loading-indicator.css\";\nimport \"./button.css\";\n\n/* Colour, material and press live in button.css keyed on data-variant, so a\n   consumer's utility classes still override them. Raised to press: the\n   button sinks 1px into an inner shade instead of scaling. */\nconst variants = {\n  default: \"\",\n  secondary: \"\",\n  ghost: \"\",\n  destructive: \"\",\n};\n\nconst sizes = {\n  sm: \"h-8 gap-1.5 rounded-full px-3 text-[13px] max-md:min-h-11 [@media(pointer:coarse)]:min-h-11\",\n  md: \"h-[var(--control-height)] gap-2 rounded-full px-3.5 text-sm\",\n  lg: \"h-11 gap-2 rounded-full px-[18px] text-[15px]\",\n  icon: \"size-[var(--control-height)] rounded-full p-0\",\n};\n\nexport type ButtonProps = ComponentProps<\"button\"> & {\n  variant?: keyof typeof variants;\n  size?: keyof typeof sizes;\n  asChild?: boolean;\n  /** Shows a spinner over the label and swallows clicks while keeping focus and width. */\n  loading?: boolean;\n  /** Animate a meaningful label/icon change, such as saving → saved. */\n  feedbackKey?: string;\n};\n\nexport function Button({\n  className,\n  variant = \"default\",\n  size = \"md\",\n  asChild = false,\n  loading = false,\n  feedbackKey,\n  disabled,\n  \"aria-disabled\": ariaDisabled,\n  children,\n  onClick,\n  onClickCapture,\n  tabIndex,\n  type = \"button\",\n  ...props\n}: ButtonProps) {\n  const Component = asChild ? Slot : \"button\";\n  const nativeDisabled = Boolean(disabled);\n  const blocked =\n    nativeDisabled ||\n    loading ||\n    ariaDisabled === true ||\n    ariaDisabled === \"true\";\n\n  return (\n    <Component\n      data-slot=\"button\"\n      data-variant={variant}\n      data-size={size}\n      type={asChild ? undefined : type}\n      className={cn(\n        \"relative inline-flex shrink-0 items-center justify-center whitespace-nowrap border border-transparent font-medium tracking-[-0.006em] outline-none focus-visible:outline-solid focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-ring disabled:pointer-events-none aria-disabled:pointer-events-none [&_svg]:size-4 [&_svg]:shrink-0\",\n        variants[variant],\n        sizes[size],\n        className,\n      )}\n      disabled={asChild ? undefined : nativeDisabled}\n      aria-disabled={nativeDisabled || ariaDisabled ? true : undefined}\n      aria-busy={loading || undefined}\n      tabIndex={asChild && nativeDisabled ? -1 : tabIndex}\n      {...props}\n      onKeyDown={(event) => {\n        if (blocked && (event.key === \"Enter\" || event.key === \" \")) {\n          event.preventDefault();\n        }\n        props.onKeyDown?.(event);\n      }}\n      onClickCapture={(event) => {\n        if (blocked) {\n          event.preventDefault();\n          event.stopPropagation();\n          return;\n        }\n        onClickCapture?.(event);\n      }}\n      onClick={onClick}\n    >\n      {loading && asChild && (\n        <LoaderCircle data-slot=\"loading-indicator\" aria-hidden=\"true\" />\n      )}\n      {loading && !asChild && feedbackKey === undefined && (\n        <span data-slot=\"button-busy\" aria-hidden=\"true\">\n          <LoaderCircle data-slot=\"loading-indicator\" />\n        </span>\n      )}\n      <Slottable>\n        {loading && !asChild && feedbackKey === undefined ? (\n          <span data-slot=\"button-label\" data-hidden=\"true\">\n            {children}\n          </span>\n        ) : feedbackKey !== undefined && !asChild ? (\n          <ButtonFeedback stateKey={feedbackKey}>\n            {loading && (\n              <LoaderCircle data-slot=\"loading-indicator\" aria-hidden=\"true\" />\n            )}\n            {children}\n          </ButtonFeedback>\n        ) : (\n          children\n        )}\n      </Slottable>\n    </Component>\n  );\n}\n\nfunction ButtonFeedback({\n  stateKey,\n  children,\n}: {\n  stateKey: string;\n  children: ReactNode;\n}) {\n  const content = useRef<HTMLSpanElement>(null);\n  const [initialKey] = useState(stateKey);\n  const [width, setWidth] = useState<number>();\n  useLayoutEffect(() => {\n    const element = content.current;\n    if (!element) return;\n    const measure = () => setWidth(element.offsetWidth);\n    measure();\n    const observer = new ResizeObserver(measure);\n    observer.observe(element);\n    return () => observer.disconnect();\n  }, [stateKey]);\n  return (\n    <span data-slot=\"button-feedback\" style={{ width }}>\n      <span\n        key={stateKey}\n        ref={content}\n        data-slot=\"button-feedback-label\"\n        data-enter={stateKey !== initialKey}\n      >\n        {children}\n      </span>\n    </span>\n  );\n}\n"},{"path":"ui/copy-button.tsx","type":"registry:ui","target":"components/ui/copy-button.tsx","content":"\"use client\";\n\nimport { Check, Copy } from \"lucide-react\";\nimport { useEffect, useRef, useState, type ReactNode } from \"react\";\nimport { Button, type ButtonProps } from \"./button\";\nimport { cn } from \"./lib/cn\";\nimport \"./shared/action-label.css\";\nimport \"./copy-button.css\";\n\n/** \"rv_live_8Kq2mZt7XcP1vW9yL3nB4f2a\" → \"rv_live_8K…4f2a\". */\nexport function shortenValue(value: string, head = 10, tail = 4) {\n  return value.length <= head + tail + 1 ? value : `${value.slice(0, head)}…${value.slice(-tail)}`;\n}\n\nasync function writeClipboard(value: string) {\n  try {\n    await navigator.clipboard.writeText(value);\n    return true;\n  } catch {\n    // Older or locked-down browsers: fall back to a selected textarea.\n    const area = document.createElement(\"textarea\");\n    area.value = value;\n    area.setAttribute(\"readonly\", \"\");\n    area.style.position = \"fixed\";\n    area.style.opacity = \"0\";\n    document.body.appendChild(area);\n    area.select();\n    let ok = false;\n    try {\n      ok = document.execCommand(\"copy\");\n    } catch {\n      ok = false;\n    }\n    area.remove();\n    return ok;\n  }\n}\n\n/**\n * Copies a value and confirms what was copied, shortened in mono, on a small\n * ticket above the button. If the browser blocks the clipboard, the ticket\n * offers the value selected so it can be copied by hand.\n */\nexport function CopyButton({\n  value,\n  label,\n  display,\n  iconOnly = false,\n  variant = \"secondary\",\n  size = \"sm\",\n  resetAfterMs = 1800,\n  onCopy,\n  className,\n  children,\n}: {\n  /** The text written to the clipboard. */\n  value: string;\n  /** Accessible name, e.g. \"Copy live secret key\". */\n  label: string;\n  /** What the ticket shows; defaults to the value shortened in the middle. */\n  display?: string;\n  iconOnly?: boolean;\n  variant?: ButtonProps[\"variant\"];\n  size?: Exclude<ButtonProps[\"size\"], \"icon\">;\n  resetAfterMs?: number;\n  onCopy?: (ok: boolean) => void;\n  className?: string;\n  /** Visible label; defaults to “Copy”. */\n  children?: ReactNode;\n}) {\n  const [state, setState] = useState<\"idle\" | \"copied\" | \"failed\">(\"idle\");\n  const fallback = useRef<HTMLInputElement>(null);\n\n  useEffect(() => {\n    if (state !== \"copied\") return;\n    const timer = setTimeout(() => setState(\"idle\"), resetAfterMs);\n    return () => clearTimeout(timer);\n  }, [state, resetAfterMs]);\n\n  useEffect(() => {\n    if (state === \"failed\") fallback.current?.select();\n  }, [state]);\n\n  async function copy() {\n    const ok = await writeClipboard(value);\n    setState(ok ? \"copied\" : \"failed\");\n    onCopy?.(ok);\n  }\n\n  const copied = state === \"copied\";\n  const iconClass = { sm: \"size-8 rounded-control max-md:size-11\", md: undefined, lg: \"size-11 rounded-touch\" }[size];\n\n  return (\n    <span\n      data-slot=\"copy-button\"\n      className={cn(\"relative inline-flex\", className)}\n      onKeyDown={(event) => {\n        if (event.key === \"Escape\" && state !== \"idle\") setState(\"idle\");\n      }}\n    >\n      <span data-slot=\"copy-ticket\" data-state={state} role={state === \"failed\" ? \"alert\" : \"status\"} aria-live={state === \"failed\" ? \"assertive\" : \"polite\"}>\n        {copied && (\n          <span data-slot=\"copy-ticket-body\">\n            <Check aria-hidden=\"true\" />\n            Copied <span className=\"font-mono text-[11px] opacity-75\">{display ?? shortenValue(value)}</span>\n          </span>\n        )}\n        {state === \"failed\" && (\n          <span data-slot=\"copy-ticket-body\" data-tone=\"error\">\n            <span>Couldn’t copy. The browser blocked the clipboard; press ⌘C or Ctrl+C.</span>\n            <input\n              ref={fallback}\n              readOnly\n              value={value}\n              aria-label={`${label}: value to copy by hand`}\n              onBlur={() => setState(\"idle\")}\n              className=\"w-full rounded-[6px] bg-background px-2 py-1 font-mono text-xs text-foreground outline-none\"\n            />\n          </span>\n        )}\n      </span>\n      <Button\n        variant={variant}\n        size={iconOnly ? \"icon\" : size}\n        aria-label={label}\n        data-copied={copied}\n        className={iconOnly ? iconClass : undefined}\n        onClick={copy}\n      >\n        <span data-slot=\"copy-icon\" data-copied={copied} aria-hidden=\"true\">\n          <Copy />\n          <Check />\n        </span>\n        {!iconOnly && (\n          <span data-slot=\"action-labels\">\n            <span data-slot=\"action-label\" data-active={!copied}>{children ?? \"Copy\"}</span>\n            <span data-slot=\"action-label\" data-active={copied}>Copied</span>\n          </span>\n        )}\n      </Button>\n    </span>\n  );\n}\n"},{"path":"ui/lib/cn.ts","type":"registry:ui","target":"components/ui/lib/cn.ts","content":"import { clsx, type ClassValue } from \"clsx\";\nimport { extendTailwindMerge } from \"tailwind-merge\";\n\nconst twMerge = extendTailwindMerge({\n  extend: { theme: { radius: [\"control\", \"surface\", \"overlay\"], shadow: [\"overlay\"] } },\n});\n\nexport function cn(...inputs: ClassValue[]) {\n  return twMerge(clsx(inputs));\n}\n"},{"path":"ui/shared/loading-indicator.css","type":"registry:file","target":"components/ui/shared/loading-indicator.css","content":"/* Shared by button.tsx, confirm-morph.tsx, expanding-button-group.tsx, field.tsx; each imports it. Edit freely. */\n@layer theme, base, components, utilities;\n\n@layer components {\n  [data-slot=\"loading-indicator\"] {\n    animation: caliper-spin var(--duration-loading) var(--ease-linear) infinite;\n  }\n\n  @media (prefers-reduced-motion: reduce) {\n    [data-slot=\"loading-indicator\"] {\n      animation: none;\n    }\n  }\n}\n"},{"path":"ui/button.css","type":"registry:file","target":"components/ui/button.css","content":"/* button.tsx's styles; it imports this file. Edit freely. */\n@layer theme, base, components, utilities;\n\n@layer components {\n  [data-slot=\"button\"] {\n    transition-property: background-color, color, border-color;\n    transition-duration: var(--duration-fast);\n    transition-timing-function: var(--ease-out);\n  }\n\n  /* Button material. Raised to press: a lit top edge and a small lift; the\n     press travels 1px down and swaps the lift for an inner shade. Only\n     translate animates (release on the settle curve); shadows swap. */\n  [data-slot=\"button\"] {\n    --b-bg: var(--raised);\n    --b-bg-hover: var(--raised-hover);\n    --b-bg-press: var(--raised-press);\n    --b-fg: var(--foreground);\n    --b-shadow: var(--shadow-raised);\n    --b-shadow-press: var(--shadow-raised-press);\n    background-color: var(--b-bg);\n    color: var(--b-fg);\n    box-shadow: var(--b-shadow);\n    transition-property: background-color, color, border-color, translate, scale;\n    transition-duration:\n      var(--duration-fast), var(--duration-fast), var(--duration-fast),\n      var(--duration-normal), var(--duration-normal);\n    transition-timing-function:\n      var(--ease-out), var(--ease-out), var(--ease-out), var(--ease-settle),\n      var(--ease-settle);\n  }\n  [data-slot=\"button\"][data-variant=\"default\"] {\n    --b-bg: var(--primary);\n    --b-bg-hover: var(--primary-hover);\n    --b-bg-press: var(--primary-press);\n    --b-fg: var(--primary-foreground);\n    --b-shadow: var(--shadow-accent);\n    --b-shadow-press: var(--shadow-accent-press);\n  }\n  [data-slot=\"button\"][data-variant=\"destructive\"] {\n    --b-bg: var(--danger-fill);\n    --b-bg-hover: var(--danger-fill-hover);\n    --b-bg-press: var(--danger-fill-press);\n    --b-fg: var(--danger-fill-foreground);\n    --b-shadow: var(--shadow-danger);\n    --b-shadow-press: var(--shadow-danger-press);\n  }\n  [data-slot=\"button\"][data-variant=\"ghost\"] {\n    --b-bg: transparent;\n    --b-bg-hover: var(--muted);\n    --b-bg-press: var(--muted);\n    --b-fg: var(--muted-foreground);\n    --b-shadow: none;\n    --b-shadow-press: none;\n  }\n  @media (hover: hover) {\n    [data-slot=\"button\"]:hover {\n      background-color: var(--b-bg-hover);\n    }\n    [data-slot=\"button\"][data-variant=\"ghost\"]:hover {\n      color: var(--foreground);\n    }\n  }\n  [data-slot=\"button\"]:active:not(:disabled):not([aria-disabled=\"true\"]):not(\n      [aria-busy=\"true\"]\n    ) {\n    translate: 0 var(--motion-press-travel);\n    scale: var(--motion-press-scale);\n    background-color: var(--b-bg-press);\n    box-shadow: var(--b-shadow-press);\n    transition-duration:\n      var(--duration-fast), var(--duration-fast), var(--duration-fast), 90ms,\n      90ms;\n    transition-timing-function: var(--ease-out);\n  }\n  [data-slot=\"button\"]:is(:disabled, [aria-disabled=\"true\"]):not([aria-busy=\"true\"]) {\n    background-color: var(--sunken);\n    color: var(--muted-foreground);\n  }\n  [data-slot=\"button\"][data-variant=\"ghost\"]:is(:disabled, [aria-disabled=\"true\"]):not(\n      [aria-busy=\"true\"]\n    ) {\n    background-color: transparent;\n    box-shadow: none;\n  }\n  /* Loading keeps the exact width: the label stays in flow but invisible. */\n  [data-slot=\"button-label\"] {\n    display: inline-flex;\n    align-items: center;\n    gap: inherit;\n  }\n  /* Opacity, not visibility, so the label still names the button. */\n  [data-slot=\"button-label\"][data-hidden=\"true\"] {\n    opacity: 0;\n  }\n  [data-slot=\"button-busy\"] {\n    position: absolute;\n    inset: 0;\n    display: grid;\n    place-items: center;\n  }\n  [data-slot=\"button-feedback\"] {\n    display: inline-flex;\n    align-items: center;\n    gap: inherit;\n    overflow: clip;\n    transition: width var(--duration-normal) var(--ease-out);\n  }\n  [data-slot=\"button-feedback-label\"] {\n    display: inline-flex;\n    flex-shrink: 0;\n    align-items: center;\n    gap: inherit;\n    width: max-content;\n  }\n  [data-slot=\"button-feedback-label\"][data-enter=\"true\"] {\n    animation: caliper-feedback-enter var(--duration-normal) var(--ease-out);\n  }\n\n  [data-slot=\"button\"][aria-busy=\"true\"] {\n    pointer-events: none;\n  }\n  [data-slot=\"button\"][data-action-phase=\"error\"] {\n    color: var(--danger);\n  }\n\n  @media (prefers-reduced-motion: reduce) {\n    [data-slot=\"button\"],\n    [data-slot=\"button-feedback\"] {\n      transition: none;\n    }\n    [data-slot=\"button\"]:active:not(:disabled):not([aria-disabled=\"true\"]):not(\n        [aria-busy=\"true\"]\n      ) {\n      translate: none;\n    }\n    [data-slot=\"button-feedback-label\"],\n    [data-slot=\"button-feedback-label\"][data-enter=\"true\"] {\n      animation: none;\n    }\n  }\n\n  /* Toggle: pressed is recessed, as a toggled tool in a button group is. */\n  /* Four attributes, so it wins over the group and toolbar pressed rules in any stylesheet order. */\n  [data-slot=\"button\"][data-variant=\"ghost\"][data-toggle][aria-pressed=\"true\"] {\n    --b-bg: var(--raised-press);\n    --b-bg-hover: var(--raised-press);\n    --b-bg-press: var(--raised-press);\n    --b-fg: var(--foreground);\n    --b-shadow: var(--shadow-sunken);\n    --b-shadow-press: var(--shadow-sunken);\n  }\n}\n"},{"path":"ui/shared/action-label.css","type":"registry:file","target":"components/ui/shared/action-label.css","content":"/* Shared by action-button.tsx, copy-button.tsx, hold-to-confirm.tsx; each imports it. Edit freely. */\n@layer theme, base, components, utilities;\n\n@layer components {\n  /* Action button: every phase's label shares one grid cell, so the button\n     is always as wide as its widest label and never shifts. */\n  [data-slot=\"action-labels\"] {\n    display: inline-grid;\n    justify-items: center;\n  }\n  [data-slot=\"action-label\"] {\n    grid-area: 1 / 1;\n    display: inline-flex;\n    align-items: center;\n    gap: 8px;\n    visibility: hidden;\n    opacity: 0;\n    transition: opacity var(--duration-fast) var(--ease-out), visibility 0s linear var(--duration-fast);\n  }\n  [data-slot=\"action-label\"][data-active=\"true\"] {\n    visibility: visible;\n    opacity: 1;\n    transition: opacity var(--duration-fast) var(--ease-out), visibility 0s;\n  }\n\n  @media (prefers-reduced-motion: reduce) {\n    [data-slot=\"action-label\"],\n    [data-slot=\"action-label\"][data-active=\"true\"] {\n      transition: none !important;\n    }\n  }\n}\n"},{"path":"ui/copy-button.css","type":"registry:file","target":"components/ui/copy-button.css","content":"/* copy-button.tsx's styles; it imports this file. Edit freely. */\n@layer theme, base, components, utilities;\n\n@layer components {\n  /* Copy button: the icon swaps in place; a small inverse ticket says what\n     was copied. A failure keeps the ticket open with the value selected. */\n  [data-slot=\"copy-icon\"] {\n    position: relative;\n    display: inline-grid;\n    width: 16px;\n    height: 16px;\n  }\n  [data-slot=\"copy-icon\"] svg {\n    grid-area: 1 / 1;\n    transition:\n      opacity var(--duration-fast) var(--ease-out),\n      scale var(--duration-normal) var(--ease-settle);\n  }\n  [data-slot=\"copy-icon\"] svg:last-child,\n  [data-slot=\"copy-icon\"][data-copied=\"true\"] svg:first-child {\n    opacity: 0;\n    scale: 0.6;\n  }\n  [data-slot=\"copy-icon\"][data-copied=\"true\"] svg:last-child {\n    opacity: 1;\n    scale: 1;\n  }\n  [data-slot=\"copy-ticket\"] {\n    position: absolute;\n    right: 0;\n    bottom: calc(100% + 8px);\n    z-index: 30;\n    pointer-events: none;\n  }\n  [data-slot=\"copy-ticket-body\"] {\n    display: inline-flex;\n    align-items: center;\n    gap: 8px;\n    padding: 6px 10px;\n    border-radius: 8px;\n    background: var(--primary);\n    color: var(--primary-foreground);\n    font-size: 12px;\n    line-height: 16px;\n    font-weight: 500;\n    white-space: nowrap;\n    box-shadow: var(--shadow-feedback);\n    animation: caliper-ticket-in var(--duration-normal) var(--ease-out);\n  }\n  [data-slot=\"copy-ticket-body\"] svg {\n    width: 14px;\n    height: 14px;\n  }\n  [data-slot=\"copy-ticket-body\"][data-tone=\"error\"] {\n    display: grid;\n    width: min(320px, calc(100vw - 32px));\n    gap: 8px;\n    padding: 10px;\n    white-space: normal;\n    background: var(--elevated);\n    color: var(--danger);\n    box-shadow: var(--shadow-overlay), inset 0 0 0 1px var(--border);\n    pointer-events: auto;\n  }\n\n  @media (prefers-reduced-motion: reduce) {\n    [data-slot=\"copy-icon\"] svg {\n      transition: none;\n    }\n    [data-slot=\"copy-ticket-body\"] {\n      animation: none;\n    }\n  }\n  @keyframes caliper-ticket-in {\n    from {\n      opacity: 0;\n      translate: 0 4px;\n    }\n  }\n}\n"}],"categories":["actions"],"meta":{"tier":"free","license":"MIT","family":"actions","docs":"/docs/components/copy-button"}}