--- category: '@Integrations' --- # useSortable Wrapper for [`sortable`](https://github.com/SortableJS/Sortable). For more information on what options can be passed, see [`Sortable.options`](https://github.com/SortableJS/Sortable#options) in the `Sortable` documentation. ::: warning Currently, `useSortable` only implements drag-and-drop sorting for a single list. ::: ## Install ```bash npm i sortablejs@^1 ``` ## Usage ### Use template ref ```vue ``` ### Use specifies the selector to operate on ```vue ``` ### Use a selector to get the root element ```vue ``` ### Tips If you want to handle the `onUpdate` yourself, you can pass in `onUpdate` parameters, and we also exposed a function to move the item position. ```ts import { moveArrayElement, useSortable } from '@vueuse/integrations/useSortable' useSortable(el, list, { onUpdate: (e) => { // do something moveArrayElement(list, e.oldIndex, e.newIndex, e) // nextTick required here as moveArrayElement is executed in a microtask // so we need to wait until the next tick until that is finished. nextTick(() => { /* do something */ }) } }) ``` ## Type Declarations ```ts export interface UseSortableReturn { /** * start sortable instance */ start: () => void /** * destroy sortable instance */ stop: () => void /** * Options getter/setter * @param name a Sortable.Options property. * @param value a value. */ option: (( name: K, value: Sortable.Options[K], ) => void) & ((name: K) => Sortable.Options[K]) } export type UseSortableOptions = Options & ConfigurableDocument export declare function useSortable( selector: string, list: MaybeRef, options?: UseSortableOptions, ): UseSortableReturn export declare function useSortable( el: MaybeRefOrGetter, list: MaybeRef, options?: UseSortableOptions, ): UseSortableReturn /** * Inserts a element into the DOM at a given index. * @param parentElement * @param element * @param {number} index * @see https://github.com/Alfred-Skyblue/vue-draggable-plus/blob/a3829222095e1949bf2c9a20979d7b5930e66f14/src/utils/index.ts#L81C1-L94C2 */ export declare function insertNodeAt( parentElement: Element, element: Element, index: number, ): void /** * Removes a node from the DOM. * @param {Node} node * @see https://github.com/Alfred-Skyblue/vue-draggable-plus/blob/a3829222095e1949bf2c9a20979d7b5930e66f14/src/utils/index.ts#L96C1-L102C2 */ export declare function removeNode(node: Node): void export declare function moveArrayElement( list: MaybeRef, from: number, to: number, e?: Sortable.SortableEvent | null, ): void ```