Skip to content

GridStackItem unmounts React portal when change event fires during drag/collision (node.el is placeholder) #3459

Closed

Description

@nguoimegame

Environment

  • GridStack version: 14.1.0 (and 14.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:

  1. The grid's change listener in GridStack triggers setLayoutVersion((v) => v + 1).
  2. This causes GridStackItem's useLayoutEffect (dependent on layoutVersion) 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));
  3. During active drag/resize operations, GridStack temporarily replaces node.el with the placeholder element (<div class="grid-stack-item grid-stack-placeholder">...</div>). The placeholder does not contain .grid-stack-item-content.
  4. As a result, cont evaluates to null, updating container to null.
  5. Since container is null, 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

  1. Render multiple interactive widgets using the GridStack React wrapper (<GridStack> + <GridStackItem> or component registry).
  2. Type text into an <input> or <textarea> inside Widget A.
  3. Drag Widget A into the position of Widget B so that a layout collision occurs.
  4. 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 change events fired while node.el points to the placeholder should not unmount the child components or wipe out local component state.

Actual Behavior

  • node.el evaluates to the placeholder, which lacks .grid-stack-item-content.
  • container becomes null, 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

  1. adumesny commented on Nov 11, 2026

    @adumesny
    Member

    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 (syncContainer in gridstack-item.ts does the identical lookup, and the Teleport is dropped when the target is null).
    • Your patch works, but the part that actually fixes it is the prev?.isConnected fallback. The grid.el.children lookup is mostly redundant: it only searches the outer grid (misses sub-grid items, and node.grid can differ from the DOM parent during cross-grid drags) and hardcodes grid-stack-placeholder although placeholderClass is 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, not dist), with a regression test in each that fails without the fix. Will be in 14.1.1.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions