# Composition Guide

Choose and combine Zenos layout primitives by structural responsibility.

Zenos layout primitives describe one structural relationship at a time. Compose them instead of asking one component to manage page width, section rhythm, child flow, alignment, and decoration together.

```tsx
import { Badge, Box, Button, Container, Cluster, Grid, Section, Stack, Text } from "@zentara-co/zenos-ui";

function CompositionExample() {
  const metrics = [
    { label: "Active projects", value: "18", change: "+3 this month" },
    { label: "Contributors", value: "42", change: "Across 4 teams" },
    { label: "Deployments", value: "126", change: "98.7% successful" },
  ];

  return (
    <Section
      as="section"
      aria-labelledby="composition-heading"
      gap={{ base: "5", md: "6" }}
      padding={{ base: "5", md: "8" }}
      className="w-full rounded-2xl bg-elevation-surface-sunken"
    >
      <Container size="lg">
        <Stack gap={{ base: "5", md: "6" }}>
          <Cluster
            align={{ base: "start", md: "center" }}
            justify="between"
            gap="4"
            className="flex-col md:flex-row"
          >
            <Stack gap="2" className="max-w-2xl">
              <Badge variant="default" color="information" className="self-start">
                Workspace overview
              </Badge>
              <Text id="composition-heading" type="heading" size="h4">
                Product activity at a glance
              </Text>
              <Text color="subtle">
                Each primitive owns one relationship: page rhythm, content width, child flow, or
                alignment.
              </Text>
            </Stack>
            <Cluster gap="2">
              <Button variant="primary">Export</Button>
              <Button>View activity</Button>
            </Cluster>
          </Cluster>

          <Grid columns={{ base: 1, sm: 2, lg: 3 }} gap="3">
            {metrics.map(({ label, value, change }) => (
              <Box key={label} elevation="container" className="rounded-xl p-4 sm:p-5">
                <Stack gap="3">
                  <Text size="s" color="subtle">
                    {label}
                  </Text>
                  <Text type="heading" size="h3">
                    {value}
                  </Text>
                  <Text size="s" color="subtle">
                    {change}
                  </Text>
                </Stack>
              </Box>
            ))}
          </Grid>
        </Stack>
      </Container>
    </Section>
  );
}
```

The example follows an outside-in order: `Section` owns the region and its padding, `Container` constrains the readable width, `Stack` creates vertical rhythm, `Cluster` arranges the heading and actions, and `Grid` adapts the summary cards to available space.

## Mental model

Treat layout as a set of independent relationships:

1. **Region** — What semantic part of the page is this, and how much space surrounds it?
2. **Measure** — Should its content fill the viewport or stop at a maximum width?
3. **Flow** — Do children move along one axis, wrap as a group, or occupy tracks?
4. **Alignment** — Does content need centering or distribution within available space?
5. **Surface** — Does the region need elevation, a border, color, or another application treatment?

Give each relationship one owner. This keeps responsive changes local and prevents nested wrappers from fighting over the same gap, width, or alignment.

## Choose by relationship

Use the narrowest primitive that owns the relationship. For example, use `Cluster` for a toolbar that wraps; do not use `Grid` merely because the toolbar has several children.

## Compose outside in

Start from the largest boundary and move toward the content. Place `Section` outside `Container` when a section background or divider should span the available width while its content remains constrained:

```tsx
<Section as="section" padding={{ base: "6", lg: "12" }}>
  <Container size="xl">
    <Stack gap="8">
      <PageHeading />
      <Grid columns={{ base: 1, md: 2, xl: 3 }} gap={{ base: "4", lg: "6" }}>
        {content}
      </Grid>
    </Stack>
  </Container>
</Section>
```

Reverse that order only when the section itself should be constrained. A `Section` inside a `Container` creates a narrow semantic region; a `Container` inside a `Section` creates a full-width region with constrained content.

## Responsive values

Responsive layout props accept a direct value or a mobile-first breakpoint object:

