Sortable list
Reorder rows by dragging or with the keyboard; keyed rows move and keep their DOM.
<script lang="ts">
type Item = { id: number; text: string; color: string }
const items = state<Item[]>([
{ id: 1, text: 'Design the API', color: '#f97316' },
{ id: 2, text: 'Write the compiler', color: '#3b82f6' },
{ id: 3, text: 'Add a router', color: '#22c55e' },
{ id: 4, text: 'Ship the docs', color: '#a855f7' },
{ id: 5, text: 'Celebrate', color: '#ec4899' },
])
const dragging = state<number | null>(null)
// Moving an item in the array moves its <li> in the DOM: keyed lists never rebuild rows,
// so the input you typed in (and its focus) stays put.
function moveTo(id: number, index: number) {
const from = items.findIndex((i) => i.id === id)
if (from < 0 || from === index) return
const next = [...items]
const [item] = next.splice(from, 1)
next.splice(index, 0, item)
items.set(next)
}
const shift = (id: number, by: number) => moveTo(id, Math.max(0, Math.min(items.length - 1, items.findIndex((i) => i.id === id) + by)))
const shuffle = () => items.set([...items].sort(() => Math.random() - 0.5))
</script>
<ul>
{items.map((item, index) => (
<li
key={item.id}
draggable="true"
class={{ dragging: dragging === item.id }}
style={`--c:${item.color}`}
onDragstart={() => dragging.set(item.id)}
onDragend={() => dragging.set(null)}
onDragover={(e: DragEvent) => {
e.preventDefault()
if (dragging !== null) moveTo(dragging, index)
}}
>
<span class="handle" aria-hidden="true">⠿</span>
<span class="rank">{index + 1}</span>
<input value={item.text} aria-label={`Item ${index + 1}`} />
<div class="keys">
<button aria-label="Move up" disabled={index === 0} onClick={() => shift(item.id, -1)}>↑</button>
<button aria-label="Move down" disabled={index === items.length - 1} onClick={() => shift(item.id, 1)}>↓</button>
</div>
</li>
))}
</ul>
<div class="footer">
<span>Drag rows, or use the arrows. Edit a row first: the text moves with it.</span>
<button onClick={shuffle}>Shuffle</button>
</div>
<style>
ul {
display: grid;
gap: 4px;
margin: 0;
padding: 0;
}
li {
display: flex;
align-items: center;
gap: 0.5rem;
padding: 0.25rem 0.25rem 0.25rem 0.5rem;
border: 1px solid var(--border);
border-left: 3px solid var(--c);
border-radius: 0.5rem;
background: var(--bg);
transition:
opacity 0.15s,
box-shadow 0.15s;
}
li.dragging {
opacity: 0.4;
}
li:hover {
box-shadow: 0 2px 8px -4px rgb(0 0 0 / 0.2);
}
.handle {
color: var(--faint);
cursor: grab;
}
.rank {
width: 1.25rem;
color: var(--muted);
font-family: var(--font-mono);
font-size: 0.75rem;
}
input {
flex: 1;
min-width: 0;
min-height: 1.75rem !important;
padding: 0 0.375rem !important;
border-color: transparent !important;
background: none !important;
}
input:focus-visible {
border-color: var(--faint) !important;
}
.keys {
display: flex;
}
.keys button {
min-height: 1.5rem;
padding: 0 0.4rem;
border: none;
background: none;
box-shadow: none;
color: var(--faint);
}
.keys button:hover:not(:disabled) {
background: var(--hover);
color: var(--fg);
}
.footer {
display: flex;
align-items: center;
justify-content: space-between;
gap: 0.75rem;
margin-top: 0.75rem;
color: var(--faint);
font-size: 0.75rem;
}
.footer button {
flex: none;
font-size: 0.8rem;
}
</style>