本文へ移動
日本語

仕組み

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.

向いている場面

  • 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.

向かない場面

  • 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.

使い方

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" />

プロパティ

プロパティ型既定値説明
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>.

アクセシビリティ

  • 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.

モーション

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).

プロジェクトに追加

プロンプトを Codex または Claude Code に貼り付けます。エージェントが公開ソースを読み、書き込む内容を示したうえで、既存のコンポーネントの隣に追加します。

CopyButton.jsxcopy-button.csstokens.cssREADME.md

2 つのファイルを一緒に導入します。スタイルは tokens.css の変数を読み、予備の値も持つため、単体でも表示できます。

ボタン
「オーロラ縁ボタン」は Pro に含まれます。解除するとプレビューが表示されます。
「かじりボタン」は Pro に含まれます。解除するとプレビューが表示されます。