---
title: StackView
---

import LibraryInfo from '@theme/LibraryInfo'
import { Feedback, TokensTable, PropsTable } from '../../src/components'
import { StackView } from '@telus-uds/components-web'
import { StackView as BaseDocs } from 'docs/@telus-uds/components-base'
import AvailableOnBoth from '../../partials/_platform-considerations-both.mdx'
import NoStylisticVariants from '../../partials/_variants-none.mdx'

# StackView

<LibraryInfo />

```jsx live
<StackView space={5}>
  <Typography>First item</Typography>
  <Typography>Second item</Typography>
  <Typography>Third item</Typography>
  <Typography>Fourth item</Typography>
</StackView>
```

## Introduction

StackView puts space evenly between its children.

[Follow the appropriate instructions](importing-components) to add this component in to your app.

## Guidance

Use `StackView` to create layouts with simple, neat, even spacing.

- If a layout requires different spacing between different items, use [`Spacer`](spacer).
- If the items need to wrap if they exceed a fixed size, use [`StackWrap`](stack-wrap).
- For space _around_ an element or group of elements, wrap them in a [`Box`](box).
- For more complex grids designed using a Bootstrap-like 12-column-grid system, consider [`FlexGrid`](flex-grid).

### `space` prop

The `space` prop sets the size of spacing between items, using the [**spacing scale**](sizing#spacing-and-layout).

Simple numbers may be used, or objects (see [spacing scale docs](sizing#spacing-and-layout) for details. These examples change spacing depending on the viewport.

```jsx live
<StackView space={{ xs: 1, md: 3, xl: 5 }}>
  <Typography>First item</Typography>
  <Typography>Second item</Typography>
  <Typography>Third item</Typography>
  <Typography>Fourth item</Typography>
</StackView>
```

### `direction` prop

By default, items are stacked vertically as a flex column. Pass `direction` as `'row'` to stack horizontally.
This applies `flexDirection: 'row'` styles to the container and passes appropriate props to spacers.

```jsx live
<StackView direction="row" space={{ xs: 1, md: 3, xl: 5 }}>
  <Typography>First item</Typography>
  <Typography>Second item</Typography>
  <Typography>Third item</Typography>
  <Typography>Fourth item</Typography>
</StackView>
```

Directions of `row-reverse` and `column-reverse` may be used to reverse the visual display of the stacked children.

The `direction` prop may also be passed a responsive object keyed by viewports.

```jsx live
<StackView space={4} direction={{ xs: 'column', md: 'row' }}>
  <Typography>First item</Typography>
  <Typography>Second item</Typography>
  <Typography>Third item</Typography>
  <Typography>Fourth item</Typography>
</StackView>
```

### `divider` prop

By default, space is created by inserting a [`<Spacer>`](spacer) component between children.

If the `divider` prop is passed as true, children are instead spaced with a [`Divider`](divider).
The StackView's `space` prop value is passed down to these dividers, rendering spacers before and after the dividing line.

```jsx live
<StackView space={{ xs: 1, md: 3, xl: 5 }} divider>
  <Typography>First item</Typography>
  <Typography>Second item</Typography>
  <Typography>Third item</Typography>
  <Typography>Fourth item</Typography>
</StackView>
```

`direction` is applied to dividers automatically.

```jsx live
<StackView space={{ xs: 1, md: 3, xl: 5 }} divider direction="row">
  <Typography>First item</Typography>
  <Typography>Second item</Typography>
  <Typography>Third item</Typography>
  <Typography>Fourth item</Typography>
</StackView>
```

The `divider` prop may take an object of Divider props, which are passed to the rendered dividers.

```jsx live
<StackView
  space={{ xs: 1, md: 3, xl: 5 }}
  divider={{ variant: { decorative: true, weight: 'thick' } }}
>
  <Typography>First item</Typography>
  <Typography>Second item</Typography>
  <Typography>Third item</Typography>
  <Typography>Fourth item</Typography>
</StackView>
```

### Composition notes

`StackView` only spaces rendered, direct children. This means common React composition patterns may be used without
needing to worry about creating spurious spacing:

- Comments and nullish children can be safely used and are ignored when spacing is inserted
- Fragments may be used when composing children, items within a fragment will still be spaced

This example demonstrates how StackView responds to various common React composition patterns:

```jsx live
<StackView space={3} divider>
  {/* This empty JSX comment child is ignored and does not create space */}
  <Typography>First item</Typography>
  {true && (
    // These conditional children must be wrapped in a React Fragment.
    // It does not prevent them from being spaced.
    <>
      <Typography>Second item</Typography>
      <Typography>Third item</Typography>
    </>
  )}
  {false && <Typography>Not rendered, no extra space</Typography>}
  <Typography>Fourth item</Typography>
  <Box space={2} variant={{ background: 'light' }}>
    <Typography>Fifth item.</Typography>
    <Typography>Items within this Box are not direct children.</Typography>
    <Typography>The parent StackView does not space them.</Typography>
  </Box>
  <Typography>Sixth item</Typography>
</StackView>
```

### Accessibility

By default, StackView applies no accessibility props or values relevant to accessibility tools. It may accept
React Native accessibility props ([web docs](https://necolas.github.io/react-native-web/docs/accessibility/),
[native docs](https://reactnative.dev/docs/accessibility)), and applies them to the outer container.

#### Semantic `divider`

If the `divider` prop is used, on Web, accessibility tools will be aware that a semantic separator exists between elements.

#### `Tag` prop for semantic HTML

By default, StackView renders a `<div>` on web and a `View` with no accessibility role on native apps.

To render a HTML attribute with a semantic meaning on web, pass a string naming a HTML semantic layout tag
to the `tag` prop. An `accessibilityRole` may also be passed if a role is needed in native apps.
Heading tags (`h1`, `h2`, `h3`, `h4`, `h5`, `h6`) are by default mapped to "heading" role in native apps.

See the `tag` entry in the [props table](#props) for the full list of supported tags.

```jsx live
<StackView tag="main">
  <StackView tag="h2" space={2}>
    <Typography variant={{ size: 'h1' }}>"h2" tag on web</Typography>
    <Typography variant={{ size: 'h4' }}>
      This subheading is part of the semantic h2 heading
    </Typography>
  </StackView>
  <StackView
    direction="row"
    tag="label"
    space={2}
    nativeID="label-stack"
    accessibilityRole="header"
  >
    <Typography variant={{ size: 'h3', color: 'secondary' }}>1: </Typography>
    <Typography variant={{ size: 'h3' }}>On web, "label"; in native apps, "header"</Typography>
  </StackView>
  <StackView tag="section" space={2} accessibilityLabelledBy="label-stack">
    <Typography>This is inside a "Section" tag on web.</Typography>
    <Typography>
      On web, all the label's content serves as this section's accessible name.
    </Typography>
    <Typography>This helps users of accessibility tools understand a page's structure.</Typography>
    <Typography>
      In native apps, headers may be the closest equivalent to named sections.
    </Typography>
  </StackView>
  <StackView tag="footer" space={2}>
    <Typography>This is inside a "footer" tag on web.</Typography>
    <Typography>That footer is inside the "main" stack.</Typography>
  </StackView>
</StackView>
```

### Platform considerations

<AvailableOnBoth />

## Props

<PropsTable base={BaseDocs} />

## Tokens

<TokensTable base={BaseDocs} component="StackView" />

## Variants

<NoStylisticVariants />

To control flex alignment and positioning, the `tokens` prop may be passed an object to apply the flex layout styles `'alignItems'`, `'justifyContent'`, `'flexShrink'` and `'flexGrow'`.

As always with the `tokens` prop, this should be used only where strictly necessary to avoid creating off-brand layouts.

```jsx live
<StackView
  direction="row"
  space={8}
  tokens={{
    alignItems: 'baseline',
    justifyContent: 'flex-end',
    flexGrow: 1
  }}
>
  <Typography variant={{ size: 'large' }}>First item</Typography>
  <Typography>Second item</Typography>
  <Typography variant={{ size: 'micro' }}>Third item</Typography>
</StackView>
```

## Feedback

<Feedback component="StackView" />
