Skip to content
SortableAPI Reference

createSortable · Vanilla

comins-sortable@0.1.6Vanilla JS

Create a Vanilla DOM sorting instance and use its five lifecycle methods.

import { createSortable, type Sortable } from "comins-sortable";
export declare function createSortable(element: Element | string, options: VanillaSortableOptions): Sortable;

element is an existing browser Element or a CSS selector locating it. Required options include areaId and item. The factory immediately registers the first area and returns a Sortable instance. Initialize after DOM is ready and dispose when its owning screen is removed.

Input Contract
areaId Area ID unique within the instance; cannot be changed by an update.
item CSS selector matching sortable direct children.
getItemId Defaults to reading each item’s data-sortable-id. Supply stable string or number IDs.
copyElement Optional callback creating a DOM item in copy mode. Its ID must identify the new item too.
Area policies group, direction, disabled, handle, ignore, animation, and placeholder follow the Vanilla guide and copy guide.
Lifecycle callbacks Inherited from SortableScopeOptions.

VanillaSortableOptions combines Vanilla area options with scope callbacks. VanillaSortableAreaPatch allows partial area updates excluding the ID. This page covers creation and lifecycle; the full option dictionary remains a subsequent documentation item.

Vanilla applies DOM changes before calling onChange. If onChange throws, it rolls back. Check status === "dropped" in the later onAfterDrag event for completion; cancelled or rejected drags roll back temporary changes. Use framework adapters when React, Vue, or Svelte owns the same item DOM.

registerArea(element: Element | string, options: VanillaSortableAreaOptions): () => void;

Registers an additional DOM area in the same instance. Accepts an Element or CSS selector and returns a cleanup for that area only. Each area needs a unique areaId; all areas share the instance’s scope and drag policies.

updateArea(areaId: string, patch: VanillaSortableAreaPatch): void;

Patches the options of a registered area without changing its areaId. Omitted fields retain their values; explicitly undefined values follow option normalization. Disabling the active source or destination terminates that drag. Returns void.

refreshArea(areaId: string): void;

Refreshes geometry and configured layout animation after external code changes a registered area’s DOM. This method is required on the Vanilla interface and can be called directly. Returns void.

cancel(): void;

Cancels an active drag or a pending activation. An active drag ends with reason escape. Does nothing when idle and keeps registered areas. Returns void.

destroy(): void;

Tears down every area and active drag owned by the instance, cleaning listeners, frames, and temporary feedback. Repeated calls are allowed. This is not a reset that restores the initial order after a committed DOM change. Returns void.

A missing element or invalid element selector throws SortableError with INVALID_ELEMENT during creation. Updating or refreshing a missing area also fails. Handle synchronous setup/registration exceptions at the call site and session-reported errors through onError. Distinguish ownership of one area from ownership of the entire instance.

Use the example to disable sorting, refresh, destroy, and create a new instance. Destroy keeps DOM in place but removes drag listeners. See the Vanilla transfer example for two areas.

The npm declarations below were compared with a fixed source snapshot. The snapshot is not asserted to be the npm publishing commit.

  • comins-sortable/dist/vanilla/create-sortable.d.ts
  • comins-sortable/dist/vanilla/types.d.ts
  • npm 0.1.6 · Source snapshot