Command palette

@yasinsensoy/web

Web · Components

SortableList

List sorted by drag and drop: to-dos, menu items, ordered options, gallery order or form rows (FieldArray sortable).

8 exports

Live example

Preview

  • Finish the designDesign
  • Write the API endpointsBackend
  • Finish the testsTest
  • Update the docsDocs
  • Publish the releaseRelease

Code

import { SortableList } from "@yasinsensoy/web";

const [tasks, setTasks] = useState(list);

<SortableList items={tasks} onReorder={setTasks}>
  {(gorev) => (
    <div className="flex items-center gap-2 rounded-lg border bg-card p-3">
      {gorev.label}
    </div>
  )}
</SortableList>

Events

  1. 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

import { SortableList, SortableHandle, moveItem } from "@yasinsensoy/web";

SortableList

componentClient component

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, horizontal and grid layouts; items of different heights.

For moving between boards use Kanban, for trees TreeView.

SortableList({ items: itemsProp, defaultItems, onReorder, onMove, getKey, getLabel, children, orientation, handle, disabled, isItemDisabled, autoScroll, as, itemClassName, onDragStart, onDragEnd, className, ref, onPointerDown: onPointerDownProp, onKeyDown: onKeyDownProp, ...props }: SortableListProps<T>): JSX.Element

Examples

const [tasks, setTasks] = useState([
  { id: "1", label: "Finish the design" },
  { id: "2", label: "Write the tests" },
]);

<SortableList items={tasks} onReorder={setTasks}>
  {(task) => <div className="rounded-lg border bg-card p-3">{task.label}</div>}
</SortableList>

// With a handle, grid:
<SortableList items={photos} onReorder={setPhotos} orientation="grid" handle
  className="grid-cols-3 gap-2">
  {(photo) => (
    <div className="relative">
      <img src={photo.src} alt={photo.alt} />
      <SortableHandle className="absolute end-1 top-1" />
    </div>
  )}
</SortableList>

SortableHandle

componentClient component

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.

SortableHandle({ className, children, ...props }: SortableHandleProps): JSX.Element

Examples

<SortableList items={rows} onReorder={setRows} handle>
  {(row) => (
    <div className="flex items-center gap-2">
      <SortableHandle />
      {row.label}
    </div>
  )}
</SortableList>

moveItem

function

Moves 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.

moveItem(items: readonly T[], from: number, to: number): T[]

Examples

moveItem(["a", "b", "c"], 0, 2); // ["b", "c", "a"]

Direction of SortableList.

type SortableOrientation = "vertical" | "horizontal" | "grid"

Second argument of SortableListProps.children.

NameTypeDescription
indexnumberIndex of the item in the list (from 0).
draggingfalse | "pointer" | "keyboard"This item is being moved: "pointer" mouse/touch, "keyboard" keyboard; otherwise false.
activebooleanSome item in the list is being moved.
disabledbooleanThe item can't be dragged (disabled or isItemDisabled).

Second argument of SortableListProps.onReorder.

NameTypeDescription
itemTThe moved item.
keystringKey of the moved item.
fromnumberOld index.
tonumberNew index.

Props of the SortableList component.

Extends: Omit<ComponentProps<"ul">, "children" | "onChange" | "ref" | "defaultValue" | "onDragStart" | "onDragEnd">

NameTypeDefaultDescription
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>) => voidCalled 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) => voidOnly the order change (from → to); you move the array yourself. useFieldArray's move can be passed directly.
getKey?(item: T) => string | numberPersistent 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) => stringName 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) => ReactNodeContent 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?booleanfalsetrue: 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?booleanTurns off all sorting; items are drawn as they are.
isItemDisabled?(item: T) => booleanThis item can't be dragged (others can still take its place).
autoScroll?booleantrueWhile dragging, scrolls automatically when nearing the edge of the scrollable box (or the page).
as?"ul" | "ol" | "div""ul"List element.
itemClassName?stringClass of each item (<li>).
onDragStart?(info: { item: T; key: string; index: number; }) => voidWhen a drag starts.
onDragEnd?(info: SortableMoveInfo<T> & { cancelled: boolean; }) => voidWhen a drag ends (dropped or cancelled); to equals from on cancel.
ref?Ref<HTMLUListElement>Attached to the root element (<ul>).

Props of the SortableHandle component.

Extends: Omit<ComponentProps<"button">, "type">

NameTypeDescription
children?ReactNodeContent instead of the icon. If omitted, the ⋮⋮ handle icon is drawn.