Skip to content

Conversation

@hanneskuettner
Copy link

@hanneskuettner hanneskuettner commented Apr 28, 2025

Vue wrapper for dnd-kit

A somewhat small wrapper mapping the dnd-kit core to the Vue reactivity system.

WIP

Currently doing a self review of all the changes, but I wanted to put this out there, for other people to start having a look.

Notes

This wrapper tries to maintain the same API as the React hooks provided in @dnd-kit/react, notable differences being:

  • hooks are renamed to composables to fit the Vue nomenclature
  • The useDraggable/useDroppable/useSortable composables only accept an Element ref, but don't return a bindable ref for the element, handle, etc. This is a lot more common in the Vue world, to the best of my knowledge, e.g. as seen in this popular composables library: https://vueuse.org/core/useElementSize/

This allows for the following code, to make an element draggable

<script lang="ts" setup>
  import { useTemplateRef } from 'vue';
  import { useDraggable } from '@dnd-kit/vue';

  const elRef = useTemplateRef('el');
  useDraggable({
    id: 'draggable',
    element: elRef
  });
</script>

<template>
  <button ref="el">
    Draggable
  </button>
</template>

TODO

  • Update docs
  • Create a Vue storybook

Closes #1221

@changeset-bot
Copy link

changeset-bot bot commented Apr 28, 2025

⚠️ No Changeset found

Latest commit: 5b9c9a5

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@pkg-pr-new
Copy link

pkg-pr-new bot commented Apr 28, 2025

Open in StackBlitz

@dnd-kit/abstract

npm i https://pkg.pr.new/clauderic/dnd-kit/@dnd-kit/abstract@1668

@dnd-kit/collision

npm i https://pkg.pr.new/clauderic/dnd-kit/@dnd-kit/collision@1668

@dnd-kit/dom

npm i https://pkg.pr.new/clauderic/dnd-kit/@dnd-kit/dom@1668

@dnd-kit/geometry

npm i https://pkg.pr.new/clauderic/dnd-kit/@dnd-kit/geometry@1668

@dnd-kit/react

npm i https://pkg.pr.new/clauderic/dnd-kit/@dnd-kit/react@1668

@dnd-kit/helpers

npm i https://pkg.pr.new/clauderic/dnd-kit/@dnd-kit/helpers@1668

@dnd-kit/state

npm i https://pkg.pr.new/clauderic/dnd-kit/@dnd-kit/state@1668

@dnd-kit/vue

npm i https://pkg.pr.new/clauderic/dnd-kit/@dnd-kit/vue@1668

commit: 5b9c9a5

Copy link
Owner

@clauderic clauderic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Heroic effort 🙌 Left a few initial questions as I was reading through the code, I haven't used vue much so I definitely learned a lot!

});
const trackedSortable = useDeepSignal(sortable);

watchEffect(() => {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should / can these use batch?

Comment on lines +29 to +32
const transition = computed(() => ({
...defaultSortableTransition,
...toValue(input.transition),
}));
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we delegate this responsibility to @dnd-kit/dom? I don't remember why I did this in @dnd-kit/react in the first place and not in @dnd-kit/dom instead

@@ -0,0 +1,78 @@
{
"name": "@dnd-kit/vue",
"version": "0.1.0",
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Leaving a note here for us to make sure we sync this with whatever the latest versions are of other packages before merging

}
);

watchEffect(() => {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we use batch here also?

};

function trackRendering(callback: () => void) {
if (rendering.value === Promise.resolve()) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this ever be true?
Screenshot 2025-04-28 at 7 17 02 PM

Comment on lines +25 to +29
const renderer = {
get rendering() {
return rendering.value;
},
};
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this need to be memoized at all? Not super familiar with vue best practices

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In Vue, reactivity is opt-in unlike in react it's opt-out, that's why react has useMemo, useCallback, etc. So script will run only once, if we want to make it reactive, then we will have to opt-in using ref, computed, watch, watchEffect etc.

So no memoization is needed.

@3dfactor
Copy link

3dfactor commented Jun 7, 2025

Is this moving forward? What needs to be done for this to get merged/supported?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants