GridStackItem unmounts React portal when change event fires during drag/collision (node.el is placeholder)
Environment
- GridStack version:
14.1.0(and14.0.0) - Package:
gridstack/dist/react(react/projects/lib/src/gridstack-item.tsx) - React version: React 18 / 19
Description
In GridStack's React wrapper (GridStackItem), dragging or resizing a widget that collides with another widget causes intermediate change events to fire before the pointer interaction finishes.
When a change event fires mid-interaction:
- The grid's
changelistener inGridStacktriggerssetLayoutVersion((v) => v + 1). - This causes
GridStackItem'suseLayoutEffect(dependent onlayoutVersion) to re-run and attempt to locate.grid-stack-item-content:const node = Utils.findInGrid(grid, id, true); const cont = node?.el?.querySelector(".grid-stack-item-content") ?? null; setContainer((prev) => (prev === cont ? prev : cont)); - During active drag/resize operations, GridStack temporarily replaces
node.elwith the placeholder element (<div class="grid-stack-item grid-stack-placeholder">...</div>). The placeholder does not contain.grid-stack-item-content. - As a result,
contevaluates tonull, updatingcontainertonull. - Since
containerisnull,createPortal(children, container)unmounts. The widget's entire React subtree, internal component state, unsubmitted inputs, and DOM elements are abruptly destroyed and vanish from the screen.
Steps to Reproduce
- Render multiple interactive widgets using the GridStack React wrapper (
<GridStack>+<GridStackItem>or component registry). - Type text into an
<input>or<textarea>inside Widget A. - Drag Widget A into the position of Widget B so that a layout collision occurs.
- Observe that mid-drag (as GridStack rearranges widgets and fires
change), Widget A's content unmounts and disappears (or resets completely).
Expected Behavior
- The React portal should remain attached to the real item element during drag and resize interactions.
- Intermediate
changeevents fired whilenode.elpoints to the placeholder should not unmount the child components or wipe out local component state.
Actual Behavior
node.elevaluates to the placeholder, which lacks.grid-stack-item-content.containerbecomesnull, unmounting the React portal and losing all component state.
Root Cause
In react/projects/lib/src/gridstack-item.tsx (compiled to dist/react/gridstack-item.js):
useLayoutEffect(() => {
if (!grid)
return;
const node = Utils.findInGrid(grid, id, true);
const cont = node?.el?.querySelector(".grid-stack-item-content") ?? null;
setContainer((prev) => (prev === cont ? prev : cont));
}, [grid, id, layoutVersion, optsKey]);
node.el can point to .grid-stack-placeholder while dragging. Looking for .grid-stack-item-content on the placeholder fails and resets container to null.
Proposed Fix
When node?.el is the placeholder, locate the actual item element from grid.el.children (excluding .grid-stack-placeholder), and fall back to the previously connected container if still available:
--- a/dist/react/gridstack-item.js
+++ b/dist/react/gridstack-item.js
@@ -51,8 +51,13 @@ export function GridStackItem({ id, options = {}, children, }) {
if (!grid)
return;
const node = Utils.findInGrid(grid, id, true);
- const cont = node?.el?.querySelector(".grid-stack-item-content") ?? null;
- setContainer((prev) => (prev === cont ? prev : cont));
+ // While dragging/resizing, GridStack points node.el at its placeholder (no content container).
+ // Resolve the real item element so the portal is not unmounted by a `change` fired mid-interaction.
+ const el = node?.el?.classList.contains("grid-stack-placeholder")
+ ? (Array.from(grid.el?.children ?? []).find((c) => c.gridstackNode?.id === id && !c.classList.contains("grid-stack-placeholder")) ?? node.el)
+ : node?.el;
+ const cont = el?.querySelector(".grid-stack-item-content") ?? null;
+ setContainer((prev) => (prev === cont ? prev : (cont ?? (node && prev?.isConnected ? prev : null))));
}, [grid, id, layoutVersion, optsKey]);
We have verified that this fix keeps inputs and panel state intact during drag collisions and drop, while still allowing explicit widget removals/closes to function as expected.
Thanks for the detailed report - diagnosis confirmed. During drag/resize _onStartMoving sets node.el = placeholder (restored at the end), so any change that bumps layoutVersion makes the querySelector('.grid-stack-item-content') lookup return null and the portal unmounts.
Findings:
- Vue has the same bug (
syncContaineringridstack-item.tsdoes the identical lookup, and theTeleportis dropped when the target isnull). - Your patch works, but the part that actually fixes it is the
prev?.isConnectedfallback. Thegrid.el.childrenlookup is mostly redundant: it only searches the outer grid (misses sub-grid items, andnode.gridcan differ from the DOM parent during cross-grid drags) and hardcodesgrid-stack-placeholderalthoughplaceholderClassis configurable. In those cases it falls through to the fallback anyway. - So the fix is: when no content container is found but the node still exists and the previous container is still connected, keep it. Applied to both the React and Vue wrappers (in
src, notdist), with a regression test in each that fails without the fix. Will be in 14.1.1.