---
title: Box
---

import LibraryInfo from '@theme/LibraryInfo'
import { Feedback, TokensTable, PropsTable } from '../../src/components'
import { Box, Button, Spacer, StackView, Typography } from '@telus-uds/components-web'
import { Box as BaseDocs } from 'docs/@telus-uds/components-base'
import AvailableOnBoth from '../../partials/_platform-considerations-both.mdx'
import AdditionalVariants from '../../src/components/Variants/Box'

# Box

<LibraryInfo figmaLink="https://www.figma.com/embed?embed_host=share&url=https%3A%2F%2Fwww.figma.com%2Ffile%2F4zJn2Qn6uXOqPP1GVDipy6%2F%25F0%259F%258D%2580-Allium-Components%3Fnode-id%3D409%253A158" />

```jsx live
<Box variant={{ background: 'light' }} space={3}>
  <Typography>Hello world</Typography>
</Box>
```

## Introduction

Box creates space (padding) around content, and has variants that give content a background.

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

## Guidance

Use Box to create padding within a container, around all its content. To create space after a box, between boxes or between
items in a box, combine `Box` with other layout components, such as `StackView` and `Spacer`:

```jsx live
<Box variant={{ background: 'light' }} space={4}>
  <StackView direction={{ xs: 'column', md: 'row' }} space={4}>
    <Box flex={1} variant={{ background: 'lightest' }} space={4}>
      <Typography variant={{ size: 'h3' }}>Some content</Typography>
      <Spacer space={4} />
      <Typography>Some example content within the first box.</Typography>
      <Spacer space={2} />
      <Typography>Another line of text in the first box.</Typography>
    </Box>
    <Box flex={1} variant={{ background: 'lightest' }} space={4}>
      <Typography variant={{ size: 'h3' }}>More content</Typography>
      <Spacer space={4} />
      <Typography>Some example content within the second box.</Typography>
      <Spacer space={2} />
      <Typography>Another line of text in the second box.</Typography>
    </Box>
  </StackView>
</Box>
```

### `space` prop

The `space` prop sets the default padding on all sides, 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
<Box variant={{ background: 'light' }} space={{ xs: 2, sm: 3, lg: 5, xl: 6 }}>
  <Typography>This box has wider padding on wider screens.</Typography>
</Box>
```

### `horizontal` and `vertical` props

These props are the same as the `space` prop but apply space specifically on the left and right (`horizontal`) or top and bottom (`vertical`) sides of a box. If `space` and
`horizontal` or `vertical` are provided, `space` is treated as the default and the more specific props as overrides.

```jsx live
<Box variant={{ background: 'light' }} vertical={4} horizontal={{ xs: 2, sm: 3, lg: 5, xl: 6 }}>
  <Typography>
    This box has wider horizontal padding on wider screens, and stable vertical padding.
  </Typography>
</Box>
```

### `left`, `right`, `top` and `bottom` props

These props are the same as the `space` props above but apply to one specific side only. If one of these is provided alongside `space`,
`horizontal` or `vertical`, those are treated as the defaults and the more specific prop as an override.

```jsx live
<Box variant={{ background: 'light' }} space={4} bottom={{ xs: 2, sm: 3, lg: 5, xl: 6 }}>
  <Typography>
    This box has wider bottom padding on wider screens, and stable padding on other sides.
  </Typography>
</Box>
```

### `flex` prop and layout

By default, the box takes its minimum width as the width of its children (flex value of `0`). `flex={1}` may be passed to make the box stretch and grow to fit a flex container.

Other values greater than `0` may be passed to distribute space proportionately. See the [React Native `flex` documentation](https://reactnative.dev/docs/flexbox#flex) for more details. Behaviour is similar on web and in native apps.

```jsx live
<StackView direction="row" space={3}>
  <Box space={3} variant={{ background: 'light' }} flex={1}>
    <Typography>
      This box has flex prop set to 1. It grows and shrinks, claiming half as much space as its
      flex: 2 neighbour.
    </Typography>
  </Box>
  <Box space={3} variant={{ background: 'light' }} flex={2}>
    <Typography>
      This box has flex prop set to 2. It grows and shrinks, claiming twice as much space as its
      flex: 1 neighbour.
    </Typography>
  </Box>
</StackView>
```

### Using gradients on React Native

To use the custom gradient for mobile, a `customGradient` prop is required, this prop takes a callback
function which returns another callback with access to `children` and must return `LinearGradient` element.
For example: `variant = {{ background: 'featurePrimary' }}`, in background you must indicate the gradient you want.
:::info
LinearGradient must be installed and imported
`import LinearGradient from 'react-native-linear-gradient' `
:::

```jsx
<Box
  variant={{ background: 'featurePrimary' }}
  customGradient={(colors, styles) => (children) =>
    (
      <LinearGradient colors={colors} style={styles}>
        {children}
      </LinearGradient>
    )}
