Web · Components
SortableList
List sorted by drag and drop: to-dos, menu items, ordered options, gallery order or form rows (FieldArray sortable).
Live example
Preview
- Finish the designDesign
- Write the API endpointsBackend
- Finish the testsTest
- Update the docsDocs
- Publish the releaseRelease
Code
Events
- Events appear here when you interact with the preview.
Grid: color order (mouse, touch, keyboard: Space + arrows)
- #ef4444
- #f59e0b
- #10b981
- #3b82f6
- #8b5cf6
- #ec4899
- #14b8a6
- #64748b
- #f97316
Order of options inside a form (SelectField options in this order)
- 1.Turkish
- 2.English
- 3.German
Import
List sorted by drag and drop: to-dos, menu items, ordered options, gallery order or form rows (FieldArray sortable).
- Mouse: grab the item (or the handle with
handle) and drag; the others slide smoothly to make room, and on drop the item glides into place. - Touch: right away from the handle; from the item itself after a long press, so page scrolling isn't broken. Near the edges the list (or the page) scrolls automatically.
- Keyboard: focus the item (or the handle), pick it up with Space, move with arrow keys, drop with Space, cancel with Esc. Alt + arrow moves one step without picking up. Each step is announced to screen readers.
- Items are moved in place (no clone): typed values of form fields inside, playing videos and focus are kept.
vertical,horizontalandgridlayouts; items of different heights.
For moving between boards use Kanban, for trees TreeView.
Examples
Drag handle of a SortableList item. When handle is passed to the list, the item is dragged only from here; on touch it drags right away without a long press and the page doesn't scroll. On the keyboard, focus the handle and pick it up with Space.
Examples
moveItem
functionMoves an item in an array from index from to index to; returns a new array (the input is not changed). SortableList onReorder and useFieldArray move use the same rule.
Examples
Direction of SortableList.
SortableItemState
interfaceSecond argument of SortableListProps.children.
| Name | Type | Description |
|---|---|---|
index | number | Index of the item in the list (from 0). |
dragging | false | "pointer" | "keyboard" | This item is being moved: "pointer" mouse/touch, "keyboard" keyboard; otherwise false. |
active | boolean | Some item in the list is being moved. |
disabled | boolean | The item can't be dragged (disabled or isItemDisabled). |
SortableMoveInfo
interfaceSecond argument of SortableListProps.onReorder.
| Name | Type | Description |
|---|---|---|
item | T | The moved item. |
key | string | Key of the moved item. |
from | number | Old index. |
to | number | New index. |
SortableListProps
interfaceProps of the SortableList component.
Extends: Omit<ComponentProps<"ul">, "children" | "onChange" | "ref" | "defaultValue" | "onDragStart" | "onDragEnd">
| Name | Type | Default | Description |
|---|---|---|---|
items? | readonly T[] | Items (controlled). When the order changes onReorder is called with the new array. | |
defaultItems? | readonly T[] | Initial items in uncontrolled use; the list keeps the order itself. | |
onReorder? | (items: T[], info: SortableMoveInfo<T>) => void | Called with the new array when the order changes. In controlled use, store the array and give it back as items. To only tell the server about the move, info (from, to, key) is enough. | |
onMove? | (from: number, to: number) => void | Only the order change (from → to); you move the array yourself. useFieldArray's move can be passed directly. | |
getKey? | (item: T) => string | number | Persistent key of the item. If omitted, item.key, otherwise item.id is used. The index can't be used as a key: items get mixed up when moved. | |
getLabel? | (item: T) => string | Name of the item in screen reader announcements and the handle's name. If omitted, one of the label, title, name fields, otherwise its index is read. | |
children | (item: T, state: SortableItemState) => ReactNode | Content of the item. The row itself is an <li>; you can put a card, a row or form fields inside. To drag only from a handle, pass handle and use SortableHandle in the content. | |
orientation? | SortableOrientation | "vertical" | Layout: vertical stacked, horizontal side by side, grid a grid (give the column count with className: grid-cols-3). Which item moves where is computed from this. |
handle? | boolean | false | true: the item is dragged only from the SortableHandle inside it; the rest of the item (text selection, scrolling by touch) works as usual. Recommended for form rows and cards with lots of interaction. false: dragged from anywhere on the item (right away with a mouse, on long press on touch); the item is focusable with Tab. |
disabled? | boolean | Turns off all sorting; items are drawn as they are. | |
isItemDisabled? | (item: T) => boolean | This item can't be dragged (others can still take its place). | |
autoScroll? | boolean | true | While dragging, scrolls automatically when nearing the edge of the scrollable box (or the page). |
as? | "ul" | "ol" | "div" | "ul" | List element. |
itemClassName? | string | Class of each item (<li>). | |
onDragStart? | (info: { item: T; key: string; index: number; }) => void | When a drag starts. | |
onDragEnd? | (info: SortableMoveInfo<T> & { cancelled: boolean; }) => void | When a drag ends (dropped or cancelled); to equals from on cancel. | |
ref? | Ref<HTMLUListElement> | Attached to the root element (<ul>). |
SortableHandleProps
interfaceProps of the SortableHandle component.
Extends: Omit<ComponentProps<"button">, "type">
| Name | Type | Description |
|---|---|---|
children? | ReactNode | Content instead of the icon. If omitted, the ⋮⋮ handle icon is drawn. |