Resortable - v2.0.0-alpha.1
    Preparing search index...

    Class SortableBeta

    Main Sortable class for creating drag-and-drop sortable lists

    This is the main entry point for the Resortable library. It provides a modern, TypeScript-first approach to creating sortable drag-and-drop interfaces.

    Unlike the original Sortable.js, this version:

    • Uses modern ES modules
    • Provides full TypeScript support
    • Implements a plugin-based architecture
    • Uses modern DOM APIs
    const sortable = new Sortable(document.getElementById('items'), {
    animation: 150,
    onEnd: (evt) => {
    console.log(`Item moved from ${evt.oldIndex} to ${evt.newIndex}`);
    }
    });
    Index

    Constructors

    • Beta

      Creates a new Sortable instance

      Parameters

      • element: HTMLElement

        The DOM element to make sortable

      • options: Partial<SortableOptions> = {}

        Configuration options for the sortable behavior

      Returns Sortable

      SortableError When the provided element is invalid or options are malformed

      // Basic usage
      const sortable = new Sortable(document.getElementById('list'));

      // With options
      const sortable = new Sortable(element, {
      animation: 150,
      ghostClass: 'ghost',
      chosenClass: 'chosen'
      });

    Properties

    active: null | Sortable = null

    The currently active Sortable instance (if any drag is in progress)

    dragged: null | HTMLElement = null

    The currently dragged element (if any)

    ghost: null | HTMLElement = null

    The ghost element shown during drag (if any)

    clone: null | HTMLElement = null

    The cloned element during clone operations (if any)

    utils: {
        on: <K extends keyof HTMLElementEventMap>(
            el: HTMLElement | Document,
            type: K,
            handler: (ev: HTMLElementEventMap[K]) => void,
        ) => () => void;
        off: <K extends keyof HTMLElementEventMap>(
            el: HTMLElement | Document,
            type: K,
            handler: (ev: HTMLElementEventMap[K]) => void,
        ) => void;
        index: (el: HTMLElement) => number;
        insertAt: (parent: HTMLElement, el: HTMLElement, index: number) => void;
        closest: (
            el: null | HTMLElement,
            selector: string,
            ctx?: HTMLElement,
        ) => null | HTMLElement;
        toggleClass: (el: HTMLElement, name: string, force?: boolean) => void;
        clone: <T extends HTMLElement>(el: T) => T;
    } = ...

    Utility functions for DOM operations

    Type Declaration

    • on: <K extends keyof HTMLElementEventMap>(
          el: HTMLElement | Document,
          type: K,
          handler: (ev: HTMLElementEventMap[K]) => void,
      ) => () => void

      Add an event listener and return an unsubscribe function

    • off: <K extends keyof HTMLElementEventMap>(
          el: HTMLElement | Document,
          type: K,
          handler: (ev: HTMLElementEventMap[K]) => void,
      ) => void

      Remove a previously-registered event listener (symmetric to on)

    • index: (el: HTMLElement) => number

      Get the index of an element within its parent

    • insertAt: (parent: HTMLElement, el: HTMLElement, index: number) => void

      Insert an element at a specific index within a parent

    • closest: (
          el: null | HTMLElement,
          selector: string,
          ctx?: HTMLElement,
      ) => null | HTMLElement

      Find the nearest ancestor matching selector, optionally bounded by ctx

    • toggleClass: (el: HTMLElement, name: string, force?: boolean) => void

      Toggle a CSS class on an element; pass force to set explicitly

    • clone: <T extends HTMLElement>(el: T) => T

      Deep-clone an element (cloneNode(true))

    element: HTMLElement

    The DOM element that this Sortable instance is bound to

    Current configuration options for this Sortable instance

    dropZone: DropZone
    dragManager: DragManager
    _selectedItems?: Set<HTMLElement>
    _lastSelected?: null | HTMLElement
    _demoSelectInstalled?: boolean
    _demoSelectClickHandler?: (event: MouseEvent) => void

    Methods

    • Beta

      Gets the Sortable instance associated with a given element

      Parameters

      • element: HTMLElement

        The DOM element to look up

      Returns undefined | Sortable

      The Sortable instance, or undefined if none found

      const sortable = Sortable.get(document.getElementById('my-list'));
      
    • Beta

      Mount (register) a plugin globally so it can be used by any Sortable instance

      Parameters

      Returns void

      import { Sortable } from 'resortable';
      import { AutoScrollPlugin } from 'resortable/plugins';

      Sortable.mount(AutoScrollPlugin.create());

      // Mount multiple plugins at once
      Sortable.mount([AutoScrollPlugin.create(), SwapPlugin.create()]);
    • Beta

      Finds the closest Sortable instance to a given element

      Parameters

      • element: HTMLElement

        The element to start searching from

      • Optionalselector: string

        Optional CSS selector to filter parent elements

      Returns null | Sortable

      The closest Sortable instance, or null if none found

      const sortable = Sortable.closest(draggedElement);
      if (sortable) {
      console.log('Found sortable:', sortable);
      }
      const sortable = Sortable.closest(element, '.sortable-container');
      
    • Beta

      Destroys the Sortable instance and removes all event listeners

      Returns void

      After calling this method, the Sortable instance should not be used. All event listeners will be removed and the element will no longer be sortable.

      const sortable = new Sortable(element);
      // ... use sortable ...
      sortable.destroy(); // Clean up when done
    • Beta

      Install a plugin on this Sortable instance

      Parameters

      • name: string

        Name of the plugin to install

      Returns void

      Error if plugin is not registered or already installed

      const sortable = new Sortable(element);
      sortable.usePlugin('AutoScroll');
    • Beta

      Uninstall a plugin from this Sortable instance

      Parameters

      • name: string

        Name of the plugin to uninstall

      Returns boolean

      true if plugin was found and uninstalled, false otherwise

      sortable.removePlugin('AutoScroll');
      
    • Beta

      Check if a plugin is installed on this instance

      Parameters

      • name: string

        Name of the plugin to check

      Returns boolean

      true if plugin is installed, false otherwise

      if (sortable.hasPlugin('AutoScroll')) {
      console.log('AutoScroll is active');
      }
    • Beta

      Get all installed plugin names for this instance

      Returns string[]

      Array of installed plugin names

      const plugins = sortable.getPlugins();
      console.log('Installed plugins:', plugins);
    • Beta

      Gets the current order of elements as an array of data-id attributes

      Returns string[]

      Array of string IDs representing the current order

      This method reads the data-id attribute from each sortable item. If an item doesn't have a data-id attribute, its index will be used. The attribute name can be customized using the dataIdAttr option.

      const sortable = new Sortable(element);
      const order = sortable.toArray();
      console.log('Current order:', order); // ['item-1', 'item-2', 'item-3']
      const sortable = new Sortable(element, { dataIdAttr: 'data-item-id' });
      const order = sortable.toArray();
    • Beta

      Sorts the elements according to the given order of data-id values

      Parameters

      • order: string[]

        Array of data-id values representing the desired order

      • useAnimation: boolean = true

        Whether to animate the reorder (default: true)

      Returns void

      const sortable = new Sortable(element);
      sortable.sort(['item-3', 'item-1', 'item-2']);
      sortable.sort(['item-3', 'item-1', 'item-2'], false);
      
    • Beta

      Save the current sort order using the configured store

      Returns void

      Calls the store.set callback with this Sortable instance if configured. This allows persisting sort order to localStorage, a database, etc.

      const sortable = new Sortable(element, {
      store: {
      get: (sortable) => {
      const stored = localStorage.getItem('sort-order');
      return stored ? JSON.parse(stored) : [];
      },
      set: (sortable) => {
      localStorage.setItem('sort-order', JSON.stringify(sortable.toArray()));
      }
      }
      });

      // Later, manually trigger a save
      sortable.save();