datacontroller
z-sortableSortable
Drag-and-drop reordering for lists, grids, and tables, with handles, cross-list groups, keyboard grabbing, and animated moves.
Attributes
| Attribute | Type | Default | Description |
|---|---|---|---|
target | string | '' | 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. |
group | string | '' | Shared group name; items can be dragged between sortables with the same non-empty group. |
handle | string | '' | Selector for drag handles inside each item. Defaults to [data-sortable-handle]; with no handles the whole item is draggable. |
threshold | number | 5 | Pointer movement in pixels before a drag starts (default 5), so clicks on items still work. |
duration | number | 150 | Move animation duration in milliseconds (default 150). |
no-animation | boolean | false | Disables move animations. |
disabled | boolean | false | Disables dragging and keyboard grabbing; cancels any active session. |
Inherited from Base
| Attribute | Type | Default | Description |
|---|---|---|---|
cls | string | '' | Adds classes to named parts as key: classes pairs; a plain string targets the default part. |
stl | string | '' | 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
| Key | Default class | cls | stl | Description |
|---|---|---|---|---|
base | z-sortable | yes | no | Class applied to the managed container (default z-sortable). |
custom | — | yes | no | Extra class applied to the container; the default cls target for this component (empty by default). |
drag | z-sortable-drag | yes | no | Class of the floating drag clone (default z-sortable-drag). |
drag-state | z-drag | yes | no | Class applied to <html> while a drag is active (default z-drag), forcing grabbing cursors. |
empty | z-sortable-empty | yes | no | Class applied to the container when it has no items (default z-sortable-empty). |
item | z-sortable-item | yes | no | Class applied to each sortable item (default z-sortable-item). |
no-drag | z-sortable-nodrag | yes | no | Class applied to non-draggable items (default z-sortable-nodrag). |
placeholder | z-sortable-placeholder | yes | no | Class of the gap element left at the drag position (default z-sortable-placeholder). |
CSS Variables
| Variable | Default | Scope | Description |
|---|---|---|---|
--z-sortable-drag-z-index | 1050 | .z-sortable-drag | Stacking order of the floating drag clone. |
--z-sortable-empty-min-height | 3.5rem | .z-sortable-empty | Minimum height reserved by an empty container as a drop target. |
--z-sortable-placeholder-opacity | 0 | .z-sortable-placeholder | Opacity of the drop-position placeholder. |
Events
| Event | Detail | Flags | Description |
|---|---|---|---|
z-sortable:added | { item: HTMLElement; index: number; from: Sortable; sortable: Sortable } | bubbles | Fires 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 } | bubbles | Fires 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[] } | bubbles | Fires after any reorder with item, action (moved/added/removed), indices, and the serialized values. |
z-sortable:mount · from Controller | Record<string, unknown> | bubbles | Fires when deferred template content is mounted into the container. |
z-sortable:move | { item: HTMLElement; sortable: this; targetSortable: Sortable } | bubbles | Fires 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 } | bubbles | Fires 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 } | bubbles | Fires on the origin sortable when an item leaves for another group member. |
z-sortable:start | { item: HTMLElement; index: number; sortable: this } | cancelable, bubbles | Cancelable; 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 } | bubbles | Fires when a drag or keyboard session ends, whether dropped or cancelled. |
Methods
| Signature | Description |
|---|---|
hasAnimatingItems(): boolean | Returns whether move animations are still running. |
insertItemAnimated(item: HTMLElement, reference: Element | null): void | Inserts 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(): void | Re-syncs container/item classes, handles, and observers. |
removeItemAnimated(item: HTMLElement): void | Removes 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
| Attribute | Values | Applies to | Role | Description |
|---|---|---|---|---|
data-fn · from Controller | template | descendant | config | Marks a deferred <template> child whose content is mounted into the container on demand. |
data-sortable-handle | — | descendant | config | Marks a drag handle inside an item; only handles start drags when present. |
data-sortable-item | — | descendant | config | Marks (and is auto-added to) sortable items. |
data-sortable-nodrag | — | descendant | config | Excludes an item from dragging. |
data-sortable-target | — | descendant | config | Marks the sortable container when target is unset. |
data-value | — | descendant | read-only | Fallback identity used by serialize() when the serialized attribute is absent. |
CSS Classes
| Class | Role | Applies to | Description |
|---|---|---|---|
.z-sortable | root | z-sortable | Root class on the managed container. |
.z-sortable-drag | state | .z-sortable | The floating clone that follows the pointer during a drag. |
.z-sortable-empty | state | .z-sortable | State class on an empty container; reserves a minimum drop area. |
.z-sortable-handle | element | .z-sortable | Drag-handle affordance with a grab cursor. |
.z-sortable-placeholder | state | .z-sortable | The gap element marking the current drop position. |
Keyboard
| Keys | Action | When |
|---|---|---|
| Space Enter | Grab the focused item (or its handle); press again to drop. | — |
| ArrowUp ArrowDown ArrowLeft ArrowRight Home End | Move the grabbed item within the list. | An item is grabbed. |
| PageUp PageDown | Move the grabbed item to the previous/next sortable in the same group (Alt+arrows also works). | An item is grabbed and group is set. |
| Escape | Cancel 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.