SortableJS / vue.draggable.next

Vue 3 compatible drag-and-drop component based on Sortable.js
https://sortablejs.github.io/vue.draggable.next/#/simple
MIT License
3.82k stars 524 forks source link

issue with `transition-group` in composition API #188

Open Chevrefeuille opened 1 year ago

Chevrefeuille commented 1 year ago

First of all, thanks for the great lib!

Jsfiddle link

https://stackblitz.com/edit/vitejs-vite-tdboa5?file=src/App.vue

Step by step scenario

I am using the composition API with vue 3. I tried to make a draggable list with transitions, following the example from https://sortablejs.github.io/vue.draggable.next/#/transition-example-2 but adapting it to the composition API. However, I get the following error: Uncaught (in promise) TypeError: domElement is null.

It seems that the error comes from the item-key="order" prop, as if I remove it, or put something different (wich is not a valid key), the error disappears, but I then get the following warning [Vue warn]: <TransitionGroup> children must be keyed..

My issue seem to be the same than this person : https://stackoverflow.com/questions/72269062/draggable-with-dynamic-groups. But they didn't seem to have got any useful answer.

Am I missing something ?

Thanks!

dyc3 commented 1 year ago

item-key seems to be kinda buggy. Try omitting item-key and instead set the key prop on your elements like this:

<template #item="{ element }">
  <li :key="order">
    {{ element.name }}
  </li>
</template>
Chevrefeuille commented 1 year ago

Hello! Thanks for your answer. Although it doesn't seem to be working for me (could you make it work on the fiddle I provided?). I still have the warning [Vue warn]: <TransitionGroup> children must be keyed, and transitions are not applied when the list is sorted.

Lionad-Morotar commented 1 year ago

Hello! Thanks for your answer. Although it doesn't seem to be working for me (could you make it work on the fiddle I provided?). I still have the warning [Vue warn]: <TransitionGroup> children must be keyed, and transitions are not applied when the list is sorted.

Hello,item-key in vue-draggable-next seems to be buggy,try this: vue-draggable with transition-group,and I make your fiddle works without warning

radek-altof commented 1 year ago

Same thing happening to me. Tried to adapt the same transition example for Vue 3. Even tried copy pasting the provided example (using options api) with no modifications, still getting the same error.

Bare-bone example:

<template>
  <draggable
    tag="transition-group"
    :component-data="{
      tag: 'ul',
      name: 'flip-list',
    }"
    :list="exampleValues"
    item-key="id"
  >
    <template #item="{ element }">
      <li>{{ element.name }}</li>
    </template>
  </draggable>
</template>

<script setup>
import { ref } from 'vue';
import draggable from 'vuedraggable';

const exampleValues = ref([{
  id: 1,
  name: 'John',
}, {
  id: 2,
  name: 'Emily',
}, {
  id: 3,
  name: 'Rachel',
}, {
  id: 4,
  name: 'Chris',
}]);
</script>

<style>
.flip-list-move {
  transition: all 0.5s ease;
}
</style>

Getting the following error:

TypeError: domElement is null
    addContext componentStructure.js:2
    updated componentStructure.js:33
    updated componentStructure.js:32
    mounted vuedraggable.js:125
    createHook runtime-core.esm-bundler.js:2709
    callWithErrorHandling runtime-core.esm-bundler.js:155
    callWithAsyncErrorHandling runtime-core.esm-bundler.js:164
    __weh runtime-core.esm-bundler.js:2684
    flushPostFlushCbs runtime-core.esm-bundler.js:341
    flushJobs runtime-core.esm-bundler.js:395
    promise callback*queueFlush runtime-core.esm-bundler.js:280
    queueJob runtime-core.esm-bundler.js:274
    effect runtime-core.esm-bundler.js:5710
    triggerEffect reactivity.esm-bundler.js:394
    triggerEffects reactivity.esm-bundler.js:384
    triggerRefValue reactivity.esm-bundler.js:1000
    set value reactivity.esm-bundler.js:1045
    setup ----.vue:128
    createHook runtime-core.esm-bundler.js:2709
    ...

Running on:

tobychidi commented 1 year ago

I have this error too. Transition does not work in Vue 3

gerritvanaaken commented 1 year ago

Different error message, but might be same problem:

