Skip to content
SortableAPI Reference

createSortableScope · Core

comins-sortable@0.1.6Framework neutral

Shared area registration, option updates, cancellation, teardown, and consumer commit ownership.

import { createSortableScope, type SortableScope } from "comins-sortable/core";
export declare function createSortableScope(options?: SortableScopeOptions): SortableScope;

options defaults to an empty object. Creation does not create a DOM area; registering a browser element connects the environment. This page covers the /core entry. The same-named Svelte API is documented separately in the Svelte guide.

Core is the low-level contract that reports drag proposals. Use framework adapters for controlled framework state or createSortable for automatic DOM ordering in plain JS. The Core example below explicitly applies the proposed single-list order to DOM to demonstrate consumer responsibility.

export interface SortableScopeOptions {
onBeforeDragStart?: (context: DragContext) => boolean | void;
onDragStart?: (context: DragContext) => void;
onDrag?: (context: DragContext) => void;
onInsertDragArea?: (event: InsertDragAreaEvent) => void;
onChange?: (change: SortableChange) => void;
onAfterDrag?: (result: AfterDragResult) => void;
onError?: (error: unknown) => void;
}
Callback Timing and return contract
onBeforeDragStart Runs before activation. Return false to refuse the start; this path does not enter an active drag’s afterDrag cycle.
onDragStart Receives DragContext when activation starts. Its return value is not a state update instruction.
onDrag Receives context with pointer and destination information during a drag.
onInsertDragArea Receives changes to the insertion area during dragging.
onChange Receives a proposed reorder, transfer, copy, or swap. Core consumers must commit it to DOM or controlled state.
onAfterDrag Receives status, reason, and optionally change after cleanup. Check for dropped to identify completion.
onError Receives errors reported during callback/session handling. Synchronous registration and option errors do not all flow through this callback.

All callbacks are optional. Except for onBeforeDragStart, their return type is void. An onChange call alone is not success: an uncommitted state or DOM proposal can be rejected with state-not-committed.

registerArea(element: Element, options: SortableAreaOptions): () => void;

Registers a DOM Element and area options, returning a function that unregisters that area only. Core does not accept a selector string. areaId must be unique in the scope and item IDs must remain stable. The cleanup function is repeat-safe; unregistering an active source or destination ends the drag.

updateArea(areaId: string, patch: SortableAreaPatch): 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;

Invalidates geometry after an area’s DOM changes and plays the configured layout animation. Does not reorder data or register a new area. This method is optional on the public SortableScope type; call scope.refreshArea?.(id). 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 the owned scope, cleaning up drags, area listeners, frames, animations, and temporary feedback. Repeated calls are allowed. Create a new scope instead of registering on a destroyed one. Returns void.

Required area options are areaId and the item selector. Sortable items are matching direct children of the area. The default getItemId reads data-sortable-id. Defaults are direction: "auto", activationDistance: 4, and emptyInsertThreshold: 8. See core concepts and advanced sorting for more policies.

Situation Result
updateArea / refreshArea before initial registration SortableError, INVALID_ELEMENT
Updating a missing or unregistered area ID INVALID_ELEMENT
Registering an area on a destroyed scope INVALID_OPTION
Invalid options / selector Option validation error; handle it at the synchronous call site.

Destroy a scope you own when the screen/component is removed. If you own only an area in a shared scope, call the cleanup returned by registerArea. See lifecycle and errors for termination states.

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