A draggable and resizable grid layout with responsive breakpoints, for React.
A draggable and resizable grid layout with responsive breakpoints, for React.
React-Grid-Layout is a grid layout system much like Packery or Gridster, for React.
Unlike those systems, it is responsive and supports breakpoints. Breakpoint layouts can be provided by the user or autogenerated.
RGL is React-only and does not require jQuery.
GIF from production usage on BitMEX.com
[Demo | Changelog | CodeSandbox Editable demo]
Version 2 is a complete TypeScript rewrite with a modernized API:
@types/react-grid-layoutuseContainerWidth, useGridLayout, and useResponsiveLayout hooksgridConfig - cols, rowHeight, margin, paddingdragConfig - enable, handle, cancel, boundedresizeConfig - enable, handlespositionStrategy - transform vs absolute positioningcompactor - vertical, horizontal, or custom algorithmsreact-grid-layout - React components and hooks (v2 API)react-grid-layout/core - Pure layout algorithms (framework-agnostic)react-grid-layout/legacy - v1 flat props API for migrationreact-grid-layout/extras - Optional components like GridBackgroundSee the RFC for detailed migration examples.
| Change | Description |
|---|---|
width prop required |
Use useContainerWidth hook or provide your own measurement |
onDragStart threshold |
Now fires after 3px movement, not on mousedown. Use onMouseDown for immediate response |
| Immutable callbacks | Callback parameters are read-only. Use onLayoutChange or constraints instead of mutation |
data-grid in legacy only |
v2 requires explicit layout prop. Use legacy wrapper for data-grid |
| Pluggable compaction | Compaction is now pluggable via Compactor interface. Optional fast O(n log n) algorithm in /extras |
| UMD bundle removed | Use a bundler (Vite, webpack, esbuild) |
verticalCompact removed |
Use compactType={null} or compactor={noCompactor} |
Quick migration - change your import to use the legacy wrapper:
- import GridLayout, { Responsive, WidthProvider } from 'react-grid-layout';
+ import GridLayout, { Responsive, WidthProvider } from 'react-grid-layout/legacy';This provides 100% runtime API compatibility with v1.
TypeScript users: If you were using @types/react-grid-layout, note that v2 includes its own types with some naming changes:
Old (@types/react-grid-layout) |
New (v2) | Notes |
|---|---|---|
RGL.Layout |
LayoutItem |
Single grid item |
RGL.Layout[] |
Layout |
Array of items |
RGL.Layouts |
ResponsiveLayouts |
Breakpoint → layout map |
- import RGL from 'react-grid-layout';
- const item: RGL.Layout = { i: 'a', x: 0, y: 0, w: 1, h: 1 };
- const layouts: RGL.Layouts = { lg: [item] };
+ import { LayoutItem, ResponsiveLayouts } from 'react-grid-layout/legacy';
+ const item: LayoutItem = { i: 'a', x: 0, y: 0, w: 1, h: 1 };
+ const layouts: ResponsiveLayouts = { lg: [item] };Full migration - adopt the v2 API for new features and better tree-shaking:
import ReactGridLayout, { useContainerWidth, verticalCompactor } from 'react-grid-layout';
function MyGrid() {
const { width, containerRef, mounted } = useContainerWidth();
return (
);
}| Use Case | Recommendation |
|---|---|
| Existing v1 codebase | react-grid-layout/legacy |
| New project | v2 API with hooks |
| Custom compaction | v2 with custom Compactor |
| SSR | v2 with measureBeforeMount: true |
Know of others? Create a PR to let me know!
<React.StrictMode>| Version | Compatibility |
|---|---|
| >= 2.0.0 | React 18+, TypeScript |
| >= 0.17.0 | React 16 & 17 |
npm install react-grid-layoutInclude the stylesheets in your application:
import "react-grid-layout/css/styles.css";
import "react-resizable/css/styles.css";Or link them directly:
<link rel="stylesheet" href="/node_modules/react-grid-layout/css/styles.css" />
<link rel="stylesheet" href="/node_modules/react-resizable/css/styles.css" />import ReactGridLayout, { useContainerWidth } from "react-grid-layout";
import "react-grid-layout/css/styles.css";
import "react-resizable/css/styles.css";
function MyGrid() {
const { width, containerRef, mounted } = useContainerWidth();
const layout = [
{ i: "a", x: 0, y: 0, w: 1, h: 2, static: true },
{ i: "b", x: 1, y: 0, w: 3, h: 2, minW: 2, maxW: 4 },
{ i: "c", x: 4, y: 0, w: 1, h: 2 }
];
return (
</ReactGridLayout>
)}
</div>
);
}You can also define layout on children using data-grid:
<ReactGridLayout width={width} gridConfig={{ cols: 12, rowHeight: 30 }}>
</ReactGridLayout>Use Responsive for automatic breakpoint handling:
import { Responsive, useContainerWidth } from "react-grid-layout";
function MyResponsiveGrid() {
const { width, containerRef, mounted } = useContainerWidth();
const layouts = {
lg: [{ i: "1", x: 0, y: 0, w: 2, h: 2 }],
md: [{ i: "1", x: 0, y: 0, w: 2, h: 2 }]
};
return (
</Responsive>
)}
</div>
);
}The width prop is required. You have several options:
import ReactGridLayout, { useContainerWidth } from "react-grid-layout";
function MyGrid() {
const { width, containerRef, mounted } = useContainerWidth();
return (
);
}<ReactGridLayout width={1200}>...</ReactGridLayout>onDragStop is dropped when the release arrives before the threshold-crossing move renders (placeholder stays on the grid)
Unable to provide custom resize handle in v2
Dropping element from outside mid-drag causes placeholder to freeze
Dragging from Outside: droppingItem does not removed on leaving react-grid area whlile mouseDown still pressed in DEV mode.
Example 15 not working as expected
RTL Support for react-grid-layout
[Keyboard Navigation - Data Explorer -Dashboards (Preview)]: Resizable handle present on each tile on "Sample Demo Dashboard" page is not accessible through keyboard.
Tiles jumps to next line when Compaction Type is Horizontal
Bug with moving items breakpoints
issue: compactType can support both horizontal and vertical ?