🚀ReacUI is currently in Beta. We're actively improving it! ❣️

Grid

A flexible grid layout system built on CSS Grid with responsive capabilities

1
2
3
4
5
6

Installation

The Grid component is part of the ReacUI library. Install the package to use it in your project.

npm install reacui

Import

Import the Grid components into your React component.

import { Grid, GridItem } from 'reacui';

Basic Usage

The Grid component uses CSS Grid to create flexible layouts. You can specify the number of columns and gap size.

1
2
3
4
<Grid columns={2} gap={4}>
  <div className="bg-blue-100 dark:bg-blue-900/30 p-4 rounded-lg">1</div>
  <div className="bg-blue-100 dark:bg-blue-900/30 p-4 rounded-lg">2</div>
  <div className="bg-blue-100 dark:bg-blue-900/30 p-4 rounded-lg">3</div>
  <div className="bg-blue-100 dark:bg-blue-900/30 p-4 rounded-lg">4</div>
</Grid>

Responsive Grid

You can make a grid responsive by specifying different column counts at various breakpoints.

1
2
3
4
<Grid columns={1} sm={2} md={3} lg={4} gap={4}>
  <div className="bg-blue-100 dark:bg-blue-900/30 p-4 rounded-lg">1</div>
  <div className="bg-blue-100 dark:bg-blue-900/30 p-4 rounded-lg">2</div>
  <div className="bg-blue-100 dark:bg-blue-900/30 p-4 rounded-lg">3</div>
  <div className="bg-blue-100 dark:bg-blue-900/30 p-4 rounded-lg">4</div>
</Grid>

Grid Items

Use GridItem to control how individual items span across rows and columns of the grid.

Span 2 columns
1 column
1 column
Span 2 columns
<Grid columns={3} gap={4}>
  <GridItem colSpan={2}>
    <div className="bg-blue-100 dark:bg-blue-900/30 p-4 rounded-lg">Span 2 columns</div>
  </GridItem>
  <GridItem>
    <div className="bg-blue-100 dark:bg-blue-900/30 p-4 rounded-lg">1 column</div>
  </GridItem>
  <GridItem>
    <div className="bg-blue-100 dark:bg-blue-900/30 p-4 rounded-lg">1 column</div>
  </GridItem>
  <GridItem colSpan={2}>
    <div className="bg-blue-100 dark:bg-blue-900/30 p-4 rounded-lg">Span 2 columns</div>
  </GridItem>
</Grid>

Grid Alignment

Control how items are aligned within the grid using alignment properties.

1
2
3
<Grid 
  columns={3} 
  gap={4} 
  alignItems="center" 
  justifyItems="center"
  className="h-32"
>
  <div className="bg-blue-100 dark:bg-blue-900/30 p-4 rounded-lg">1</div>
  <div className="bg-blue-100 dark:bg-blue-900/30 p-4 rounded-lg">2</div>
  <div className="bg-blue-100 dark:bg-blue-900/30 p-4 rounded-lg">3</div>
</Grid>

API Reference

The following props are available for the Grid and GridItem components.

Grid Props

PropTypeDefaultDescription
columnsnumber1Number of columns in the grid
gapnumber4Space between grid items (in Tailwind's spacing scale)
rowGapnumber-Space between rows (overrides gap for rows)
columnGapnumber-Space between columns (overrides gap for columns)
smnumber-Number of columns at the small breakpoint
mdnumber-Number of columns at the medium breakpoint
lgnumber-Number of columns at the large breakpoint
xl--Number of columns at the extra-large breakpoint
alignItemsstring'stretch'Vertical alignment of items: 'start', 'center', 'end', 'stretch'
justifyItemsstring'start'Horizontal alignment of items: 'start', 'center', 'end', 'stretch'
justifyContentstring'start'Content distribution along the horizontal axis
alignContentstring'start'Content distribution along the vertical axis

GridItem Props

PropTypeDefaultDescription
colSpannumber1Number of columns this item should span
rowSpannumber1Number of rows this item should span
colStartnumber-Column start position (1-based)
colEndnumber-Column end position (inclusive)
rowStartnumber-Row start position (1-based)
rowEndnumber-Row end position (inclusive)