# Sortable (`<z-sortable>`)

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

- **Kind:** controller
- **Category:** data
- **Inherits:** `Controller`, `Base`

## Attributes

| Attribute | Type | Default | From | 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 | From | Description |
| --- | --- | --- | --- | --- |
| `cls` | `string` | `''` | `Base` | Adds classes to named parts as `key: classes` pairs; a plain string targets the default part. |
| `stl` | `string` | `''` | `Base` | Adds inline styles to named parts as `key: styles` pairs; a plain string targets the default part. |

## Styling Parts (`cls` / `stl`)

Default `cls` target: `custom`. Default `stl` target: `host-inner`.

| Key | Default class | cls | stl | Description |
| --- | --- | --- | --- | --- |
| `base` | `z-sortable` | ✓ | — | Class applied to the managed container (default `z-sortable`). |
| `custom` | — | ✓ | — | Extra class applied to the container; the default `cls` target for this component (empty by default). |
| `drag` | `z-sortable-drag` | ✓ | — | Class of the floating drag clone (default `z-sortable-drag`). |
| `drag-state` | `z-drag` | ✓ | — | Class applied to `<html>` while a drag is active (default `z-drag`), forcing grabbing cursors. |
| `empty` | `z-sortable-empty` | ✓ | — | Class applied to the container when it has no items (default `z-sortable-empty`). |
| `item` | `z-sortable-item` | ✓ | — | Class applied to each sortable item (default `z-sortable-item`). |
| `no-drag` | `z-sortable-nodrag` | ✓ | — | Class applied to non-draggable items (default `z-sortable-nodrag`). |
| `placeholder` | `z-sortable-placeholder` | ✓ | — | Class of the gap element left at the drag position (default `z-sortable-placeholder`). |

## Reference inventories

- [CSS Variables (3)](https://next.0build.dev/docs/components/sortable/css-vars.md)
- [CSS Classes (5)](https://next.0build.dev/docs/components/sortable/css-classes.md)

## Events

| Event | Cancelable | Detail | Description |
| --- | --- | --- | --- |
| `z-sortable:added` | — | `{ item: HTMLElement; index: number; from: Sortable; sortable: Sortable }` | 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 }` | 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[] }` | Fires after any reorder with `item`, `action` (`moved`/`added`/`removed`), indices, and the serialized `values`. |
| `z-sortable:mount` | — | `Record<string, unknown>` | Fires when deferred template content is mounted into the container. |
| `z-sortable:move` | — | `{ item: HTMLElement; sortable: this; targetSortable: Sortable }` | 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 }` | 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 }` | Fires on the origin sortable when an item leaves for another group member. |
| `z-sortable:start` | ✓ | `{ item: HTMLElement; index: number; sortable: this }` | 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 }` | Fires when a drag or keyboard session ends, whether dropped or cancelled. |

## Methods

| Method | Params | Returns | 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` | `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. |

## 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. | — |

## Notes

- Dragging uses a floating clone plus a placeholder; the original item stays in the DOM until drop.
- Near viewport edges the page auto-scrolls while dragging.
- A MutationObserver keeps classes and handles in sync when items change; the follow-up click after a drag is suppressed.

## Guides

- [Styling with cls and stl](https://next.0build.dev/docs/guides/styling.md)
- [Translating Components](https://next.0build.dev/docs/guides/i18n.md)
- [Icon Resolution](https://next.0build.dev/docs/guides/icons.md)
- [The data-source Convention](https://next.0build.dev/docs/guides/data-source.md)
- [Openers & Triggers](https://next.0build.dev/docs/guides/triggers.md)
- [Dialog Behavior](https://next.0build.dev/docs/guides/dialogs.md)
- [Utilities](https://next.0build.dev/docs/guides/utilities.md)
- [Runtime](https://next.0build.dev/docs/guides/runtime.md)

## Related

- [Filter](https://next.0build.dev/docs/components/filter.md)
