Blocks Design System

Box

The layout primitive. Every style prop maps to a design token, and every prop is responsive. Reach for Box when an element needs spacing, color, borders or flex layout; use Stack or Grid to lay out a list of children.

tsx
import { Box } from '@blocks/design-system'

Box has no client directive. It renders in a Server Component, a Client Component, or plain React without a framework.

Usage

Plan
Review
Source · box/basic.tsx
box/basic.tsx
import { Box } from '@blocks/design-system'

export default function Basic() {
  return (
    <Box display="flex" gap="md" padding="lg" backgroundColor="surface">
      <Box padding="md" backgroundColor="bg" border="default">
        Plan
      </Box>
      <Box padding="md" backgroundColor="bg" border="default">
        Review
      </Box>
    </Box>
  )
}

Rows and columns

Set display="flex" and flexDirection. The visual order follows the DOM order, which is also the reading and Tab order.

  1. Fetch diff
  2. Review
  3. Comment
  1. Fetch diff
  2. Review
  3. Comment
Source · box/direction.tsx
box/direction.tsx
import { Box } from '@blocks/design-system'

const steps = ['Fetch diff', 'Review', 'Comment']

export default function Direction() {
  return (
    <Box display="flex" flexDirection="column" gap="lg">
      <Box as="ol" display="flex" flexDirection="row" gap="sm" margin="none" padding="none" aria-label="Steps in a row">
        {steps.map((step) => (
          <Box as="li" key={step} padding="sm" border="default" dangerouslySetStyle={{ listStyle: 'none' }}>
            {step}
          </Box>
        ))}
      </Box>
      <Box as="ol" display="flex" flexDirection="column" gap="sm" margin="none" padding="none" aria-label="Steps in a column">
        {steps.map((step) => (
          <Box as="li" key={step} padding="sm" border="default" dangerouslySetStyle={{ listStyle: 'none' }}>
            {step}
          </Box>
        ))}
      </Box>
    </Box>
  )
}

Responsive values

Pass an object keyed by breakpoint instead of a single value: base applies everywhere, and sm (40rem), md (48rem), lg (64rem) and xl (80rem) apply from that width up. This summary stacks below md and becomes a row above it.

Source · box/responsive-summary.tsx
box/responsive-summary.tsx
import { Box } from '@blocks/design-system'

export default function ResponsiveSummary() {
  return (
    <Box
      as="article"
      aria-labelledby="summary-title"
      display="flex"
      flexDirection={{ base: 'column', md: 'row' }}
      alignItems={{ base: 'stretch', md: 'center' }}
      justifyContent="space-between"
      gap="md"
      padding="lg"
      border="default"
      backgroundColor="bg-subtle"
    >
      <Box>
        <Box as="h3" id="summary-title" margin="none">
          Mount Hood, 5,370 ft
        </Box>
        <Box as="p" margin="none" color="text-muted">
          Snow depth 84 in · updated 6:00 AM
        </Box>
      </Box>
      <Box as="a" href="#station-history" padding="sm" border="accent" textAlign="center">
        View station history
      </Box>
    </Box>
  )
}
Responsive station summary

Spacing

padding, margin and their X, Y, Top, Right, Bottom and Left variants take a space token, doubling from 1px: none, xxs, xs, sm, md, lg, xl, xxl, xxxl, xxxxl, xxxxxl. Margins also take "auto". maxWidth takes a measure token, sm (26rem), md (38rem) or lg (46rem).

paddingX="lg" paddingY="sm" marginBottom="md"
marginX="auto" maxWidth="sm"
Source · box/spacing.tsx
box/spacing.tsx
import { Box } from '@blocks/design-system'

export default function Spacing() {
  return (
    <Box backgroundColor="surface" padding={{ base: 'sm', md: 'lg' }}>
      <Box backgroundColor="bg" paddingX="lg" paddingY="sm" marginBottom="md" border="default">
        paddingX="lg" paddingY="sm" marginBottom="md"
      </Box>
      <Box backgroundColor="bg" padding="md" marginX="auto" maxWidth="sm" border="default">
        marginX="auto" maxWidth="sm"
      </Box>
    </Box>
  )
}

Rendering another element

as renders a different element and keeps that element’s attributes typed. Prefer the element with the right semantics, such as as="nav" or as="article", over adding a role.

Source · box/as-element.tsx
box/as-element.tsx
import { Box } from '@blocks/design-system'

