# React Native Collapsible (expand / collapse)

> An accessible React Native collapsible for iOS, Android and the web, like shadcn/ui's: a trigger that shows and hides content with an animated height. Swap the chevron for any icon, move it or hide it, change or stop its turn, or make any element the trigger with asChild. Controlled or uncontrolled, reduce motion respected. Use it as one element with props, or compose the parts.

# Collapsible

One section that shows and hides its content: "Show more", advanced settings, a filter group. A `CollapsibleTrigger` opens and closes its `CollapsibleContent`, whose height animates. For a stack of sections where opening one can close another, use [Accordion](./accordion).

```tsx
import { Collapsible, CollapsibleContent, CollapsibleTrigger } from 'patine-ui';
```

The quickest way: give `Collapsible` a `trigger`, and its children become the content. A string is drawn as a label with a chevron; an element, such as a `Button`, becomes the trigger itself.

```tsx live
import { Text } from 'react-native';
import { Collapsible } from 'patine-ui';

export default function Example() {
  return (
    <Collapsible trigger="Shipping details">
      <Text>Ships in two days from our warehouse in Lisbon.</Text>
    </Collapsible>
  );
}
```

## Compose the parts

For full control, leave `trigger` out and compose `CollapsibleTrigger` and `CollapsibleContent`.

```tsx live
import { Collapsible, CollapsibleContent, CollapsibleTrigger, Text, VStack } from 'patine-ui';

export default function Example() {
  return (
    <Collapsible style={{ maxWidth: 360, width: '100%' }}>
      <CollapsibleTrigger>Advanced settings</CollapsibleTrigger>
      <CollapsibleContent>
        <VStack gap="xs" style={{ paddingVertical: 8 }}>
          <Text color="textSecondary">Proxy: off</Text>
          <Text color="textSecondary">Cache size: 512 MB</Text>
          <Text color="textSecondary">Log level: warnings</Text>
        </VStack>
      </CollapsibleContent>
    </Collapsible>
  );
}
```

## Example: an order summary at checkout

A phone checkout: the order summary folds away so the payment form comes first. The trigger shows the total either way, so it never has to be opened just to see the price.

```tsx live
import { useState } from 'react';
import { Collapsible, CollapsibleContent, CollapsibleTrigger, HStack, Separator, Text, TextField, VStack } from 'patine-ui';

const ITEMS = [
  { name: 'Linen shirt, sand, M', price: 59 },
  { name: 'Canvas tote × 2', price: 48 },
  { name: 'Shipping', price: 6 },
];
const total = ITEMS.reduce((sum, item) => sum + item.price, 0);

export default function Example() {
  const [open, setOpen] = useState(false);
  return (
    <VStack gap="md" style={{ width: '100%', maxWidth: 420 }}>
      <Collapsible open={open} onOpenChange={setOpen}>
        <CollapsibleTrigger accessibilityLabel={`${open ? 'Hide' : 'Show'} order summary, total $${total}`}>
          <HStack justify="space-between" style={{ flex: 1 }}>
            <Text color="primary">{open ? 'Hide order summary' : 'Show order summary'}</Text>
            <Text weight="semibold">${total}.00</Text>
          </HStack>
        </CollapsibleTrigger>
        <CollapsibleContent>
          <VStack gap="xs" style={{ paddingTop: 8 }}>
            {ITEMS.map((item) => (
              <HStack key={item.name} justify="space-between">
                <Text color="textSecondary">{item.name}</Text>
                <Text>${item.price}.00</Text>
              </HStack>
            ))}
          </VStack>
        </CollapsibleContent>
      </Collapsible>
      <Separator />
      <TextField label="Card number" placeholder="1234 1234 1234 1234" keyboardType="number-pad" />
      <HStack gap="sm">
        <TextField label="Expiry" placeholder="MM / YY" style={{ flex: 1 }} />
        <TextField label="CVC" placeholder="123" style={{ flex: 1 }} />
      </HStack>
    </VStack>
  );
}
```

## Any icon, anywhere

Everything about the icon is yours:

- `icon` swaps the chevron for any node: an icon from your set, an emoji, text. It still turns as it opens.
- `renderIcon={({ open }) => …}` draws it from the open state, such as + and −. It isn't turned.
- `iconPosition`: `'start'` (before the label), `'end'` (after it) or `'none'`.
- `rotation={{ closed, open }}` sets the turn in degrees; `rotation={false}` keeps it still.

```tsx live
import { Collapsible, CollapsibleContent, CollapsibleTrigger, Text, VStack } from 'patine-ui';
import { Icon } from '@patine-ui/icons';

const Body = ({ children }) => <Text color="textSecondary" style={{ paddingBottom: 8 }}>{children}</Text>;

export default function Example() {
  return (
    <VStack gap="xs" style={{ maxWidth: 360, width: '100%' }}>
      <Collapsible>
        <CollapsibleTrigger renderIcon={({ open }) => <Text size="lg" color="primary">{open ? '−' : '+'}</Text>}>
          Plus and minus
        </CollapsibleTrigger>
        <CollapsibleContent><Body>Drawn from the open state.</Body></CollapsibleContent>
      </Collapsible>
      <Collapsible>
        <CollapsibleTrigger icon={<Icon name="arrowRight" color="primary" />} iconPosition="start" rotation={{ closed: 0, open: 90 }}>
          A pointing hand that turns down
        </CollapsibleTrigger>
        <CollapsibleContent><Body>Any node turns between the two angles.</Body></CollapsibleContent>
      </Collapsible>
      <Collapsible>
        <CollapsibleTrigger icon={<Icon name="chevronDown" color="primary" />} rotation={false}>A still arrow</CollapsibleTrigger>
        <CollapsibleContent><Body>rotation=false keeps it as it is.</Body></CollapsibleContent>
      </Collapsible>
      <Collapsible>
        <CollapsibleTrigger iconPosition="none">No icon</CollapsibleTrigger>
        <CollapsibleContent><Body>The label alone opens it.</Body></CollapsibleContent>
      </Collapsible>
    </VStack>
  );
}
```

