← ui

copy

ui

copy to the clipboard, confirmed in place, never with a toast

npx shadcn add @ag/copy
Already installed? Preview an update
npx shadcn add @ag/copy --dry-run

Your installed source stays unchanged until you update it. How updates work

Latest changes · 2026-10-26
  • The mark and the mark with its word are a ghost Button, at a button's heights and corners beside any other control.
  • New: CopyField, a value in the field every input is, read-only, the copy on its front. veiled draws it as Veiled does, with an eye to peek it beside the copy: <CopyField value={key} veiled label="license key" />. copy now depends on button, input, material and veiled.
Updated 2026-10-26 · release notes

also called copy to clipboard

shelf text & code

uses tooltip, button, input, material, veiled

used by code, data-view, deployment, comment-thread, email-address, browser, lightbox

beside a command

npx shadcn add @ag/copy

The field every input is, read-only: the copy on its front, the strip at its end.

Dots stand for it, as Veiled draws one: the eye peeks it for five seconds, the copy takes it whole without showing it.

in glass

A well of glass, the copy on its front: the strip at its end, part of the glass.

note.txt

a little context goes a long way

order has shipped

MiddleTruncate as the face keeps the end you check; tip shows the whole address, which is what's copied.

  • savings
  • trading
  • cold

Each row changes only who speaks on the hover. A tip silences the cut text's own.

no tip
tip: the address
tip: words
the mark alone
0x71C7656EC7ab88b098defB751B7401B5f6d8976F

Reads the current value when pressed. An empty field leaves your clipboard intact.

the demo above, verbatim

copy.demo.tsx
import { Sample } from "@/app/samples"
import { Code } from "@/registry/base-nova/ui/code"
import { Copy, CopyField } from "@/registry/base-nova/ui/copy"
import { CopyLiveExample } from "@/registry/base-nova/ui/copy-live.demo"
import { MaterialProvider } from "@/registry/base-nova/ui/material"
import { MiddleTruncate } from "@/registry/base-nova/ui/middle-truncate"

// #region keys
const installId = "3f9c2a1e-7b44-4d0a-9e61-0c5d8f2b7a13"
const licenseKey =
  "eyJ2IjoxLCJwcm9qZWN0IjoiaG9sbG93IiwiZXJhIjoidjEiLCJlbWFpbCI6InlvdUBleGFtcGxlLmNvbSJ9.fen3zBQtpQLdNdt6mI2CzkibI-nJ_hVjpGl5obdo65SRKQz9WCVoBBNZ_Cozb6paNjz5zMVz54ycu8V8Xdu9Ag"
// #endregion

// #region wallets
const savings = "0x71C7656EC7ab88b098defB751B7401B5f6d8976F"
const wallets = [
  { name: "savings", address: savings },
  { name: "trading", address: "0x71C7656EC7ab88b098defB751B7401B5f6d8A21c" },
  { name: "cold", address: "bc1qxy2kgdygjrsqtzq2n0yrf2493p83kkfjhx0wlh" },
]
// #endregion

