What use is there to nesting DndContext if events don't go higher than the first parent? #766
-
Hello there,
So from this situation, I would need a stucture like this:
But what good is this if all my events (onDragStart, onDragEnd) are stopped at Context 3 and will never reach Context 1 or 2 ? If I want events to be shared, I have to remove the second and third context ? But now I can't have different modifiers on the DndContexts and I can't have a DragOverlay on only one of the containers. Or am I missing something ? Thanks |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 2 replies
-
Currently, the best way to do this is to have a single You can also use You can leverage the function DraggableItem() {
const {...} = useDraggable({
id,
data: {
modifiers: [restrictToVerticalAxis],
renderDragOverlay: ({active}) => <Item id={active.id} />,
},
});
}
function App() {
const [active, setActive] = useState(null);
return (
<DndContext
modifiers={active ? active.data.current.modifiers : []}
onDragStart={({active}) => {
setActive(active);
}}
onDragEnd={() => setActive(null)}
>
<DragOverlay>
{active?.data.current?.renderDragOverlay?.(active)}
</DragOverlay> In the future, there will likely be new APIs introduced to facilitate configuring |
Beta Was this translation helpful? Give feedback.
-
Is there any other way now to communicate across |
Beta Was this translation helpful? Give feedback.
-
I had a very similar issue with this setup
And all my So my hack around it was to I leave it to the reader to fill in the rest (because I can't paste code from work) |
Beta Was this translation helpful? Give feedback.
-
I followed @clauderic advice. However, it is not possible for me to set the logic via the dragger. I have several data/functions not reachable at the dragger. I also tried @stomachfat solution but this gave unwanted re-renders with me. My solution works with one parent provider. The draggables and droppables have a ‘context/grouping’. So that a draggable can only be placed on droppables with the same context. When this does not match, the droppable is ‘disabled’. So because you are working with a ‘context/grouping’, you can also have multiple ‘DragOverLay’ in your application if you want. I push a profile using ‘useContext’ to the provider. The profile contains the options like ‘onDragStart’, ‘onDragEnd’ and ‘collisionDetection’ and so on. When you register the functions with ‘useDndMonitor’, they are all executed when active. This did not give the desired result with me. Code says more than 1000 words so below is how I did it ... have fun with it! Provider
Droppable
Dragger
Somewhere in your code where drag and drop starts. For me, this is the same place where I have my ‘DragOverlay’.
|
Beta Was this translation helpful? Give feedback.
Currently, the best way to do this is to have a single
<DndContext>
provider. You can dynamically change what modifiers are active or what theDragOverlay
renders based on the type of active element.You can also use
useDndMonitor()
to manage state locally rather than having to manage state at the top level of your application where<DndContext>
is rendered.You can leverage the
data
property ofuseDraggable
anduseSortable
to storemodifiers
that should be applied when that type of draggable item is active, and also pass a component that should be rendered for the DragOverlay.