Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Apply VueUse composables in Vue 3/Nuxt projects to replace custom implementations with battle-tested utilities.
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
references/useSortable.md
1---2category: '@Integrations'3---45# useSortable67Wrapper for [`sortable`](https://github.com/SortableJS/Sortable).89For more information on what options can be passed, see [`Sortable.options`](https://github.com/SortableJS/Sortable#options) in the `Sortable` documentation.1011::: warning12Currently, `useSortable` only implements drag-and-drop sorting for a single list.13:::1415## Install1617```bash18npm i sortablejs@^119```2021## Usage2223### Use template ref2425```vue26<script setup lang="ts">27import { useSortable } from '@vueuse/integrations/useSortable'28import { shallowRef, useTemplateRef } from 'vue'2930const el = useTemplateRef('el')31const list = shallowRef([{ id: 1, name: 'a' }, { id: 2, name: 'b' }, { id: 3, name: 'c' }])3233useSortable(el, list)34</script>3536<template>37<div ref="el">38<div v-for="item in list" :key="item.id">39{{ item.name }}40</div>41</div>42</template>43```4445### Use specifies the selector to operate on4647```vue48<script setup lang="ts">49import { useSortable } from '@vueuse/integrations/useSortable'50import { shallowRef, useTemplateRef } from 'vue'5152const el = useTemplateRef('el')53const list = shallowRef([{ id: 1, name: 'a' }, { id: 2, name: 'b' }, { id: 3, name: 'c' }])5455const animation = 2005657const { option } = useSortable(el, list, {58handle: '.handle',59// or option set60// animation61})6263// You can use the option method to set and get the option of Sortable64option('animation', animation)65// option('animation') // 20066</script>6768<template>69<div ref="el">70<div v-for="item in list" :key="item.id">71<span>{{ item.name }}</span>72<span class="handle">*</span>73</div>74</div>75</template>76```7778### Use a selector to get the root element7980```vue81<script setup lang="ts">82import { useSortable } from '@vueuse/integrations/useSortable'83import { shallowRef } from 'vue'8485const list = shallowRef([{ id: 1, name: 'a' }, { id: 2, name: 'b' }, { id: 3, name: 'c' }])8687useSortable('#dv', list)88</script>8990<template>91<div id="dv">92<div v-for="item in list" :key="item.id">93<span>{{ item.name }}</span>94</div>95</div>96</template>97```9899### Return Values100101| Property | Description |102| -------- | ---------------------------------------------------------------- |103| `start` | Initialize the Sortable instance (called automatically on mount) |104| `stop` | Destroy the Sortable instance |105| `option` | Get or set Sortable options at runtime |106107```ts108const { start, stop, option } = useSortable(el, list)109110// Stop sorting111stop()112113// Start sorting again114start()115116// Get/set options117option('animation', 200) // set118const animation = option('animation') // get119```120121### Watch Element Changes122123Use the `watchElement` option to automatically reinitialize Sortable when the element changes (useful with `v-if`).124125```ts126import { useSortable } from '@vueuse/integrations/useSortable'127128useSortable(el, list, {129watchElement: true, // auto-reinitialize when element changes130})131```132133### Custom Update Handler134135If you want to handle the `onUpdate` yourself, you can pass in `onUpdate` parameters, and we also exposed a function to move the item position.136137```ts138import { moveArrayElement, useSortable } from '@vueuse/integrations/useSortable'139140useSortable(el, list, {141onUpdate: (e) => {142// do something143moveArrayElement(list, e.oldIndex, e.newIndex, e)144// nextTick required here as moveArrayElement is executed in a microtask145// so we need to wait until the next tick until that is finished.146nextTick(() => {147/* do something */148})149}150})151```152153### Helper Functions154155The following helper functions are also exported:156157| Function | Description |158| ------------------------------------------ | ----------------------------------------------------- |159| `moveArrayElement(list, from, to, event?)` | Move an element in an array from one index to another |160| `insertNodeAt(parent, element, index)` | Insert a DOM node at a specific index |161| `removeNode(node)` | Remove a DOM node from its parent |162163## Type Declarations164165```ts166export interface UseSortableReturn {167/**168* start sortable instance169*/170start: () => void171/**172* destroy sortable instance173*/174stop: () => void175/**176* Options getter/setter177* @param name a Sortable.Options property.178* @param value a value.179*/180option: (<K extends keyof Sortable.Options>(181name: K,182value: Sortable.Options[K],183) => void) &184(<K extends keyof Sortable.Options>(name: K) => Sortable.Options[K])185}186export interface UseSortableOptions extends Options, ConfigurableDocument {187/**188* Watch the element reference for changes and automatically reinitialize Sortable189* when the element changes.190*191* When `false` (default), Sortable is only initialized once on mount.192* You must manually call `start()` if the element reference changes.193*194* When `true`, automatically watches the element reference and reinitializes195* Sortable whenever it changes (e.g., conditional rendering with v-if).196*197* @default false198*/199watchElement?: boolean200}201export declare function useSortable<T>(202selector: string,203list: MaybeRef<T[]>,204options?: UseSortableOptions,205): UseSortableReturn206export declare function useSortable<T>(207el: MaybeRefOrGetter<MaybeElement>,208list: MaybeRef<T[]>,209options?: UseSortableOptions,210): UseSortableReturn211/**212* Inserts a element into the DOM at a given index.213* @param parentElement214* @param element215* @param {number} index216* @see https://github.com/Alfred-Skyblue/vue-draggable-plus/blob/a3829222095e1949bf2c9a20979d7b5930e66f14/src/utils/index.ts#L81C1-L94C2217*/218export declare function insertNodeAt(219parentElement: Element,220element: Element,221index: number,222): void223/**224* Removes a node from the DOM.225* @param {Node} node226* @see https://github.com/Alfred-Skyblue/vue-draggable-plus/blob/a3829222095e1949bf2c9a20979d7b5930e66f14/src/utils/index.ts#L96C1-L102C2227*/228export declare function removeNode(node: Node): void229export declare function moveArrayElement<T>(230list: MaybeRef<T[]>,231from: number,232to: number,233e?: Sortable.SortableEvent | null,234): void235```236