Upload Progress & Retry
A file row with live percent, Cancel, and a Retry path when the upload fails.
AI prompt
Build a file upload progress row. It shows a file icon, the file name "report-final.pdf", the size, a thin determinate progress bar and a percent number. States: uploading (bar fills, Cancel link), done (green check, "Uploaded", no bar), failed (red message "Upload failed. Check your connection.", Retry button), cancelled (grey "Cancelled", Retry). Include a small switch "Fail at 60 percent" for testing. Light minimal style, rounded corners, 1 pixel borders, accessible: progress uses role progressbar with aria-valuenow, state changes are announced via aria-live, buttons have clear labels. Plain HTML, CSS and JavaScript.About this pattern
Definition
An upload row shows file name, a determinate progress bar and the current state: waiting, uploading, done, failed or cancelled. Each state offers the right next action.
Purpose & UX principles
- Percent plus a bar tells people exactly how long is left; Cancel gives control; Retry recovers from failures without starting over.
- Announce completion and failure to screen readers; do not rely on colour alone.
- Keep the file name visible and truncate it in the middle for long names.
Interaction states
- Idle, uploading (percent rises), done (check), failed (error text and Retry), cancelled. The "Fail at 60%" switch makes the next upload fail so you can test the error path.
Motion specification
- Bar width updates smoothly every 100ms; state icons fade in over 200ms.
- Reduced motion: the bar still changes width but without easing transitions.
Responsive behaviour
- Row stretches to the container; buttons stay at least 28px tall for touch.
- On very narrow widths, drop the percent to under the name.
Code
<div class="bx"><div class="row"><div class="ic">PDF</div><div class="mid"><div class="top"><b>report-final.pdf</b><span id="pc">0%</span></div><div class="bar" role="progressbar" aria-valuemin="0" aria-valuemax="100" aria-valuenow="0" id="bar"><i id="fill"></i></div><p class="st" id="st" aria-live="polite">Ready to upload · 2.4 MB</p></div></div>
<div class="act"><button id="go" type="button">Upload</button><button id="cn" type="button" hidden>Cancel</button></div><label class="sw"><input id="fl" type="checkbox"> Fail at 60%</label></div>: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}
.bx{width:min(300px,100%);display:grid;gap:10px}
.row{display:flex;gap:10px;align-items:center;padding:12px;border:1px solid var(--line);border-radius:12px;background:var(--surface)}
.ic{width:36px;height:44px;border-radius:6px;background:#eeeeea;display:grid;place-items:center;font-size:10px;font-weight:700;color:var(--muted)}
.mid{flex:1;min-width:0}.top{display:flex;justify-content:space-between;gap:8px;font-size:13px}.top b{font-weight:600;overflow:hidden;text-overflow:ellipsis;white-space:nowrap}.top span{color:var(--muted);font-variant-numeric:tabular-nums}
.bar{height:5px;border-radius:3px;background:#eaeae6;margin:7px 0 5px;overflow:hidden}.bar i{display:block;height:100%;width:0;background:var(--text);transition:width .1s linear}
.bar.err i{background:var(--danger)}.bar.ok i{background:#1a8f4a}
.st{font-size:11.5px;color:var(--muted)}.st.err{color:var(--danger)}
.act{display:flex;gap:6px}.act button{height:30px;padding:0 16px;border-radius:99px;border:1px solid var(--line);background:var(--surface);font-size:12.5px;cursor:pointer}
#go{background:var(--accent);color:var(--accent-ink);border-color:var(--accent)}#go:disabled{opacity:.4;cursor:default}
.sw{display:flex;gap:6px;align-items:center;font-size:12px;color:var(--muted)}
button:focus-visible,input:focus-visible{outline:2px solid var(--accent);outline-offset:2px}
@media (prefers-reduced-motion:reduce){.bar i{transition:none}}const $=id=>document.getElementById(id),go=$('go'),cn=$('cn'),fill=$('fill'),bar=$('bar'),pc=$('pc'),st=$('st'),fl=$('fl');let p=0,t=0;
function set(n){p=n;fill.style.width=n+'%';pc.textContent=Math.round(n)+'%';bar.setAttribute('aria-valuenow',Math.round(n));}
function state(k,msg,err){bar.className='bar '+(k||'');st.className='st'+(err?' err':'');st.textContent=msg;}
function start(){clearInterval(t);set(0);state('', 'Uploading…');go.disabled=true;cn.hidden=false;
t=setInterval(()=>{set(p+2+Math.random()*3);if(fl.checked&&p>=60){clearInterval(t);set(60);state('err','Upload failed. Check your connection.',1);go.textContent='Retry';go.disabled=false;cn.hidden=true;fl.checked=false;}
else if(p>=100){clearInterval(t);set(100);state('ok','✓ Uploaded');go.textContent='Upload again';go.disabled=false;cn.hidden=true;}},100);}
go.addEventListener('click',start);cn.addEventListener('click',()=>{clearInterval(t);state('', 'Cancelled');go.textContent='Retry';go.disabled=false;cn.hidden=true;});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>Upload Progress & Retry — 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}}
.bx{width:min(300px,100%);display:grid;gap:10px}
.row{display:flex;gap:10px;align-items:center;padding:12px;border:1px solid var(--line);border-radius:12px;background:var(--surface)}
.ic{width:36px;height:44px;border-radius:6px;background:#eeeeea;display:grid;place-items:center;font-size:10px;font-weight:700;color:var(--muted)}
.mid{flex:1;min-width:0}.top{display:flex;justify-content:space-between;gap:8px;font-size:13px}.top b{font-weight:600;overflow:hidden;text-overflow:ellipsis;white-space:nowrap}.top span{color:var(--muted);font-variant-numeric:tabular-nums}
.bar{height:5px;border-radius:3px;background:#eaeae6;margin:7px 0 5px;overflow:hidden}.bar i{display:block;height:100%;width:0;background:var(--text);transition:width .1s linear}
.bar.err i{background:var(--danger)}.bar.ok i{background:#1a8f4a}
.st{font-size:11.5px;color:var(--muted)}.st.err{color:var(--danger)}
.act{display:flex;gap:6px}.act button{height:30px;padding:0 16px;border-radius:99px;border:1px solid var(--line);background:var(--surface);font-size:12.5px;cursor:pointer}
#go{background:var(--accent);color:var(--accent-ink);border-color:var(--accent)}#go:disabled{opacity:.4;cursor:default}
.sw{display:flex;gap:6px;align-items:center;font-size:12px;color:var(--muted)}
button:focus-visible,input:focus-visible{outline:2px solid var(--accent);outline-offset:2px}
@media (prefers-reduced-motion:reduce){.bar i{transition:none}}
</style></head>
<body><div class="stage">
<div class="bx"><div class="row"><div class="ic">PDF</div><div class="mid"><div class="top"><b>report-final.pdf</b><span id="pc">0%</span></div><div class="bar" role="progressbar" aria-valuemin="0" aria-valuemax="100" aria-valuenow="0" id="bar"><i id="fill"></i></div><p class="st" id="st" aria-live="polite">Ready to upload · 2.4 MB</p></div></div>
<div class="act"><button id="go" type="button">Upload</button><button id="cn" type="button" hidden>Cancel</button></div><label class="sw"><input id="fl" type="checkbox"> Fail at 60%</label></div>
</div>
<script>
const $=id=>document.getElementById(id),go=$('go'),cn=$('cn'),fill=$('fill'),bar=$('bar'),pc=$('pc'),st=$('st'),fl=$('fl');let p=0,t=0;
function set(n){p=n;fill.style.width=n+'%';pc.textContent=Math.round(n)+'%';bar.setAttribute('aria-valuenow',Math.round(n));}
function state(k,msg,err){bar.className='bar '+(k||'');st.className='st'+(err?' err':'');st.textContent=msg;}
function start(){clearInterval(t);set(0);state('', 'Uploading…');go.disabled=true;cn.hidden=false;
t=setInterval(()=>{set(p+2+Math.random()*3);if(fl.checked&&p>=60){clearInterval(t);set(60);state('err','Upload failed. Check your connection.',1);go.textContent='Retry';go.disabled=false;cn.hidden=true;fl.checked=false;}
else if(p>=100){clearInterval(t);set(100);state('ok','✓ Uploaded');go.textContent='Upload again';go.disabled=false;cn.hidden=true;}},100);}
go.addEventListener('click',start);cn.addEventListener('click',()=>{clearInterval(t);state('', 'Cancelled');go.textContent='Retry';go.disabled=false;cn.hidden=true;});
</script>
</body></html>