JavaScript library for reorderable drag-and-drop lists on modern browsers and touch devices
—
Core constructor, static methods, and instance methods for creating and managing sortable lists.
Creates a new Sortable instance on the specified HTML element.
/**
* Creates a sortable drag-and-drop interface on an HTML element
* @param el - HTMLElement to make sortable
* @param options - Configuration options object
*/
function Sortable(el: HTMLElement, options?: SortableOptions): Sortable;Usage Examples:
import Sortable from "sortablejs";
// Basic sortable list
const sortable = new Sortable(document.getElementById('list'), {
animation: 150,
ghostClass: 'sortable-ghost'
});
// Sortable with advanced options
const advancedSortable = new Sortable(document.getElementById('items'), {
group: 'shared',
animation: 300,
handle: '.drag-handle',
filter: '.no-drag',
onEnd: (evt) => console.log('Drag ended', evt)
});Static factory method to create a sortable instance (alternative to constructor).
/**
* Static factory method to create a sortable instance
* @param el - HTMLElement to make sortable
* @param options - Configuration options object
* @returns New Sortable instance
*/
Sortable.create(el: HTMLElement, options?: SortableOptions): Sortable;Retrieve the Sortable instance associated with an HTML element.
/**
* Get the Sortable instance associated with an element
* @param element - Element to get the instance for
* @returns Sortable instance or undefined if none exists
*/
Sortable.get(element: HTMLElement): Sortable | undefined;Register plugins with Sortable for extended functionality.
/**
* Mount plugins to extend Sortable functionality
* @param plugins - One or more plugin objects to register
*/
Sortable.mount(...plugins: SortablePlugin[]): void;Usage Examples:
import Sortable, { MultiDrag, AutoScroll } from "sortablejs";
// Mount plugins before creating sortable instances
Sortable.mount(new MultiDrag(), new AutoScroll());
// Now create sortables with plugin support
const sortable = Sortable.create(el, {
multiDrag: true,
selectedClass: 'selected'
});Current version of the Sortable library.
Sortable.version: string;Collection of utility functions for DOM manipulation and helper operations.
Sortable.utils: SortableUtils;Reference to the currently active (dragging) Sortable instance.
Sortable.active: Sortable | null;Reference to the element currently being dragged.
Sortable.dragged: HTMLElement | null;Reference to the ghost placeholder element during drag operations.
Sortable.ghost: HTMLElement | null;Reference to the cloned element (when using clone mode).
Sortable.clone: HTMLElement | null;Serializes the sortable items into an array of data-id values or indices.
/**
* Serialize the sortable's item order
* @returns Array of data-id attribute values or generated indices
*/
toArray(): string[];Usage Examples:
// HTML with data-id attributes
// <ul id="list">
// <li data-id="item-1">First</li>
// <li data-id="item-2">Second</li>
// <li data-id="item-3">Third</li>
// </ul>
const sortable = Sortable.create(document.getElementById('list'));
const order = sortable.toArray(); // ['item-1', 'item-2', 'item-3']
// Save order for later restoration
localStorage.setItem('list-order', JSON.stringify(order));Programmatically reorder the sortable items according to an array of IDs.
/**
* Reorder items according to an array
* @param order - Array of item IDs in desired order
* @param useAnimation - Whether to animate the reordering
*/
sort(order: string[], useAnimation?: boolean): void;Usage Examples:
const sortable = Sortable.create(document.getElementById('list'));
// Reorder items programmatically
sortable.sort(['item-3', 'item-1', 'item-2']);
// Reorder with animation
sortable.sort(['item-2', 'item-3', 'item-1'], true);
// Restore saved order
const savedOrder = JSON.parse(localStorage.getItem('list-order'));
if (savedOrder) {
sortable.sort(savedOrder);
}Save the current item order using the configured store option.
/**
* Save the current sorting order using the store option
*/
save(): void;Find the closest draggable ancestor element.
/**
* Find the closest draggable ancestor element
* @param el - Starting element
* @param selector - CSS selector (optional, defaults to options.draggable)
* @returns Closest draggable element or null
*/
closest(el: HTMLElement, selector?: string): HTMLElement | null;Get or set a configuration option on the sortable instance.
/**
* Get or set a configuration option
* @param name - Option name
* @param value - Option value (omit to get current value)
* @returns Current option value when getting
*/
option(name: string, value?: any): any;Usage Examples:
const sortable = Sortable.create(el);
// Get current animation duration
const currentAnimation = sortable.option('animation'); // 0
// Update animation duration
sortable.option('animation', 300);
// Disable the sortable
sortable.option('disabled', true);
// Enable it again
sortable.option('disabled', false);Clean up the sortable instance and remove all event listeners.
/**
* Destroy the sortable instance and cleanup event listeners
*/
destroy(): void;Usage Examples:
const sortable = Sortable.create(el);
// Later, when component unmounts or is no longer needed
sortable.destroy();
// The element is no longer sortable and all event listeners are removedMain event handler for drag and drop events (used internally).
/**
* Main event handler for drag and drop events
* @param evt - DOM event object
*/
handleEvent(evt: Event): void;interface SortablePlugin {
name: string;
defaults?: object;
eventOptions?: object;
initializePlugin?: (sortable: Sortable) => void;
}
interface SortableUtils {
on(el: HTMLElement, event: string, fn: Function): void;
off(el: HTMLElement, event: string, fn: Function): void;
css(el: HTMLElement, prop: string, value?: string): string | void;
find(ctx: HTMLElement, tagName: string, iterator?: Function): HTMLElement[];
is(el: HTMLElement, selector: string): boolean;
extend(dst: object, ...src: object[]): object;
throttle(fn: Function, ms: number): Function;
closest(el: HTMLElement, selector: string, ctx?: HTMLElement): HTMLElement | null;
toggleClass(el: HTMLElement, name: string, state?: boolean): void;
clone(el: HTMLElement): HTMLElement;
index(el: HTMLElement, selector?: string): number;
nextTick(fn: Function): number;
cancelNextTick(id: number): void;
detectDirection(el: HTMLElement): string;
getChild(el: HTMLElement, childNum: number, options?: object): HTMLElement;
expando: string;
}Install with Tessl CLI
npx tessl i tessl/npm-sortablejs