Sortable list

Rows the reader puts in order, by dragging, by keyboard, or with buttons. Covers sortable-list and sortable-item. Vertical only.

Form control. It takes input in a form, so it is drawn on the Every input type block, beside every other control, as it starts and with an error where it has one.

Anatomy

┌───────────────────────────────────────┐
│ ⠿  Write the brief              ⌃  ⌄  │  ← row: handle, content, move buttons
└───────────────────────────────────────┘
┌───────────────────────────────────────┐
│ ⠿  Review the draft    ← dragged, lifted (shadow-float), follows the pointer
└───────────────────────────────────────┘
┌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┐
╎                                       ╎  ← placeholder: where it will land
└╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┘
  • The list is a ul role="list" named by label. Rows are cards (surface, border, shadow-raised, rounded-control) with a gap, so a lifted row reads as picked up from among them.
  • A row (sortable-item) holds its content, and with name a hidden input name[] with its value. Rows are moved in the DOM, so the inputs post the order shown, with or without the script.
  • The handle is a button named "Reorder" and the row's label (grabText), described by the list's instructions, aria-pressed while the row is picked up. touch-action: none on it alone, so a finger anywhere else scrolls the page. Hidden until the script runs: without it the button would do nothing.
  • The move buttons, "Move … up" and "Move … down", are the route for a reader who cannot drag (WCAG 2.5.7). Shown on the row's hover and focus, and always on a coarse pointer, which has no hover; aria-disabled at the ends, so a button that just became an end keeps focus.
  • The placeholder is a dashed box over the gap the other rows open.
  • The status region speaks each step.

States

At restAs rendered: the server's order
DraggingThe row data-dragging (lifted, no transition, following the pointer), the list root data-dragging (grabbing cursor, no text selection), the other rows translated out of the way
Picked up by keyboardThe row data-lifted (lifted, ringed), its handle aria-pressed="true"
SettlingEvery row animates from where it was drawn to its slot (FLIP), motion-safe
A disabled rowNo handle press, no buttons; other rows can pass it
A disabled listNothing moves; the hidden inputs still post

Behaviour (dsSortable)

InputResult
Press on the handle and move 4pxPicks the row up; it follows the pointer under pointer capture
With :handle="false", a mouse or pen press anywhere on the row that is not a controlThe same; touch still uses the handle
MovingA row gives way once the dragged row's leading edge passes its middle, and slides past (CSS transforms, duration-popover-enter); the placeholder follows; the nearest scrolling box scrolls within 48px of its edge
ReleaseDrops; the row settles into its slot
Escape while dragging, a cancelled pointerEverything goes back
Space or Enter on the handlePicks up; "Picked up X, position 2 of 5"
ArrowUp / ArrowDown while picked upMoves one place; "Moved to position 3 of 5"
Space or Enter while picked upDrops; "Dropped X at position 3 of 5"
Escape while picked upPuts it back; "Cancelled. X is back at position 2 of 5". Stopped, so a modal around it stays open
Focus leaving the handle while picked upDrops where it is
Move up / Move downOne place at once, announced

After any drop that changed the order, reorder is dispatched from the root, bubbling, with { order: [values], moved: value, from, to }, positions from 0. The announcements are props (pickedText, movedText, droppedText, cancelledText) with :item, :position and :total, English by default.

Livewire. Rows carry the host's wire:key; the host persists order and re-renders, and the morph moves the rows to its order. Nothing in the behaviour is held across a morph: the rows are read afresh on every press, and the handles' and buttons' visibility is bound to dsReady, so a morph that rewrites their attributes keeps them shown.

Accessibility

Rolelist and listitem; each handle a toggle button
Namelabel names the list; a handle is "Reorder X", described by instructions
KeyboardAs above. Every row is reachable without a pointer, and every move is possible without dragging
FocusStays on the handle (or the move button) through every move; the moved row is brought into view
AnnouncedEach step, politely, in the status region
MotionThe slide and the settle are transitions behind motion-safe:; with reduced motion rows jump

Out of scope

  • Moving between two lists, horizontal lists and grids.
  • Dragging several rows at once.

Help and error

Since batch 30B, help and error in the one field pattern every control shares (partials/field-message, the markup ds::input draws): the help as muted meta text, or in its place the error with its icon in the danger colour, never both. The message's id ({id}-help, {id}-error) is named by the control's aria-describedby, after anything the host passed. With neither prop the control renders exactly as before (tests/FieldMessagesUnchangedTest.php).

The list's own keyboard instructions already have {id}-help, so the field message's ids are {id}-field-help and {id}-field-error. hideLabel (default true, so 1.x-era markup is unchanged) set to false shows label above the list in the field label's style, and the list is then named by it (aria-labelledby).

Do not

  • Do not leave out value. It is what posts and what reorder reports; the position is not an identity.
  • Do not put the only copy of the order in the DOM. Persist reorder, or a re-render restores the server's order.
  • Do not make a row's whole content a drag area with :handle="false" when it holds controls a reader presses often. Presses on controls are passed through, but the row's text can no longer be selected.

From the package repo: edit this page on GitHub.

Was this page helpful?