export default function AsElement() {
  return (
    <Box as="nav" display="flex" gap="md" aria-label="Station sections">
      <Box as="a" href="#conditions" paddingX="md" color="accent-text">
        Conditions
      </Box>
      <Box as="a" href="#history" paddingX="md" color="accent-text">
        History
      </Box>
    </Box>
  )
}

Pass a component when it accepts className, such as next/link. In a Server Component the component can be a local server function: Box never crosses into the client, so nothing has to be serialized.

Source · box/as-component.tsx
box/as-component.tsx
import { Box } from '@blocks/design-system'
import Link from 'next/link'

// Any component that accepts className works as the host. Box keeps the
// component's own props, such as next/link's href, fully typed.
export default function AsComponent() {
  return (
    <Box as={Link} href="/components/box#refs" padding="sm" border="accent" display="inline-block">
      Jump to refs with next/link
    </Box>
  )
}

Refs

Box forwards ref to the element it renders, along with IDs, ARIA attributes and event handlers. An event handler or ref means the caller is a Client Component.

Source · box/ref-focus.tsx
box/ref-focus.tsx
'use client'

import { Box } from '@blocks/design-system'
import { useRef } from 'react'

export default function RefFocus() {
  const input = useRef<HTMLInputElement>(null)
  return (
    <Box display="flex" gap="sm" alignItems="center">
      <Box as="label" htmlFor="station-search">
        Station
      </Box>
      <Box as="input" id="station-search" ref={input} padding="xs" border="default" />
      <Box as="button" type="button" padding="xs" border="accent" onClick={() => input.current?.focus()}>
        Focus the search
      </Box>
    </Box>
  )
}

Escape hatches

Box takes no className or style. When a value has no token, use dangerouslySetClassName or dangerouslySetStyle, applied after Box’s own classes and styles, so the exceptions are easy to search for.

A one-off dashed outline
Source · box/escape-hatch.tsx
box/escape-hatch.tsx
import { Box } from '@blocks/design-system'

export default function EscapeHatch() {
  return (
    <Box
      padding="md"
      border="default"
      // No token for a dashed outline: say so explicitly.
      dangerouslySetStyle={{ outline: '2px dashed var(--ds-color-warning)', outlineOffset: 4 }}
    >
      A one-off dashed outline
    </Box>
  )
}

Props

