Sortable Ranking Bars
Horizontal bars that glide into their new order when the sort changes.
AI prompt
Build a ranking of six frameworks with values (Alpha 82, Bravo 64, Charlie 91, Delta 47, Echo 73, Foxtrot 58), each row 34px tall with a 60px label, a horizontal bar whose width is value divided by the maximum and the number at the end. Add a group of three buttons "High to low", "Low to high" and "A to Z" with aria-pressed. When a sort is chosen, use the FLIP technique: record each row's top, reorder the DOM, record the new tops and animate each row from translateY(old minus new) to 0 over 420ms cubic-bezier(.2,.7,.2,1) with element.animate. Announce "Sorted high to low" in an aria-live polite region. Bars have a dark fill with a light track. Under prefers-reduced-motion reorder without animation.About this pattern
Definition
A ranked list where each row has a horizontal bar. Changing the sort order moves the rows smoothly to their new positions instead of redrawing the list.
Purpose & UX principles
- Animated reordering lets people follow which item went where, which a sudden jump hides.
- Sort controls are a labelled group of buttons with
aria-pressed, and the new order is announced. - Values are written as numbers beside the bars, so the chart does not rely on length alone.
Interaction states
- Sorted by value (descending), by value (ascending), by name; the active button is pressed.
Motion specification
- FLIP: rows are measured, reordered and animated from their old positions with a 420ms cubic-bezier(.2,.7,.2,1) transform.
- Bars keep their width; only position changes.
- Reduced motion: rows jump to place.
Responsive behaviour
- Rows fill the width; labels truncate with ellipsis on narrow screens.
Code
<div class="box">
<div class="seg" role="group" aria-label="Sort order"><button data-s="hi" aria-pressed="true">High to low</button><button data-s="lo" aria-pressed="false">Low to high</button><button data-s="az" aria-pressed="false">A to Z</button></div>
<ul class="rk" id="rk"></ul><p class="live" id="live" aria-live="polite"></p>
</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}
.box{width:min(300px,100%);display:grid;gap:10px}
.seg{display:flex;gap:4px}
.seg button{height:28px;padding:0 11px;border-radius:99px;border:1px solid var(--line);background:var(--surface);font:inherit;font-size:12px;cursor:pointer}
.seg button[aria-pressed=true]{background:var(--accent);color:var(--accent-ink);border-color:var(--accent)}
.seg button:hover:not([aria-pressed=true]){border-color:var(--accent)}
.seg button:focus-visible{outline:2px solid var(--accent);outline-offset:2px}
.rk{list-style:none;display:grid;gap:4px}
.rk li{display:grid;grid-template-columns:62px 1fr 28px;align-items:center;gap:8px;height:30px;font-size:12.5px}
.rk .tr{height:10px;border-radius:5px;background:#e8e8e4;overflow:hidden}
.rk .tr i{display:block;height:100%;background:#171717;border-radius:5px}
.rk em{font-style:normal;text-align:right;font-variant-numeric:tabular-nums;font-weight:600}
.live{font-size:11px;color:var(--muted);min-height:14px}const data=[['Alpha',82],['Bravo',64],['Charlie',91],['Delta',47],['Echo',73],['Foxtrot',58]],mx=91;
const rk=document.getElementById('rk'),live=document.getElementById('live'),reduce=matchMedia('(prefers-reduced-motion: reduce)').matches;
const rows={};data.forEach(([n,v])=>{const li=document.createElement('li');li.innerHTML='<span>'+n+'</span><span class="tr"><i style="width:'+(v/mx*100)+'%"></i></span><em>'+v+'</em>';rows[n]=li;});
const by={hi:(a,b)=>b[1]-a[1],lo:(a,b)=>a[1]-b[1],az:(a,b)=>a[0].localeCompare(b[0])},say={hi:'high to low',lo:'low to high',az:'A to Z'};
function sort(k,anim){const old=new Map([...rk.children].map(l=>[l,l.getBoundingClientRect().top]));
data.slice().sort(by[k]).forEach(([n])=>rk.append(rows[n]));
if(anim&&!reduce)[...rk.children].forEach(l=>{const d=old.get(l)-l.getBoundingClientRect().top;if(d)l.animate([{transform:'translateY('+d+'px)'},{transform:'none'}],{duration:420,easing:'cubic-bezier(.2,.7,.2,1)'});});}
sort('hi',false);
document.querySelectorAll('.seg button').forEach(b=>b.addEventListener('click',()=>{document.querySelectorAll('.seg button').forEach(x=>x.setAttribute('aria-pressed',String(x===b)));sort(b.dataset.s,true);live.textContent='Sorted '+say[b.dataset.s];}));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>Sortable Ranking Bars — 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}}
.box{width:min(300px,100%);display:grid;gap:10px}
.seg{display:flex;gap:4px}
.seg button{height:28px;padding:0 11px;border-radius:99px;border:1px solid var(--line);background:var(--surface);font:inherit;font-size:12px;cursor:pointer}
.seg button[aria-pressed=true]{background:var(--accent);color:var(--accent-ink);border-color:var(--accent)}
.seg button:hover:not([aria-pressed=true]){border-color:var(--accent)}
.seg button:focus-visible{outline:2px solid var(--accent);outline-offset:2px}
.rk{list-style:none;display:grid;gap:4px}
.rk li{display:grid;grid-template-columns:62px 1fr 28px;align-items:center;gap:8px;height:30px;font-size:12.5px}
.rk .tr{height:10px;border-radius:5px;background:#e8e8e4;overflow:hidden}
.rk .tr i{display:block;height:100%;background:#171717;border-radius:5px}
.rk em{font-style:normal;text-align:right;font-variant-numeric:tabular-nums;font-weight:600}
.live{font-size:11px;color:var(--muted);min-height:14px}
</style></head>
<body><div class="stage">
<div class="box">
<div class="seg" role="group" aria-label="Sort order"><button data-s="hi" aria-pressed="true">High to low</button><button data-s="lo" aria-pressed="false">Low to high</button><button data-s="az" aria-pressed="false">A to Z</button></div>
<ul class="rk" id="rk"></ul><p class="live" id="live" aria-live="polite"></p>
</div>
</div>
<script>
const data=[['Alpha',82],['Bravo',64],['Charlie',91],['Delta',47],['Echo',73],['Foxtrot',58]],mx=91;
const rk=document.getElementById('rk'),live=document.getElementById('live'),reduce=matchMedia('(prefers-reduced-motion: reduce)').matches;
const rows={};data.forEach(([n,v])=>{const li=document.createElement('li');li.innerHTML='<span>'+n+'</span><span class="tr"><i style="width:'+(v/mx*100)+'%"></i></span><em>'+v+'</em>';rows[n]=li;});
const by={hi:(a,b)=>b[1]-a[1],lo:(a,b)=>a[1]-b[1],az:(a,b)=>a[0].localeCompare(b[0])},say={hi:'high to low',lo:'low to high',az:'A to Z'};
function sort(k,anim){const old=new Map([...rk.children].map(l=>[l,l.getBoundingClientRect().top]));
data.slice().sort(by[k]).forEach(([n])=>rk.append(rows[n]));
if(anim&&!reduce)[...rk.children].forEach(l=>{const d=old.get(l)-l.getBoundingClientRect().top;if(d)l.animate([{transform:'translateY('+d+'px)'},{transform:'none'}],{duration:420,easing:'cubic-bezier(.2,.7,.2,1)'});});}
sort('hi',false);
document.querySelectorAll('.seg button').forEach(b=>b.addEventListener('click',()=>{document.querySelectorAll('.seg button').forEach(x=>x.setAttribute('aria-pressed',String(x===b)));sort(b.dataset.s,true);live.textContent='Sorted '+say[b.dataset.s];}));
</script>
</body></html>