---
title: ExpandCollapse
---

import LibraryInfo from '@theme/LibraryInfo'
import { Feedback, ExternalCodeBlock, TokensTable, PropsTable } from '../../src/components'
import {
  ExpandCollapse,
  Spacer,
  StackView,
  TextButton,
  Typography
} from '@telus-uds/components-web'
import { ExpandCollapse as BaseDocs } from 'docs/@telus-uds/components-base'
import Example from '../../src/components/Examples/ExpandCollapse'

# ExpandCollapse

<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%3D2311%253A17421" />

<Example />

## Introduction

Flexible base component for lists where some or all items are collapsible headers.

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

## Guidance

ExpandCollapse is designed for flexibility - components for a particular interaction pattern can be
built on top of it. [SideNav](side-nav) is an example of a component built on
top of ExpandCollapse that facilitates a particular interaction pattern.

### `ExpandCollapse`

The outer `ExpandCollapse` represents the container for one or more collapsible `ExpandCollapse.Panel`s
and manages state on which panels are open and closed.

#### Children

`ExpandCollapse` takes a render function as its child and passes it an object, `expandProps`, which can be spread
onto any `ExpandCollapse.Panel`s. These don't need to be direct children and can be anywhere inside
the `ExpandCollapse` container.

Any number or arrangment of non-expandable items may be included alongside expandable panels.

```jsx live
<ExpandCollapse>
  {(expandProps) => (
    <StackView space={3}>
      <ExpandCollapse.Panel
        {...expandProps}
        panelId="first"
        control={<Typography>First panel</Typography>}
      >
        <Typography>Some content within the first panel</Typography>
      </ExpandCollapse.Panel>
      <ExpandCollapse.Panel
        {...expandProps}
        panelId="second"
        control={<Typography>Second panel</Typography>}
      >
        <Typography>Some content within the second panel</Typography>
      </ExpandCollapse.Panel>
      <ExpandCollapse.Panel panelId="third" content>
        <Typography>Some non-expandable content</Typography>
      </ExpandCollapse.Panel>
    </StackView>
  )}
</ExpandCollapse>
```

#### `maxOpen` prop

By default, any number of items may be opened at once. If desired, a number may be passed
to ExpandCollapse's `maxItems` props to pin it only allow that many items to be open at a time.

The `Accordion` component is a shorthand for `ExpandCollapse` with maxOpen set as 1.

#### `initialOpen` prop

An array of panel Ids may be passed to the `initialOpen` prop to set those panels as open
by default.

```jsx live
<ExpandCollapse maxOpen={2} initialOpen={['first', 'second']}>
  {(expandProps) => (
    <StackView>
      <ExpandCollapse.Panel
        {...expandProps}
        panelId="first"
        control={<Typography>First panel</Typography>}
      >
        <Typography>Some content within the first panel</Typography>
      </ExpandCollapse.Panel>
      <ExpandCollapse.Panel
        {...expandProps}
        panelId="second"
        control={<Typography>Second panel</Typography>}
      >
        <Typography>Some content within the second panel</Typography>
      </ExpandCollapse.Panel>
      <ExpandCollapse.Panel
        {...expandProps}
        panelId="third"
        control={<Typography>Third panel</Typography>}
      >
        <Typography>Some content within the third panel</Typography>
      </ExpandCollapse.Panel>
    </StackView>
  )}
</ExpandCollapse>
```

#### Controlled behaviour with `open` prop

An ExpandCollapse may be controlled by a parent by passing an array of panel IDs to its
`open` prop and a function that updates state to `onChange`. When delegating responsibility
to the parent like this, `initialOpen` prop should not be used and `open` should always be
defined as an array (pass an empty array when nothing is open).

<ExternalCodeBlock
  file="expand-collapse-controlled"
  scope={{ ExpandCollapse, Spacer, StackView, TextButton, Typography }}
/>

### `ExpandCollapse.Panel`

Each section that may be opened and closed should be wrapped in an `ExpandCollapse.Panel` and
the `expandProps` from the `ExpandCollapse` parent should be spread to each of these.

#### `panelId` prop (required)

Every ExpandCollapse.Panel must also take a `panelId` prop. This string will be used by the
`ExpandCollapse` parent to manage whether this panel is open or closed.

Note that unlike HTML element `id` attributes, this does not need to be unique on the page.
It is only used in the scope of the one ExpandCollapse parent. It should normally be unique
within this one ExpandCollapse parent. If a panel id was to appear more than once, opening
and closing each panel with the shared ID would open all others with the same ID.

#### `control` prop (required)

The `control` prop contains the rendered content in the pressable section that opens and
closes the panel.

The icon and spacing around this content are controlled by the theme, which may be
overridden by passing the panel a `controlTokens` object, for example,
`controlTokens={{ icon: null }}` removes the panel's control's icon.

### Accessibility

An ExpandCollapse panel's control is announced to assistive technology as a `button` with
platform-appropriate notification of its expanded state automatically applied (`aria-expanded`
on web and React Native's `accessibilityState.expanded` applied in native apps).

### Platform considerations

ExpandCollapse is available on both web and native apps.

## Props

### ExpandCollapse

<PropsTable base={BaseDocs} />

### ExpandCollapse.Panel

<PropsTable base={BaseDocs.Panel} />

## Tokens

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

### Use of `dataSet`

Use `dataSet` prop to pass custom data attributes. Here's an example:

```jsx live
<ExpandCollapse>
  {(expandProps) => (
    <StackView space={3}>
      <ExpandCollapse.Panel
        {...expandProps}
        panelId="first"
        dataSet={{ 'field-id': 'test field id', 'entry-id': 'test entry id' }}
        control={<Typography>First panel</Typography>}
      >
        <Typography>Some content within the first panel</Typography>
      </ExpandCollapse.Panel>
      <ExpandCollapse.Panel
        {...expandProps}
        panelId="second"
        control={<Typography>Second panel</Typography>}
      >
        <Typography>Some content within the second panel</Typography>
      </ExpandCollapse.Panel>
      <ExpandCollapse.Panel panelId="third" content>
        <Typography>Some non-expandable content</Typography>
      </ExpandCollapse.Panel>
    </StackView>
  )}
</ExpandCollapse>
```

## Variants

#### Compact

```jsx live
<ExpandCollapse maxOpen={2} initialOpen={['first', 'second']}>
  {(expandProps) => (
    <StackView>
      <ExpandCollapse.Panel
        {...expandProps}
        panelId="first"
        variant={{ compact: true }}
        control={<Typography>First panel</Typography>}
      >
        <Typography>Some content within the first panel</Typography>
      </ExpandCollapse.Panel>
      <ExpandCollapse.Panel
        {...expandProps}
        panelId="second"
        variant={{ compact: true }}
        control={<Typography>Second panel</Typography>}
      >
        <Typography>Some content within the second panel</Typography>
      </ExpandCollapse.Panel>
      <ExpandCollapse.Panel
        {...expandProps}
        panelId="third"
        variant={{ compact: true }}
        control={<Typography>Third panel</Typography>}
      >
        <Typography>Some content within the third panel</Typography>
      </ExpandCollapse.Panel>
    </StackView>
  )}
</ExpandCollapse>
```

## Feedback

<Feedback component="ExpandCollapse" />