## Your own trigger

`asChild` makes the trigger's one child the trigger itself, like Radix: a `Button`, a card, any pressable of yours. It gets `onPress` (its own still runs first), `aria-expanded` and `aria-controls`.

```tsx live
import { useState } from 'react';
import { Button, Collapsible, CollapsibleContent, CollapsibleTrigger, Text, VStack } from 'patine-ui';

export default function Example() {
  const [open, setOpen] = useState(false);
  return (
    <Collapsible open={open} onOpenChange={setOpen} style={{ maxWidth: 360, width: '100%' }}>
      <VStack gap="sm">
        <Text>Reply from Maya: "Sounds good, see you at 10."</Text>
        <CollapsibleContent>
          <Text color="textSecondary">Earlier: "Can we move the call?" · "Sure, what time?"</Text>
        </CollapsibleContent>
        <CollapsibleTrigger asChild>
          <Button variant="outline" size="sm" style={{ alignSelf: 'flex-start' }}>
            {open ? 'Hide thread' : 'Show thread'}
          </Button>
        </CollapsibleTrigger>
      </VStack>
    </Collapsible>
  );
}
```

## Nested sections

Collapsibles nest: an open section grows with a child that opens inside it. For a large or data-driven tree with selection, checkboxes, lazy loading and keyboard navigation, use [TreeView (Pro)](../blocks/tree-view).

```tsx live
import { Collapsible, CollapsibleContent, CollapsibleTrigger, Text } from 'patine-ui';

export default function Example() {
  return (
    <Collapsible defaultOpen style={{ maxWidth: 360, width: '100%' }}>
      <CollapsibleTrigger>src</CollapsibleTrigger>
      <CollapsibleContent style={{ paddingStart: 20 }}>
        <Collapsible>
          <CollapsibleTrigger>components</CollapsibleTrigger>
          <CollapsibleContent style={{ paddingStart: 20 }}>
            <Text style={{ paddingVertical: 8 }}>Button.tsx</Text>
          </CollapsibleContent>
        </Collapsible>
        <Text style={{ paddingVertical: 8 }}>index.ts</Text>
      </CollapsibleContent>
    </Collapsible>
  );
}
```

## Designs

- **Brand** and **Material**: a chevron after the label that flips up when open, as in shadcn/ui and Material's expandable list items.
- **iOS** and **web**: a disclosure arrow before the label that turns down when open, as in the Files app and the browser's `<details>`.

## Accessibility

- The trigger is a `button` with `aria-expanded`; on the web it names its content with `aria-controls`, and the content is a `region` labelled by the trigger.
- Enter and Space open and close it. A keyboard focus shows a ring. It's at least 44pt tall.
- The icon is hidden from screen readers, since the expanded state says it. With `renderIcon` returning text, keep it decorative (+ and −) and let the label carry the meaning.
- With the OS set to reduce motion, the content opens and closes at once and the icon turns without animating.

## Props

### Collapsible

Also takes React Native's `View` props.

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `open` | `boolean` | — | Open, controlled. Pair with `onOpenChange`. |
| `defaultOpen` | `boolean` | `false` | Open at first, when uncontrolled. |
| `onOpenChange` | `(open: boolean) => void` | — | Called when the user opens or closes it, not when `open` changes. |
| `disabled` | `boolean` | `false` | The trigger ignores presses. |
| `trigger` | `ReactNode` | — | Shorthand: a string label with a chevron, or an element that becomes the trigger. `children` are then the content. |
| `theme` | `DeepPartial<Theme>` | — | Theme override. |
| `design` | `'brand' \| 'ios' \| 'material' \| 'web' \| 'native'` | from `PatineProvider` | The look to follow: [brand, iOS, Material or web](../guides/designs), or `'native'` for the platform's own. |

### CollapsibleTrigger

Also takes React Native's `Pressable` props.

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `children` | `ReactNode` | **required** | The label. A string is drawn in the theme's text style. |
| `asChild` | `boolean` | `false` | Make the one child the trigger instead of a label row. |
| `icon` | `ReactNode \| null` | the chevron | Any node in place of the chevron; it turns with `rotation`. `null` draws none. |
| `renderIcon` | `({ open }) => ReactNode` | — | Draws the icon from the open state; wins over `icon` and isn't turned. |
| `iconPosition` | `'start' \| 'end' \| 'none'` | the design's | Before the label, after it, or none. |
| `rotation` | `{ closed: number; open: number } \| false` | the design's | The icon's turn in degrees; `false` keeps it still. |
| `style` | `StyleProp<ViewStyle>` | — | The row. |
| `labelStyle` | `StyleProp<TextStyle>` | — | The label's text. |

### CollapsibleContent

Also takes React Native's `View` props; `style` goes on the content's box, inside the animated frame. It unmounts once closed.

### DisclosureIcon

The turning icon on its own, for your own disclosure rows: `<DisclosureIcon open={open} theme={theme} defaultRotation={{ closed: -90, open: 0 }} />`, with the same `icon`, `renderIcon` and `rotation` props.
