drag-and-drop
drag-and-drop copied to clipboard
[Vue] Custom Draggable component example
Potentially look in to either adding something like the following snippet to the docs, or as a renderless component
This allows for use with things like firebase where realtime updates can casue some wacky behavior if you implement auto update
This example includes type infetence and is generic for users to implement their own list rendering method with v-for
<script setup lang="ts" generic="T">
import { vAutoAnimate } from "@formkit/auto-animate/vue";
import { useDragAndDrop } from "@formkit/drag-and-drop/vue";
const props = defineProps<{
itemKey?: keyof T;
handle?: string;
wrapper?: string;
}>();
const model = defineModel<T[]>({
default: [],
});
const [parent, items] = useDragAndDrop<T>(model.value, {
dragHandle: props.handle || undefined,
});
watchDebounced(
items,
() => {
model.value = items.value;
},
{
deep: true,
debounce: 100,
}
);
</script>
<template>
<component :is="wrapper || 'div'" v-if="modelValue" ref="parent" v-auto-animate>
<template
v-for="item of items"
:key="typeof item === 'object' ? item[itemKey] : item"
>
<slot :item />
</template>
</component>
</template>
Usage:
<script setup lang="ts">
const data = ref(Array(10).map((i) => `item ${i}`));
</script>
<template>
<Draggable v-model="data" v-slot="{ item }">
<li>
{{ item }}
</li>
</Draggable>
</template>