export default function Demo() {
  return (
    <div className="space-y-8">
      <Sample name="command" title="beside a command">
        <Code compact lang="sh" className="max-w-xl">
          npx shadcn add @ag/copy
        </Code>
      </Sample>
      <Sample
        name="field"
        title="a value in its own field"
        description="The field every input is, read-only: the copy on its front, the strip at its end."
        with="keys"
      >
        <div className="w-full max-w-md">
          <CopyField value={installId} label="install id" />
        </div>
      </Sample>
      <Sample
        name="veiled"
        title="a key never on screen"
        description="Dots stand for it, as Veiled draws one: the eye peeks it for five seconds, the copy takes it whole without showing it."
        with="keys"
      >
        <div className="w-full max-w-md">
          <CopyField
            value={licenseKey}
            veiled
            label="license key"
            tip="copy without showing it"
          />
        </div>
      </Sample>
      <Sample
        name="field-glass"
        title="in glass"
        description="A well of glass, the copy on its front: the strip at its end, part of the glass."
        with="keys"
      >
        <MaterialProvider material="glass">
          <div className="w-full max-w-lg space-y-3 rounded-xl bg-[url(/lightbox/lake.jpg)] bg-cover bg-center p-6">
            <CopyField value={installId} label="install id" />
            <CopyField value={licenseKey} veiled label="license key" />
          </div>
        </MaterialProvider>
      </Sample>
      <Sample name="label" glance title="in a panel header">
        <div className="max-w-xl overflow-hidden rounded-xl border border-foreground/10">
          <div className="flex items-center justify-between gap-4 border-b border-foreground/10 bg-fill-band py-2 pl-4 pr-2">
            <span className="font-mono text-xs text-muted-foreground">
              note.txt
            </span>
            <Copy value="a little context goes a long way" label />
          </div>
          <p className="p-4 text-sm leading-relaxed text-foreground/80">
            a little context goes a long way
          </p>
        </div>
      </Sample>
      <Sample name="text" title="the text is the button">
        <p className="text-sm text-foreground/80">
          order{" "}
          <Copy value="A7K-29Q4" className="font-mono">
            A7K-29Q4
          </Copy>{" "}
          has shipped
        </p>
      </Sample>
      <Sample
        name="addresses"
        title="addresses too long for their column"
        description="MiddleTruncate as the face keeps the end you check; tip shows the whole address, which is what's copied."
        with="wallets"
      >
        <ul className="w-full max-w-56 space-y-1 font-mono text-xs">
          {wallets.map((w) => (
            <li key={w.address} className="flex items-baseline gap-3">
              <span className="w-14 shrink-0 font-sans text-muted-foreground">
                {w.name}
              </span>
              <Copy value={w.address} tip={w.address}>
                <MiddleTruncate text={w.address} tail={6} />
              </Copy>
            </li>
          ))}
        </ul>
      </Sample>
      <Sample
        name="tooltip"
        title="one hover, one tooltip"
        description="Each row changes only who speaks on the hover. A tip silences the cut text's own."
        with="wallets"
      >
        <dl className="grid w-full max-w-md grid-cols-[7rem_minmax(0,14rem)] items-baseline gap-x-4 gap-y-2 text-xs">
          <dt className="text-muted-foreground">no tip</dt>
          <dd className="flex font-mono">
            <Copy value={savings}>
              <MiddleTruncate text={savings} tail={6} />
            </Copy>
          </dd>
          <dt className="text-muted-foreground">tip: the address</dt>
          <dd className="flex font-mono">
            <Copy value={savings} tip={savings}>
              <MiddleTruncate text={savings} tail={6} />
            </Copy>
          </dd>
          <dt className="text-muted-foreground">tip: words</dt>
          <dd className="flex font-mono">
            <Copy value={savings} tip="copy the savings address">
              <MiddleTruncate text={savings} tail={6} />
            </Copy>
          </dd>
          <dt className="text-muted-foreground">the mark alone</dt>
          <dd className="flex items-center gap-1 font-mono">
            <MiddleTruncate text={savings} tail={6} />
            <Copy value={savings} tip="copy" />
          </dd>
        </dl>
      </Sample>
      <Sample name="live" title="copy the current value">
        <CopyLiveExample />
      </Sample>
    </div>
  )
}

the source, every file an install writes

copy.tsx
"use client"

// Copy to the clipboard, with the only feedback that matters: the control says it
// worked, in place, and goes back on its own. No toast, nothing to dismiss.

import { Check, CircleAlert, Clipboard, Eye, EyeOff } from "lucide-react"
import * as React from "react"
import { cn } from "@/lib/utils"
import { Button } from "@/registry/base-nova/ui/button"
import {
  frontButton,
  InputGroup,
  type InputSize,
} from "@/registry/base-nova/ui/input"
import type { Material } from "@/registry/base-nova/ui/material"
import { Tip } from "@/registry/base-nova/ui/tooltip"
import { usePeek, veil } from "@/registry/base-nova/ui/veiled"

