— 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.

@vitus-labs/coolgridv2.9.1 · MIT
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.

01

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.

02

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.

03

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.

04

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.

beforeHand-rolled flex grid
Layout.tsx
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.

after@vitus-labs/coolgrid
Layout.tsx
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.

size=8
size=4
auto
auto
auto
columns
size
gap
Layout.tsx
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} />
Row + Col ruleslive
reading the stylesheet…

— Features

Everything a page grid needs.

  • Container

    centered with auto margins and a responsive max-width per 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, gutter sets the space between rows

  • Col size

    spans size of columns; 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: none at that breakpoint, so it isn't focusable

  • Row size

    a size on 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, spaceAround or spaceEvenly for 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, rowComponent and colComponent swap what each level renders

  • Theme fallback

    props, then theme.grid.*, then theme.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 css on 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 a columns prop, a Col's size has nothing to divide by and the columns share the space equally. Pass the bundled theme or your own.

  • Breakpoints follow the viewport

    Sizes switch on viewport min-width queries, 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.

01

A page layout

Gap and padding set once on the Container. Columns stack on phones and sit side by side from md.

Page.tsx
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>
)
02

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.

Cards.tsx
<Container gap={16} gutter={24}>
  <Row size={[12, 6, 4]}>
    {cards.map((c) => (
      <Col key={c.id}>
        <Card {...c} />
      </Col>
    ))}
  </Row>
</Container>
03

Hide a column on small screens

size={0} renders display: none at that breakpoint.

Dashboard.tsx
<Row>
  <Col size={{ xs: 0, lg: 3 }}><Filters /></Col>
  <Col size={{ xs: 12, lg: 9 }}><Results /></Col>
</Row>
04

Your own breakpoints and widths

Any breakpoint names, any column count. Coolgrid reads grid.columns and grid.container from the same theme.

theme.ts
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/unistyle
Sources · 32 claims, each traced to v2.9.1 code or docs
  1. Exports Col, Container, Provider, Row, themeui-system:packages/coolgrid/src/index.ts
  2. Bundled theme: Bootstrap-like breakpoints, 12 columns, container widthsui-system:packages/coolgrid/src/theme.ts
  3. 12 columns in the bundled themeui-system:packages/coolgrid/src/theme.ts
  4. Context keys cascade Container → Row → Colui-system:packages/coolgrid/src/constants.ts
  5. Cascaded keys include columns, size, gap, padding, gutter, colCss, rowCss, components, contentAlignXui-system:packages/coolgrid/src/constants.ts
  6. Columns and container width: props, then theme.grid.*, then theme.coolgrid.*ui-system:packages/coolgrid/src/useContext.tsx
  7. Responsive values through unistyle makeItResponsiveui-system:packages/coolgrid/src/Col/styled.ts
  8. Col width = size / columns minus the gapui-system:packages/coolgrid/src/Col/styled.ts
  9. Size clamped to the column countui-system:packages/coolgrid/src/Col/styled.ts
  10. Col without size grows equally (flex-basis 0, flex-grow 1)ui-system:packages/coolgrid/src/Col/styled.ts
  11. size 0 hides the column with display: noneui-system:packages/coolgrid/src/utils.ts
  12. Hidden columns are not focusable (display: none since 2.8)ui-system:packages/coolgrid/CHANGELOG.md
  13. Gap and padding split in half per columnui-system:packages/coolgrid/src/Col/styled.ts
  14. Row negative margins cancel the gap; gutter is inter-row spacingui-system:packages/coolgrid/src/Row/styled.ts
  15. Container centered with auto margins and responsive max-widthui-system:packages/coolgrid/src/Container/styled.ts
  16. Container width can be a function of the theme widthsui-system:packages/coolgrid/src/Container/component.tsx
  17. Row size and other config become defaults for Cols via contextui-system:packages/coolgrid/src/Row/component.tsx
  18. Container reads only its props and the theme; Row reads the Container contextui-system:packages/coolgrid/src/Row/component.tsx
  19. component / rowComponent / colComponent overridesui-system:packages/coolgrid/src/Col/component.tsx
  20. contentAlignX type includes spaceEvenlyui-system:packages/coolgrid/src/types.ts
  21. Alignment map covers spaceBetween, spaceAround and spaceEvenly (space-evenly)ui-system:packages/unistyle/src/styles/alignContent.ts
  22. Row maps contentAlignX through ALIGN_CONTENT_MAP_Xui-system:packages/coolgrid/src/Row/styled.ts
  23. Breakpoints are viewport min-width media queriesui-system:packages/unistyle/src/responsive/createMediaQueries.ts
  24. Native Row measures width with onLayout; Col computes pixel widthsui-system:packages/coolgrid/src/Row/component.native.tsx
  25. Native Col skips width until onLayout provides the parent widthui-system:packages/coolgrid/src/Col/styled.ts
  26. Grid context memoized with a content-stable valueui-system:packages/coolgrid/src/useContext.tsx
  27. Peer dependencies: react, core, unistyle, optional react-native >= 0.76npm:@vitus-labs/coolgrid/package.json
  28. README: gutter is the vertical space between Rowsnpm:@vitus-labs/coolgrid/README.md
  29. This site's grid comes from coolgridsite:src/ui/Grid.tsx
  30. This site's theme: 12 columnssite:src/ui/theme.ts
  31. Product page sections laid out with Row / Colsite:src/components/products/ProductPage.tsx
  32. Demo reads the inserted Row / Col rulessite:src/components/products/demos/CoolgridDemo.tsx