>
  <Typography>
    Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus vitae turpis sit amet nulla
    faucibus tincidunt in vitae leo. Integer luctus arcu eget est rutrum, non aliquam risus
    tincidunt. Pellentesque suscipit eros ac sem imperdiet, nec consequat eros convallis. Etiam quis
    pretium magna, quis interdum enim. Ut sed dui tristique, gravida orci ut, aliquam diam. Integer
    aliquam fermentum ante, a placerat odio ullamcorper eu. Nulla vel velit eu velit pellentesque
    facilisis.
  </Typography>
</Box>
```

### Custom Image Background

Box supports a custom responsive image background. The `backgroundImage` prop takes an object with the following properties:

- Use `src` to specify the image URL.
- Use `alt` to specify the image alt text.
- Use `resizeMode` to resize the image when the frame doesn't match the raw image dimensions.
  It can be set to one of the following values:

  - `cover`: scale the image uniformly (maintain the image's aspect ratio)
    so that both dimensions (width and height) of the image will be equal to or
    larger than the corresponding dimension of the view (minus padding).
  - `contain`: scale the image uniformly (maintain the image's aspect ratio)
    so that both dimensions (width and height) of the image will be equal to or less than
    the corresponding dimension of the view (minus padding).
  - `stretch`: scale width and height independently.
  - `repeat`: repeat the image to cover the frame of the view.
  - `center`: center the image in the view along both dimensions.

  It defaults to `cover` when no value is provided.

- Use `position` to place the background image on the side of the box with `top`, `bottom`, `left`, `right` or `none`. The positioning is only available when `resizeMode` is set to `contain`.
- Use `align` to determine where the background image should appear, with `start`, `center`, `end` or `stretch`. The alignment is only available when `resizeMode` is set to `contain`.

```jsx live
<Box
  variant={{ background: 'brand' }}
  backgroundImage={{
    alt: 'Gamer girl with headphones ',
    resizeMode: { xl: 'cover', lg: 'repeat', md: 'stretch', sm: 'center', xs: 'contain' },
    src: '/universal-design-system/img/gamer-girl-with-headphones.png',
    position: { xl: 'top', lg: 'left', md: 'bottom', sm: 'right', xs: 'bottom' },
    align: { xl: 'start', lg: 'center', md: 'end', sm: 'stretch', xs: 'start' }
  }}
>
  <Box space={5}>
    <StackView direction={{ xs: 'column', md: 'row' }} space={4}>
      <Box flex={1}>
        <Typography block variant={{ size: 'h2', inverse: true }}>
          Connecting with the community
        </Typography>
        <Box top={4}>
          <Typography variant={{ inverse: true }}>
            In the gaming community, pixels dissolve borders as players from every corner of the
            world come together
          </Typography>
        </Box>
      </Box>
      <Box flex={1} space={4}></Box>
    </StackView>
  </Box>
</Box>
```

```jsx live
<Box
  backgroundImage={{
    alt: 'Phone with three cameras',
    resizeMode: 'contain',
    src: '/universal-design-system/img/iPhone_15_Pro_Max.png',
    position: 'right',
    align: 'start'
  }}
>
  <Box space={5}>
    <StackView direction={{ xs: 'column', md: 'row' }} space={4}>
      <Box flex={1}>
        <Typography block variant={{ size: 'h2' }}>
          Pocket Wonders: Navigating the Realm of Modern Smartphones
        </Typography>
        <Box top={4}>
          <Typography>
            Explore the extraordinary universe tucked within your pocket with the latest
            smartphones.
          </Typography>
        </Box>
      </Box>
      <Box flex={1} space={4}></Box>
    </StackView>
  </Box>
</Box>
```

### Accessibility

By default, Box 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.

#### `Tag` prop for semantic HTML

By default, Box 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
<Box tag="main">
  <Box tag="h2" space={2}>
    <Typography variant={{ size: 'h1' }}>"h2" tag on web</Typography>
    <Spacer space={2} />
    <Typography variant={{ size: 'h4' }}>
      This subheading is part of the semantic h2 heading
    </Typography>
  </Box>
  <Box tag="article" space={2}>
    <Typography>This is inside an "Article" tag on web.</Typography>
    <Typography>In native apps, it is treated like any other content.</Typography>
    <Typography>The h2 above it is treated as accessibilityRole="heading".</Typography>
  </Box>
  <Box tag="label" space={2} nativeID="label-box" accessibilityRole="header">
    <Typography>All text in this box is treated as a "Label" on web.</Typography>
    <Typography>In native apps, it is set to behave as a "header".</Typography>
  </Box>
  <Box tag="section" space={2} accessibilityLabelledBy="label-box">
    <Typography>This is inside a "Section" tag on web.</Typography>
    <Typography>On web, the label box's content is this section box'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>
  </Box>
</Box>
```

### Platform considerations

<AvailableOnBoth />

#### Scrollable boxes

The prop `scroll` may be used to make a box scrollable. This changes the box from rendering a `View` to rendering a `ScrollView`. If an object is passed to the `scroll` prop, its properties are passed
to the `ScrollView` as props.

On native apps, where screens do not scroll by default, a `Box` with `scroll` set and appropriate spacing and background colour is therefore a good first container for an app screen.

Do not use `scroll` to make a box within a page scrollable, unless it is as part of a component with a design that is accessible and makes it visually apparent that additional content can be reached. See [`Tabs`](tabs) for an example of such a component.

## Props

<PropsTable base={BaseDocs} />

## Tokens

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

## Variants

Please note that these variants support multiple brands, and must be used if you are building an application that is interoperable.

### Background

Controls the background colour of the box. If not set, the box's background is transparent.

#### lightest

A pure white background.

```jsx live
<Box variant={{ background: 'lightest' }} space={4}>
  <Typography>background: 'lightest'</Typography>
</Box>
```

#### lighter

A light grey background.

```jsx live
<Box variant={{ background: 'lighter' }} space={4}>
  <Typography>background: 'lighter'</Typography>
</Box>
```

#### light

A subtle grey background.

```jsx live
<Box variant={{ background: 'light' }} space={4}>
  <Typography>background: 'light'</Typography>
</Box>
```

#### dark

A heavy grey background. Content within this box should use the `inverse` variant where it is available.

```jsx live
<Box variant={{ background: 'dark' }} space={4}>
  <Typography variant={{ inverse: true }}>background: 'dark'</Typography>
</Box>
```

#### darker

A darker grey background.

```jsx live
<Box variant={{ background: 'darker' }} space={4}>
  <Typography variant={{ inverse: true }}>background: 'darker'</Typography>
</Box>
```

#### darkest

A very dark background. Content within this box should use the `inverse` variant where it is available.

```jsx live
<Box variant={{ background: 'darkest' }} space={4}>
  <Typography variant={{ inverse: true }}>background: 'darkest'</Typography>
</Box>
```

#### critical

A dark red background. Use to alert users to a problem that requires immediate attention. Content within this box should use the `inverse` variant where it is available.

```jsx live
<Box variant={{ background: 'critical' }} space={4}>
  <Typography variant={{ inverse: true }}>background: 'critical'</Typography>
</Box>
```

#### danger

A pink / soft red background. Use carefully to alert users of a problem or warn that an action may have a cost
such as costing money or losing data. Content within this box should use `danger` variants where they are available.

```jsx live
<Box variant={{ background: 'danger' }} space={4}>
  <Button variant={{ danger: true }} onPress={() => {}}>
    background: 'danger'
  </Button>
</Box>
```

#### warning

A yellow background. Use carefully to alert users to something requiring careful attention.

```jsx live
<Box variant={{ background: 'warning' }} space={4}>
  <Typography variant={{ bold: true }}>background: 'warning'</Typography>
</Box>
```

#### positive

A green background. Use when notifying users of a successful event, such as an action having completed successfully.

```jsx live
<Box variant={{ background: 'positive' }} space={4}>
  <Typography variant={{ bold: true }}>background: 'positive'</Typography>
</Box>
```

#### black

A black background. Limit use to align with marketing campaigns such as Black Friday.

```jsx live
<Box variant={{ background: 'black' }} space={4}>
  <Typography variant={{ inverse: true }}>background: 'black'</Typography>
</Box>
```

##### featurePrimary

```jsx live
<Box variant={{ background: 'featurePrimary' }} space={4}>
  <Typography variant={{ inverse: true }}>Gradient: 'featurePrimary'</Typography>
</Box>
```

##### featureSecondary

```jsx live
<Box variant={{ background: 'featureSecondary' }} space={4}>
  <Typography variant={{ inverse: true }}>Gradient: 'featureSecondary'</Typography>
</Box>
```

##### featureNeutral

```jsx live
<Box space={15} variant={{ background: 'featureNeutral' }} space={4}>
  <Typography>Gradient: 'featureNeutral'</Typography>
</Box>
```

#### featureBrand

A brand background. Use sparingly as part of approved on-brand designs that create a bold visual impression. Content within this box should use the `inverse` variant where it is available.

```jsx live
<Box variant={{ background: 'featureBrand' }} space={4}>
  <Typography variant={{ inverse: true }}>background: 'featutureBrand'</Typography>
</Box>
```

## Additional Variants

Please note that if you are building an application that is brand specific you can use these variants.

These brand specific variants are defined in the community themes and you'd need to install those relevant packages and use `mergeThemes` from `@telus-uds/system-constants` to merge the brand specific theme with the core theme.

Here's an example of how to update the application's entry point to merge the Koodo community theme with the core theme:

```jsx
import { StrictMode } from 'react'
import { createRoot } from 'react-dom/client'
import { BaseProvider } from '@telus-uds/components-base'
import themeKoodo from '@telus-uds/theme-koodo'
import themeKoodoCommunity from '@telus-uds/themes-community.theme-koodo'
import { mergeThemes } from '@telus-uds/system-constants'

import App from './App'

const rootElement = document.getElementById('root')
const root = createRoot(rootElement)

root.render(
  <StrictMode>
    <BaseProvider defaultTheme={mergeThemes(themeKoodoCommunity, themeKoodo)}>
      <App />
    </BaseProvider>
  </StrictMode>
)
```

<AdditionalVariants />

## Feedback

<Feedback component="Box" />
