StealThis .dev

Clipboard Copy

Copy-to-clipboard button with success feedback animation — icon changes to checkmark, reverts after 2s. Works on any text.

Open in Lab
vanilla-js css
Targets: JS HTML

Code

Clipboard Copy

A reusable copy-to-clipboard button with polished success feedback. When clicked, the icon morphs into a checkmark with a brief scale animation, then reverts to the copy icon after 2 seconds — giving users clear confirmation without interrupting their flow.

How it works

  1. Each copy button holds a data-copy attribute pointing to the ID of the <code> element to copy
  2. navigator.clipboard.writeText() writes the text asynchronously
  3. On success, a .is-copied class swaps the icon and triggers a CSS keyframe animation
  4. A setTimeout resets the state after 2 seconds
  5. Falls back gracefully if navigator.clipboard is unavailable

States

  • Default — copy icon, neutral background
  • Hover — subtle highlight on the button
  • Copied — checkmark icon, green accent, scale pulse
  • Error — red flash if clipboard write fails

When to use it

  • Code snippet blocks
  • API key / token displays
  • Shareable URL fields
  • Any “one-click copy” interface