Skip to main content

Box

Library
UDS Base
Repository
github.com/telus/universal-design-system
Package
npmjs.com/package/@telus-uds/ds-allium
Package version
1.14.0
Loading...

Introduction

import { Box } from '@telus-uds/ds-allium'

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

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:

Loading...

space prop

The space prop sets the default padding on all sides, using the spacing scale.

Simple numbers may be used, or objects (see spacing scale docs for details. These examples change spacing depending on the viewport.

Loading...

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.

Loading...

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.

Loading...

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 for more details. Behaviour is similar on web and in native apps.

Loading...

Accessibility

By default, Box applies no accessibility props or values relevant to accessibility tools. It may accept React Native accessibility props (web docs, native docs), 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 for the full list of supported tags.

Loading...

Platform considerations

The component is available on both native platforms and web.

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 for an example of such a component.

Props

NameTypeDefaultDescription
bottomspacingIndexPropType | spacingObjectPropTypeverticalSets bottom padding using the theme's spacing scale. @see {@link SpacingValue}
children*nodeBox accepts any content as children.
flexnumberSets the 'flex' style, which controls 'flexGrow', 'flexShrink' and 'flexBasis' styles. Set as 1 for the box to stretch and grow to fit in the available space, or another number to share space disproportionately with siblings. See https://reactnative.dev/docs/flexbox#flex for details. With the default '0', the box takes its minimum width from its content.
horizontalspacingIndexPropType | spacingObjectPropTypespaceSets left and right padding using the theme's spacing scale. @see {@link SpacingValue}
leftspacingIndexPropType | spacingObjectPropTypehorizontalSets left padding using the theme's spacing scale. @see {@link SpacingValue}
rightspacingIndexPropType | spacingObjectPropTypehorizontalSets right padding using the theme's spacing scale. @see {@link SpacingValue}
scrollbool | ScrollView.propTypes ? PropTypes.shape(ScrollView.propTypes) : PropTypes.objectRenders a scrollable ScrollView instead of an unscrollable View. May take an object of ScrollView props which are passed to the rendered ScrollView.
spacespacingIndexPropType | spacingObjectPropTypeSets default padding on all sides of the box using the theme's spacing scale. @see {@link SpacingValue}
tag'h1' | 'h2' | 'h3' | 'h4' | 'h5' | 'h6' | 'article' | 'aside' | 'blockquote' | 'footer' | 'figure' | 'form' | 'header' | 'ul' | 'li' | 'main' | 'nav' | 'section' | 'label'Optional semantic HTML tag, to apply to the container on web. Does not change styling. In native apps, if a header tag is provided ("h1", "h2" etc) and accessibilityRole prop is not defined, the accessibilityRole will default to "heading".
testIDstringUse in tests if the box itself needs to be targetted and no other way of selecting the box is available (e.g. it cannot be targetted using an appropriate 'accessibilityRole').
tokenscustomSystem tokens prop, see tokens for more details
topspacingIndexPropType | spacingObjectPropTypeverticalSets top padding using the theme's spacing scale. @see {@link SpacingValue}
variantobjectOfSystem variant prop, see variants for more details
verticalspacingIndexPropType | spacingObjectPropTypespaceSets top and bottom padding using the theme's spacing scale. @see {@link SpacingValue}

Tokens

In exceptional circumstances, the following tokens can be passed to this component to override its default styles. Do not do this unless absolutely necessary. Read more about overriding styles.

View Tokens
Prop NameToken PropertyToken Type
tokensbackgroundColorcolor

Figma

Variants

Background

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

{ background: 'lightest' }

A pure white background.

Loading...

{ background: 'light' }

A subtle grey background.

Loading...

{ background: 'dark' }

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

Loading...

{ background: 'darkest' }

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

Loading...

{ background: '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.

Loading...

{ background: '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.

Loading...

{ background: 'warning' }

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

Loading...

{ background: 'positive' }

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

Loading...

{ background: 'brand' }

A purple 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.

Loading...

Feedback