datacontrollerz-sortable

Sortable

Drag-and-drop reordering for lists, grids, and tables, with handles, cross-list groups, keyboard grabbing, and animated moves.

Attributes

AttributeTypeDefaultDescription
targetstring''Selector of the sortable container inside the host. Defaults to [data-sortable-target], a single ul/ol/table child (tables use their first tbody), or the host itself.
groupstring''Shared group name; items can be dragged between sortables with the same non-empty group.
handlestring''Selector for drag handles inside each item. Defaults to [data-sortable-handle]; with no handles the whole item is draggable.
thresholdnumber5Pointer movement in pixels before a drag starts (default 5), so clicks on items still work.
durationnumber150Move animation duration in milliseconds (default 150).
no-animationbooleanfalseDisables move animations.
disabledbooleanfalseDisables dragging and keyboard grabbing; cancels any active session.
Inherited from Base
AttributeTypeDefaultDescription
clsstring''Adds classes to named parts as key: classes pairs; a plain string targets the default part.
stlstring''Adds inline styles to named parts as key: styles pairs; a plain string targets the default part.

Custom Styling (cls / stl)

Default cls target:custom · defaultstl target:host-inner

KeyDefault classclsstlDescription
basez-sortableyesnoClass applied to the managed container (default z-sortable).
custom—yesnoExtra class applied to the container; the default cls target for this component (empty by default).
dragz-sortable-dragyesnoClass of the floating drag clone (default z-sortable-drag).
drag-statez-dragyesnoClass applied to <html> while a drag is active (default z-drag), forcing grabbing cursors.
emptyz-sortable-emptyyesnoClass applied to the container when it has no items (default z-sortable-empty).
itemz-sortable-itemyesnoClass applied to each sortable item (default z-sortable-item).
no-dragz-sortable-nodragyesnoClass applied to non-draggable items (default z-sortable-nodrag).
placeholderz-sortable-placeholderyesnoClass of the gap element left at the drag position (default z-sortable-placeholder).

CSS Variables

VariableDefaultScopeDescription
--z-sortable-drag-z-index1050.z-sortable-dragStacking order of the floating drag clone.
--z-sortable-empty-min-height3.5rem.z-sortable-emptyMinimum height reserved by an empty container as a drop target.
--z-sortable-placeholder-opacity0.z-sortable-placeholderOpacity of the drop-position placeholder.

Events

EventDetailFlagsDescription
z-sortable:added{ item: HTMLElement; index: number; from: Sortable; sortable: Sortable }bubblesFires on the receiving sortable when an item arrives from another group member. Detail includes the item and its new index.
z-sortable:cancel{ item: HTMLElement; sortable: this }bubblesFires when a session is cancelled (Escape or a vetoed start) and the item returns to its origin.
z-sortable:change{ item: HTMLElement; action: "moved"; index: number; previousIndex: number; values: string[] }bubblesFires after any reorder with item, action (moved/added/removed), indices, and the serialized values.
z-sortable:mount · from ControllerRecord<string, unknown>bubblesFires when deferred template content is mounted into the container.
z-sortable:move{ item: HTMLElement; sortable: this; targetSortable: Sortable }bubblesFires while the dragged item hovers a new position (and on keyboard moves). Detail includes the item and target index.
z-sortable:moved{ item: HTMLElement; index: number; previousIndex: number; sortable: this }bubblesFires after an item is moved (drag drop, keyboard drop, or move()). Detail: item, index, previousIndex, sortable.
z-sortable:removed{ item: HTMLElement; previousIndex: number; to: Sortable; sortable: Sortable }bubblesFires on the origin sortable when an item leaves for another group member.
z-sortable:start{ item: HTMLElement; index: number; sortable: this }cancelable, bubblesCancelable; fires when a drag or keyboard grab begins. Detail: item, index, sortable. preventDefault() cancels the session.
z-sortable:stop{ item: HTMLElement; index: number; sortable: this }bubblesFires when a drag or keyboard session ends, whether dropped or cancelled.

Methods

SignatureDescription
hasAnimatingItems(): booleanReturns whether move animations are still running.
insertItemAnimated(item: HTMLElement, reference: Element | null): voidInserts an element at an index with the move animation.
items(): HTMLElement[]Returns the current item elements in DOM order.
move(fromIndex: number, toIndex: number, animate?: boolean = true): Promise<void>Moves the item at fromIndex to toIndex, optionally without animation; fires moved/change/stop.
refresh(): voidRe-syncs container/item classes, handles, and observers.
removeItemAnimated(item: HTMLElement): voidRemoves an element with the move animation.
serialize(attribute?: string = 'data-id'): string[]Returns the item order as strings: the given attribute (default data-id), falling back to data-value then id.

Data Hooks

AttributeValuesApplies toRoleDescription
data-fn · from ControllertemplatedescendantconfigMarks a deferred <template> child whose content is mounted into the container on demand.
data-sortable-handle—descendantconfigMarks a drag handle inside an item; only handles start drags when present.
data-sortable-item—descendantconfigMarks (and is auto-added to) sortable items.
data-sortable-nodrag—descendantconfigExcludes an item from dragging.
data-sortable-target—descendantconfigMarks the sortable container when target is unset.
data-value—descendantread-onlyFallback identity used by serialize() when the serialized attribute is absent.

CSS Classes

ClassRoleApplies toDescription
.z-sortablerootz-sortableRoot class on the managed container.
.z-sortable-dragstate.z-sortableThe floating clone that follows the pointer during a drag.
.z-sortable-emptystate.z-sortableState class on an empty container; reserves a minimum drop area.
.z-sortable-handleelement.z-sortableDrag-handle affordance with a grab cursor.
.z-sortable-placeholderstate.z-sortableThe gap element marking the current drop position.

Keyboard

KeysActionWhen
Space EnterGrab the focused item (or its handle); press again to drop.—
ArrowUp ArrowDown ArrowLeft ArrowRight Home EndMove the grabbed item within the list.An item is grabbed.
PageUp PageDownMove the grabbed item to the previous/next sortable in the same group (Alt+arrows also works).An item is grabbed and group is set.
EscapeCancel the grab or drag and return the item to its origin.—

Accessibility

  • Handles are kept button-accessible; keyboard grabbing moves items with live position feedback and restores focus after drops.
  • Interactive content inside items (inputs, buttons, links, editable regions) never starts a drag.