Local storage
dnd-grid/localstorage-exampleFreeComponent
Persist layout to local storage and restore on load.
Install it
npx shadcn@latest add https://dnd-grid.com/r/localstorage-example.jsonSource
1"use client";23import { DndGrid, type Layout, layoutSchema } from "@dnd-grid/react";4import { useEffect, useState } from "react";56const STORAGE_KEY = "dnd-grid-layout";78const defaultLayout: Layout = [0, 1, 2, 3, 4, 5].map((i) => ({9 id: i.toString(),10 x: (i * 2) % 12,11 y: Math.floor(i / 6) * 2,12 w: 2,13 h: 2,14}));1516const normalizeLayout = (value: unknown): Layout | null => {17 if (!Array.isArray(value)) {18 return null;19 }2021 const normalized = value.map((item, index) => {22 if (!item || typeof item !== "object") {23 return null;24 }25 const record = item as Record<string, unknown>;26 const candidate = record.id ?? record.i ?? record.key;2728 let id: string;29 if (typeof candidate === "string" && candidate.trim().length > 0) {30 id = candidate;31 } else if (typeof candidate === "number" && Number.isFinite(candidate)) {32 id = candidate.toString();33 } else {34 id = `item-${index}`;35 }3637 return {38 ...record,39 id,40 };41 });4243 if (normalized.some((item) => item === null)) {44 return null;45 }4647 const parsed = layoutSchema.safeParse(normalized);48 return parsed.success ? (parsed.data as Layout) : null;49};5051export function LocalStorageExample() {52 const [layout, setLayout] = useState<Layout>(defaultLayout);5354 useEffect(() => {55 const saved = localStorage.getItem(STORAGE_KEY);56 if (!saved) {57 return;58 }59 try {60 const parsed = JSON.parse(saved);61 const normalized = normalizeLayout(parsed);62 if (normalized) {63 setLayout(normalized);64 return;65 }66 } catch {67 // ignore and fall back to default layout68 }69 setLayout(defaultLayout);70 localStorage.removeItem(STORAGE_KEY);71 }, []);7273 const handleLayoutChange = (newLayout: Layout) => {74 setLayout(newLayout);75 localStorage.setItem(STORAGE_KEY, JSON.stringify(newLayout));76 };7778 const resetLayout = () => {79 setLayout(defaultLayout);80 localStorage.removeItem(STORAGE_KEY);81 };8283 return (84 <div>85 <button onClick={resetLayout} type="button">86 Reset Layout87 </button>8889 <DndGrid90 cols={12}91 layout={layout}92 onLayoutChange={handleLayoutChange}93 rowHeight={50}94 >95 {layout.map((item) => (96 <div className="grid-item" key={item.id}>97 {item.id}98 </div>99 ))}100 </DndGrid>101 </div>102 );103}
What it pulls in
npm packages
Files it writes
More from dnd-grid
All 19 items| Component | Registry | Kind | Access | Installs | Command |
|---|---|---|---|---|---|
| Allow overlapallow-overlap-example | dnd-grid | Components | Free | 3 deps | |
| Aspect ratio constraintsaspect-ratio-constraints-example | dnd-grid | Components | Free | 3 deps | |
| Basic examplebasic-example | dnd-grid | Components | Free | 3 deps | |
| Boundedbounded-example | dnd-grid | Components | Free | 3 deps | |
| Compactor showcasecompactor-showcase-example | dnd-grid | Components | Free | 3 deps | |
| Compositioncomposition-example | dnd-grid | Components | Free | 3 deps | |
| Constraintsconstraints-example | dnd-grid | Components | Free | 3 deps | |
| Drag from outsidedrag-from-outside-example | dnd-grid | Components | Free | 3 deps |
