Skip to Content
vitus·labs
Guides

Responsive Layouts

Breakpoints and rootSize, responsive values in themes and props, Element layout props, the coolgrid Container/Row/Col grid, and how it all maps to React Native.

Responsive styling in the UI system works the same way everywhere. You set breakpoints once in the theme. Then any supported value, whether a theme property, an Element prop or a grid prop, can be a scalar, an array or a breakpoint object. Unistyle turns these into mobile-first @media rules, so layout is correct in server-rendered and static HTML with no JavaScript measuring.

1. Breakpoints and rootSize

Responsive values need two things from the theme:

  • breakpoints: name → minimum width in px. The smallest one (usually xs: 0) is the baseline with no media query.
  • rootSize: the root font size in px. Unistyle converts numeric lengths to rem with it (24 → 1.5rem with rootSize: 16). Media queries use em units (md: 768 → min-width: 48em).

They must reach components through unistyle's Provider, or a provider that delegates to it. It sorts the breakpoints and precomputes the media helpers. Without it, responsive arrays and objects are not expanded.

src/ui/Provider.tsx
'use client'

import './init'
import { Provider } from '@vitus-labs/unistyle'
import type { ReactNode } from 'react'

const theme = {
  rootSize: 16,
  breakpoints: { xs: 0, sm: 576, md: 768, lg: 992, xl: 1200 },
}

export function UIProvider({ children }: { children: ReactNode }) {
  return <Provider theme={theme}>{children}</Provider>
}

You can start from the defaults: unistyle exports breakpoints ({ rootSize: 16, breakpoints: { xs … xxl } }) and coolgrid exports theme (the same plus a grid section, see below).

With rocketstyle, pass unistyle's provider through: <Provider provider={UnistyleProvider} theme={theme} mode={mode}>. See Theming & Dark Mode.

Breakpoint names are free-form. { mobile: 0, tablet: 600, desktop: 1024 } works the same way, and responsive objects then use those keys.

2. Responsive value formats

Every responsive value accepts three forms (details):

// scalar: every breakpoint
{ fontSize: 16 }

// array: by position, mobile-first; the last value carries forward
{ fontSize: [14, 16, 18] }           // xs: 14, sm: 16, md+: 18
{ fontSize: [14, null, 18] }         // xs: 14, sm: 14 (hole inherits), md+: 18

// object: by breakpoint name; missing breakpoints inherit the previous one
{ fontSize: { xs: 14, lg: 18 } }     // xs–md: 14, lg+: 18

Write the mobile layout first and override upwards. Each breakpoint only emits the declarations that change, so a repeated value produces no extra CSS.

In rocketstyle themes

A rocketstyle theme becomes CSS through makeItResponsive (as in the themeToCss helper from Building a Design System), so theme values can be responsive too:

export const Section = rs({ name: 'Section', component: Element })
  .attrs({ tag: 'section', block: true })
  .theme({
    paddingX: [16, 24, 32],              // 1rem → 1.5rem → 2rem
    paddingY: { xs: 48, lg: 96 },
    fontSize: [15, null, 17],
  })
  .styles(themeToCss)

In plain styled components

Use makeItResponsive directly with a theme prop:

import { config } from '@vitus-labs/core'
import { makeItResponsive, styles } from '@vitus-labs/unistyle'

const Box = config.styled('div')`
  ${makeItResponsive({ key: '$box', css: config.css, styles })}
`

<Box $box={{ display: 'grid', gap: [8, 16], gridTemplateColumns: ['1fr', null, '1fr 1fr'] }} />

3. Layout with Element

Element layout props are responsive: direction, alignX, alignY, gap, block, equalCols and the content* / beforeContent* / afterContent* variants.

A media object that stacks on phones and goes side by side from md:

import { Element, Text } from '@vitus-labs/elements'

<Element
  block
  beforeContent={<Avatar />}
  afterContent={<FollowButton />}
  direction={{ xs: 'rows', md: 'inline' }}
  alignX={{ xs: 'center', md: 'left' }}
  alignY="center"
  gap={[12, null, 24]}
>
  <Text tag="h3">Ada Lovelace</Text>
</Element>

gap is the space between the slots. It follows the direction: vertical margin in rows, horizontal margin in inline.

Which props apply

With beforeContent or afterContent, direction / alignX / alignY lay out the three slots. Without side content, Element renders one wrapper, which takes contentDirection / contentAlignX / contentAlignY (contentDirection defaults to 'rows'). To lay out plain children responsively, use the content* props:

<Element block contentDirection={['rows', null, 'inline']} contentAlignX="spaceBetween">
  <Logo />
  <Nav />
</Element>

4. Grids with coolgrid

Coolgrid provides a Container → Row → Col grid. Every grid prop accepts the same responsive formats.

