Vue Draggable - Getting Started
This tutorial guides you through the following steps:
- Add a Draggable to a page.
- Configure Draggable options.
- Handle events.
Each section in this tutorial covers a single configuration step. You can find the complete source code in the following GitHub repository:
Create Draggable
Add DevExtreme to your Vue application and use the following code to create a Draggable:
- <template>
- <DxDraggable />
- </template>
- <script setup lang="ts">
- import { DxDraggable, type DxDraggableTypes } from 'devextreme-vue/draggable';
- import 'devextreme/dist/css/dx.fluent.blue.light.css';
- </script>
Draggable does not include default visual elements. Specify custom markup as follows:
- <template>
- <DxDraggable id="note-1">
- <div class="note">
- <div class="color-indicator"></div>
- <div class="text-container">
- <div class="body-text-box">Install New Router in Dev Room</div>
- <div class="detail-text-box">Amelia Harper</div>
- </div>
- </div>
- </DxDraggable>
- </template>
This example defines Draggable markup within a custom component, Note.
This component configures two interfaces:
export interface NoteInfo: Specifies note information.interface NoteProps: Defines properties of the Note component.
Create an array of NoteInfo objects to define four notes. Implement the Note component within a <template> tag and use the v-for directive to add these notes to the app:
- <template>
- <DxDraggable
- :id="id"
- :group="group"
- :boundary="boundary"
- >
- <div class="note">
- <div class="color-indicator"></div>
- <div class="text-container">
- <div class="body-text-box">{{ task }}</div>
- <div class="detail-text-box">{{ assignee }}</div>
- </div>
- </div>
- </DxDraggable>
- </template>
- <script setup lang="ts">
- // ...
- export interface NoteInfo {
- id: string;
- task: string;
- assignee: string;
- }
- interface NoteProps extends NoteInfo {
- group?: string;
- boundary?: string;
- // ...
- }
- const {
- id,
- group = 'notes',
- boundary = '.board',
- task,
- assignee,
- // ...
- } = defineProps<NoteProps>();
- </script>
- <template>
- <div class="boundary-text">Dragging Boundary</div>
- <div class="board">
- <template v-for="note in notes">
- <Note
- :id="note.id"
- :task="note.task"
- :assignee="note.assignee"
- />
- </template>
- </div>
- <div class="boundary-text">Dragging Boundary</div>
- </template>
- <script setup lang="ts">
- // ...
- import Note, { type NoteInfo } from "./Note.vue";
- const notes: NoteInfo[] = [
- {
- id: 'note-1',
- task: 'Install New Router in Dev Room',
- assignee: 'Amelia Harper',
- },
- {
- id: 'note-2',
- task: '👨💻 Launch New Website',
- assignee: 'Brett Wade',
- },
- {
- id: 'note-3',
- task: 'Prepare 2026 Marketing Plan',
- assignee: 'Robert Reagan',
- },
- {
- id: 'note-4',
- task: '🖥️ Approve Personal Computer Upgrade Plan',
- assignee: 'Bart Arnaz',
- },
- ];
- </script>
Configure Draggable Options
This example specifies the following Draggable properties:
- boundary: Constrains Draggable movement inside a container (
.board). - group: Specifies an interaction group (used in events such as onDragMove).
Handle Events
This example specifies the following event handlers to update the note's Z-index (bring to front while dragging):
This example also implements a @click handler on the parent <div> element within the Note component:
- <template>
- <DxDraggable ...
- :style="{zIndex: currentZIndex}"
- @drag-start="handleDragStart"
- @drag-move="handleDragMove"
- @drag-end="handleDragEnd"
- >
- <div
- :class="`note ${isOverlapped ? 'overlapped' : ''}`"
- @click="handleClick"
- >
- <!-- ... -->
- </div>
- </DxDraggable>
- </template>
- <script setup lang="ts">
- import { ref } from "vue";
- // ...
- const overlappedComponentId = ref<string | null>(null);
- const currentZIndex = ref<number>(0);
- function updateZIndex() {
- const zIndexToUpdate = zIndex.value + 1;
- currentZIndex.value = zIndexToUpdate;
- zIndex.value = zIndexToUpdate;
- }
- function handleDragStart() {
- updateZIndex();
- }
- function handleDragMove(e: DxDraggableTypes.DragMoveEvent) {
- if (e.toComponent !== e.component) {
- const toComponentId = e.toComponent.element().id;
- startOverlap(toComponentId);
- overlappedComponentId.value = toComponentId;
- } else {
- stopOverlap();
- overlappedComponentId.value = null;
- }
- }
- function handleDragEnd() {
- stopOverlap();
- }
- function handleClick() {
- updateZIndex();
- }
- </script>
- <template>
- <div class="boundary-text">Dragging Boundary</div>
- <div class="board">
- <template v-for="note in notes">
- <Note ...
- :is-overlapped="overlappedId === note.id"
- v-model:z-index="zIndex"
- :start-overlap="startOverlap"
- :stop-overlap="stopOverlap"
- />
- </template>
- </div>
- <div class="boundary-text">Dragging Boundary</div>
- </template>
- <script setup lang="ts">
- import { ref } from "vue";
- // ...
- const overlappedId = ref<string | null>(null);
- const zIndex = ref<number>(0);
- function startOverlap(id: string) {
- overlappedId.value = id;
- }
- function stopOverlap() {
- overlappedId.value = null;
- }
- </script>
- <style scoped>
- .note.overlapped {
- outline: 1px dashed var(--dx-color-primary);
- }
- </style>
Refer to the following repository for the complete source code of this tutorial: