Command Palette

Search for a command to run...

Copy button

An icon button that copies a value and confirms the copy in its tooltip.

Reach for CopyButton — the ready-made instance of this pattern. Compose it by hand, as shown below, when the button has to look different (a labeled button, a different icon) — the feedback rule stays the same.

When to use

Put a copy button next to a value people carry somewhere else: an ID, a serial number, an email address, a link, a code snippet. Copying is invisible — the clipboard gives no sign that anything happened — so every copy button confirms the copy in a tooltip.

Anatomy

  • Icon button — a ghost icon Button beside the value it copies.
  • Tooltip — a Tooltip on the button with two states. At rest it names the action (Copy). After a successful copy it switches to the confirmation (Copied!) for 2 seconds, then reverts.

Usage

import { CopyButton } from "@smartacteam/ambient-web/copy-button";
<CopyButton value={serialNumber} label="Copy serial number" />

Composing by hand

CopyTooltip is the tooltip half of the pattern and useCopyToClipboard the copied state; wrap any button in them.

import { Button } from "@smartacteam/ambient-web/button";
import { CopyTooltip } from "@smartacteam/ambient-web/copy-button";
import { useCopyToClipboard } from "@smartacteam/ambient-web/utils";
const { isCopied, copy } = useCopyToClipboard({ text: url });
 
<CopyTooltip copied={isCopied} label="Copy link">
  <Button onClick={copy}>Copy link</Button>
</CopyTooltip>;

Snippet.CopyButton and CodeBlock.CopyButton are composed this way.

Guidelines

  • Use CopyTooltip for the tooltip. It stays open through the click — a tooltip trigger closes on click by default, which would dismiss the confirmation at the moment it matters — and it opens when the copy lands, so a tap on a touch screen, which has no hover, still shows the confirmation.
  • Confirm only a copy that succeeded. navigator.clipboard.writeText can reject; useCopyToClipboard sets isCopied after it resolves.
  • The tooltip carries the feedback. An icon swap (clipboard → checkmark) can accompany the tooltip. A toast is too loud for an action this small.
  • Say what gets copied when it's ambiguous. Copy is enough beside a single value; with several copy buttons in view, label each (Copy serial number). The label is also the button's accessible name.
  • Keep the confirmation short. Copied! — the value itself stays out of the tooltip.