First a Vue Warning in the console: [Vue warn]: Unhandled error during execution of updated at <Draggable onUpdate=fn<changeSongs> modelValue=

Then a solid JavaScript error: Uncaught (in promise) TypeError: Cannot set properties of null (setting '__draggable_context')

Changing the "tag" Property from "transition-group" back to "ul" fixes the errors, but the animation is gone, of course.

gerritvanaaken commented 1 year ago

I switched to a forked version of this repo: https://www.npmjs.com/package/zhyswan-vuedraggable

This fixed the issue for me. If you still get (chinese) error codes in the console, try to translate them with deepl.com – I had to slightly modify my HTML code to make it all work perfectly.

90ker commented 1 year ago

I have the same problem, and I found the [el] in vNode is 'null' when mounted.

a572251465 commented 1 year ago

Hello, do you have any plans to fix this issue

durbanitas commented 11 months ago

Hello there - seeing the same problem.

Any solutions?

stanJ commented 10 months ago

Hello! Thanks for your answer. Although it doesn't seem to be working for me (could you make it work on the fiddle I provided?). I still have the warning [Vue warn]: <TransitionGroup> children must be keyed, and transitions are not applied when the list is sorted.

Hello,item-key in vue-draggable-next seems to be buggy,try this: vue-draggable with transition-group,and I make your fiddle works without warning

It is still not solved. In your example, if you delete <transition-group>, it still works. Because the result comes from animation: 200

cfrow commented 8 months ago

What ultimately worked for me was to remove tag="transition-group".

boutzamat commented 8 months ago

I might be late to the party, but literally removing the tag="transition-group" (as mentioned above) and v-binding the computed "dragOptions" as in the demo, worked for me:

const transitionOptions = computed(() => { return { animation: 200, group: "description", disabled: false, ghostClass: "ghost", }; });

niladrix719 commented 6 months ago

its not working for me

        <draggable
            :key="updateCount"
            :item-key="updateCount.toString()"
            v-model="SimulatorState.circuit_list"
            class="list-group"
            tag="ul"
            :component-data="{
                tag: 'div',
                type: 'transition-group',
                name: !drag ? 'flip-list' : null,
            }"
            v-bind="dragOptions"
            @start="drag = true"
            @end="drag = false"
        >

and i have this computed :

const dragOptions = computed(() => {
    return {
        animation: 200,
        group: 'description',
        disabled: false,
        ghostClass: 'ghost',
    }
})
danrot commented 5 months ago

I just ran into the same issue, and I found a way to solve it. The problem seems to be that the transition-group tag expects a tag property, which it will use to render the transition group. I've passed a tag name to component-data and then the error was gone (not sure yet if everything works though). Currently it looks like that for me (slightly adjusted compared to the original code without testing):

    <draggable
        v-model="data"
        handle=".handle"
        tag="transition-group"
        :component-data="{ tag: 'div' }"
        @start="drag = true"
        @end="drag = false"
      >
        <template v-slot:item="{ element }">
          <div :key="`section_${element.id}`" :name="!drag ? 'flip-list' : null" class="section-wrapper">
            <div class="handle">handle</div>
            {{ JSON.stringify(element) }}
          </div>
        </template>
      </draggable>
avinmaster commented 4 months ago

Also, you can do like this (from example):

<draggable
      v-model="myArray"
      group="people"
      @start="drag = true"
      @end="drag = false"
      item-key="id"
      animation="200"
  >
    <template #item="{element}">
      <div>{{ element.name }}</div>
    </template>
  </draggable>
Durss commented 4 months ago

Most of the "solutions" above consist of removing the transition-group and setting the animation prop. You'll get an animation when dragging items but that is not equivalent.

The difference between the 2 solutions being that if you sort items by code, you won't get any transition with the animation prop. With the transition-group you should have an animation transition even when sorting via code.

I personally switched to this alternative that's still active to date: https://github.com/Alfred-Skyblue/vue-draggable-plus

ReaganM02 commented 3 months ago

Any update on this? I'm facing the same issue, and following the codes provided above are not working for me.

Durss commented 3 months ago

Any update on this? I'm facing the same issue, and following the codes provided above are not working for me.

This project is kinda dead. Last release is 4 years old. Check my message above yours for an alternative that fixes this issue