---
title: Button
---

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

# Button

<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%3D224%253A463" />

```jsx live
<Button onPress={() => alert('Hello world!')}>Click me</Button>
```

## Introduction

Buttons allow users to perform an action.

They have multiple styles for various needs, and are ideal for calling attention to where a user needs to do something in order to move forward in a flow.

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

## Guidance

Provide a function as the `onPress` prop to perform an action when clicked.

- Use buttons to move through a transaction
- Aim to use only one primary button variant per page
- Keep the text short and precise. It should fit on a single line (our recommendation is 2-24 characters for French and English)
- Make sure the button text describes an action
- Buttons should not be disabled. Use inline (on blur) error messaging,
  to provide feedback when the form is invalid

### Alternatives

**Do not** use Button for:

- Navigating to a URL which represents a choice in the current action flow: consider [ButtonLink](button-link)
- One navigation action taking the user to a page that is the only logical next step: consider [ChevronLink](chevron-link)
- Smaller actions that occur within the current page or screen: consider [TextButton](text-button)
- Navigating to a page outside of the user's current action flow: consider [Link](link)

### Using refs

`Button` component provides ref forwarding, which allows the user to trigger events like focusing, clicking, etc., programmatically:

```jsx live
function buttonRefExample() {
  const ref = useRef()
  const handlePress = () => ref.current.focus()

  return (
    <StackView>
      <Typography>Click THIS to focus on THAT</Typography>
      <Button onPress={handlePress}>THIS</Button>
      <Button ref={ref}>THAT</Button>
    </StackView>
  )
}
```

### Accessibility

Insert [A11yText](a11y-text) any time an interactive element's label lacks context
that a sighted user would see in the content around it. Many users of assistive technology explore a page
by cycling through the interactive elements first.

```jsx live
<StackView space={2}>
  <Typography>The new MegaPhone is our loudest phone yet.</Typography>
  <Button variant={{ priority: 'high' }} onPress={() => {}}>
    Add to cart{<A11yText text=", MegaPhone" />}
  </Button>
</StackView>
```

In the above example, screenreaders read "Add to cart, MegaPhone" on focusing the button.

Appending hidden text at the end of the element is the best practice for voice to text technology.

Button is by default described to accessibility tools as having the `button` role. Button accepts
React Native's accessibility props ([web docs](https://necolas.github.io/react-native-web/docs/accessibility/),
[native docs](https://reactnative.dev/docs/accessibility)).

### Platform considerations

<AvailableOnBoth />

Button background animated transitions are not currently supported in iOS or Android apps. Focus states are not well supported on native platforms.

## Props

<PropsTable base={BaseDocs} />

## Tokens

:::warning
Note: `borderWidth` token is deprecated and will be removed in the next major release. Please consider using `borderRightWidth`, `borderLeftWidth`, `borderTopWidth`, and `borderBottomWidth` to achieve the same outcome.
:::

<TokensTable id="Button" base={BaseDocs} />

## Variants

### Priority

- Priority indicates the importance of the call-to-action
- Limit the amount of high priority buttons on a page so that you proritize meaningful user flows

```jsx live
<div style={{ display: 'flex', gap: '1em' }}>
  <Button onPress={noop} variant={{ priority: 'high' }}>
    High
  </Button>

  <Button onPress={noop} variant={{ priority: 'low' }}>
    Low
  </Button>
</div>
```

### Danger

- Danger indicates a user action that is destructive that will result in loss of information or cause significant monetary charges
- Limit the use of the danger button whenever possible

```jsx live
<Button onPress={noop} variant={{ danger: true }}>
  Danger
</Button>
```

### Size

- Available in default or small size
- Size is not responsive, unless otherwise specified in code

```jsx live
<div style={{ display: 'flex', flexDirection: 'column', gap: '1rem' }}>
  <Button onPress={noop} variant={{ priority: 'high' }}>
    Regular
  </Button>
  <Button onPress={noop} variant={{ size: 'small', priority: 'high' }}>
    Small
  </Button>
</div>
```

### Inverse

- Both high and low priority buttons are available in inverse
- Use inverse buttons on darker backgrounds

```jsx live dark
<Box variant={{ background: 'black' }} space={3}>
  <StackWrap space={3}>
    <Button onPress={noop} variant={{ priority: 'high', inverse: true }}>
      High
    </Button>
    <Button onPress={noop} variant={{ priority: 'low', inverse: true }}>
      Low
    </Button>
  </StackWrap>
</Box>
```

### Icons

Icons may be imported and passed to the `icon` prop.

By default, icons are aligned left, ensuring icons align when multiple buttons are
stacked vertically.

```jsx
import { Profile, Settings } from '@telus-uds/palette-allium/build/web/icons'
```

```jsx live
<StackView space={3}>
  <Button icon={Icons.Profile}>Profile</Button>
  <Button icon={Icons.Settings}>Settings</Button>
</StackView>
```

The icon may be aligned to the right of the text using `iconPosition` prop.

```jsx
import { LinkExternal } from '@telus-uds/palette-allium/build/web/icons'
```

```jsx live
<Button icon={Icons.LinkExternal} iconPosition="right">
  Open external page
</Button>
```

### Text

- Use the Text variant to visually deprioritize the Button
- It maintains the same height of Button so that you can put them next to each other without additional effort to vertically align
- Visually appears similar to Link, renders as Button element
- Works with low/high priority and danger variants

```jsx live dark
<>
  <div style={{ display: 'flex', gap: '1em' }}>
    <Button onPress={noop} variant={{ text: true, priority: 'high' }}>
      High Priority Text
    </Button>
    <Button onPress={noop} variant={{ text: true, priority: 'low' }}>
      Low Priority Text
    </Button>
    <Button onPress={noop} variant={{ text: true, danger: true }}>
      Danger Text
    </Button>
  </div>
  <div style={{ backgroundColor: '#000', padding: '10px' }}>
    <Button onPress={noop} variant={{ text: true, inverse: true }}>
      Inverse Text
    </Button>
  </div>
</>
```

### Width

- Available in default, full-width or responsive.
- Default-width - expands based on content.
- Full-width `variant={{ width: 'full'}}` - expands to width of the container.
- Responsive `variant={{ width: 'responsive'}}` - automatically applies full-width when is displayed on the XS viewport.

```jsx live dark
<div style={{ display: 'flex', gap: '1em', flexDirection: 'column' }}>
  <Button>Regular width</Button>
  <Button variant={{ width: 'full' }}>Full width</Button>
  <Button variant={{ width: 'responsive' }}>Responsive width</Button>
</div>
```

## Feedback

<Feedback component="Button" />
