Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Trouble with nested droppables #1437

Closed
ssalka opened this issue Jun 13, 2024 · 1 comment
Closed

Trouble with nested droppables #1437

ssalka opened this issue Jun 13, 2024 · 1 comment

Comments

@ssalka
Copy link

ssalka commented Jun 13, 2024

  • Using "@dnd-kit/core": "^6.1.0"

I am building a mini app similar to Infinite Craft, where you can drag objects into a space, and combine them.

My basic setup is like this (this is inside a component which itself is rendered under a DnDContext):

<div id="sidebar">
  {objects.map(obj => <DraggableCard object={obj} />)}
</div>
<div id="space" {...droppableProps}>
  {droppedObjects.map(obj => <DraggableDroppableCard object={obj} />)}
</div>

Since objects can be combined, after a draggable card is dropped, it turns into DraggableDroppableCard which has a nested droppable space in it, so you can drag other objects into it. It also can be moved around freely in the space.

Internally, it looks like this:

<div {...draggableProps}> // have tried switching the order of these
  <div {...droppableProps}>
    {obj.name}
  </div>
</div>

I have tried having the draggable div come both first & last (above/below the droppable) and have noticed the following:

  1. With drag first, rendering a droppable inside, I can drag new objects from the sidebar into it, and they combine without issue. However dragging another object that's already in the main droppable space to combine with doesn't do anything. In particular, the event.over inside the onDragEnd callback is null, so I can't tell which object was just dropped onto. The main difference IMO is that sidebar objects are not yet inside any droppable container, but those items in the main space are. Still, I feel like dnd-kit should be recognizing that there's a nested droppable there (the nested droppable's isOver state is also false when hovering over).
  2. With drop first, rendering a draggable inside, neither case works - dropping from the sidebar nor moving an existing dropped object into another. So I'm inclined to think option (1) is more correct, but I can't seem to get both cases working.

Short of helping me figure out this issue (which would be most appreciated), it would be awesome to see more Storybook examples using nested droppable areas.

@ssalka
Copy link
Author

ssalka commented Jun 15, 2024

I actually was able to get this working! What fixed things for me was using a single div for both the draggable & droppable props:

const style = {
  ...(transform && { transform: `translate3d(${transform.x}px, ${transform.y}px, 0)` })
};

const setRef = React.useCallback(
  (node: HTMLDivElement | null) => {
    setDragRef(node);
    setDropRef(node);
  },
  [setDragRef, setDropRef]
);

return (
  <div
    {...attributes}
    {...listeners}
    ref={setRef}
    style={style}
  >
    ...
  </div>
);

@ssalka ssalka closed this as completed Jun 15, 2024
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

No branches or pull requests

1 participant