# Density

Retune component spacing and default control sizes for comfortable or compact interfaces.

Density is a cross-component spacing preset. It tightens or relaxes padding, gaps, title scale, and default control heights without changing color, radius, or interaction behavior.

Zenos provides two values:

- `comfortable` is the default. It preserves the balanced spacing used by existing Zenos components.
- `compact` reduces whitespace for dashboards, data-heavy forms, side panels, and other application UI.

## Compare the presets

The same Card is rendered at both densities below. The compact version reduces its internal spacing and title scale while keeping the same semantic surface, border, and corner radius.

```tsx
import { Card, Stack } from "@zentara-co/zenos-ui";

function DensityComparisonExample() {
  return (
    <Stack direction="row" gap="4" wrap="wrap" align="start" className="w-full justify-center">
      {(["comfortable", "compact"] as const).map((density) => (
        <Card key={density} density={density} className="w-full max-w-64">
          <Card.Header>
            <Card.Title className="capitalize">{density}</Card.Title>
            <Card.Description>
              {density === "comfortable"
                ? "Balanced spacing for everyday product surfaces."
                : "Tighter spacing for information-dense workflows."}
            </Card.Description>
          </Card.Header>
          <Card.Content className="text-sm text-text-subtle">
            Radius and color stay the same.
          </Card.Content>
        </Card>
      ))}
    </Stack>
  );
}
```

Use an explicit `density` prop when one component needs to differ from its surroundings:

```tsx
<Card density="compact">
  <Card.Header>
    <Card.Title>Recent activity</Card.Title>
  </Card.Header>
  <Card.Content>{/* dense content */}</Card.Content>
</Card>
```

## Set density for a subtree

`DensityProvider` applies one density to every supported descendant. This is the preferred option for a dashboard, settings panel, or application shell because related components change together.

```tsx
import { Button, Card, DensityProvider, Input, Stack } from "@zentara-co/zenos-ui";
import { useState } from "react";

function DensityWorkspaceExample() {
  const [density, setDensity] = useState<"comfortable" | "compact">("comfortable");

  return (
    <Stack gap="4" className="w-full max-w-md">
      <Stack direction="row" gap="2" role="group" aria-label="Preview density">
        {(["comfortable", "compact"] as const).map((value) => (
          <Button
            key={value}
            size="sm"
            variant={density === value ? "primary" : "outline"}
            aria-pressed={density === value}
            onClick={() => setDensity(value)}
            className="capitalize"
          >
            {value}
          </Button>
        ))}
      </Stack>

      <DensityProvider density={density}>
        <Card>
          <Card.Header>
            <Card.Title>Payout threshold</Card.Title>
            <Card.Description>Choose when the next automatic payout is created.</Card.Description>
          </Card.Header>
          <Card.Content>
            <Input aria-label="Payout threshold" defaultValue="$2,500.00" />
          </Card.Content>
          <Card.Footer className="gap-2">
            <Button variant="primary">Save threshold</Button>
            <Button variant="ghost">Cancel</Button>
          </Card.Footer>
        </Card>
      </DensityProvider>
    </Stack>
  );
}
```

```tsx
import { Button, Card, DensityProvider, Input } from "@zentara-co/zenos-ui";

export function Dashboard({ children }: { children: React.ReactNode }) {
  return <DensityProvider density="compact">{children}</DensityProvider>;
}
```

Density resolves from the most specific source:

1. An explicit component `density` prop.
2. The nearest `DensityProvider`.
3. The `comfortable` default.

For controls with a `size` prop, an explicit size remains authoritative. A compact provider changes an unsized Button or Input to `sm`, but it does not replace `size="lg"`.

## What changes

### Comfortable

- Card regions use the spacing-4 token (`p-4`, 16px) on every outer edge.
- Card titles use the body-large scale.
- Unsized Button and Input controls resolve to `md`.

### Compact

- Card regions use the spacing-3 token (`p-3`, 12px) on every outer edge.
- Card titles use the body-medium scale.
- Unsized Button and Input controls resolve to `sm`.

Density does not change semantic color roles, border radius, component variants, accessibility behavior, or touch-target decisions that a product applies explicitly.

## Supported components

The first density-aware surface is the Card family:

- `Card`
- `ClickableCard`
- `SelectableCard`

Because the interactive card components share Card parts, their headers, content, footers, and nested controls inherit the same density behavior. `Button` and `Input` also honor the provider when their `size` prop is omitted.

Components with intrinsic pixel dimensions, such as Avatar, do not inherit density. Continue to use their explicit `size` API.

## Choosing a density

Use `comfortable` for marketing surfaces, onboarding, editorial content, and focused tasks where scanning space is valuable. Use `compact` for repeated controls, dense configuration, data management, and constrained panels.

Keep one density across a coherent region. Mixing presets card by card creates uneven rhythm; use a local override only when the component has a genuinely different information-density requirement.

Source: https://zenos-ui.pages.dev/foundations/density/