Skip to content
IndexRequest a UIAboutContact

Copy Feedback Button

A copy button that swaps its icon and label to a check for a moment after copying.

Open alone ↗

AI prompt

PROMPT: PASTE INTO YOUR AI CODING TOOL
Build a code snippet row, 300px wide, with monospace text "npm i qearth-ui" on the left and a 32px tall pill button on the right containing a copy icon and the label "Copy". On click call navigator.clipboard.writeText with the snippet; on success cross-fade the icon to a check and change the label to "Copied" for 1.5 seconds with a 180ms fade and scale from 0.8 to 1, then restore. If the Clipboard API is unavailable or rejects, fall back to selecting the text and document.execCommand copy; if that also fails show "Press Ctrl+C" with the text selected. Announce the result in an aria-live region. The button has an aria-label that includes the snippet. Keep a 2px focus-visible ring. Under prefers-reduced-motion swap the icon without transitions.

About this pattern

Definition

A small button next to a snippet of text. Pressing it copies the text and the button briefly changes to a confirmation, then returns to normal.

Purpose & UX principles

  • Gives immediate, local feedback at the spot where the action happened.
  • Faster and calmer than a toast for such a small action.
  • Handles failure honestly: if the browser blocks copying, the label says so and the text is selected for manual copy.

Interaction states

  • Idle, copied (check icon, "Copied"), failed ("Press Ctrl+C"), focus-visible.

Motion specification

  • Icons cross-fade with a slight scale (0.8 to 1) in 180ms.
  • The confirmation lasts 1.5 seconds.
  • Reduced motion: icons swap instantly.

Responsive behaviour

  • The button is at least 32px tall in dense code blocks and 44px on touch layouts.
  • The snippet scrolls horizontally if it is longer than the container.

Code

HTML
<div class="row"><code id="code">npm i qearth-ui</code>
  <button class="cp" id="cp" aria-label="Copy command npm i qearth-ui"><span class="ic"><svg class="a" viewBox="0 0 24 24" aria-hidden="true"><rect x="9" y="9" width="11" height="11" rx="2"/><path d="M5 15V6a2 2 0 0 1 2-2h9"/></svg><svg class="b" viewBox="0 0 24 24" aria-hidden="true"><path d="M5 12.500l4.500 4.500L19 7.500"/></svg></span><span id="lb">Copy</span></button></div>
<p class="live" id="live" aria-live="polite"></p>

Related patterns

Icon Button

↗
Icon ButtonButtons & Actions

Toast

↗
ToastFeedback & Notifications

Animated Checkmark Button

↗
Animated Checkmark ButtonMicro Interactions