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### Using Component100101```vue102<script setup lang="ts">103import { UseSortable } from '@vueuse/integrations/useSortable/component'104import { shallowRef } from 'vue'105106const list = shallowRef([107{ id: 1, name: 'a' },108{ id: 2, name: 'b' },109{ id: 3, name: 'c' },110])111</script>112113<template>114<UseSortable v-model="list" as="ol" :options="{ animation: 150 }">115<li v-for="item in list" :key="item.id">116{{ item.name }}117</li>118</UseSortable>119</template>120```121122You can also access the helper functions like `start`, `stop`, and `option` from the slot scope:123124```vue125<template>126<UseSortable v-slot="{ stop, start }" v-model="list">127<button @click="stop()">128Stop Sorting129</button>130<button @click="start()">131Start Sorting132</button>133<div v-for="item in list" :key="item.id">134{{ item.name }}135</div>136</UseSortable>137</template>138```139140### Return Values141142| Property | Description |143| -------- | ---------------------------------------------------------------- |144| `start` | Initialize the Sortable instance (called automatically on mount) |145| `stop` | Destroy the Sortable instance |146| `option` | Get or set Sortable options at runtime |147148```ts149const { start, stop, option } = useSortable(el, list)150151// Stop sorting152stop()153154// Start sorting again155start()156157// Get/set options158option('animation', 200) // set159const animation = option('animation') // get160```161162### Watch Element Changes163164Use the `watchElement` option to automatically reinitialize Sortable when the element changes (useful with `v-if`).165166```ts167import { useSortable } from '@vueuse/integrations/useSortable'168169useSortable(el, list, {170watchElement: true, // auto-reinitialize when element changes171})172```173174### Custom Update Handler175176If you want to handle the `onUpdate` yourself, you can pass in `onUpdate` parameters, and we also exposed a function to move the item position.177178```ts179import { moveArrayElement, useSortable } from '@vueuse/integrations/useSortable'180181useSortable(el, list, {182onUpdate: (e) => {183// do something184moveArrayElement(list, e.oldIndex, e.newIndex, e)185// nextTick required here as moveArrayElement is executed in a microtask186// so we need to wait until the next tick until that is finished.187nextTick(() => {188/* do something */189})190}191})192```193194### Helper Functions195196The following helper functions are also exported:197198| Function | Description |199| ------------------------------------------ | ----------------------------------------------------- |200| `moveArrayElement(list, from, to, event?)` | Move an element in an array from one index to another |201| `insertNodeAt(parent, element, index)` | Insert a DOM node at a specific index |202| `removeNode(node)` | Remove a DOM node from its parent |203204## Type Declarations205206```ts207export interface UseSortableReturn {208/**209* start sortable instance210*/211start: () => void212/**213* destroy sortable instance214*/215stop: () => void216/**217* Options getter/setter218* @param name a Sortable.Options property.219* @param value a value.220*/221option: (<K extends keyof Sortable.Options>(222name: K,223value: Sortable.Options[K],224) => void) &225(<K extends keyof Sortable.Options>(name: K) => Sortable.Options[K])226}227export interface UseSortableOptions extends Options, ConfigurableDocument {228/**229* Watch the element reference for changes and automatically reinitialize Sortable230* when the element changes.231*232* When `false` (default), Sortable is only initialized once on mount.233* You must manually call `start()` if the element reference changes.234*235* When `true`, automatically watches the element reference and reinitializes236* Sortable whenever it changes (e.g., conditional rendering with v-if).237*238* @default false239*/240watchElement?: boolean241}242export declare function useSortable<T>(243selector: string,244list: MaybeRef<T[]>,245options?: UseSortableOptions,246): UseSortableReturn247export declare function useSortable<T>(248el: MaybeRefOrGetter<MaybeElement>,249list: MaybeRef<T[]>,250options?: UseSortableOptions,251): UseSortableReturn252/**253* Inserts a element into the DOM at a given index.254* @param parentElement255* @param element256* @param {number} index257* @see https://github.com/Alfred-Skyblue/vue-draggable-plus/blob/a3829222095e1949bf2c9a20979d7b5930e66f14/src/utils/index.ts#L81C1-L94C2258*/259export declare function insertNodeAt(260parentElement: Element,261element: Element,262index: number,263): void264/**265* Removes a node from the DOM.266* @param {Node} node267* @see https://github.com/Alfred-Skyblue/vue-draggable-plus/blob/a3829222095e1949bf2c9a20979d7b5930e66f14/src/utils/index.ts#L96C1-L102C2268*/269export declare function removeNode(node: Node): void270export declare function moveArrayElement<T>(271list: MaybeRef<T[]>,272from: number,273to: number,274e?: Sortable.SortableEvent | null,275): void276```277