Cross-grid dragging
Put two or more grids on one dnd-kit provider and tiles can be dragged between them. The
source grid loses the tile; the destination gains it. Each grid keeps its own controlled layout.
<SnapGridGroup> is that shared provider — it’s just dnd-kit’s DragDropProvider, named for intent
— so each grid hosts itself with useGridContainer, and a dragged tile floats itself across the
seam (no overlay to wire).
Cross-grid is the managed, grid-to-grid case. To drag between a grid and a non-grid dnd-kit sortable (a list, a kanban column), see dnd-kit interop.
// Grid.tsx — each grid hosts itself and exposes its own `group`; the shared
// provider comes from the enclosing SnapGridGroup.
import { type Layout, useGridContainer } from "@snapgridjs/solid";
import { For } from "solid-js";
import { Tile } from "./Tile";
export function Grid(props: {
layout: Layout;
width: number;
onLayoutChange: (layout: Layout) => void;
}) {
// The options getter keeps the grid tracking your live props.
const grid = useGridContainer(() => ({
layout: props.layout,
width: props.width,
onLayoutChange: props.onLayoutChange,
gridConfig: cfg,
}));
return (
<div ref={grid.ref} style={grid.style()}>
<For each={props.layout.map((it) => it.i)}>
{(id) => <Tile id={id} group={grid.group()} />}
</For>
</div>
);
}// Tile.tsx
import { useGridItem } from "@snapgridjs/solid";
export function Tile(props: { id: string; group: string }) {
const tile = useGridItem({ id: props.id, group: props.group });
return (
<div ref={tile.ref} style={tile.style()} class="tile">
{props.id}
</div>
);
}// Board.tsx — two grids under one SnapGridGroup exchange tiles.
import { SnapGridGroup } from "@snapgridjs/solid";
import { createSignal } from "solid-js";
import { Grid } from "./Grid";
export function Board() {
const [left, setLeft] = createSignal(/* … */);
const [right, setRight] = createSignal(/* … */);
const w = 480;
return (
<SnapGridGroup>
<Grid layout={left()} width={w} onLayoutChange={setLeft} />
<Grid layout={right()} width={w} onLayoutChange={setRight} />
</SnapGridGroup>
);
}Note the split into three components. <SnapGridGroup> renders the DragDropProvider, so each
<Grid> is a child of it — the only place useGridContainer can see the dnd-kit manager, since
Solid resolves context from the owner chain. Calling it in Board itself would silently register
no grid.
How it works
Cross-grid dragging is just two grids on one dnd-kit DragDropProvider. <SnapGridGroup>
supplies that shared provider; a drag that starts in one grid is received by whichever grid
dnd-kit’s collision system resolves under the pointer. On drop:
- the source grid removes the tile and fires its
onLayoutChangewith the tile gone; - the destination grid inserts the tile at the drop cell and fires its
onLayoutChangewith the tile added.
Both updates happen in the same gesture, each against its own layout state.
Item ids must be unique across every grid in a group. They share one drag manager. If two
grids can hold an item with the same i, a cross-grid drop can’t tell them apart.
<SnapGridGroup> is exactly dnd-kit’s <DragDropProvider> for headless grids like these — use
whichever you like; grids under one provider exchange tiles either way. Prefer the turnkey shell?
Swap each <Grid> for a <GridLayout> with a render-prop child ({(item) => …})
inside a <SnapGridGroup> — it bundles the container and items for you. A turnkey grid only
joins a provider snapgrid supplied: under a plain <DragDropProvider>, each mints its own.
No overlay to wire. A dragged tile floats itself — it lifts into the top layer and crosses the gap between grids unclipped, carrying its own content with it. So the preview always shows the right tile, from whichever grid it came, with nothing extra to render.
Callbacks
The destination grid signals a received tile through onLayoutChange only. It does not fire
onDragStop, because it never fired onDragStart (the source grid owns the drag’s start/stop
pair). Wire cross-grid persistence to onLayoutChange on both grids.
Dropping outside any grid
If a tile is dropped outside every grid in the group, the move reverts. The source keeps it and no
onLayoutChange fires.
Want a grid inside a tile of another rather than side-by-side siblings? See Nested grids — they also cross levels by default, and you can give a sub-grid its own provider to keep it self-contained.