Skip to content

Commit a7d284a

Browse files
authored
Merge pull request #3460 from adumesny/master
react/vue: keep item content mounted on change mid-drag #3459
2 parents 8539dcb + b0003d6 commit a7d284a

5 files changed

Lines changed: 92 additions & 1 deletion

File tree

‎doc/CHANGES.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ Change log
55
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->
66
**Table of Contents** *generated with [DocToc](http://doctoc.herokuapp.com/)*
77

8+
- [14.1.1 (TBD)](#1411-tbd)
89
- [14.1.0 (2026-11-04)](#1410-2026-11-04)
910
- [14.0.0 (2026-09-20)](#1400-2026-09-20)
1011
- [13.3.0 (2026-09-11)](#1330-2026-09-11)
@@ -148,6 +149,9 @@ Change log
148149

149150
<!-- END doctoc generated TOC please keep comment here to allow auto update -->
150151

152+
## 14.1.1 (TBD)
153+
* fix: [#3459](https://github.com/gridstack/gridstack.js/issues/3459) React/Vue wrappers: a `change` event fired mid drag/resize no longer unmounts the item's content (state lost)
154+
151155
## 14.1.0 (2026-11-04)
152156
* feat: [#3451](https://github.com/gridstack/gridstack.js/pull/3451), [#3452](https://github.com/gridstack/gridstack.js/pull/3452) dragging between grids now goes by how much of the item is over a grid (like jquery-ui's `intersect` before v6) rather than where the cursor is. Also make it possible to insert above a nested grid even when parent grid overlapps exactly.
153157
* feat: [#3356](https://github.com/gridstack/gridstack.js/issues/3356) auto-scroll triggers off the cursor nearing an edge (not just widget being clipped)

‎react/projects/lib/gridstack-react.test.tsx‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,47 @@ describe("GridStack React wrapper", () => {
338338
expect(document.querySelector('[data-testid="h"]')?.textContent).toBe("5");
339339
});
340340

341+
it("#3459 keeps item content mounted when change fires while node.el is the drag placeholder", async () => {
342+
let mounts = 0;
343+
function Probe() {
344+
useState(() => { mounts++; return 0; });
345+
return <span data-testid="probe">hi</span>;
346+
}
347+
348+
await act(async () => {
349+
root.render(
350+
<GridStack
351+
options={{
352+
column: 12, cellHeight: 50, margin: 0,
353+
children: [{ id: "p1", x: 0, y: 0, w: 2, h: 2, component: "P" }],
354+
}}
355+
components={{ P: () => <Probe /> }}
356+
/>
357+
);
358+
});
359+
await act(flush);
360+
expect(document.querySelector('[data-testid="probe"]')).not.toBeNull();
361+
362+
const g = (container.querySelector(".grid-stack") as GridHTMLElement).gridstack as GridStackInstance;
363+
const node = g.engine.nodes[0];
364+
const realEl = node.el!;
365+
366+
// what _onStartMoving leaves behind: node.el is the placeholder (no .grid-stack-item-content)
367+
node.el = g.placeholder;
368+
const t = g as unknown as { _triggerChangeEvent(): unknown };
369+
await act(async () => {
370+
node.h = 3;
371+
node._dirty = true;
372+
t._triggerChangeEvent();
373+
});
374+
await act(flush);
375+
376+
expect(document.querySelector('[data-testid="probe"]')).not.toBeNull();
377+
expect(mounts).toBe(1);
378+
379+
node.el = realEl;
380+
});
381+
341382
it("#2960 useGridStack().removeWidget takes an element, matching core's signature", async () => {
342383
let api: ReturnType<typeof useGridStack> | undefined;
343384
function Probe() {

‎react/projects/lib/src/gridstack-item.tsx‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,9 @@ export function GridStackItem({
7575
const node = Utils.findInGrid(grid, id, true);
7676
const cont =
7777
(node?.el?.querySelector(".grid-stack-item-content") as HTMLElement | null) ?? null;
78-
setContainer((prev) => (prev === cont ? prev : cont));
78+
// Mid drag/resize `node.el` is GS's placeholder (no content container); a `change` fired then
79+
// must not null the container or the portal unmounts and the widget loses its state (#3459).
80+
setContainer((prev) => (prev === cont ? prev : !cont && node && prev?.isConnected ? prev : cont));
7981
}, [grid, id, layoutVersion, optsKey]);
8082

8183
// Tracks whether the component is "truly unmounting" vs. Strict Mode double-invoke.

‎vue/projects/lib/gridstack-vue.test.ts‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,4 +301,45 @@ describe('GridStack Vue wrapper', () => {
301301

302302
expect(document.querySelector('[data-testid="h"]')?.textContent).toBe('5')
303303
})
304+
305+
it('keeps item content mounted when change fires while node.el is the drag placeholder (#3459)', async () => {
306+
let mounts = 0
307+
const Probe = defineComponent({
308+
setup() {
309+
mounts++
310+
return () => h('span', { 'data-testid': 'probe' }, 'hi')
311+
},
312+
})
313+
314+
const Root = defineComponent({
315+
setup() {
316+
const options = {
317+
column: 12, cellHeight: 50, margin: 0,
318+
children: [{ id: 'p1', x: 0, y: 0, w: 2, h: 2, component: 'P' }],
319+
}
320+
return () => h(GridStack, { options, components: { P: Probe } })
321+
},
322+
})
323+
324+
;({ app, container } = mountApp(Root))
325+
await flush()
326+
expect(document.querySelector('[data-testid="probe"]')).not.toBeNull()
327+
328+
const g = (container.querySelector('.grid-stack') as GridHTMLElement).gridstack!
329+
const node = g.engine.nodes[0]
330+
const realEl = node.el!
331+
332+
// what _onStartMoving leaves behind: node.el is the placeholder (no .grid-stack-item-content)
333+
node.el = g.placeholder
334+
const t = g as unknown as { _triggerChangeEvent(): unknown }
335+
node.h = 3
336+
node._dirty = true
337+
t._triggerChangeEvent()
338+
await flush()
339+
340+
expect(document.querySelector('[data-testid="probe"]')).not.toBeNull()
341+
expect(mounts).toBe(1)
342+
343+
node.el = realEl
344+
})
304345
})

‎vue/projects/lib/src/gridstack-item.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ export const GridStackItem = defineComponent({
5151
if (!g) return
5252
const node = Utils.findInGrid(g, props.id, true)
5353
const cont = (node?.el?.querySelector('.grid-stack-item-content') as HTMLElement | null) ?? null
54+
// Mid drag/resize `node.el` is GS's placeholder (no content container); a `change` fired then
55+
// must not null the target or the teleport unmounts and the widget loses its state (#3459).
56+
if (!cont && node && containerEl.value?.isConnected) return
5457
containerEl.value = cont
5558
}
5659

0 commit comments

Comments
 (0)