```tsx
<Cluster gap={{ base: "2", md: "4" }} justify={{ base: "start", lg: "between" }} />
```

`base` applies everywhere. Each larger breakpoint overrides it and remains active until another value replaces it. Omitted breakpoints inherit the last applicable value through CSS.

Add a breakpoint when the content needs a different relationship, not simply because another breakpoint exists. A card collection may move from one to two columns at `md` and remain there through `2xl`; it does not need a new value at every step.

Prefer responsive props when the primitive owns the behavior:

```tsx
<Grid columns={{ base: 1, md: 2 }} gap={{ base: "3", lg: "5" }} />
```

Use `className` for application-specific decoration or a layout behavior that is intentionally outside the primitive's API:

```tsx
<Box elevation="raised" className="rounded-xl border border-border-default p-5">
  <Stack gap="3">...</Stack>
</Box>
```

## Common composition patterns

### Heading with actions

Use `Cluster` when the actions should stay together and wrap naturally as space narrows:

```tsx
<Cluster align="center" justify="between" gap="4">
  <Stack gap="1">
    <Text type="heading" size="h4">
      Team members
    </Text>
    <Text color="subtle">Manage access to this workspace.</Text>
  </Stack>
  <Cluster gap="2">
    <Button variant="primary">Export</Button>
    <Button>Invite member</Button>
  </Cluster>
</Cluster>
```

### Centered state inside normal flow

Let the surrounding `Stack` own its relationship to siblings and let `Center` own only the state region's internal alignment:

```tsx
<Stack gap="6">
  <PageHeading />
  <Center minHeight="48" padding="6">
    <EmptyState />
  </Center>
</Stack>
```

### Repeated content

Use `Grid` when repeated items should share tracks. The item component owns its internal `Stack`; the parent owns only the gap between items.

```tsx
<Grid columns={{ base: 1, sm: 2, xl: 4 }} gap="4">
  {projects.map((project) => (
    <ProjectCard key={project.id} project={project} />
  ))}
</Grid>
```

## Ownership boundaries

- Let one parent own the gap between siblings. Avoid combining parent `gap` with child margins for the same relationship.
- Let `Container` own maximum width and inline gutters. Do not repeat `max-width` on every child section.
- Let the nearest layout primitive own alignment. Avoid pushing children into position with arbitrary margins.
- Keep decoration on the surface that visually owns it. A border around a group belongs on the group's `Box`, not on each internal layout wrapper.
- Keep feature behavior outside generic layout components. Layout primitives should not fetch data, coordinate routes, or infer application state.

## Avoid unnecessary wrappers

Not every primitive belongs in every composition. If a `Grid` already owns the relationship between cards, it does not need an extra `Stack` solely to exist. If a `Box` has no semantics, elevation, or customization role, a fragment may be clearer.

Add a wrapper when it owns a real boundary: semantics, spacing, width, alignment, responsive behavior, or decoration. Remove it when it only forwards children and duplicates a relationship already owned elsewhere.

## Semantics

All layout primitives use `as` for polymorphism and forward the selected element's native props and ref. Choose elements from the content's meaning: use `section` for a labeled thematic region, `article` for independently distributable content, `nav` for navigation, and `ul` or `ol` for a true list.

Layout appearance alone does not justify a landmark, heading, list, or interactive role. Do not add click handlers to a `Box` or `Stack`; render a native link or button when the surface is interactive. Preserve a logical heading order even when visual size changes.

## Related foundations

- [Spacing](/foundations/spacing/) defines the shared rhythm used by `gap` and padding props.
- [Breakpoints](/foundations/breakpoints/) documents the mobile-first responsive scale.
- [Aspect Ratio](/layout/aspect-ratio/) reserves stable media geometry inside composed layouts.
- [Box](/layout/box/) adds semantic wrapping and elevation without owning child flow.

Source: https://zenos-ui.pages.dev/layout/composition/