Box props
PropTypeDefaultDescription
alignItemsResponsiveValue<AlignItemsValue>—Aligns children on the cross axis. Responsive.
alignSelfResponsiveValue<AlignSelfValue>—Overrides the parent's `alignItems` for this element. Responsive.
asElementType—The element or component to render, e.g. `as={NextLink}`.
backgroundColorResponsiveValue<"text" | "overlay" | "bg" | "bg-subtle" | "surface" | "surface-hover" | "surface-active" | "selected" | "border-subtle" | "border" | "border-strong" | "focus-ring" | "accent" | ... 24 more ... | "text-muted">—A background color token, e.g. `"surface"`. Responsive.
borderResponsiveValue<BorderValue>—A border on all sides: `none`, `default`, `accent` or `danger`. Responsive.
borderBottomResponsiveValue<BorderValue>—A bottom border: `none`, `default`, `accent` or `danger`. Responsive.
borderLeftResponsiveValue<BorderValue>—A left border: `none`, `default`, `accent` or `danger`. Responsive.
borderRightResponsiveValue<BorderValue>—A right border: `none`, `default`, `accent` or `danger`. Responsive.
borderTopResponsiveValue<BorderValue>—A top border: `none`, `default`, `accent` or `danger`. Responsive.
colorResponsiveValue<"text" | "overlay" | "bg" | "bg-subtle" | "surface" | "surface-hover" | "surface-active" | "selected" | "border-subtle" | "border" | "border-strong" | "focus-ring" | "accent" | ... 24 more ... | "text-muted">—A text color token. Responsive.
displayResponsiveValue<DisplayValue>—The CSS `display`. Responsive.
flexResponsiveValue<"none" | "auto" | "initial" | "1">—The CSS `flex` shorthand. Responsive.
flexDirectionResponsiveValue<FlexDirectionValue>—The flex main axis. Responsive.
flexGrowResponsiveValue<0 | 1>—`1` to grow into free space. Responsive.
flexShrinkResponsiveValue<0 | 1>—`0` to stop shrinking below its size. Responsive.
flexWrapResponsiveValue<FlexWrapValue>—Whether flex children wrap. Responsive.
gapResponsiveValue<"xxs" | "xs" | "sm" | "md" | "lg" | "xl" | "xxl" | "xxxl" | "xxxxl" | "xxxxxl" | "none">—A space token between flex or grid children, e.g. `"md"`. Responsive.
heightResponsiveValue<HeightValue>—A space token, `"100vh"` or `"full"`. Responsive.
justifyContentResponsiveValue<JustifyContentValue>—Distributes children along the main axis. Responsive.
marginResponsiveValue<MarginSpaceValue>—Margin on all sides. A space token or `"auto"`, e.g. `"md"`. Responsive.
marginBottomResponsiveValue<MarginSpaceValue>—Margin on the bottom. A space token or `"auto"`, e.g. `"md"`. Responsive.
marginLeftResponsiveValue<MarginSpaceValue>—Margin on the left. A space token or `"auto"`, e.g. `"md"`. Responsive.
marginRightResponsiveValue<MarginSpaceValue>—Margin on the right. A space token or `"auto"`, e.g. `"md"`. Responsive.
marginTopResponsiveValue<MarginSpaceValue>—Margin on the top. A space token or `"auto"`, e.g. `"md"`. Responsive.
marginXResponsiveValue<MarginSpaceValue>—Margin on the left and right. A space token or `"auto"`, e.g. `"md"`. Responsive.
marginYResponsiveValue<MarginSpaceValue>—Margin on the top and bottom. A space token or `"auto"`, e.g. `"md"`. Responsive.
maxHeightResponsiveValue<HeightValue>—A space token, `"100vh"` or `"full"`. Responsive.
maxWidthResponsiveValue<MeasureValue>—A measure token (`sm`, `md`, `lg`), `"100vw"` or `"full"`. Responsive.
minHeightResponsiveValue<HeightValue>—A space token, `"100vh"` or `"full"`. Responsive.
paddingResponsiveValue<"xxs" | "xs" | "sm" | "md" | "lg" | "xl" | "xxl" | "xxxl" | "xxxxl" | "xxxxxl" | "none">—Padding on all sides. A space token, e.g. `"md"`. Responsive.
paddingBottomResponsiveValue<"xxs" | "xs" | "sm" | "md" | "lg" | "xl" | "xxl" | "xxxl" | "xxxxl" | "xxxxxl" | "none">—Padding on the bottom. A space token, e.g. `"md"`. Responsive.
paddingLeftResponsiveValue<"xxs" | "xs" | "sm" | "md" | "lg" | "xl" | "xxl" | "xxxl" | "xxxxl" | "xxxxxl" | "none">—Padding on the left. A space token, e.g. `"md"`. Responsive.
paddingRightResponsiveValue<"xxs" | "xs" | "sm" | "md" | "lg" | "xl" | "xxl" | "xxxl" | "xxxxl" | "xxxxxl" | "none">—Padding on the right. A space token, e.g. `"md"`. Responsive.
paddingTopResponsiveValue<"xxs" | "xs" | "sm" | "md" | "lg" | "xl" | "xxl" | "xxxl" | "xxxxl" | "xxxxxl" | "none">—Padding on the top. A space token, e.g. `"md"`. Responsive.
paddingXResponsiveValue<"xxs" | "xs" | "sm" | "md" | "lg" | "xl" | "xxl" | "xxxl" | "xxxxl" | "xxxxxl" | "none">—Padding on the left and right. A space token, e.g. `"md"`. Responsive.
paddingYResponsiveValue<"xxs" | "xs" | "sm" | "md" | "lg" | "xl" | "xxl" | "xxxl" | "xxxxl" | "xxxxxl" | "none">—Padding on the top and bottom. A space token, e.g. `"md"`. Responsive.
positionResponsiveValue<PositionValue>—The CSS `position`. Responsive.
textAlignResponsiveValue<TextAlignValue>—The CSS `text-align`. Responsive.
widthResponsiveValue<WidthValue>—A space token, `"100vw"` or `"full"`. Responsive.

Also accepts styling escape hatches, and the native attributes of the element it renders.

Accessibility

  • Box renders a <div>, which has no role. Choose the element with as (nav, section, article, ul), and give landmarks a name with aria-label or aria-labelledby.
  • flexDirection and responsive layout change only the visual arrangement. Keep the source order meaningful, because screen readers and the keyboard follow it.
  • Color props take semantic tokens, which keep text readable in both themes.