Copy Feedback Button
A copy button that swaps its icon and label to a check for a moment after copying.
AI prompt
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
<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>:root{--bg:#F1F1EF;--surface:#fff;--surface-2:#F7F7F5;--line:#E0E0DC;--text:#171717;--muted:#6f6f6f;--accent:#171717;--accent-ink:#fff;--danger:#d92d20}
*{box-sizing:border-box;margin:0}
.row{display:flex;align-items:center;justify-content:space-between;gap:12px;width:min(300px,100%);padding:8px 8px 8px 14px;background:var(--surface);border:1px solid var(--line);border-radius:12px}
code{font:13px ui-monospace,SFMono-Regular,Menlo,monospace;overflow:auto;white-space:nowrap}
.cp{display:inline-flex;align-items:center;gap:6px;height:32px;padding:0 12px;border-radius:99px;border:1px solid var(--line);background:var(--bg);font:inherit;font-size:12.5px;font-weight:500;cursor:pointer;transition:border-color .15s,background .15s}
.cp:hover{border-color:var(--accent)}
.cp:focus-visible{outline:2px solid var(--accent);outline-offset:2px}
.cp.done{background:var(--accent);border-color:var(--accent);color:var(--accent-ink)}
.ic{position:relative;width:16px;height:16px}
.ic svg{position:absolute;inset:0;width:16px;height:16px;fill:none;stroke:currentColor;stroke-width:2;stroke-linecap:round;stroke-linejoin:round;transition:opacity .18s,transform .18s}
.ic .b{opacity:0;transform:scale(.8)}
.cp.done .a{opacity:0;transform:scale(.8)}.cp.done .b{opacity:1;transform:none}
.live{margin-top:10px;font-size:12px;color:var(--muted);text-align:center;min-height:16px}
@media (prefers-reduced-motion:reduce){.ic svg,.cp{transition:none}}const cp=document.getElementById('cp'),lb=document.getElementById('lb'),code=document.getElementById('code'),live=document.getElementById('live');let t=0;
function done(ok){clearTimeout(t);cp.classList.toggle('done',ok);lb.textContent=ok?'Copied':'Press Ctrl+C';live.textContent=ok?'Copied to clipboard':'Copy failed. Press Ctrl+C.';t=setTimeout(()=>{cp.classList.remove('done');lb.textContent='Copy';live.textContent='';},1500);}
function sel(){const r=document.createRange();r.selectNodeContents(code);const s=getSelection();s.removeAllRanges();s.addRange(r);}
cp.addEventListener('click',()=>{const txt=code.textContent;
const fb=()=>{sel();let ok=false;try{ok=document.execCommand('copy');}catch(e){}done(ok);};
if(navigator.clipboard&&navigator.clipboard.writeText)navigator.clipboard.writeText(txt).then(()=>done(true),fb);else fb();});One complete HTML file. Save it and open it in a browser.
<!doctype html>
<html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><title>Copy Feedback Button — demo</title>
<style>
:root{--bg:#F1F1EF;--surface:#fff;--surface-2:#F7F7F5;--line:#E0E0DC;--text:#171717;--muted:#6f6f6f;--accent:#171717;--accent-ink:#fff;--danger:#d92d20}
*{box-sizing:border-box;margin:0}
html,body{height:100%}body{background:var(--bg);color:var(--text);font:15px/1.5 -apple-system,BlinkMacSystemFont,'Segoe UI',Inter,Roboto,sans-serif;-webkit-font-smoothing:antialiased}.stage{min-height:100%;display:grid;place-items:center;padding:24px}button,input,select,textarea{font-family:inherit}@media (prefers-reduced-motion:reduce){html{scroll-behavior:auto}}
.row{display:flex;align-items:center;justify-content:space-between;gap:12px;width:min(300px,100%);padding:8px 8px 8px 14px;background:var(--surface);border:1px solid var(--line);border-radius:12px}
code{font:13px ui-monospace,SFMono-Regular,Menlo,monospace;overflow:auto;white-space:nowrap}
.cp{display:inline-flex;align-items:center;gap:6px;height:32px;padding:0 12px;border-radius:99px;border:1px solid var(--line);background:var(--bg);font:inherit;font-size:12.5px;font-weight:500;cursor:pointer;transition:border-color .15s,background .15s}
.cp:hover{border-color:var(--accent)}
.cp:focus-visible{outline:2px solid var(--accent);outline-offset:2px}
.cp.done{background:var(--accent);border-color:var(--accent);color:var(--accent-ink)}
.ic{position:relative;width:16px;height:16px}
.ic svg{position:absolute;inset:0;width:16px;height:16px;fill:none;stroke:currentColor;stroke-width:2;stroke-linecap:round;stroke-linejoin:round;transition:opacity .18s,transform .18s}
.ic .b{opacity:0;transform:scale(.8)}
.cp.done .a{opacity:0;transform:scale(.8)}.cp.done .b{opacity:1;transform:none}
.live{margin-top:10px;font-size:12px;color:var(--muted);text-align:center;min-height:16px}
@media (prefers-reduced-motion:reduce){.ic svg,.cp{transition:none}}
</style></head>
<body><div class="stage">
<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>
</div>
<script>
const cp=document.getElementById('cp'),lb=document.getElementById('lb'),code=document.getElementById('code'),live=document.getElementById('live');let t=0;
function done(ok){clearTimeout(t);cp.classList.toggle('done',ok);lb.textContent=ok?'Copied':'Press Ctrl+C';live.textContent=ok?'Copied to clipboard':'Copy failed. Press Ctrl+C.';t=setTimeout(()=>{cp.classList.remove('done');lb.textContent='Copy';live.textContent='';},1500);}
function sel(){const r=document.createRange();r.selectNodeContents(code);const s=getSelection();s.removeAllRanges();s.addRange(r);}
cp.addEventListener('click',()=>{const txt=code.textContent;
const fb=()=>{sel();let ok=false;try{ok=document.execCommand('copy');}catch(e){}done(ok);};
if(navigator.clipboard&&navigator.clipboard.writeText)navigator.clipboard.writeText(txt).then(()=>done(true),fb);else fb();});
</script>
</body></html>