clauderic / dnd-kit

The modern, lightweight, performant, accessible and extensible drag & drop toolkit for React.
http://dndkit.com
MIT License
12.86k stars 640 forks source link

DndContext reducers all point to the same initial state variable, which is causing id collisions across nested and sibling providers #46

Closed pbteja1998 closed 3 years ago

pbteja1998 commented 3 years ago

Hi, First of all, great job building this. It's an awesome library.

I am facing a problem with a use case where I have a nested structure. Can you please help resolve this?

I have a structure similar to the following:

<DndContext>
   <div>
        <Draggable />
        <Draggable />
        {/* multiple draggable items */}
    </div>

    <div>
          <Droppable>
             <DndContext>
                  <SortableContext>
                        <SortableItem />
                        <SortableItem />
                        {/* multiple sortable items */}
                  </SortableContext>
             </DndContext>
         </Droppable>

        <Droppable>
             <DndContext>
                  <SortableContext>
                        <SortableItem />
                        <SortableItem />
                        {/* multiple sortable items */}
                  </SortableContext>
             </DndContext>
         </Droppable>
         {/* Multiple similar droppable items */}
    </div>
</DndContext>

Now, I am able to move the draggable items to droppable zones. But the events are not being detected when trying to sort items in the individual droppable zones. I assume it is something related to event bubbling, but not sure. Any help in this regard will be appreciated. Thanks 🙏

Codesandbox Link: https://codesandbox.io/s/dnd-kit-gm09q?file=/src/App.js

Demo: https://user-images.githubusercontent.com/17903466/104116991-a325a680-5343-11eb-96e0-469b168f83dd.mov

EDIT: In the same codesandbox, add an empty section and try to add Page 12 to it before anything else. It isn't getting added. I don't know if I am missing something here.

clauderic commented 3 years ago

Hey @pbteja1998, I haven't had time to look in-depth into the root cause of the issue in your case, that is, whether it is an issue in the library or a consumer error, but from what I can tell it appears like there was an id collision with your sortable items. I updated the id of the sortable items to something like sortable-${id} which seems to have resolved the collision. More investigation will be required though.

https://codesandbox.io/s/dnd-kit-forked-hhfkk?file=/src/App.js

As for the issue with pages that are scrolled in the sidebar, it's an issue that was reported here and for which a fix is in progress: #43

pbteja1998 commented 3 years ago

hi @clauderic, Thanks a lot for the help. Changing the id to sortable-${id} seems to have fixed it.

Here is the updated code sandbox for reference. https://codesandbox.io/s/dnd-kit-gm09q?file=/src/App.js

And Changing the collision strategy to closestCenter also fixed the second scroll bug that I asked about.

Again, thanks a lot for the fast response.

I have one last question. Why is the id collision happening? From my understanding, they are in separate lists, aren't they? I was under the assumption that draggable items in the same list should have different ids, but it seems like all the draggable items on the entire page should have a different ID. Can you please clarify this?

Thanks.

clauderic commented 3 years ago

Hey @pbteja1998, after some investigation I've found that this was indeed an issue with the library and not a consumer error. Thanks for reporting this issue, it allowed me to uncover a really bad bug https://github.com/clauderic/dnd-kit/pull/51

pbteja1998 commented 3 years ago

Hey @pbteja1998, after some investigation I've found that this was indeed an issue with the library and not a consumer error. Thanks for reporting this issue, it allowed me to uncover a really bad bug #51

@clauderic Awesome. Thanks for the clarification. Now, I will just wait for #43 to get fixed. Will try to go through the code myself and see if I can fix it myself and will raise a PR if I do so.

clauderic commented 3 years ago

Hey @pbteja1998, the PR with the fix is already open here #51 if you're curious about the fix, but thanks for offering to look into it

pbteja1998 commented 3 years ago

Hey @pbteja1998, the PR with the fix is already open here #51 if you're curious about the fix, but thanks for offering to look into it

@clauderic Will this also fix #43 -- fixing RectIntersection algorithm inside a scrollable container?

clauderic commented 3 years ago

Oh sorry, I misread your comment. It will not, no. I have a separate fix in progress for #43

pbteja1998 commented 3 years ago

Oh sorry, I misread your comment. It will not, no. I have a separate fix in progress for #43

Awesome. Thanks.

clauderic commented 3 years ago

The fix has shipped in @dnd-kit/core^1.0.1, let me know if you run into other issues related to unexpected id collisions

pbteja1998 commented 3 years ago

Will update and revert back from sortable-${id} to id and check.