apps/docs/vue/quickstart.mdx
import {Story} from '/snippets/story.mdx'; import Intro from '/snippets/quickstart/intro.mdx';
<Intro />The @dnd-kit/vue package provides a set of Vue composables and components that you can use to build drag and drop interfaces. It is a thin Vue integration layer built on top of the vanilla library, so all of the same concepts are shared and can be used. You can refer to the vanilla documentation of these concepts, such as plugins, modifiers, and sensors.
Before getting started, make sure you install @dnd-kit/vue in your project:
yarn add @dnd-kit/vue
pnpm add @dnd-kit/vue
bun add @dnd-kit/vue
Let's get started by creating draggable elements that can be dropped over droppable targets. To do so, we'll be using the useDraggable composable.
The useDraggable composable requires a unique id and a template ref for the element. It returns reactive properties like isDragging that you can use in your template.
<script setup>
import {ref} from 'vue';
import {useDraggable} from '@dnd-kit/vue';
const element = ref(null);
const {isDragging} = useDraggable({id: 'draggable', element});
</script>
<template>
<button ref="element">
Draggable
</button>
</template>
In order for our draggable elements to have targets where they can be dropped, we need to create droppable elements. To do so, we'll be using the useDroppable composable.
Like useDraggable, the useDroppable composable requires a unique id and an element ref.
<script setup>
import {ref} from 'vue';
import {useDroppable} from '@dnd-kit/vue';
const element = ref(null);
const {isDropTarget} = useDroppable({id: 'droppable', element});
</script>
<template>
<div ref="element" :style="{width: '300px', height: '300px'}">
<slot />
</div>
</template>
Now that we have both draggable and droppable elements, we can put them together to create a simple drag and drop interaction.
We'll be using the DragDropProvider component to wrap our draggable and droppable elements. This component handles the drag and drop interactions and allows us to listen to drag and drop events.
const parent = ref(undefined);
function onDragEnd(event) { if (event.canceled) return; parent.value = event.operation.target?.id; } </script>
<template> <DragDropProvider @dragEnd="onDragEnd"> <Draggable v-if="parent == null" /><Droppable id="droppable">
<Draggable v-if="parent === 'droppable'" />
</Droppable>
const element = ref(null); const {isDragging} = useDraggable({id: 'draggable', element}); </script>
<template> <button ref="element"> Draggable </button> </template> ``` ```vue Droppable.vue <script setup> import {ref} from 'vue'; import {useDroppable} from '@dnd-kit/vue';const props = defineProps(['id']); const element = ref(null); const {isDropTarget} = useDroppable({ id: props.id, element, }); </script>
<template> <div ref="element" :style="{width: '300px', height: '300px'}"> <slot /> </div> </template> ``` </CodeGroup>Now that you have a basic understanding of how to make elements draggable and droppable, you can explore the concepts covered in this quickstart guide in more detail:
<CardGroup> <Card title="DragDropProvider" icon="sitemap" href="/vue/components/drag-drop-provider"> Create drag and drop contexts for your draggable and droppable elements. </Card> <Card title="useDraggable" icon="bullseye-pointer" href="/vue/composables/use-draggable"> Learn how to make elements draggable with the `useDraggable` composable. </Card> <Card title="useDroppable" icon="expand" href="/vue/composables/use-droppable"> Learn how to create droppable targets with the `useDroppable` composable. </Card> <Card title="useSortable" icon="layer-group" href="/vue/composables/use-sortable"> Learn how to create sortable elements with the `useSortable` composable. </Card> </CardGroup>