# React Native Swipeable Row (Swipe Actions)

> Swipe actions for React Native list rows on iOS, Android and the web with no gesture library: leading and trailing actions, full swipe to delete, one open row at a time, arrow keys on the web and actions screen readers can reach. Works with List and ListItem.

# SwipeableRow

Swipe a row to reveal actions, as in Mail: trailing actions from the right, leading ones from the left. With `fullSwipe`, a long swipe runs the outermost action. Opening a row closes any other open row, and tapping an open row closes it. Wrap a `ListItem` in it, inside a `List`.

```tsx
import { SwipeableRow } from 'patine-ui';
```

```tsx live
import { useState } from 'react';
import { List, ListItem, SwipeableRow } from 'patine-ui';

export default function Example() {
  const [mail, setMail] = useState(['Flight confirmation', 'Your receipt', 'Team lunch on Friday']);
  const remove = (subject) => setMail((all) => all.filter((m) => m !== subject));
  return (
    <List title="Inbox" style={{ width: 340 }}>
      {mail.map((subject) => (
        <SwipeableRow
          key={subject}
          fullSwipe
          leadingActions={[{ label: 'Pin', variant: 'primary', onPress: () => {} }]}
          trailingActions={[
            { label: 'Delete', variant: 'destructive', onPress: () => remove(subject) },
            { label: 'More', onPress: () => {} },
          ]}
        >
          <ListItem title={subject} description="Swipe me" />
        </SwipeableRow>
      ))}
    </List>
  );
}
```

## Example: a to-do list

A task list: swipe right to complete a task, swipe left to flag or delete it. A full swipe left deletes in one go, and every action is also in the screen reader's actions menu.

```tsx live
import { useState } from 'react';
import { List, ListItem, SwipeableRow, Text } from 'patine-ui';
import { Icon } from '@patine-ui/icons';

const TASKS = [
  { id: 1, title: 'Renew passport', due: 'Today' },
  { id: 2, title: 'Book the dentist', due: 'Tomorrow' },
  { id: 3, title: 'Send the Q4 invoices', due: 'Friday' },
  { id: 4, title: 'Return the library books', due: 'Saturday' },
];

export default function Example() {
  const [tasks, setTasks] = useState(TASKS.map((t) => ({ ...t, done: false, flagged: false })));
  const set = (id: number, change: object) => setTasks((all) => all.map((t) => (t.id === id ? { ...t, ...change } : t)));
  return (
    <List title={`${tasks.filter((t) => !t.done).length} to do`} style={{ width: '100%', maxWidth: 400 }}>
      {tasks.map((task) => (
        <SwipeableRow
          key={task.id}
          fullSwipe
          leadingActions={[
            { label: task.done ? 'Undo' : 'Done', icon: <Icon name="check" color="#FFFFFF" />, color: '#16A34A', onPress: () => set(task.id, { done: !task.done }) },
          ]}
          trailingActions={[
            { label: 'Delete', icon: <Icon name="trash" color="#FFFFFF" />, variant: 'destructive', onPress: () => setTasks((all) => all.filter((t) => t.id !== task.id)) },
            { label: task.flagged ? 'Unflag' : 'Flag', icon: <Icon name="flag" color="#FFFFFF" />, color: '#F59E0B', onPress: () => set(task.id, { flagged: !task.flagged }) },
          ]}
        >
          <ListItem
            title={task.title}
            titleStyle={task.done ? { textDecorationLine: 'line-through', opacity: 0.5 } : undefined}
            description={task.due}
            showChevron={false}
            trailing={task.flagged ? <Icon name="flag" color="warning" size="sm" accessibilityLabel="Flagged" /> : null}
          />
        </SwipeableRow>
      ))}
      {tasks.length ? null : <Text color="textSecondary">Nothing left. Enjoy the weekend.</Text>}
    </List>
  );
}
```

## Actions

Each action has a `label`, an optional `icon` drawn above it, an `onPress`, and a `variant`: `'destructive'` (red), `'primary'`, or `'default'` (a neutral gray). Pass `color` for a colour of your own. The first action on each side sits at the edge, and it's the one a full swipe runs.

## Accessibility

- On iOS and Android, screen readers reach the action buttons right after the row, without swiping.
- On the web, focus the row and press the left arrow to open the trailing actions or the right arrow for the leading ones, then Tab to them; Escape closes the row. Closed actions are hidden from the keyboard.
- Action buttons are as tall as the row and at least 74pt wide.

## Props

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `children` | `ReactNode` | — | The row, usually a `ListItem`. |
| `trailingActions` | `SwipeAction[]` | — | Revealed by swiping left; the first sits at the edge. |
| `leadingActions` | `SwipeAction[]` | — | Revealed by swiping right. |
| `fullSwipe` | `boolean` | `false` | A long swipe runs the edge action. |
| `backgroundColor` | `string` | the List's fill | The row's fill over the actions. |
| `disabled` | `boolean` | `false` | |
| `onOpenChange` | `(side: 'leading' \| 'trailing' \| null) => void` | — | |
| `style` | `StyleProp<ViewStyle>` | — | |
| `theme` / `design` | | provider's | Action widths: 74 iOS, 88 Material, 80 web and brand. |

### SwipeAction

| Field | Type | Description |
| --- | --- | --- |
| `label` | `string` | Shown and read by screen readers. |
| `icon` | `ReactNode` | Drawn above the label. |
| `variant` | `'default' \| 'primary' \| 'destructive'` | The fill. |
| `color` | `string` | A fill of your own. |
| `onPress` | `() => void` | The row closes after it runs. |
