Sortable
A list whose items are put in order by dragging a handle or with the keys.
On this page
A sortable is a list whose items are put in order by dragging a handle or with the keys. It has no row of its own: put it on a list, a stack of cards or anything that holds items, and give each item a handle, a button named with what it moves. The sortable behavior carries an item while the others make room for it, and moves its node once, when it is dropped.
Sortable
<div class="max-w(420) mx-auto"> <ol class="list sortable" aria-label="Priorities this quarter"> <li class="list-row sortable-item" data-value="onboarding"> <button type="button" class="sortable-handle" aria-pressed="false" aria-label="Move Onboarding"> <svg class="icon" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"> <circle cx="6" cy="4" r="1" fill="currentColor" stroke="none" /><circle cx="10" cy="4" r="1" fill="currentColor" stroke="none" /><circle cx="6" cy="8" r="1" fill="currentColor" stroke="none" /><circle cx="10" cy="8" r="1" fill="currentColor" stroke="none" /><circle cx="6" cy="12" r="1" fill="currentColor" stroke="none" /><circle cx="10" cy="12" r="1" fill="currentColor" stroke="none" /> </svg> </button> <span class="list-body"> <span class="list-title">Onboarding</span><span class="list-description">First run, invitations and the empty states</span> </span> <span class="list-end">Mar 2</span> </li> <li class="list-row sortable-item" data-value="billing"> <button type="button" class="sortable-handle" aria-pressed="false" aria-label="Move Billing"> <svg class="icon" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"> <circle cx="6" cy="4" r="1" fill="currentColor" stroke="none" /><circle cx="10" cy="4" r="1" fill="currentColor" stroke="none" /><circle cx="6" cy="8" r="1" fill="currentColor" stroke="none" /><circle cx="10" cy="8" r="1" fill="currentColor" stroke="none" /><circle cx="6" cy="12" r="1" fill="currentColor" stroke="none" /><circle cx="10" cy="12" r="1" fill="currentColor" stroke="none" /> </svg> </button> <span class="list-body"> <span class="list-title">Billing</span><span class="list-description">Invoices in every currency</span> </span> <span class="list-end">Mar 16</span> </li> <li class="list-row sortable-item" data-value="search"> <button type="button" class="sortable-handle" aria-pressed="false" aria-label="Move Search"> <svg class="icon" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"> <circle cx="6" cy="4" r="1" fill="currentColor" stroke="none" /><circle cx="10" cy="4" r="1" fill="currentColor" stroke="none" /><circle cx="6" cy="8" r="1" fill="currentColor" stroke="none" /><circle cx="10" cy="8" r="1" fill="currentColor" stroke="none" /><circle cx="6" cy="12" r="1" fill="currentColor" stroke="none" /><circle cx="10" cy="12" r="1" fill="currentColor" stroke="none" /> </svg> </button> <span class="list-body"> <span class="list-title">Search</span><span class="list-description">Filters that can be saved and shared</span> </span> <span class="list-end">Apr 6</span> </li> <li class="list-row sortable-item" data-value="exports"> <button type="button" class="sortable-handle" aria-pressed="false" aria-label="Move Exports"> <svg class="icon" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"> <circle cx="6" cy="4" r="1" fill="currentColor" stroke="none" /><circle cx="10" cy="4" r="1" fill="currentColor" stroke="none" /><circle cx="6" cy="8" r="1" fill="currentColor" stroke="none" /><circle cx="10" cy="8" r="1" fill="currentColor" stroke="none" /><circle cx="6" cy="12" r="1" fill="currentColor" stroke="none" /><circle cx="10" cy="12" r="1" fill="currentColor" stroke="none" /> </svg> </button> <span class="list-body"> <span class="list-title">Exports</span><span class="list-description">CSV and PDF for every report</span> </span> <span class="list-end">Apr 27</span> </li> </ol> <p class="sortable-status" role="status"></p></div><script type="module"> import { attach } from '@master/ui/behaviors' attach()</script>import { useEffect, useRef } from 'react'import { attach } from '@master/ui/behaviors'import GripVerticalIcon from '@master/icons-react/native/16/soft/grip-vertical'export function Example() { const root = useRef<HTMLDivElement>(null) useEffect(() => attach(root.current!), []) return ( <div ref={root} className="max-w(420) mx-auto"> <ol className="list sortable" aria-label="Priorities this quarter"> <li className="list-row sortable-item" data-value="onboarding"> <button type="button" className="sortable-handle" aria-pressed="false" aria-label="Move Onboarding"> <GripVerticalIcon className="icon" /> </button> <span className="list-body"> <span className="list-title">Onboarding</span><span className="list-description">First run, invitations and the empty states</span> </span> <span className="list-end">Mar 2</span> </li> <li className="list-row sortable-item" data-value="billing"> <button type="button" className="sortable-handle" aria-pressed="false" aria-label="Move Billing"> <GripVerticalIcon className="icon" /> </button> <span className="list-body"> <span className="list-title">Billing</span><span className="list-description">Invoices in every currency</span> </span> <span className="list-end">Mar 16</span> </li> <li className="list-row sortable-item" data-value="search"> <button type="button" className="sortable-handle" aria-pressed="false" aria-label="Move Search"> <GripVerticalIcon className="icon" /> </button> <span className="list-body"> <span className="list-title">Search</span><span className="list-description">Filters that can be saved and shared</span> </span> <span className="list-end">Apr 6</span> </li> <li className="list-row sortable-item" data-value="exports"> <button type="button" className="sortable-handle" aria-pressed="false" aria-label="Move Exports"> <GripVerticalIcon className="icon" /> </button> <span className="list-body"> <span className="list-title">Exports</span><span className="list-description">CSV and PDF for every report</span> </span> <span className="list-end">Apr 27</span> </li> </ol> <p className="sortable-status" role="status"></p> </div> )}<script> import { attach } from '@master/ui/behaviors' import GripVerticalIcon from '@master/icons-svelte/native/16/soft/grip-vertical'</script><div class="max-w(420) mx-auto" {@attach attach}> <ol class="list sortable" aria-label="Priorities this quarter"> <li class="list-row sortable-item" data-value="onboarding"> <button type="button" class="sortable-handle" aria-pressed="false" aria-label="Move Onboarding"> <GripVerticalIcon class="icon" /> </button> <span class="list-body"> <span class="list-title">Onboarding</span><span class="list-description">First run, invitations and the empty states</span> </span> <span class="list-end">Mar 2</span> </li> <li class="list-row sortable-item" data-value="billing"> <button type="button" class="sortable-handle" aria-pressed="false" aria-label="Move Billing"> <GripVerticalIcon class="icon" /> </button> <span class="list-body"> <span class="list-title">Billing</span><span class="list-description">Invoices in every currency</span> </span> <span class="list-end">Mar 16</span> </li> <li class="list-row sortable-item" data-value="search"> <button type="button" class="sortable-handle" aria-pressed="false" aria-label="Move Search"> <GripVerticalIcon class="icon" /> </button> <span class="list-body"> <span class="list-title">Search</span><span class="list-description">Filters that can be saved and shared</span> </span> <span class="list-end">Apr 6</span> </li> <li class="list-row sortable-item" data-value="exports"> <button type="button" class="sortable-handle" aria-pressed="false" aria-label="Move Exports"> <GripVerticalIcon class="icon" /> </button> <span class="list-body"> <span class="list-title">Exports</span><span class="list-description">CSV and PDF for every report</span> </span> <span class="list-end">Apr 27</span> </li> </ol> <p class="sortable-status" role="status"></p></div><script setup>import { onMounted, onUnmounted, ref } from 'vue'import { attach } from '@master/ui/behaviors'import GripVerticalIcon from '@master/icons-vue/native/16/soft/grip-vertical'const root = ref(null)let detachonMounted(() => { detach = attach(root.value) })onUnmounted(() => detach?.())</script><template> <div ref="root" class="max-w(420) mx-auto"> <ol class="list sortable" aria-label="Priorities this quarter"> <li class="list-row sortable-item" data-value="onboarding"> <button type="button" class="sortable-handle" aria-pressed="false" aria-label="Move Onboarding"> <GripVerticalIcon class="icon" /> </button> <span class="list-body"> <span class="list-title">Onboarding</span><span class="list-description">First run, invitations and the empty states</span> </span> <span class="list-end">Mar 2</span> </li> <li class="list-row sortable-item" data-value="billing"> <button type="button" class="sortable-handle" aria-pressed="false" aria-label="Move Billing"> <GripVerticalIcon class="icon" /> </button> <span class="list-body"> <span class="list-title">Billing</span><span class="list-description">Invoices in every currency</span> </span> <span class="list-end">Mar 16</span> </li> <li class="list-row sortable-item" data-value="search"> <button type="button" class="sortable-handle" aria-pressed="false" aria-label="Move Search"> <GripVerticalIcon class="icon" /> </button> <span class="list-body"> <span class="list-title">Search</span><span class="list-description">Filters that can be saved and shared</span> </span> <span class="list-end">Apr 6</span> </li> <li class="list-row sortable-item" data-value="exports"> <button type="button" class="sortable-handle" aria-pressed="false" aria-label="Move Exports"> <GripVerticalIcon class="icon" /> </button> <span class="list-body"> <span class="list-title">Exports</span><span class="list-description">CSV and PDF for every report</span> </span> <span class="list-end">Apr 27</span> </li> </ol> <p class="sortable-status" role="status"></p> </div></template>#What the stylesheet draws
- The handle. A button that holds a grip icon, quiet until the pointer or the keys reach it, with a closed-hand cursor. It is 24 by 28px and reaches 44px on a touch screen without moving the grip. It takes
touch-action: none, so a drag that starts on it never scrolls the page, and the rest of the item still does. - The carried item lifts: a raised sheet grows around it a little past its edges, so its content does not move, with the lifted shadow and a 1.5% swell. It follows the pointer through
--sortable-offset, which the behavior writes, with no easing between the pointer and the item. A rule the row draws under itself, such as a list's hairline, stays with the row it parted. - Making room. The items between the carried one's place and where it would land glide aside,
sortable-item-backtoward the start andsortable-item-forwardtoward the end, by--sortable-size: the carried item's size with the gap after it, measured as it is picked up. The gap that opens is where it lands. Its own place stays empty, so the reader sees where it came from. - The drop. The items that made room already stand where they land, so the move takes the room away and puts the node in the gap in one change. Inside a view transition, each item its own picture through
view-transition-name: match-element, only the carried item travels, from where it was released to its place. The root saysaria-busy="true"while that happens, and nothing else glides.
Without view transitions the carried item takes its place at once; the others are already there. With reduced motion there is no swell, no glide and no transition.
#Cards
Anything that holds items can be sorted. Here each item is a card in a stack with a gap, which the room the cards make includes; the sheet a carried card lifts on takes the card's corner through --sortable-radius.
Cards
<div class="max-w(360) mx-auto"> <ol class="list sortable gap-xs" aria-label="Release checklist" style="--sortable-radius: var(--radius-surface)"> <li class="card sortable-item flex items-center gap-sm p-sm" data-value="freeze"> <button type="button" class="sortable-handle" aria-pressed="false" aria-label="Move Freeze the branch"> <svg class="icon" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"> <circle cx="6" cy="4" r="1" fill="currentColor" stroke="none" /><circle cx="10" cy="4" r="1" fill="currentColor" stroke="none" /><circle cx="6" cy="8" r="1" fill="currentColor" stroke="none" /><circle cx="10" cy="8" r="1" fill="currentColor" stroke="none" /><circle cx="6" cy="12" r="1" fill="currentColor" stroke="none" /><circle cx="10" cy="12" r="1" fill="currentColor" stroke="none" /> </svg> </button> <span class="text-sm fg-text-strong">Freeze the branch</span> </li> <li class="card sortable-item flex items-center gap-sm p-sm" data-value="notes"> <button type="button" class="sortable-handle" aria-pressed="false" aria-label="Move Write the release notes"> <svg class="icon" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"> <circle cx="6" cy="4" r="1" fill="currentColor" stroke="none" /><circle cx="10" cy="4" r="1" fill="currentColor" stroke="none" /><circle cx="6" cy="8" r="1" fill="currentColor" stroke="none" /><circle cx="10" cy="8" r="1" fill="currentColor" stroke="none" /><circle cx="6" cy="12" r="1" fill="currentColor" stroke="none" /><circle cx="10" cy="12" r="1" fill="currentColor" stroke="none" /> </svg> </button> <span class="text-sm fg-text-strong">Write the release notes</span> </li> <li class="card sortable-item flex items-center gap-sm p-sm" data-value="staging"> <button type="button" class="sortable-handle" aria-pressed="false" aria-label="Move Deploy to staging"> <svg class="icon" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"> <circle cx="6" cy="4" r="1" fill="currentColor" stroke="none" /><circle cx="10" cy="4" r="1" fill="currentColor" stroke="none" /><circle cx="6" cy="8" r="1" fill="currentColor" stroke="none" /><circle cx="10" cy="8" r="1" fill="currentColor" stroke="none" /><circle cx="6" cy="12" r="1" fill="currentColor" stroke="none" /><circle cx="10" cy="12" r="1" fill="currentColor" stroke="none" /> </svg> </button> <span class="text-sm fg-text-strong">Deploy to staging</span> </li> <li class="card sortable-item flex items-center gap-sm p-sm" data-value="announce"> <button type="button" class="sortable-handle" aria-pressed="false" aria-label="Move Announce the release"> <svg class="icon" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"> <circle cx="6" cy="4" r="1" fill="currentColor" stroke="none" /><circle cx="10" cy="4" r="1" fill="currentColor" stroke="none" /><circle cx="6" cy="8" r="1" fill="currentColor" stroke="none" /><circle cx="10" cy="8" r="1" fill="currentColor" stroke="none" /><circle cx="6" cy="12" r="1" fill="currentColor" stroke="none" /><circle cx="10" cy="12" r="1" fill="currentColor" stroke="none" /> </svg> </button> <span class="text-sm fg-text-strong">Announce the release</span> </li> </ol> <p class="sortable-status" role="status"></p></div><script type="module"> import { attach } from '@master/ui/behaviors' attach()</script>import { useEffect, useRef } from 'react'import { attach } from '@master/ui/behaviors'import GripVerticalIcon from '@master/icons-react/native/16/soft/grip-vertical'export function Example() { const root = useRef<HTMLDivElement>(null) useEffect(() => attach(root.current!), []) return ( <div ref={root} className="max-w(360) mx-auto"> <ol className="list sortable gap-xs" aria-label="Release checklist" style={{ '--sortable-radius': 'var(--radius-surface)' } as React.CSSProperties}> <li className="card sortable-item flex items-center gap-sm p-sm" data-value="freeze"> <button type="button" className="sortable-handle" aria-pressed="false" aria-label="Move Freeze the branch"> <GripVerticalIcon className="icon" /> </button> <span className="text-sm fg-text-strong">Freeze the branch</span> </li> <li className="card sortable-item flex items-center gap-sm p-sm" data-value="notes"> <button type="button" className="sortable-handle" aria-pressed="false" aria-label="Move Write the release notes"> <GripVerticalIcon className="icon" /> </button> <span className="text-sm fg-text-strong">Write the release notes</span> </li> <li className="card sortable-item flex items-center gap-sm p-sm" data-value="staging"> <button type="button" className="sortable-handle" aria-pressed="false" aria-label="Move Deploy to staging"> <GripVerticalIcon className="icon" /> </button> <span className="text-sm fg-text-strong">Deploy to staging</span> </li> <li className="card sortable-item flex items-center gap-sm p-sm" data-value="announce"> <button type="button" className="sortable-handle" aria-pressed="false" aria-label="Move Announce the release"> <GripVerticalIcon className="icon" /> </button> <span className="text-sm fg-text-strong">Announce the release</span> </li> </ol> <p className="sortable-status" role="status"></p> </div> )}<script> import { attach } from '@master/ui/behaviors' import GripVerticalIcon from '@master/icons-svelte/native/16/soft/grip-vertical'</script><div class="max-w(360) mx-auto" {@attach attach}> <ol class="list sortable gap-xs" aria-label="Release checklist" style="--sortable-radius: var(--radius-surface)"> <li class="card sortable-item flex items-center gap-sm p-sm" data-value="freeze"> <button type="button" class="sortable-handle" aria-pressed="false" aria-label="Move Freeze the branch"> <GripVerticalIcon class="icon" /> </button> <span class="text-sm fg-text-strong">Freeze the branch</span> </li> <li class="card sortable-item flex items-center gap-sm p-sm" data-value="notes"> <button type="button" class="sortable-handle" aria-pressed="false" aria-label="Move Write the release notes"> <GripVerticalIcon class="icon" /> </button> <span class="text-sm fg-text-strong">Write the release notes</span> </li> <li class="card sortable-item flex items-center gap-sm p-sm" data-value="staging"> <button type="button" class="sortable-handle" aria-pressed="false" aria-label="Move Deploy to staging"> <GripVerticalIcon class="icon" /> </button> <span class="text-sm fg-text-strong">Deploy to staging</span> </li> <li class="card sortable-item flex items-center gap-sm p-sm" data-value="announce"> <button type="button" class="sortable-handle" aria-pressed="false" aria-label="Move Announce the release"> <GripVerticalIcon class="icon" /> </button> <span class="text-sm fg-text-strong">Announce the release</span> </li> </ol> <p class="sortable-status" role="status"></p></div><script setup>import { onMounted, onUnmounted, ref } from 'vue'import { attach } from '@master/ui/behaviors'import GripVerticalIcon from '@master/icons-vue/native/16/soft/grip-vertical'const root = ref(null)let detachonMounted(() => { detach = attach(root.value) })onUnmounted(() => detach?.())</script><template> <div ref="root" class="max-w(360) mx-auto"> <ol class="list sortable gap-xs" aria-label="Release checklist" style="--sortable-radius: var(--radius-surface)"> <li class="card sortable-item flex items-center gap-sm p-sm" data-value="freeze"> <button type="button" class="sortable-handle" aria-pressed="false" aria-label="Move Freeze the branch"> <GripVerticalIcon class="icon" /> </button> <span class="text-sm fg-text-strong">Freeze the branch</span> </li> <li class="card sortable-item flex items-center gap-sm p-sm" data-value="notes"> <button type="button" class="sortable-handle" aria-pressed="false" aria-label="Move Write the release notes"> <GripVerticalIcon class="icon" /> </button> <span class="text-sm fg-text-strong">Write the release notes</span> </li> <li class="card sortable-item flex items-center gap-sm p-sm" data-value="staging"> <button type="button" class="sortable-handle" aria-pressed="false" aria-label="Move Deploy to staging"> <GripVerticalIcon class="icon" /> </button> <span class="text-sm fg-text-strong">Deploy to staging</span> </li> <li class="card sortable-item flex items-center gap-sm p-sm" data-value="announce"> <button type="button" class="sortable-handle" aria-pressed="false" aria-label="Move Announce the release"> <GripVerticalIcon class="icon" /> </button> <span class="text-sm fg-text-strong">Announce the release</span> </li> </ol> <p class="sortable-status" role="status"></p> </div></template>#The behavior
sortable takes the root, the element with the sortable class, and returns the function that detaches it.
import { sortable } from '@master/ui/behaviors/sortable.js'const priorities = sortable(document.querySelector('.sortable'))priorities.value // ['onboarding', 'billing', 'search', 'exports']- The pointer. A press on a handle that moves more than 4px picks the item up; less is a press. The handle says
aria-pressed="true"while it is carried, the item follows the pointer and the items it passes make room. Where it would land is read from where the items stand in the layout, not from the room they are making, so the place never chases itself. The release moves it, and Escape puts it back while the others glide home. A list that scrolls is scrolled while the pointer is near one of its ends, faster the further in it is. Only the pointer that started the drag moves it. - Without a drag. A press on a handle that does not move, a click or a tap, picks the item up as Enter does and focuses its handle. A press on another item's handle moves it to that place and drops it; a press on its own handle drops it where it is. A pointer that cannot drag, and a screen reader's double tap, sort the list this way.
- The keys. Enter or Space on a handle picks the item up. The arrow keys along the list move it one place at a time; they do nothing until an item is picked up, so a screen reader's own arrow keys keep working. Enter or Space drops it, Escape puts it back where it was picked up, and leaving the handle drops it.
- What it says. Each pick, move, drop and cancel is said in the
sortable-statusregion, arole="status"inside the root or right after it, with the item's name and its place: "Billing, position 3 of 4." Passmessagesto say it in the page's language. - The move is the item node itself moving, with
moveBefore()where the browser has it, so what the item holds keeps its state: a field's text, a playing video, focus. Elsewhere it is inserted again and its handle is focused again. - Events. The root dispatches
inputandchangeafter a drop that changed the order. The function it returns has avalue, the order of the items, each one'sdata-valueor its text. Set it to put them in that order; that dispatches nothing.
sortable-row turns the list across: the offset is horizontal, the landing line stands upright, and ArrowLeft and ArrowRight move, the other way around from right to left.
Without the behavior the list keeps its order and stays readable.
A sortable is one list. Where an item also moves between lists, such as the stages of a board, use a kanban.
#Keyboard
| Keys | Action |
|---|---|
| Enter, Space | On a handle, pick the item up; again, drop it where it is |
| ArrowUp, ArrowDown | Move the item that is picked up one place; ArrowLeft and ArrowRight in sortable-row, the other way around from right to left |
| Home, End | Move the item that is picked up to the first or the last place |
| Escape | Put the item back where it was picked up, or where a drag started |
| Tab | Leave the handle, which drops the item |
#Accessibility
- Name each handle with what it moves: "Move Billing". The handle is the item's only control for its order, and dragging is never the only way: a press picks an item up and a press on another handle puts it there, and every move the pointer makes, the keys make too.
- Keep the status region in the page from the start, empty: a region added at the moment it speaks is often not heard.
- When the order matters to what the page does, such as priority or the steps of a workflow, say so near the list, and save the new order the way the page saves anything else.
#React, Svelte and Vue
The sortable component writes the items in the order it holds, attaches the behavior, and moves an item in its own list when the behavior asks, so the framework's rows and the page agree. Copy it into the project with npx master-ui add sortable; React, Svelte and Vue says how the components are built and used.
#Classes
| Class | Description |
|---|---|
sortable | What holds the items: a list or a stack. While an item is carried the cursor is a closed hand |
sortable-row | The items run across: the offset and the room the items make are horizontalApply with sortable |
sortable-item | An item. It stands for its data-value, or its text. Carried, it lifts on a raised sheet a little past its edges and follows the pointer |
sortable-item-back | Set by the behavior during a drag on the items between the carried one's place and where it would land, when it is carried toward the end: each glides toward the start by --sortable-sizeApply with sortable-item |
sortable-item-forward | Set by the behavior during a drag when the carried item goes toward the start: each item it passes glides toward the end by --sortable-sizeApply with sortable-item |
sortable-handle | The button that carries the item, holding a grip icon: name it with what it moves. A 24 by 28px target, 44px on touch screens |
sortable-status | A region that says each pick, move and drop, inside the root or right after it. It takes no room |
#Custom properties
| Property | Default | Description |
|---|---|---|
--sortable-offset | How far the carried item is from its place along the list; the script writes it | |
--sortable-size | On the root during a drag: the carried item's size with the gap after it, the distance the others make room by; the script writes it | |
--sortable-radius | var(--radius-control) | The corner of the sheet a carried item lifts on |
#Related
- ListRows of things with media, words and something at the end.
- CardA raised sheet whose parts follow the room the card is given.
- SwipeA row that slides aside to show what can be done to it.
- KanbanA board of columns whose items are moved within a column or into another, by dragging, with the keys or by choosing a place.