@snapgridjs/react 0.1.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +45 -9
- package/dist/index.cjs +560 -377
- package/dist/index.d.cts +156 -95
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.mts +156 -95
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +559 -379
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -15,7 +15,7 @@ Draggable, resizable, responsive grid layouts for React — with pluggable packi
|
|
|
15
15
|
## Why snapgrid
|
|
16
16
|
|
|
17
17
|
- **Controlled & predictable** — you own the layout array; every change comes back through `onLayoutChange`. No hidden state.
|
|
18
|
-
- **Headless
|
|
18
|
+
- **Headless-first** — compose `useGridContainer` + hooks under a dnd-kit `DragDropProvider` for full control of your markup — or drop in the turnkey [`<GridLayout>`](https://snapgrid.dev/docs/guides/component-layer) (react-grid-layout-style) when you don't need that. Ships **no CSS**.
|
|
19
19
|
- **Pluggable packing** — `vertical` / `horizontal` / `none`, plus `masonry` / `gravity` / `shelf` from [`@snapgridjs/extras`](https://www.npmjs.com/package/@snapgridjs/extras), or your own `Compactor`.
|
|
20
20
|
- **Cross-grid dragging** — wrap grids in a `<SnapGridGroup>` and drag tiles between them.
|
|
21
21
|
- **Nested grids** — drop a grid inside a tile of another grid; each level keeps its own isolated drag session.
|
|
@@ -33,8 +33,11 @@ pnpm add @snapgridjs/react @dnd-kit/react @dnd-kit/dom
|
|
|
33
33
|
|
|
34
34
|
## Quick start
|
|
35
35
|
|
|
36
|
+
snapgrid is **headless-first**: you compose hooks with a dnd-kit `DragDropProvider` and render your own markup.
|
|
37
|
+
|
|
36
38
|
```tsx
|
|
37
|
-
import {
|
|
39
|
+
import { DragDropProvider } from "@dnd-kit/react";
|
|
40
|
+
import { type Layout, useContainerWidth, useGridContainer, useGridItem } from "@snapgridjs/react";
|
|
38
41
|
import { useState } from "react";
|
|
39
42
|
|
|
40
43
|
export function Board() {
|
|
@@ -45,20 +48,53 @@ export function Board() {
|
|
|
45
48
|
{ i: "c", x: 8, y: 0, w: 4, h: 2 },
|
|
46
49
|
]);
|
|
47
50
|
|
|
51
|
+
// You supply the dnd-kit provider; useGridContainer runs inside it (in Surface).
|
|
48
52
|
return (
|
|
49
53
|
<div ref={containerRef}>
|
|
50
|
-
<
|
|
51
|
-
{layout
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
54
|
+
<DragDropProvider>
|
|
55
|
+
<Surface layout={layout} width={width} onLayoutChange={setLayout} />
|
|
56
|
+
</DragDropProvider>
|
|
57
|
+
</div>
|
|
58
|
+
);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function Surface({
|
|
62
|
+
layout,
|
|
63
|
+
width,
|
|
64
|
+
onLayoutChange,
|
|
65
|
+
}: { layout: Layout; width: number; onLayoutChange: (next: Layout) => void }) {
|
|
66
|
+
const { containerProps, group } = useGridContainer({ layout, width, onLayoutChange });
|
|
67
|
+
return (
|
|
68
|
+
<div {...containerProps}>
|
|
69
|
+
{layout.map((it) => (
|
|
70
|
+
<Tile key={it.i} id={it.i} group={group} />
|
|
71
|
+
))}
|
|
72
|
+
</div>
|
|
73
|
+
);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
function Tile({ id, group }: { id: string; group: string }) {
|
|
77
|
+
const { ref, style } = useGridItem(id, group);
|
|
78
|
+
return (
|
|
79
|
+
<div ref={ref} style={style} className="tile">
|
|
80
|
+
{id}
|
|
57
81
|
</div>
|
|
58
82
|
);
|
|
59
83
|
}
|
|
60
84
|
```
|
|
61
85
|
|
|
86
|
+
**Prefer a ready-made component?** The turnkey [`<GridLayout>`](https://snapgrid.dev/docs/guides/component-layer) wraps these same hooks (and supplies the provider) in a react-grid-layout-style API:
|
|
87
|
+
|
|
88
|
+
```tsx
|
|
89
|
+
<GridLayout layout={layout} width={width} onLayoutChange={setLayout}>
|
|
90
|
+
{layout.map((item) => (
|
|
91
|
+
<div key={item.i} className="tile">
|
|
92
|
+
{item.i}
|
|
93
|
+
</div>
|
|
94
|
+
))}
|
|
95
|
+
</GridLayout>
|
|
96
|
+
```
|
|
97
|
+
|
|
62
98
|
→ Full walkthrough in [**Getting Started**](https://snapgrid.dev/docs/getting-started).
|
|
63
99
|
|
|
64
100
|
## License
|