/** How long the control admits it copied. Long enough to read at a glance, short
 *  enough that a second copy is never blocked by the first one's applause. */
export const COPIED_MS = 1400

/** `copied` flips true on a successful write and clears itself. The timer is cleared
 *  on unmount and on every new copy, so a fast second press restarts it rather than
 *  being cut short by the first one's expiry. */
export function useCopy(text: string | (() => string)) {
  const [copied, setCopied] = React.useState(false)
  const [error, setError] = React.useState<unknown>(null)
  const timer = React.useRef(0)
  React.useEffect(() => () => window.clearTimeout(timer.current), [])
  const copy = React.useCallback(async () => {
    const value = typeof text === "function" ? text() : text
    // Empty is not a copy: writing "" silently wipes what the reader already had.
    window.clearTimeout(timer.current)
    setCopied(false)
    setError(null)
    if (!value) return false
    try {
      await navigator.clipboard.writeText(value)
      setCopied(true)
      timer.current = window.setTimeout(() => setCopied(false), COPIED_MS)
      return true
    } catch (cause) {
      setError(cause)
      return false
    }
  }, [text])
  return { copied, copy, error }
}

export interface CopyProps
  extends Omit<React.ComponentProps<"button">, "value"> {
  /** The text to write. A function is read at press time, for a live value. */
  value: string | (() => string)
  /** Show the word beside the mark. Off in tight corners, where the mark is enough. */
  label?: boolean
  /** The text itself is the button (an address, a hash, an order number in a row):
   *  shown as given, the mark beside it under the hand and while it confirms, so a
   *  column of values reads as values and not as a column of buttons. It shrinks with
   *  its row, so a face that truncates (`MiddleTruncate`) still does. */
  children?: React.ReactNode
  /** Its tooltip (`Tip`), and the only one on its hover: a `MiddleTruncate` inside
   *  stops showing its own. `tip={address}` shows the whole value even when it fits;
   *  without one, a cut `MiddleTruncate` shows the whole only when cut. */
  tip?: React.ReactNode
}

const UNAVAILABLE =
  "Clipboard unavailable. Try again or select the text to copy."

/** One press, three faces: the mark, the mark and its word (`label`), or your text
 *  with the mark beside it (`children`). */
