Grid
A flexible grid component for React applications. This component uses CSS Grid to create layouts with automatic positioning of elements.
Installation
yarn add @automattic/grid
Usage
import { Grid } from '@automattic/grid';
const MyLayout = () => {
const layout = [
{ key: 'a', width: 1 },
{ key: 'b', width: 3 },
{ key: 'c', width: 1 },
];
return (
<Grid layout={ layout } columns={ 6 }>
<div key="a">a</div>
<div key="b">b</div>
<div key="c">c</div>
</Grid>
);
};
API
Grid Component
The main component exported by this package.
Props
layout(required): An array of layout items with the following properties:key(string): A unique identifier that matches thekeyprop of the corresponding child componentwidth(number): The number of columns this item spansheight(number, optional): The number of rows this item spans (defaults to 1)order(number, optional): In responsive mode, determines the order of items (lower values displayed first)fullWidth(boolean, optional): In responsive mode, forces an item to always span all available columns
columns(required): Total number of columns in the gridclassName(optional): Additional CSS class to apply to the grid containerspacing(optional): Grid gap multiplier size, defaults to 2 (e.g. A spacing of 2 results in a gap of 8px, it's multiplied by 4)rowHeight(optional): Height of each row in pixels or auto (e.g., 50, "auto")minColumnWidth(optional): Minimum width in pixels for each column; when provided, enables responsive mode that automatically adjusts columns based on container width
Standard vs. Responsive Mode
The Grid component can operate in two modes:
Standard Mode (Default)
In standard mode, items are positioned automatically in a grid with the specified number of columns. Each item can span multiple columns and rows.
// Standard layout with 6 columns
<Grid
layout={ [
{ key: 'a', width: 2 },
{ key: 'b', width: 4 },
] }
columns={ 6 }
>
<div key="a">Item A</div>
<div key="b">Item B</div>
</Grid>
Responsive Mode
When minColumnWidth is provided, the Grid activates responsive mode, which automatically adjusts the number of columns based on the container width. In this mode:
- Items flow according to their
orderproperty (or original array order if not specified) - The grid will use the available space to fit as many columns as possible, based on the
minColumnWidth - Items that can't fit on a row will wrap to the next row
- Items with
fullWidthset totruewill always span all available columns
// Responsive layout that adapts to container width
<Grid
layout={ [
{ key: 'a', width: 2, order: 1 },
{ key: 'b', width: 2, order: 2 },
{ key: 'c', width: 4, order: 3, fullWidth: true },
] }
minColumnWidth={ 150 } // Each column should be at least 150px
>
<div key="a">Item A</div>
<div key="b">Item B</div>
<div key="c">Item C (always full width)</div>
</Grid>