Provide the grid theme

Coolgrid has no built-in column count. It reads columns from props or from theme.grid.columns, and container widths from theme.grid.container. Pass its default theme, or your own with a grid section:

import { Col, Container, Provider, Row, theme } from '@vitus-labs/coolgrid'

// theme = rootSize + breakpoints + grid.columns (12) + grid.container widths
<Provider theme={theme}>
  <Container>
    <Row gap={16}>
      <Col size={4}>Column 1</Col>
      <Col size={4}>Column 2</Col>
      <Col size={4}>Column 3</Col>
    </Row>
  </Container>
</Provider>

coolgrid's Provider is unistyle's Provider, so one provider covers the grid, Element and rocketstyle. To combine it with your own tokens, merge the objects:

src/ui/theme.ts
import { theme as gridTheme } from '@vitus-labs/coolgrid'

export const theme = {
  ...gridTheme,
  breakpoints: { xs: 0, sm: 560, md: 900, lg: 1100, xl: 1440 },
  grid: {
    columns: 12,
    container: { xs: '100%', xl: 1440 },
  },
  // …your colors, fonts, spacing
}

Without columns from a prop or the theme, size has nothing to divide by and every Col falls back to auto width.

Responsive columns

<Container>
  <Row gap={[12, null, 24]} gutter={16}>
    <Col size={{ xs: 12, md: 6, lg: 4 }}>Card 1</Col>
    <Col size={{ xs: 12, md: 6, lg: 4 }}>Card 2</Col>
    <Col size={{ xs: 12, md: 12, lg: 4 }}>Card 3</Col>
  </Row>
</Container>
  • size: column span. size={0} hides the column at that breakpoint. A Col without size grows to fill the remaining space. A size larger than columns is clamped to 100%.
  • gap: horizontal space between columns. gutter: vertical space between rows. padding: inner padding of each column. Numbers become rem.
  • columns can also change per breakpoint: <Container columns={[4, null, 12]}>.

A sidebar that disappears on phones:

<Row>
  <Col size={{ xs: 0, md: 3 }}>Sidebar</Col>
  <Col size={{ xs: 12, md: 9 }}>Main content</Col>
</Row>

Cascading configuration

Container and Row pass columns, size, gap, padding, gutter, colCss, rowCss and contentAlignX down through context, and every level can override them. Set the layout once:

<Container gap={16} size={{ xs: 12, sm: 6, lg: 3 }}>
  <Row>
    {products.map((p) => (
      <Col key={p.id}>
        <ProductCard {...p} />
      </Col>
    ))}
  </Row>
</Container>

The container width comes from the width prop or theme.grid.container, and accepts the same responsive formats: <Container width={{ xs: '100%', lg: 960 }}>.

5. Responsive logic in JavaScript

Prefer responsive values. They compile to CSS, so they are right in the first paint. When you really need to branch in JavaScript (for example to render a different component), use useBreakpoint() or useMediaQuery():

import { useBreakpoint } from '@vitus-labs/hooks'

function Navigation() {
  const bp = useBreakpoint() // 'xs' | 'sm' | 'md' | … from theme.breakpoints
  return bp === 'xs' || bp === 'sm' ? <MobileMenu /> : <DesktopMenu />
}

Both are hydration-safe. During SSR and hydration useBreakpoint() returns the smallest breakpoint and useMediaQuery() returns false, then they update. On server-rendered or statically exported pages, the first paint therefore shows the mobile branch. See SSR & Static Export.

6. React Native

The same theme and the same responsive values work on React Native. Configure the native connector, including its createMediaQueries:

src/init.ts
import { init } from '@vitus-labs/core'
import * as connector from '@vitus-labs/connector-native'
import { Text, View } from 'react-native'

init({ ...connector, component: View, textComponent: Text })

What changes on native:

  • No @media. The native createMediaQueries checks Dimensions.get('window').width >= breakpoint when a style is resolved. Breakpoints are compared against the window width in density-independent pixels, not em.
  • Rotation and resize. Components with dynamic styles subscribe to useWindowDimensions() and re-render when the window changes. Fully static templates do not.
  • Units. Numeric theme values are output as px numbers, not rem. rem / em strings are not converted, so use numbers.
  • Grid. Row measures its own width with onLayout, and Col computes pixel widths from it (minus the gap) instead of CSS percentages. The coolgrid docs note the .native differences.
  • Hooks. useBreakpoint() reads the theme breakpoints and tracks Dimensions changes. useMediaQuery() is web-only.
  • Selectors. Nested blocks such as &:hover { … } or @media … { … } inside your own CSS are skipped. Style objects can't express them.

See the React Native connector for the full parsing rules.

Edit on GitHub

Last updated on

On this page