export function Copy({
  value,
  label = false,
  children,
  tip,
  className,
  onClick,
  ...props
}: CopyProps) {
  const { copied, copy, error } = useCopy(value)
  const text = children !== undefined
  if (text && label)
    throw new Error("Copy: text is its own label, pass children or label")
  const state = error ? "error" : copied ? "copied" : "idle"
  const press = (event: React.MouseEvent<HTMLButtonElement>) => {
    onClick?.(event)
    if (!event.defaultPrevented) void copy()
  }
  const mark = error ? <CircleAlert /> : copied ? <Check /> : <Clipboard />
  // The mark and its word are a Button, ghost (nothing until the hand comes), so
  // they stand at a button's heights and corners beside any other control. The
  // word sits in a cell as wide as its widest form, so "copied" moves nothing.
  const button = !text ? (
    <Button
      data-slot="copy"
      data-face="mark"
      data-state={state}
      variant="ghost"
      size={label ? "sm" : "icon-sm"}
      icon={label ? mark : undefined}
      onClick={press}
      aria-label={error ? "Copy failed. Try again" : copied ? "Copied" : "Copy"}
      title={error && tip === undefined ? UNAVAILABLE : undefined}
      className={cn(
        "shrink-0 font-mono text-muted-foreground hover:text-foreground data-[state=copied]:text-foreground",
        className,
      )}
      {...props}
    >
      {label ? (
        <span data-slot="copy-label" className="grid">
          <span aria-hidden className="invisible col-start-1 row-start-1">
            copied
          </span>
          <span className="col-start-1 row-start-1 text-left">
            {error ? "retry" : copied ? "copied" : "copy"}
          </span>
        </span>
      ) : (
        mark
      )}
    </Button>
  ) : (
    // The text is the button: the value reads as itself in its line (an order
    // number in a sentence), so no button surface, only a faint wash under the hand.
    // The accessible name carries the state; text at rest is its own name.
    <button
      data-slot="copy"
      data-face="text"
      data-state={state}
      type="button"
      onClick={press}
      aria-label={
        error ? "Copy failed. Try again" : copied ? "Copied" : undefined
      }
      title={error && tip === undefined ? UNAVAILABLE : undefined}
      className={cn(
        "group/copy -mx-1 inline-flex min-w-0 max-w-full cursor-pointer items-center gap-1 rounded-sm px-1 text-left transition-colors hover:bg-fill-row-hover focus-visible:bg-fill-row-hover focus-visible:outline-none disabled:pointer-events-none disabled:opacity-40",
        className,
      )}
      {...props}
    >
      <span data-slot="copy-face" className="flex min-w-0">
        {children}
      </span>
      {/* Beside text the mark is a step dimmer than the words, and waits for the hand. */}
      <span
        data-slot="copy-mark"
        aria-hidden
        className="shrink-0 text-current/60 opacity-0 transition-opacity group-hover/copy:opacity-100 group-focus-visible/copy:opacity-100 group-data-[state=copied]/copy:opacity-100 group-data-[state=error]/copy:opacity-100 [&_svg]:size-3"
      >
        {mark}
      </span>
    </button>
  )
  if (tip === undefined) return button
  // Over text, the tip keeps to the text's bounds: it starts where the text starts
  // and is no wider than it (8rem at least, so words under a short value still
  // read), a whole value wrapping where it must instead of hanging past the value.
  // The mark alone centres its tip.
  return (
    <Tip
      content={error ? UNAVAILABLE : tip}
      align={text ? "start" : "center"}
      className={
        text ? "max-w-[max(var(--anchor-width),8rem)] wrap-anywhere" : undefined
      }
    >
      {button}
    </Tip>
  )
}

export interface CopyFieldProps {
  /** What the copy writes, whole. */
  value: string
  /** A value that identifies its owner (a license key, a token), as `Veiled` draws
   *  one: dots, an eye on the front beside the copy to peek it for a few seconds. */
  veiled?: boolean
  /** Its tooltip on the copy mark. */
  tip?: React.ReactNode
  /** An accessible name for the value ("license key"). */
  label: string
  size?: InputSize
  material?: Material
  className?: string
}

/** A value to be copied, in the field every input is (`InputGroup`): read-only and
 *  monospaced in the well, cut at its end when longer than the field, the copy on
 *  the field's front, the strip at its end. An install id, an API key; a license key
 *  `veiled`, copied without being shown. */
export function CopyField({
  value,
  veiled = false,
  tip,
  label,
  size = "lg",
  material,
  className,
}: CopyFieldProps) {
  const { shown, toggle } = usePeek(value)
  const hidden = veiled && !shown
  return (
    <InputGroup
      readOnly
      value={hidden ? veil(value) : value}
      aria-label={hidden ? `${label}, hidden` : label}
      size={size}
      material={material}
      className={cn("font-mono", className)}
      onFocus={(event) => {
        if (!hidden) event.currentTarget.select()
      }}
      actions={
        <>
          {veiled && (
            <Button
              data-slot="copy-field-peek"
              variant="ghost"
              size="icon-sm"
              className={frontButton(size)}
              aria-label={shown ? "hide" : "show"}
              pressed={shown}
              onClick={toggle}
            >
              {shown ? <EyeOff aria-hidden /> : <Eye aria-hidden />}
            </Button>
          )}
          <Copy value={value} tip={tip} className={frontButton(size)} />
        </>
      }
    />
  )
}