SearchCtrlK

  • ↑↓navigate
  • ↵open
  • escclose

Can’t find it? Contact us

sortable

Why JS: dragging an item between or within lists, and moving one from the keyboard, need pointer and key listeners.

tsx
import { sortable } from "@sdxc/ui/mixins";

Drag a task, or focus one and press Shift with an arrow.

View code
tsx
<section data-sortable>
	<span hidden mix={[sortable({ moved: "Moved to {list}" })]} />
	<span data-sortable-status role="status" mix={[visuallyHidden()]} />
	{lists.map((list) => (
		<div data-sortable-list={list.value}>
			<h3 data-sortable-title>{list.title}</h3>
			<ul data-sortable-items>
				{list.tasks.map((task) => (
					<li data-sortable-item={task.id}>
						<a href={task.href}>{task.title}</a>
					</li>
				))}
			</ul>
		</div>
	))}
</section>

Usage

Moves items within and between the lists inside the nearest SORTABLE_ATTRIBUTE root. A mouse or pen press on an item that travels past a few pixels lifts it under a ghost, places it between the items under the pointer, and posts its move form on release; Escape puts it back. Shift+ArrowUp/Down move the focused item past its neighbour, and Shift+ArrowLeft/Right to the top of the list on either side. A touch press scrolls as usual.

Signature

typescript
sortable(options?: SortableOptions): MixinDescriptor<HTMLElement>

Parameters

ParameterTypeRequiredDescription
optionsSortableOptionsNo

Examples

tsx
<span hidden mix={[sortable({ moved: "Moved to {list}" })]} />

The events, constants and types its module publishes, each imported from @sdxc/ui/mixins too.

Event

SortableMoveEvent

Dispatched on an item once it is dropped or moved by key, before its move form posts; preventDefault() keeps the item in place and the form from posting, for a consumer that saves the move itself.

typescript
new SortableMoveEvent(init: { item: string; from: string; to: string; before?: string | null })
PropertyTypeRequiredDescription
itemreadonly stringYesThe moved item's value.
fromreadonly stringYesThe list it left.
toreadonly stringYesThe list it landed in, the same as from for a move within a list.
beforereadonly string | nullYesThe item it landed before, null when it landed last or its form takes no position.
Constant

SORTABLE_ATTRIBUTE

Attribute on the element holding every list the mixin moves items between.

typescript
const SORTABLE_ATTRIBUTE = "data-sortable"
Constant

SORTABLE_LIST_ATTRIBUTE

Attribute a list carries its value on, which a move sends in the item's form.

typescript
const SORTABLE_LIST_ATTRIBUTE = "data-sortable-list"
Constant

SORTABLE_ITEMS_ATTRIBUTE

Attribute on the element inside a list that holds its items; the list itself without one.

typescript
const SORTABLE_ITEMS_ATTRIBUTE = "data-sortable-items"
Constant

SORTABLE_ITEM_ATTRIBUTE

Attribute an item carries its value on, unique across the lists.

typescript
const SORTABLE_ITEM_ATTRIBUTE = "data-sortable-item"
Constant

SORTABLE_FORM_ATTRIBUTE

Attribute an item's move form names its item with, so a move finds the form to post.

typescript
const SORTABLE_FORM_ATTRIBUTE = "data-sortable-form"
Constant

SORTABLE_FIELD_ATTRIBUTE

Attribute a move form names its list field with. A move appends that field after the form's own, with the list's value, and the next render of the form drops it again.

typescript
const SORTABLE_FIELD_ATTRIBUTE = "data-sortable-field"
Constant

SORTABLE_POSITION_ATTRIBUTE

Attribute a move form names its position field with. A move sends the value of the item it lands before in that field, or an empty value when it lands last. A form without one moves its item only between lists, to the top.

typescript
const SORTABLE_POSITION_ATTRIBUTE = "data-sortable-position"
Constant

SORTABLE_COUNT_ATTRIBUTE

Attribute on a list's count, which a move between lists keeps current until the page answers.

typescript
const SORTABLE_COUNT_ATTRIBUTE = "data-sortable-count"
Constant

SORTABLE_TITLE_ATTRIBUTE

Attribute on a list's title, which a keyboard move announces.

typescript
const SORTABLE_TITLE_ATTRIBUTE = "data-sortable-title"
Constant

SORTABLE_STATUS_ATTRIBUTE

Attribute on the live region a keyboard move is announced through.

typescript
const SORTABLE_STATUS_ATTRIBUTE = "data-sortable-status"
Constant

SORTABLE_SCROLLER_ATTRIBUTE

Attribute on the element that scrolls while the pointer rests near its edge mid-drag.

typescript
const SORTABLE_SCROLLER_ATTRIBUTE = "data-sortable-scroller"
Constant

SORTABLE_ACTIVE_ATTRIBUTE

Attribute set on the root for as long as a drag lasts.

typescript
const SORTABLE_ACTIVE_ATTRIBUTE = "data-sortable-active"
Constant

SORTABLE_DRAGGING_ATTRIBUTE

Attribute set on the item being dragged, for as long as the drag lasts.

typescript
const SORTABLE_DRAGGING_ATTRIBUTE = "data-sortable-dragging"
Constant

SORTABLE_TARGET_ATTRIBUTE

Attribute set on another list while the dragged item would land in it.

typescript
const SORTABLE_TARGET_ATTRIBUTE = "data-sortable-target"
Constant

SORTABLE_GHOST_ATTRIBUTE

Attribute on the copy of the item that follows the pointer.

typescript
const SORTABLE_GHOST_ATTRIBUTE = "data-sortable-ghost"
Interface

SortableOptions

Options for sortable.

MemberTypeRequiredDescription
movedstringNoWhat a keyboard move announces, with {list} where the list's title goes, such as "Moved to {list}". Without it a keyboard move is silent.
@sdxc/ui

Can’t find it? Contact us