Repository navigation
Description
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.
Activity
adumesny commentedon Nov 11, 2026
Thanks for the detailed report - diagnosis confirmed. During drag/resize
_onStartMovingsetsnode.el = placeholder(restored at the end), so anychangethat bumpslayoutVersionmakes thequerySelector('.grid-stack-item-content')lookup returnnulland the portal unmounts.Findings:
syncContaineringridstack-item.tsdoes the identical lookup, and theTeleportis dropped when the target isnull).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.src, notdist), with a regression test in each that fails without the fix. Will be in 14.1.1.react/vue: keep item content mounted on change mid-drag gridstack#3459
Merge pull request #3460 from adumesny/master