— Layout & primitives · @vitus-labs/coolgrid
A grid you configure once.
Container, Row and Col, the grid you already know. Set columns, gap and gutter on the Container and every Row and Col below inherits them; override at any level. Sizes take a number, an array or a breakpoint map, and on React Native the same components measure themselves and compute pixel widths.
npm i @vitus-labs/coolgrid @vitus-labs/core @vitus-labs/unistyle- components: Container, Row, Col
- 3
- columns in the bundled theme, or any count
- 12
- same components on both
- web · native
— Why coolgrid
Set it high. Override it low.
Configuration cascades
columns, size, gap, padding, gutter, colCss, rowCss and the component overrides flow from Container to Row to Col through context. A Row can change the gap for its own columns without touching the rest of the page.
Responsive by value
<Col size={{ xs: 12, md: 8 }}>, <Row gap={[8, 16, 24]}>. Every prop goes through unistyle, so each breakpoint emits a mobile-first min-width rule with only what changed.
Gaps that add up
Columns size to size / columns minus the gap, and the Row's negative margins cancel the outer half-gaps. Rows of columns that sum to the column count fill the row exactly.
The same grid on native
On React Native, Row measures its width with onLayout and Col computes pixel widths from it, because calc() doesn't exist there. Your layout code doesn't change.
— Before / after
Same layout. No width math.
A main column and a sidebar that stack on phones, with a 16px gap. On the left the percentages, gutters and media query are written by hand; on the right they're props.
import { styled } from '@vitus-labs/styler'
const Row = styled.div`
display: flex;
flex-wrap: wrap;
margin: 0 -0.5rem;
`
const Col = styled.div`
flex: 0 0 100%;
max-width: 100%;
padding: 0 0.5rem;
box-sizing: border-box;
@media (min-width: 48em) {
flex-basis: ${({ $span }) => ($span / 12) * 100}%;
max-width: ${({ $span }) => ($span / 12) * 100}%;
}
`
<Row>
<Col $span={8}>Main</Col>
<Col $span={4}>Sidebar</Col>
</Row>Every column type carries its own percentage per breakpoint, and the gutter math has to match on Row and Col.
import { Col, Container, Provider, Row, theme } from '@vitus-labs/coolgrid'
<Provider theme={theme}>
<Container gap={16}>
<Row>
<Col size={{ xs: 12, md: 8 }}>Main</Col>
<Col size={{ xs: 12, md: 4 }}>Sidebar</Col>
</Row>
</Container>
</Provider>Breakpoints and the column count come from the theme, the gap from the Container. Each Col says only how many columns it spans.
— Live, not a screenshot
Move a column, read the CSS.
A real Container, Row and Col on this page's 12-column theme. Change the span, the gap or the column count; the rules next to it are read from the stylesheet the engine wrote.
import { Col, Container, Row } from '@vitus-labs/coolgrid'
// theme: { breakpoints, grid: { columns: 12 } } via Provider
<Container columns={columns} gap={gap}>
<Row>
<Col size={size}>…</Col>
<Col size={columns - size}>…</Col>
</Row>
<Row>
<Col>auto</Col>
<Col>auto</Col>
<Col>auto</Col>
</Row>
</Container><Container columns={12} gap={16}> … <Col size={8} />reading the stylesheet…
— Features
Everything a page grid needs.
- Container
centered with auto margins and a responsive
max-widthper breakpoint from the theme - width={fn}
derive a Container's widths from the theme's, or pass your own value or map
- Row
flex-wrap row; negative margins cancel the column gap,
guttersets the space between rows - Col size
spans
sizeofcolumns; a size above the column count is clamped to 100% - Auto columns
a Col without a size grows to share the remaining space equally
- size={0}
hides a column with
display: noneat that breakpoint, so it isn't focusable - Row size
a
sizeon Row (or Container) becomes the default for every Col inside - Any column count
12 from the bundled theme, or
columns={24},columns={5}at any level - contentAlignX
left,center,right,spaceBetween,spaceAroundorspaceEvenlyfor the columns of a row - colCss · rowCss
extra CSS for every Col or Row below, cascaded like the rest of the config
- Custom elements
component,rowComponentandcolComponentswap what each level renders - Theme fallback
props, then
theme.grid.*, thentheme.coolgrid.*for columns and container widths
— API
Three components. One theme.
Every name links to its reference in the docs.
— Honest limits
When a grid of columns is wrong.
Where it isn't the right fit, and what to reach for instead.
Two-dimensional layouts
Coolgrid is a flex-wrap column grid. Spans across rows, named areas and auto-placement belong to CSS grid; write it with
csson an Element or a styled component.No theme, no widths
Columns and container widths come from the theme passed to
Provider. Without one, and without acolumnsprop, a Col's size has nothing to divide by and the columns share the space equally. Pass the bundledthemeor your own.Breakpoints follow the viewport
Sizes switch on viewport
min-widthqueries, not on the width of the parent. A grid inside a narrow sidebar still uses the page breakpoints; use container queries there.Native widths arrive after layout
On React Native a Col's width depends on the Row's measured width, so it gets its width after the first
onLayout, not on the very first frame.
— Recipes
Copy, paste, ship.
A page layout
Gap and padding set once on the Container. Columns stack on phones and sit side by side from md.
import { Col, Container, Provider, Row, theme } from '@vitus-labs/coolgrid'
export const Page = () => (
<Provider theme={theme}>
<Container gap={24} padding={16}>
<Row>
<Col size={{ xs: 12, md: 3 }}><Nav /></Col>
<Col size={{ xs: 12, md: 9 }}><Article /></Col>
</Row>
</Container>
</Provider>
)Cards that reflow
A size on the Row applies to every Col in it. Arrays map to breakpoints in order: one, two, then three per row.
<Container gap={16} gutter={24}>
<Row size={[12, 6, 4]}>
{cards.map((c) => (
<Col key={c.id}>
<Card {...c} />
</Col>
))}
</Row>
</Container>Hide a column on small screens
size={0} renders display: none at that breakpoint.
<Row>
<Col size={{ xs: 0, lg: 3 }}><Filters /></Col>
<Col size={{ xs: 12, lg: 9 }}><Results /></Col>
</Row>Your own breakpoints and widths
Any breakpoint names, any column count. Coolgrid reads grid.columns and grid.container from the same theme.
export const theme = {
rootSize: 16,
breakpoints: { phone: 0, tablet: 600, desktop: 1024, wide: 1440 },
grid: {
columns: 24,
container: { phone: '100%', desktop: 960, wide: 1320 },
},
}— FAQ
Questions, answered.
What's the difference between gap and gutter?
gap is the horizontal space between columns; each Col gets half of it as margin and the Row cancels the outer halves. gutter sets the vertical space between rows.
Is 12 columns the default?
It's the bundled theme's value. Coolgrid has no built-in column count: it reads columns from props or the theme passed to Provider.
Can I nest grids?
Yes. Put a Container inside a Col: a Container reads only its own props and the theme, so the nested grid can use its own gap or column count. A bare Row inside a Col inherits the outer Container's settings instead.
Is this site built with it?
Yes. The sections of this page are laid out with coolgrid's Row and Col on a 12-column theme.
— @vitus-labs/coolgrid
Configure the grid once. Just place columns.
npm i @vitus-labs/coolgrid @vitus-labs/core @vitus-labs/unistyleSources · 32 claims, each traced to v2.9.1 code or docs
- Exports Col, Container, Provider, Row, themeui-system:packages/coolgrid/src/index.ts
- Bundled theme: Bootstrap-like breakpoints, 12 columns, container widthsui-system:packages/coolgrid/src/theme.ts
- 12 columns in the bundled themeui-system:packages/coolgrid/src/theme.ts
- Context keys cascade Container → Row → Colui-system:packages/coolgrid/src/constants.ts
- Cascaded keys include columns, size, gap, padding, gutter, colCss, rowCss, components, contentAlignXui-system:packages/coolgrid/src/constants.ts
- Columns and container width: props, then theme.grid.*, then theme.coolgrid.*ui-system:packages/coolgrid/src/useContext.tsx
- Responsive values through unistyle makeItResponsiveui-system:packages/coolgrid/src/Col/styled.ts
- Col width = size / columns minus the gapui-system:packages/coolgrid/src/Col/styled.ts
- Size clamped to the column countui-system:packages/coolgrid/src/Col/styled.ts
- Col without size grows equally (flex-basis 0, flex-grow 1)ui-system:packages/coolgrid/src/Col/styled.ts
- size 0 hides the column with display: noneui-system:packages/coolgrid/src/utils.ts
- Hidden columns are not focusable (display: none since 2.8)ui-system:packages/coolgrid/CHANGELOG.md
- Gap and padding split in half per columnui-system:packages/coolgrid/src/Col/styled.ts
- Row negative margins cancel the gap; gutter is inter-row spacingui-system:packages/coolgrid/src/Row/styled.ts
- Container centered with auto margins and responsive max-widthui-system:packages/coolgrid/src/Container/styled.ts
- Container width can be a function of the theme widthsui-system:packages/coolgrid/src/Container/component.tsx
- Row size and other config become defaults for Cols via contextui-system:packages/coolgrid/src/Row/component.tsx
- Container reads only its props and the theme; Row reads the Container contextui-system:packages/coolgrid/src/Row/component.tsx
- component / rowComponent / colComponent overridesui-system:packages/coolgrid/src/Col/component.tsx
- contentAlignX type includes spaceEvenlyui-system:packages/coolgrid/src/types.ts
- Alignment map covers spaceBetween, spaceAround and spaceEvenly (space-evenly)ui-system:packages/unistyle/src/styles/alignContent.ts
- Row maps contentAlignX through ALIGN_CONTENT_MAP_Xui-system:packages/coolgrid/src/Row/styled.ts
- Breakpoints are viewport min-width media queriesui-system:packages/unistyle/src/responsive/createMediaQueries.ts
- Native Row measures width with onLayout; Col computes pixel widthsui-system:packages/coolgrid/src/Row/component.native.tsx
- Native Col skips width until onLayout provides the parent widthui-system:packages/coolgrid/src/Col/styled.ts
- Grid context memoized with a content-stable valueui-system:packages/coolgrid/src/useContext.tsx
- Peer dependencies: react, core, unistyle, optional react-native >= 0.76npm:@vitus-labs/coolgrid/package.json
- README: gutter is the vertical space between Rowsnpm:@vitus-labs/coolgrid/README.md
- This site's grid comes from coolgridsite:src/ui/Grid.tsx
- This site's theme: 12 columnssite:src/ui/theme.ts
- Product page sections laid out with Row / Colsite:src/components/products/ProductPage.tsx
- Demo reads the inserted Row / Col rulessite:src/components/products/demos/CoolgridDemo.tsx