# Layout

> The components of songstress/ui for layout, each as Songstress draws it, with its props and an example.

Page: https://songstress.app/docs/develop/components/layout/

Components that arrange others: stacks, grids, surfaces, sections, and what folds or switches. None of them draws anything of its own but room, a surface or a rule.

Each picture is the SDK’s own test tree for the component, drawn by the web app; every app draws the same tree with its own controls, the phones natively. Each example is the one in the SDK’s doc comment, which its tests compile, and the props are read from its types.

## Stack [#stack]

<Shot name="gallery/stack" alt="Stacks with each step of gap and padding, from none to xlarge, and their alignments" />

Blocks under one another: a `stack` node.

```tsx
import { Stack, Text } from 'songstress/ui'

const header = (
  <Stack gap="xsmall">
    <Text style="heading">Played lately</Text>
    <Text tone="muted">12 songs this week</Text>
  </Stack>
)
```

A `Stack`'s or an `HStack`'s props.

| Field | Type | Description |
|---|---|---|
| `children?` | `UiNode` | What it holds, in order. |
| `grow?` | `boolean \| undefined` | In a horizontal stack, it takes the room left over. Elsewhere it does nothing. |
| `width?` | `Width \| undefined` | Across its parent, or as wide as what it holds. Left out, the block's own way. |
| `gap?` | `Space \| undefined` | Room between its children. Left out, "medium". |
| `padding?` | `Space \| undefined` | Room inside it, around its children. Left out, "none". |
| `align?` | `"center" \| "end" \| "start" \| "stretch" \| undefined` | How its children line up across it. |
| `justify?` | `"between" \| "center" \| "end" \| "start" \| undefined` | How its children share the room along it. Left out, "start". |
| `wrap?` | `boolean \| undefined` | Whether a horizontal stack's children go on to a new line when they do not fit. Left out, true. |

## HStack [#hstack]

<Shot name="gallery/h-stack" alt="A horizontal stack: a text that takes the room left, beside a button" />

Blocks beside one another, going on to a new line when they do not fit
(`wrap`): a `stack` node with `direction: "horizontal"`.

```tsx
import { Badge, Glyph, HStack, Spacer, Text } from 'songstress/ui'

const row = (
  <HStack gap="small" align="center">
    <Glyph name="headphones" />
    <Text>Listening now</Text>
    <Spacer />
    <Badge tone="accent">Live</Badge>
  </HStack>
)
```

A `Stack`'s or an `HStack`'s props.

| Field | Type | Description |
|---|---|---|
| `children?` | `UiNode` | What it holds, in order. |
| `grow?` | `boolean \| undefined` | In a horizontal stack, it takes the room left over. Elsewhere it does nothing. |
| `width?` | `Width \| undefined` | Across its parent, or as wide as what it holds. Left out, the block's own way. |
| `gap?` | `Space \| undefined` | Room between its children. Left out, "medium". |
| `padding?` | `Space \| undefined` | Room inside it, around its children. Left out, "none". |
| `align?` | `"center" \| "end" \| "start" \| "stretch" \| undefined` | How its children line up across it. |
| `justify?` | `"between" \| "center" \| "end" \| "start" \| undefined` | How its children share the room along it. Left out, "start". |
| `wrap?` | `boolean \| undefined` | Whether a horizontal stack's children go on to a new line when they do not fit. Left out, true. |

## Grid [#grid]

<Shot name="gallery/grid" alt="A grid of blocks in columns, and one that fits as many as there is room for" />

Blocks in columns: a `grid` node.

```tsx
import { Box, Grid, Text } from 'songstress/ui'

const totals = (
  <Grid columns={2} gap="small">
    <Box variant="card"><Text style="label">Plays</Text><Text style="title">42</Text></Box>
    <Box variant="card"><Text style="label">Hours</Text><Text style="title">3</Text></Box>
  </Grid>
)
```

A `Grid`'s props.

| Field | Type | Description |
|---|---|---|
| `children?` | `UiNode` | What it holds, row by row. |
| `grow?` | `boolean \| undefined` | In a horizontal stack, it takes the room left over. Elsewhere it does nothing. |
| `width?` | `Width \| undefined` | Across its parent, or as wide as what it holds. Left out, the block's own way. |
| `columns?` | `"auto" \| 2 \| 3 \| 4 \| 5 \| 6 \| undefined` | How many columns, or as many as fit (`"auto"`). A number is the most: at a phone's width a grid shows two. Left out, "auto". |
| `gap?` | `Space \| undefined` | Room between its cells. Left out, "medium". |
| `padding?` | `Space \| undefined` | Room inside it. Left out, "none". |

## Box [#box]

<Shot name="gallery/box" alt="Three boxes: plain, the app’s card, and a quiet inset" />

A surface around blocks: a `box` node, `plain`, the app's `card`, or a
quiet `inset`.

```tsx
import { Box, Text } from 'songstress/ui'

const week = (
  <Box variant="card" padding="medium" gap="xsmall">
    <Text style="label">This week</Text>
    <Text style="title">42 plays</Text>
  </Box>
)
```

A `Box`'s props.

| Field | Type | Description |
|---|---|---|
| `children?` | `UiNode` | What it holds, top to bottom. |
| `grow?` | `boolean \| undefined` | In a horizontal stack, it takes the room left over. Elsewhere it does nothing. |
| `width?` | `Width \| undefined` | Across its parent, or as wide as what it holds. Left out, the block's own way. |
| `variant?` | `"card" \| "inset" \| "plain" \| undefined` | Left out, "plain". |
| `padding?` | `Space \| undefined` | Room inside it. Left out, none for plain, the app's own for card and inset. |
| `gap?` | `Space \| undefined` | Room between its children. Left out, "medium". |
| `align?` | `"center" \| "end" \| "start" \| "stretch" \| undefined` | How its children line up across it. Left out, "stretch". |

