Ir al contenido
Español

Cómo funciona

Uses navigator.clipboard.writeText, and falls back to a hidden textarea with execCommand('copy') when the page is not secure or permission is denied, restoring focus and the selection afterwards. The copy, check and alert icons share one cell and cross-fade with scale and blur. In labelled mode an invisible sizer holds the longest label, so the button keeps its size across states. Repeated clicks restart the timer and unmounting clears it. Filled variants (solid and ink) turn green after a copy; the icon-only bubble is an ink pill with a caret.

Úsalo para

  • Beside order, tracking or reference numbers.
  • Invite codes, coupon codes and other short text people paste elsewhere.
  • Code blocks, masked API keys and share links.

Busca otra opción cuando

  • Rich text or images: the clipboard call here handles plain text only.
  • Content the person cannot see: show it first, then offer to copy it.

Uso

import { CopyButton } from './CopyButton.jsx'

// Icon only: an ink pill bubble confirms the copy
<CopyButton text={inviteCode} variant="outline" aria-label="Copy invite code" />

// Ink button with a label, inside a read-only link field
<CopyButton text={inviteUrl} variant="ink" showLabel label="Copy link" />

// Read the value at click time
<CopyButton text={() => inputRef.current?.value ?? ''} size="sm" aria-label="Copy" />

Propiedades

PropiedadTipoPor defectoQué hace
textstring | (() => string)—Text to copy. Pass a function to read the value at click time.
resetDelaynumber1600How long the result stays before the button returns, in ms.
showLabelbooleanfalseShows text beside the icon. The width is fixed to the longest label, so the layout never shifts.
tooltipbooleantrueIcon-only mode: shows a small bubble above the button after copying.
labelstring'Copy'Idle label, also the accessible name in icon-only mode. Override with aria-label for something specific.
copiedLabelstring'Copied'Text shown and announced after a successful copy.
failedLabelstring'Copy failed'Text shown and announced when copying fails.
variant'ghost' | 'outline' | 'solid' | 'ink''ghost'Ghost sits beside text; outline is a raised surface with a hairline ring; solid (accent) and ink (text colour, inverts in dark) are filled and turn green after a copy.
size'sm' | 'md''md'Height 28px or 36px; radius 10px or 12px.
onCopy(text) => void—Called after a successful copy.
onError(error) => void—Called when copying fails.
...restButtonHTMLAttributes—Other attributes, such as aria-label and disabled, go to the <button>.

Accesibilidad

  • A polite live region next to the button announces “Copied” or “Copy failed”.
  • Icon-only buttons have an aria-label; in labelled mode the inactive labels are hidden from assistive technology.
  • The fallback returns focus to the button and restores the text selection.
  • Reduced motion removes scale, blur and the drawn check, keeping the fade.

Movimiento

Icon out: 120ms ease-in (scale 0.4, blur 4px). Icon in: opacity 240ms ease-out, scale 360ms spring. Check stroke: 320ms ease-out after 60ms. Label resize: 240ms standard. Bubble in: 160ms / 240ms spring; out: 120ms ease-in. Press scale 0.94 (0.97 with a label).

Añádelo a tu proyecto

Copia la instrucción en Codex o Claude Code. El agente lee el código publicado, te muestra lo que va a escribir y añade el componente junto a los tuyos.

CopyButton.jsxcopy-button.csstokens.cssREADME.md

Los dos archivos se instalan juntos. La hoja de estilos lee las variables de tokens.css y lleva valores de reserva, así que también funciona sola.

Botones
Botón de borde aurora viene con Pro. La vista previa aparece al desbloquearlo.
Botón con mordiscos viene con Pro. La vista previa aparece al desbloquearlo.