Features
- One component, many layouts. A single list, several lists side by side (a board), horizontal lists, nested
hierarchies.
- Reorder, transfer, nest and clone. Drop an item between others, into another list, inside another item, or drag
a copy out of a template list.
- Multi-selection: select several items and move them together.
- A drag that is always a reorder: nesting takes a deliberate gesture (hold Alt, or drop on the far side of a card),
so a plain drag never nests by accident. On touch a card is lifted by holding it, so the page still scrolls.
- Guard rails:
maxItems per list, a canDrop function, and a cancelable before-change event, so your app has
the last word on a move.
- Keyboard and screen readers. Space lifts the focused item, the arrow keys choose the place, Space drops it, and
the move is announced.
- Persistence built in: a
persist-request event with the new positions, and a persistKey that keeps the positions across reloads.
- Small. The component is 17 KB gzipped (19 KB with the React wrapper), against about 490 KB for the whole
@editora/ui-core catalog it comes from.
- More than one board: sortables with the same
group take each other's cards, and a list can have a height of its
own and scroll by itself.
- Your own markup, with or without React: custom cards, list headers and empty states, and a free
data field on
every item and list.
- Translatable and quiet: every text it shows or announces can be replaced (
labels), and the hint line can be hidden.
- Headless, for the markup you already have:
createSortable(element) and the useSortable() React hook add
dragging (mouse, touch, keyboard, between lists, auto-scroll) to your own list, grid or board, in 5 KB gzipped and with
no stylesheet. See Headless.
Install
npm install @editora/ui-sortable
For the React wrapper, React is a peer dependency: npm install @editora/ui-sortable react react-dom.
Quick start
Import the package once to register the element, then give it lists and items:
<script type="importmap">
{
"imports": {
"@editora/ui-sortable": "https://cdn.jsdelivr.net/npm/@editora/ui-sortable/dist/index.esm.js",
"@editora/ui-core/sortable": "https://cdn.jsdelivr.net/npm/@editora/ui-core/dist/sortable.esm.js"
}
}
</script>
<ui-sortable></ui-sortable>
<script type="module">
import '@editora/ui-sortable';
const sortable = document.querySelector('ui-sortable');
sortable.lists = [
{ id: 'backlog', label: 'Backlog' },
{ id: 'in-progress', label: 'In Progress' },
{ id: 'done', label: 'Done' }
];
sortable.items = [
{ id: 'story-1', label: 'Audit onboarding', listId: 'backlog' },
{ id: 'story-2', label: 'Ship release notes', listId: 'in-progress' },
{ id: 'story-3', label: 'Review handoff', listId: 'done' }
];
sortable.addEventListener('change', (event) => console.log('order', event.detail.items));
</script>
The element does not move your data: it fires change with the new items, and you store them. With a bundler, drop the
import map and import '@editora/ui-sortable'; without one, the import map above is all it needs.
React
import { Sortable, type SortableItem, type SortableList } from '@editora/ui-sortable/react';
const lists: SortableList[] = [
{ id: 'backlog', label: 'Backlog' },
{ id: 'done', label: 'Done' }
];
const items: SortableItem[] = [
{ id: 'story-1', label: 'Audit onboarding', listId: 'backlog' },
{ id: 'story-2', label: 'Ship release notes', listId: 'done' }
];
export default function Example() {
return (
<Sortable
lists={lists}
items={items}
dropzoneStyle="container"
onChange={(detail) => {
console.log(detail.items);
}}
/>
);
}
@editora/ui-sortable/react is the wrapper on its own, without the rest of @editora/ui-react.
A controlled board
Keep the items, and the selection, in state. This board has a template list that clones what you drag out of it, a
horizontal list, a nested item and a persistKey that remembers the positions:
import * as React from 'react';
import { Sortable, type SortableItem, type SortableList } from '@editora/ui-sortable/react';
const lists: SortableList[] = [
{ id: 'templates', label: 'Templates', cloneOnDrag: true },
{ id: 'backlog', label: 'Backlog' },
{ id: 'active', label: 'In Progress' },
{ id: 'done', label: 'Done', orientation: 'horizontal' }
];
const initialItems: SortableItem[] = [
{ id: 'template-brief', label: 'Launch brief template', listId: 'templates', cloneOnDrag: true },
{ id: 'epic', label: 'Release epic', listId: 'backlog' },
{ id: 'brief', label: 'Draft launch brief', listId: 'backlog', parentId: 'epic' },
{ id: 'review', label: 'Design review', listId: 'active' }
];
export default function ReleaseBoard() {
const [items, setItems] = React.useState(initialItems);
const [selection, setSelection] = React.useState<string[]>(['epic']);
return (
<Sortable
lists={lists}
items={items}
selection={selection}
persistKey="release-board"
onItemsChange={setItems}
onSelectionChange={(detail) => setSelection(detail.selection)}
onPersistRequest={(detail) => {
console.log(detail.persistence.records);
}}
/>
);
}
A flat list
For a single list that only reorders, turn nesting off:
import * as React from 'react';
import { Sortable, type SortableItem, type SortableList } from '@editora/ui-sortable/react';
const lists: SortableList[] = [
{ id: 'queue', label: 'Editorial Queue' }
];
const initialItems: SortableItem[] = [
{ id: 'headline', label: 'Headline polish', listId: 'queue' },
{ id: 'legal', label: 'Legal review', listId: 'queue' },
{ id: 'seo', label: 'SEO metadata', listId: 'queue' }
];
export default function FlatSortableQueue() {
const [items, setItems] = React.useState(initialItems);
return (
<Sortable
lists={lists}
items={items}
allowNesting={false}
onItemsChange={setItems}
/>
);
}
Your own markup
renderItem, renderListHeader and renderEmptyState replace the default item, list header and empty message. The
context says whether the item is selected, how many items a list holds, and so on:
import * as React from 'react';
import { Sortable, type SortableItem, type SortableList } from '@editora/ui-sortable/react';
const lists: SortableList[] = [
{ id: 'todo', label: 'To do' },
{ id: 'done', label: 'Done' }
];
const initialItems: SortableItem[] = [
{ id: 'brief', label: 'Write the brief', description: 'One page, with the audience.', listId: 'todo' },
{ id: 'review', label: 'Legal review', listId: 'todo' }
];
export default function CustomRendering() {
const [items, setItems] = React.useState(initialItems);
const [selection, setSelection] = React.useState<string[]>(['brief']);
return (
<Sortable
lists={lists}
items={items}
selection={selection}
onItemsChange={setItems}
onSelectionChange={(detail) => setSelection(detail.selection)}
renderItem={(item, context) => (
<div style={{ display: 'grid', gap: 8 }}>
<div style={{ display: 'flex', justifyContent: 'space-between', gap: 12 }}>
<strong>{item.label}</strong>
<span>{context.selected ? 'Selected' : 'Ready'}</span>
</div>
{item.description && <div>{item.description}</div>}
<small>{context.list?.label}</small>
</div>
)}
renderListHeader={(list, context) => (
<div style={{ display: 'flex', justifyContent: 'space-between', gap: 12 }}>
<strong>{list.label}</strong>
<span>{context.itemCount} items</span>
</div>
)}
renderEmptyState={(list) => (
<div>
No items in {list.label} yet. Drag something here to get started.
</div>
)}
/>
);
}
Refusing moves
A list can hold a limited number of items (maxItems, counted at every nesting level), canDrop decides whether items
may go somewhere, and onBeforeChange is the last word before anything changes. A place that is refused is never
highlighted, the keyboard skips it, and the card you are holding turns red over it:
import * as React from 'react';
import { Sortable, type SortableItem, type SortableList } from '@editora/ui-sortable/react';
type Task = { reviewed: boolean };
const lists: SortableList[] = [
{ id: 'todo', label: 'To do' },
{ id: 'doing', label: 'Doing', maxItems: 2 },
{ id: 'done', label: 'Done' }
];
const initialItems: SortableItem[] = [
{ id: 'brief', label: 'Write the brief', listId: 'todo', data: { reviewed: true } },
{ id: 'budget', label: 'Approve the budget', listId: 'todo', data: { reviewed: false } },
{ id: 'launch', label: 'Plan the launch', listId: 'todo', data: { reviewed: true } },
{ id: 'assets', label: 'Collect the assets', listId: 'doing', data: { reviewed: true } }
];
export default function GuardedBoard() {
const [items, setItems] = React.useState(initialItems);
return (
<Sortable
lists={lists}
items={items}
allowNesting={false}
// Only reviewed work goes to Done: over it the lifted card turns red, and a drop does nothing.
canDrop={(drop) =>
drop.targetListId !== 'done' || drop.items.every((item) => (item.data as Task).reviewed)
}
// The last word before anything changes: return false to cancel (here, at most two cards at a time).
onBeforeChange={(detail) => detail.movedIds.length <= 2}
onItemsChange={setItems}
/>
);
}
before-change is a cancelable event (detail is what change will report, plus target and clone):
event.preventDefault() cancels the move, puts a dragged card back, and fires no change. moveSelection() returns
false when a move did not happen.
Without React
renderItem, renderListHeader and renderEmptyState are properties of the element too. Each returns a new DOM node, a
string of HTML (escape anything that came from a user), or nothing to keep the built-in markup. A card is drawn again when
its data changes and when it is selected or deselected:
<script type="importmap">
{
"imports": {
"@editora/ui-sortable": "https://cdn.jsdelivr.net/npm/@editora/ui-sortable/dist/index.esm.js",
"@editora/ui-core/sortable": "https://cdn.jsdelivr.net/npm/@editora/ui-core/dist/sortable.esm.js"
}
}
</script>
<ui-sortable></ui-sortable>
<script type="module">
import '@editora/ui-sortable';
const board = document.querySelector('ui-sortable');
board.lists = [
{ id: 'todo', label: 'To do', maxItems: 3 },
{ id: 'done', label: 'Done' }
];
board.items = [
{ id: 'acme', label: 'Invoice Acme', listId: 'todo', data: { amount: 1200 } },
{ id: 'globex', label: 'Quote Globex', listId: 'todo', data: { amount: 450 } },
{ id: 'initech', label: 'Chase Initech', listId: 'done', data: { amount: 80 } }
];
// Draw each card yourself. Return a new DOM node (or a string of HTML you have escaped).
board.renderItem = (item, { selected }) => {
const row = document.createElement('div');
row.style.cssText = 'display: flex; justify-content: space-between; gap: 12px';
const name = document.createElement('strong');
name.textContent = item.label;
const amount = document.createElement('span');
amount.textContent = `$${item.data.amount}${selected ? ' (selected)' : ''}`;
row.append(name, amount);
return row;
};
// Say no before a move happens: nothing over $1,000 goes to Done.
board.canDrop = ({ targetListId, items }) =>
targetListId !== 'done' || items.every((item) => item.data.amount <= 1000);
board.addEventListener('change', (event) => console.log('moved', event.detail.movedIds));
</script>
Two boards
Give two sortables the same group and a card held over one of them can be dropped in the other. Each one reports its own
change (the one it left with external.direction === 'out', the one it reached with 'in'), so each keeps its own state;
onBeforeChange on either can cancel the move, and the other one's canDrop and maxItems apply:
import * as React from 'react';
import { Sortable, type SortableItem, type SortableList } from '@editora/ui-sortable/react';
const backlogLists: SortableList[] = [{ id: 'backlog', label: 'Backlog' }];
const sprintLists: SortableList[] = [{ id: 'sprint', label: 'This sprint', maxItems: 3, maxHeight: 280 }];
export default function TwoBoards() {
const [backlog, setBacklog] = React.useState<SortableItem[]>([
{ id: 'audit', label: 'Audit onboarding', listId: 'backlog' },
{ id: 'notes', label: 'Ship release notes', listId: 'backlog' },
{ id: 'handoff', label: 'Review handoff', listId: 'backlog' }
]);
const [sprint, setSprint] = React.useState<SortableItem[]>([
{ id: 'login', label: 'Fix login', listId: 'sprint' },
{ id: 'docs', label: 'Write docs', listId: 'sprint' }
]);
// Sortables with the same group take each other's cards. Each one reports its own change,
// so each keeps its own state.
return (
<div style={{ display: 'grid', gridTemplateColumns: '1fr 1fr', gap: 24, alignItems: 'start' }}>
<Sortable lists={backlogLists} items={backlog} group="planning" allowNesting={false} onItemsChange={setBacklog} />
<Sortable lists={sprintLists} items={sprint} group="planning" allowNesting={false} onItemsChange={setSprint} />
</div>
);
}
Ids must be unique across the group (a drop onto a board that already has one of the ids is refused; a copy gets a fresh
id there). Dragging between boards is for the pointer: for the keyboard and for code there is
moveSelectionTo(otherSortable, { targetListId, beforeId?, parentId?, clone? }).
Headless: your own markup
When you already render the list yourself (your components, your CSS, your state), use the headless entry instead of the
component. It adds dragging to the children of an element, and nothing else: no markup, no stylesheet, no ui-core. It is
5 KB gzipped (the component is 24 KB).
<ul id="tasks" aria-label="Tasks">
<li data-id="brief">Write the brief</li>
<li data-id="copy">Review the copy</li>
<li data-id="ship">Ship it</li>
</ul>
<style>
#tasks { list-style: none; padding: 0; width: 280px; font: 15px system-ui, sans-serif; }
#tasks li { padding: 10px 14px; margin: 6px 0; border: 1px solid #d0d7de; border-radius: 8px; background: #fff; cursor: grab; }
</style>
<script type="module">
import { createSortable } from '@editora/ui-sortable/headless';
// Your own list: drag an item, or focus one and press Space, then the arrow keys.
const list = createSortable(document.getElementById('tasks'), {
onSort: () => console.log('order', list.toArray().join(', '))
});
</script>
A drag follows the pointer with a copy of the item while the others slide aside; Escape puts it back. On touch, an item
lifts after a short press, so a swipe still scrolls the page. From the keyboard, Space picks up the focused item, the
arrow keys move it (Up and Down in a column, Left and Right in a row, all four in a grid), Home and End jump, Space drops
and Escape cancels; each step is announced to screen readers. Near the edge of a scrolling list or of the window, the
list scrolls. A click on an item is still a click, and a press in a field or a link inside it is left alone.
In React
useSortable gives you a ref for the list element and calls onChange with the reordered array. React keeps
rendering the list: after a drop the elements go back where React put them, and your state decides the order.