## Section [#section]

<Shot name="gallery/section" alt="A section with its title over the blocks it holds" />

Blocks under a heading: a `section` node.

```tsx
import { Section, Text } from 'songstress/ui'

const today = (
  <Section title="Today" footer="Counted on this device.">
    <Text>3 songs started</Text>
  </Section>
)
```

A `Section`'s props.

| Field | Type | Description |
|---|---|---|
| `children?` | `UiNode` | What it holds. |
| `grow?` | `boolean \| undefined` | In a horizontal stack, it takes the room left over. Elsewhere it does nothing. |
| `width?` | `Width \| undefined` | Across its parent, or as wide as what it holds. Left out, the block's own way. |
| `title?` | `string \| undefined` | The section's heading. |
| `footer?` | `string \| undefined` | A line of help under the section. |

## Spacer [#spacer]

A spacer draws nothing: it is room, of a step or, with `grow`, all that is left in a horizontal stack.

Room between blocks: a `spacer` node.

```tsx
import { HStack, Spacer, Text } from 'songstress/ui'

// In an HStack, a Spacer takes the room left: the count goes to the far end.
const line = (
  <HStack>
    <Text>So What</Text>
    <Spacer />
    <Text tone="muted">12 plays</Text>
  </HStack>
)
```

A `Spacer`'s props.

| Field | Type | Description |
|---|---|---|
| `grow?` | `boolean \| undefined` | In a horizontal stack, it takes the room left over. Elsewhere it does nothing. |
| `width?` | `Width \| undefined` | Across its parent, or as wide as what it holds. Left out, the block's own way. |
| `size?` | `Size \| undefined` | How much room. |

## Divider [#divider]

<Shot name="gallery/divider" alt="A divider across its parent" />

A line between blocks: a `divider` node.

```tsx
import { Divider, Stack, Text } from 'songstress/ui'

const parts = (
  <Stack gap="small">
    <Text>This week</Text>
    <Divider />
    <Text>Last week</Text>
  </Stack>
)
```

A `Divider`'s props.

| Field | Type | Description |
|---|---|---|
| `grow?` | `boolean \| undefined` | In a horizontal stack, it takes the room left over. Elsewhere it does nothing. |
| `width?` | `Width \| undefined` | Across its parent, or as wide as what it holds. Left out, the block's own way. |

## Collapsible [#collapsible]

<Shot name="gallery/collapsible" alt="A collapsible titled More, open on what it holds" />

Blocks a tap opens and closes: a `collapsible` node. With `onChange`, it
is open exactly when `open` says.

```tsx
import { Collapsible, Text, useState } from 'songstress/ui'

function Credits() {
  const [open, setOpen] = useState(false)
  return (
    <Collapsible title="Credits" open={open} onChange={setOpen}>
      <Text>Recorded in one take.</Text>
    </Collapsible>
  )
}
```

A `Collapsible`'s props.

| Field | Type | Description |
|---|---|---|
| `onChange?` | `Handler<boolean> \| undefined` | Told `true` or `false` when the person opens or closes it. |
| `children?` | `UiNode` | What it holds, shown open. |
| `grow?` | `boolean \| undefined` | In a horizontal stack, it takes the room left over. Elsewhere it does nothing. |
| `width?` | `Width \| undefined` | Across its parent, or as wide as what it holds. Left out, the block's own way. |
| `title` | `string` | What shows while it is closed. |
| `open?` | `boolean \| undefined` | Open at first; with `onChange`, open exactly when this says. |

## Tabs [#tabs]

<Shot name="gallery/tabs" alt="Tabs: a switcher over the group of blocks it shows" />

One of several groups of blocks, under a switcher: a `tabs` node, its
tabs its `Tab` children.

```tsx
import { Tab, Tabs, Text, useState } from 'songstress/ui'

function Totals() {
  const [tab, setTab] = useState('week')
  return (
    <Tabs value={tab} onSelect={setTab}>
      <Tab id="week" title="This week"><Text>42 plays</Text></Tab>
      <Tab id="year" title="This year"><Text>1,204 plays</Text></Tab>
    </Tabs>
  )
}
```

A `Tabs`'s props: its tabs are its children, each a `Tab`.

| Field | Type | Description |
|---|---|---|
| `onSelect?` | `Handler<string> \| undefined` | Told the `id` of the tab the person chose. |
| `children?` | `UiNode` | Its tabs: `Tab` elements. |
| `grow?` | `boolean \| undefined` | In a horizontal stack, it takes the room left over. Elsewhere it does nothing. |
| `width?` | `Width \| undefined` | Across its parent, or as wide as what it holds. Left out, the block's own way. |
| `value?` | `string \| undefined` | The `id` of the tab shown. Left out, the first. |

### Tab [#tab]

One tab of a `Tabs`: its id, its title on the switcher, and its blocks.

```tsx
import { Tab, Tabs, Text } from 'songstress/ui'

const totals = (
  <Tabs>
    <Tab id="week" title="This week" icon="calendar"><Text>42 plays</Text></Tab>
    <Tab id="year" title="This year"><Text>1,204 plays</Text></Tab>
  </Tabs>
)
```

A `Tab`'s props: one tab of a `Tabs`.

| Field | Type | Description |
|---|---|---|
| `id` | `string` | Unique in its `Tabs`, at most 40 characters. |
| `title` | `string` | Its title on the switcher, at most 40 characters. |
| `icon?` | `IconName \| undefined` | An icon from Songstress's icon set. |
| `children?` | `UiNode` | What it shows. |
