# 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

**Example: box/basic.tsx** (`examples/box/basic.tsx`)

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

**Example: box/direction.tsx** (`examples/box/direction.tsx`)

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

**Example: Responsive station summary** (`examples/box/responsive-summary.tsx`)

```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>
  )
}
```

## 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).

**Example: box/spacing.tsx** (`examples/box/spacing.tsx`)

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

**Example: box/as-element.tsx** (`examples/box/as-element.tsx`)

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

**Example: box/as-component.tsx** (`examples/box/as-component.tsx`)

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

**Example: box/ref-focus.tsx** (`examples/box/ref-focus.tsx`)

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

**Example: box/escape-hatch.tsx** (`examples/box/escape-hatch.tsx`)

```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**

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `alignItems` | `ResponsiveValue<AlignItemsValue>` | — | Aligns children on the cross axis. Responsive. |
| `alignSelf` | `ResponsiveValue<AlignSelfValue>` | — | Overrides the parent's `alignItems` for this element. Responsive. |
| `as` | `ElementType` | — | The element or component to render, e.g. `as={NextLink}`. |
| `backgroundColor` | `ResponsiveValue<"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. |
| `border` | `ResponsiveValue<BorderValue>` | — | A border on all sides: `none`, `default`, `accent` or `danger`. Responsive. |
| `borderBottom` | `ResponsiveValue<BorderValue>` | — | A bottom border: `none`, `default`, `accent` or `danger`. Responsive. |
| `borderLeft` | `ResponsiveValue<BorderValue>` | — | A left border: `none`, `default`, `accent` or `danger`. Responsive. |
| `borderRight` | `ResponsiveValue<BorderValue>` | — | A right border: `none`, `default`, `accent` or `danger`. Responsive. |
| `borderTop` | `ResponsiveValue<BorderValue>` | — | A top border: `none`, `default`, `accent` or `danger`. Responsive. |
| `color` | `ResponsiveValue<"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. |
| `display` | `ResponsiveValue<DisplayValue>` | — | The CSS `display`. Responsive. |
| `flex` | `ResponsiveValue<"none" \| "auto" \| "initial" \| "1">` | — | The CSS `flex` shorthand. Responsive. |
| `flexDirection` | `ResponsiveValue<FlexDirectionValue>` | — | The flex main axis. Responsive. |
| `flexGrow` | `ResponsiveValue<0 \| 1>` | — | `1` to grow into free space. Responsive. |
| `flexShrink` | `ResponsiveValue<0 \| 1>` | — | `0` to stop shrinking below its size. Responsive. |
| `flexWrap` | `ResponsiveValue<FlexWrapValue>` | — | Whether flex children wrap. Responsive. |
| `gap` | `ResponsiveValue<"xxs" \| "xs" \| "sm" \| "md" \| "lg" \| "xl" \| "xxl" \| "xxxl" \| "xxxxl" \| "xxxxxl" \| "none">` | — | A space token between flex or grid children, e.g. `"md"`. Responsive. |
| `height` | `ResponsiveValue<HeightValue>` | — | A space token, `"100vh"` or `"full"`. Responsive. |
| `justifyContent` | `ResponsiveValue<JustifyContentValue>` | — | Distributes children along the main axis. Responsive. |
| `margin` | `ResponsiveValue<MarginSpaceValue>` | — | Margin on all sides. A space token or `"auto"`, e.g. `"md"`. Responsive. |
| `marginBottom` | `ResponsiveValue<MarginSpaceValue>` | — | Margin on the bottom. A space token or `"auto"`, e.g. `"md"`. Responsive. |
| `marginLeft` | `ResponsiveValue<MarginSpaceValue>` | — | Margin on the left. A space token or `"auto"`, e.g. `"md"`. Responsive. |
| `marginRight` | `ResponsiveValue<MarginSpaceValue>` | — | Margin on the right. A space token or `"auto"`, e.g. `"md"`. Responsive. |
| `marginTop` | `ResponsiveValue<MarginSpaceValue>` | — | Margin on the top. A space token or `"auto"`, e.g. `"md"`. Responsive. |
| `marginX` | `ResponsiveValue<MarginSpaceValue>` | — | Margin on the left and right. A space token or `"auto"`, e.g. `"md"`. Responsive. |
| `marginY` | `ResponsiveValue<MarginSpaceValue>` | — | Margin on the top and bottom. A space token or `"auto"`, e.g. `"md"`. Responsive. |
| `maxHeight` | `ResponsiveValue<HeightValue>` | — | A space token, `"100vh"` or `"full"`. Responsive. |
| `maxWidth` | `ResponsiveValue<MeasureValue>` | — | A measure token (`sm`, `md`, `lg`), `"100vw"` or `"full"`. Responsive. |
| `minHeight` | `ResponsiveValue<HeightValue>` | — | A space token, `"100vh"` or `"full"`. Responsive. |
| `padding` | `ResponsiveValue<"xxs" \| "xs" \| "sm" \| "md" \| "lg" \| "xl" \| "xxl" \| "xxxl" \| "xxxxl" \| "xxxxxl" \| "none">` | — | Padding on all sides. A space token, e.g. `"md"`. Responsive. |
| `paddingBottom` | `ResponsiveValue<"xxs" \| "xs" \| "sm" \| "md" \| "lg" \| "xl" \| "xxl" \| "xxxl" \| "xxxxl" \| "xxxxxl" \| "none">` | — | Padding on the bottom. A space token, e.g. `"md"`. Responsive. |
| `paddingLeft` | `ResponsiveValue<"xxs" \| "xs" \| "sm" \| "md" \| "lg" \| "xl" \| "xxl" \| "xxxl" \| "xxxxl" \| "xxxxxl" \| "none">` | — | Padding on the left. A space token, e.g. `"md"`. Responsive. |
| `paddingRight` | `ResponsiveValue<"xxs" \| "xs" \| "sm" \| "md" \| "lg" \| "xl" \| "xxl" \| "xxxl" \| "xxxxl" \| "xxxxxl" \| "none">` | — | Padding on the right. A space token, e.g. `"md"`. Responsive. |
| `paddingTop` | `ResponsiveValue<"xxs" \| "xs" \| "sm" \| "md" \| "lg" \| "xl" \| "xxl" \| "xxxl" \| "xxxxl" \| "xxxxxl" \| "none">` | — | Padding on the top. A space token, e.g. `"md"`. Responsive. |
| `paddingX` | `ResponsiveValue<"xxs" \| "xs" \| "sm" \| "md" \| "lg" \| "xl" \| "xxl" \| "xxxl" \| "xxxxl" \| "xxxxxl" \| "none">` | — | Padding on the left and right. A space token, e.g. `"md"`. Responsive. |
| `paddingY` | `ResponsiveValue<"xxs" \| "xs" \| "sm" \| "md" \| "lg" \| "xl" \| "xxl" \| "xxxl" \| "xxxxl" \| "xxxxxl" \| "none">` | — | Padding on the top and bottom. A space token, e.g. `"md"`. Responsive. |
| `position` | `ResponsiveValue<PositionValue>` | — | The CSS `position`. Responsive. |
| `textAlign` | `ResponsiveValue<TextAlignValue>` | — | The CSS `text-align`. Responsive. |
| `width` | `ResponsiveValue<WidthValue>` | — | A space token, `"100vw"` or `"full"`. Responsive. |

Also accepts [styling escape hatches](https://design-system.blocks-ui.com/components/box.md#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.
