Drag to Reorder
A sortable list where rows are dragged by a handle and the others move aside live.
AI prompt
Build a four-item sortable list (Design, Build, Test, Ship), 280px wide, each row 48px tall with a six-dot drag handle on the left (a button, 44px wide, touch-action none) and a 14px label. On pointerdown on a handle, capture the pointer and remember the offset between pointer and row top. While moving, set the row transform so it follows the pointer; when the pointer crosses the vertical midpoint of a sibling, move the row in the DOM before or after that sibling and recompute the transform from its new natural position. Style the dragged row with a stronger border, a light shadow and a z-index. On release animate the transform back to 0 in 200ms. Keyboard: with the handle focused, Alt+ArrowUp and Alt+ArrowDown move the row, keep focus on the handle and announce "Moved to position N of 4" in an aria-live region. Under prefers-reduced-motion remove transitions.About this pattern
Definition
A list whose items can be rearranged by dragging. The dragged item follows the pointer while the others shift to show where it will land.
Purpose & UX principles
- Lets people prioritise or arrange items directly.
- A dedicated handle avoids accidental drags and keeps scrolling usable.
- Keyboard users reorder with Alt and the arrow keys on the handle, and the change is announced.
Interaction states
- Rest, hover (handle darkens), dragging (row lifted with shadow), dropped, focus-visible on the handle.
Motion specification
- While dragging the row follows the pointer exactly; neighbours move instantly as the order changes.
- The lift shadow fades in over 150ms and the row settles in 200ms on release.
- Reduced motion: no shadow transition and an instant drop.
Responsive behaviour
- Rows are 48px tall with a 44px handle target.
- On touch the handle uses
touch-action: noneso the drag does not scroll the page.
Code
<div class="box">
<ul class="sl" id="sl"></ul>
<p class="live" id="live" aria-live="polite">Drag the handles, or use Alt + arrow keys.</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(280px,100%);display:grid;gap:10px}
.sl{list-style:none;display:grid;gap:6px}
.it{position:relative;display:flex;align-items:center;height:48px;border:1px solid var(--line);border-radius:12px;background:var(--surface);font-size:14px;transition:box-shadow .15s,border-color .15s}
.it.drag{z-index:5;border-color:var(--accent);box-shadow:0 10px 24px rgba(0,0,0,.14)}
.h{width:44px;height:100%;display:grid;place-items:center;background:none;border:0;color:var(--muted);cursor:grab;touch-action:none;font-size:16px;letter-spacing:-2px}
.h:hover{color:var(--text)}
.h:focus-visible{outline:2px solid var(--accent);outline-offset:-3px;border-radius:10px}
.it.settle{transition:transform .2s cubic-bezier(.2,.7,.2,1)}
.live{font-size:12px;color:var(--muted);text-align:center}
@media (prefers-reduced-motion:reduce){.it,.it.settle{transition:none}}const ul=document.getElementById('sl'),live=document.getElementById('live');
['Design','Build','Test','Ship'].forEach(t=>{const li=document.createElement('li');li.className='it';li.innerHTML='<button class="h" aria-label="Reorder '+t+'">⋮⋮</button><span>'+t+'</span>';ul.append(li);});
const rows=()=>[...ul.children];
const say=li=>{live.textContent='Moved to position '+(rows().indexOf(li)+1)+' of '+rows().length;};
ul.addEventListener('pointerdown',e=>{
const h=e.target.closest('.h');if(!h)return;const li=h.parentElement;e.preventDefault();h.setPointerCapture(e.pointerId);
li.classList.remove('settle');li.classList.add('drag');const grab=e.clientY-li.getBoundingClientRect().top;
const move=ev=>{li.style.transform='none';
for(const s of rows()){if(s===li)continue;const r=s.getBoundingClientRect(),mid=r.top+r.height/2,me=li.getBoundingClientRect();
if(s.compareDocumentPosition(li)&Node.DOCUMENT_POSITION_FOLLOWING&&ev.clientY>mid)ul.insertBefore(li,s.nextSibling);
else if(s.compareDocumentPosition(li)&Node.DOCUMENT_POSITION_PRECEDING&&ev.clientY<mid)ul.insertBefore(li,s);}
li.style.transform='none';const nat=li.getBoundingClientRect().top;li.style.transform='translateY('+(ev.clientY-grab-nat)+'px)';};
const up=()=>{h.removeEventListener('pointermove',move);h.removeEventListener('pointerup',up);h.removeEventListener('pointercancel',up);
li.classList.add('settle');li.classList.remove('drag');li.style.transform='';say(li);};
h.addEventListener('pointermove',move);h.addEventListener('pointerup',up);h.addEventListener('pointercancel',up);
});
ul.addEventListener('keydown',e=>{const h=e.target.closest('.h');if(!h||!e.altKey)return;const li=h.parentElement;
if(e.key==='ArrowUp'&&li.previousElementSibling){e.preventDefault();ul.insertBefore(li,li.previousElementSibling);h.focus();say(li);}
if(e.key==='ArrowDown'&&li.nextElementSibling){e.preventDefault();ul.insertBefore(li.nextElementSibling,li);h.focus();say(li);}});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>Drag to Reorder — 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(280px,100%);display:grid;gap:10px}
.sl{list-style:none;display:grid;gap:6px}
.it{position:relative;display:flex;align-items:center;height:48px;border:1px solid var(--line);border-radius:12px;background:var(--surface);font-size:14px;transition:box-shadow .15s,border-color .15s}
.it.drag{z-index:5;border-color:var(--accent);box-shadow:0 10px 24px rgba(0,0,0,.14)}
.h{width:44px;height:100%;display:grid;place-items:center;background:none;border:0;color:var(--muted);cursor:grab;touch-action:none;font-size:16px;letter-spacing:-2px}
.h:hover{color:var(--text)}
.h:focus-visible{outline:2px solid var(--accent);outline-offset:-3px;border-radius:10px}
.it.settle{transition:transform .2s cubic-bezier(.2,.7,.2,1)}
.live{font-size:12px;color:var(--muted);text-align:center}
@media (prefers-reduced-motion:reduce){.it,.it.settle{transition:none}}
</style></head>
<body><div class="stage">
<div class="box">
<ul class="sl" id="sl"></ul>
<p class="live" id="live" aria-live="polite">Drag the handles, or use Alt + arrow keys.</p>
</div>
</div>
<script>
const ul=document.getElementById('sl'),live=document.getElementById('live');
['Design','Build','Test','Ship'].forEach(t=>{const li=document.createElement('li');li.className='it';li.innerHTML='<button class="h" aria-label="Reorder '+t+'">⋮⋮</button><span>'+t+'</span>';ul.append(li);});
const rows=()=>[...ul.children];
const say=li=>{live.textContent='Moved to position '+(rows().indexOf(li)+1)+' of '+rows().length;};
ul.addEventListener('pointerdown',e=>{
const h=e.target.closest('.h');if(!h)return;const li=h.parentElement;e.preventDefault();h.setPointerCapture(e.pointerId);
li.classList.remove('settle');li.classList.add('drag');const grab=e.clientY-li.getBoundingClientRect().top;
const move=ev=>{li.style.transform='none';
for(const s of rows()){if(s===li)continue;const r=s.getBoundingClientRect(),mid=r.top+r.height/2,me=li.getBoundingClientRect();
if(s.compareDocumentPosition(li)&Node.DOCUMENT_POSITION_FOLLOWING&&ev.clientY>mid)ul.insertBefore(li,s.nextSibling);
else if(s.compareDocumentPosition(li)&Node.DOCUMENT_POSITION_PRECEDING&&ev.clientY<mid)ul.insertBefore(li,s);}
li.style.transform='none';const nat=li.getBoundingClientRect().top;li.style.transform='translateY('+(ev.clientY-grab-nat)+'px)';};
const up=()=>{h.removeEventListener('pointermove',move);h.removeEventListener('pointerup',up);h.removeEventListener('pointercancel',up);
li.classList.add('settle');li.classList.remove('drag');li.style.transform='';say(li);};
h.addEventListener('pointermove',move);h.addEventListener('pointerup',up);h.addEventListener('pointercancel',up);
});
ul.addEventListener('keydown',e=>{const h=e.target.closest('.h');if(!h||!e.altKey)return;const li=h.parentElement;
if(e.key==='ArrowUp'&&li.previousElementSibling){e.preventDefault();ul.insertBefore(li,li.previousElementSibling);h.focus();say(li);}
if(e.key==='ArrowDown'&&li.nextElementSibling){e.preventDefault();ul.insertBefore(li.nextElementSibling,li);h.focus();say(li);}});
</script>
</body></html>