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/useDraggable.md
1---2category: Elements3---45# useDraggable67Make elements draggable.89## Usage1011```vue12<script setup lang="ts">13import { useDraggable } from '@vueuse/core'14import { useTemplateRef } from 'vue'1516const el = useTemplateRef('el')1718// `style` will be a helper computed for `left: ?px; top: ?px;`19const { x, y, style } = useDraggable(el, {20initialValue: { x: 40, y: 40 },21})22</script>2324<template>25<div ref="el" :style="style" style="position: fixed">26Drag me! I am at {{ x }}, {{ y }}27</div>28</template>29```3031### Return Values3233| Property | Type | Description |34| ------------ | ---------------------- | --------------------------------------- |35| `x` | `Ref<number>` | Current x position |36| `y` | `Ref<number>` | Current y position |37| `position` | `Ref<{x, y}>` | Current position object |38| `isDragging` | `ComputedRef<boolean>` | Whether currently dragging |39| `style` | `ComputedRef<string>` | CSS style string `left: ?px; top: ?px;` |4041### Options4243```ts44useDraggable(el, {45// Initial position (default: { x: 0, y: 0 })46initialValue: { x: 40, y: 40 },47// Restrict dragging to specific axis: 'x', 'y', or 'both' (default)48axis: 'both',49// Only trigger when clicking directly on the element (default: false)50exact: false,51// Prevent default browser behavior (default: false)52preventDefault: true,53// Stop event propagation (default: false)54stopPropagation: false,55// Use capture phase for events (default: true)56capture: true,57// Disable dragging (default: false)58disabled: false,59// Mouse buttons that trigger drag (default: [0] - left button)60buttons: [0],61// Pointer types to listen to (default: ['mouse', 'touch', 'pen'])62pointerTypes: ['mouse', 'touch', 'pen'],63// Custom drag handle element (default: target element)64handle: handleRef,65// Container element for bounds (default: none)66containerElement: containerRef,67// Element to attach pointermove/pointerup events (default: window)68draggingElement: window,69// Callbacks70onStart: (position, event) => {71// Return false to prevent dragging72},73onMove: (position, event) => {},74onEnd: (position, event) => {},75})76```7778### Prevent Default7980Set `preventDefault: true` to override the default drag-and-drop behavior of certain elements in the browser (e.g., images).8182```ts83import { useDraggable } from '@vueuse/core'8485const { x, y, style } = useDraggable(el, {86preventDefault: true,87})88```8990### Container Bounds9192Use `containerElement` to constrain dragging within a container.9394```ts95import { useDraggable } from '@vueuse/core'9697const { x, y } = useDraggable(el, {98containerElement: containerRef,99})100```101102Set `autoScroll: true` to enable auto-scroll when dragging near the edges.103104```ts105const { x, y, style } = useDraggable(el, {106autoScroll: {107speed: 2, // Control the speed of auto-scroll.108margin: 30, // Set the margin from the edge that triggers auto-scroll.109direction: 'both' // Determine the direction of auto-scroll.110},111})112```113114## Component Usage115116```vue117<template>118<UseDraggable v-slot="{ x, y }" :initial-value="{ x: 10, y: 10 }">119Drag me! I am at {{ x }}, {{ y }}120</UseDraggable>121</template>122```123124For component usage, additional props `storageKey` and `storageType` can be passed to the component and enable the persistence of the element position.125126```vue127<template>128<UseDraggable storage-key="vueuse-draggable" storage-type="session">129Refresh the page and I am still in the same position!130</UseDraggable>131</template>132```133134## Type Declarations135136```ts137export interface UseDraggableOptions {138/**139* Only start the dragging when click on the element directly140*141* @default false142*/143exact?: MaybeRefOrGetter<boolean>144/**145* Prevent events defaults146*147* @default false148*/149preventDefault?: MaybeRefOrGetter<boolean>150/**151* Prevent events propagation152*153* @default false154*/155stopPropagation?: MaybeRefOrGetter<boolean>156/**157* Whether dispatch events in capturing phase158*159* @default true160*/161capture?: boolean162/**163* Element to attach `pointermove` and `pointerup` events to.164*165* @default window166*/167draggingElement?: MaybeRefOrGetter<168HTMLElement | SVGElement | Window | Document | null | undefined169>170/**171* Element for calculating bounds (If not set, it will use the event's target).172*173* @default undefined174*/175containerElement?: MaybeRefOrGetter<176HTMLElement | SVGElement | null | undefined177>178/**179* Handle that triggers the drag event180*181* @default target182*/183handle?: MaybeRefOrGetter<HTMLElement | SVGElement | null | undefined>184/**185* Pointer types that listen to.186*187* @default ['mouse', 'touch', 'pen']188*/189pointerTypes?: PointerType[]190/**191* Initial position of the element.192*193* @default { x: 0, y: 0 }194*/195initialValue?: MaybeRefOrGetter<Position>196/**197* Callback when the dragging starts. Return `false` to prevent dragging.198*/199onStart?: (position: Position, event: PointerEvent) => void | false200/**201* Callback during dragging.202*/203onMove?: (position: Position, event: PointerEvent) => void204/**205* Callback when dragging end.206*/207onEnd?: (position: Position, event: PointerEvent) => void208/**209* Axis to drag on.210*211* @default 'both'212*/213axis?: "x" | "y" | "both"214/**215* Disabled drag and drop.216*217* @default false218*/219disabled?: MaybeRefOrGetter<boolean>220/**221* Mouse buttons that are allowed to trigger drag events.222*223* - `0`: Main button, usually the left button or the un-initialized state224* - `1`: Auxiliary button, usually the wheel button or the middle button (if present)225* - `2`: Secondary button, usually the right button226* - `3`: Fourth button, typically the Browser Back button227* - `4`: Fifth button, typically the Browser Forward button228*229* @see https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent/button#value230* @default [0]231*/232buttons?: MaybeRefOrGetter<number[]>233/**234* Whether to restrict dragging within the visible area of the container.235*236* If enabled, the draggable element will not leave the visible area of its container,237* ensuring it remains within the viewport of the container during the drag.238*239* @default false240*/241restrictInView?: MaybeRefOrGetter<boolean>242/**243* Whether to enable auto-scroll when dragging near the edges.244*245* @default false246*/247autoScroll?: MaybeRefOrGetter<248| boolean249| {250/**251* Speed of auto-scroll.252*253* @default 2254*/255speed?: MaybeRefOrGetter<number | Position>256/**257* Margin from the edge to trigger auto-scroll.258*259* @default 30260*/261margin?: MaybeRefOrGetter<number | Position>262/**263* Direction of auto-scroll.264*265* @default 'both'266*/267direction?: "x" | "y" | "both"268}269>270}271export interface UseDraggableReturn {272x: Ref<number>273y: Ref<number>274position: Ref<Position>275isDragging: ComputedRef<boolean>276style: ComputedRef<string>277}278/**279* Make elements draggable.280*281* @see https://vueuse.org/useDraggable282* @param target283* @param options284*/285export declare function useDraggable(286target: MaybeRefOrGetter<HTMLElement | SVGElement | null | undefined>,287options?: UseDraggableOptions,288): UseDraggableReturn289```290