# React Native Alert Dialog

> An accessible React Native alert dialog for iOS, Android and the web, built like shadcn/ui's AlertDialog: a confirm or cancel question with AlertDialogAction and AlertDialogCancel, a destructive action, Cancel focused first, no backdrop dismiss, and iOS alert, Material 3, web and brand designs. Zero native code. Use it as one element with props, or compose the parts.

# AlertDialog

A [`Dialog`](./dialog) that interrupts to ask a question the user must answer: delete this, discard changes, sign out. A press on the backdrop doesn't close it, so the answer is always a choice, and Cancel takes focus first, so Enter on the web doesn't confirm by accident. In the iOS design it looks like a native iOS alert.

```tsx
import {
  AlertDialog,
  AlertDialogAction,
  AlertDialogCancel,
  AlertDialogContent,
  AlertDialogDescription,
  AlertDialogFooter,
  AlertDialogHeader,
  AlertDialogTitle,
  AlertDialogTrigger,
} from 'patine-ui';
```

The quickest way: give `AlertDialog` a `title`, a `description`, a `confirmLabel` and `onConfirm`, and it draws the parts itself, with Cancel first. `destructive` draws the confirm button in the danger color.

```tsx live
import { AlertDialog, Button } from 'patine-ui';

export default function Example() {
  return (
    <AlertDialog
      trigger={<Button variant="destructive">Delete file</Button>}
      title="Delete this file?"
      description="It will be gone for good. This can't be undone."
      confirmLabel="Delete"
      destructive
      onConfirm={() => {}}
    />
  );
}
```

## Compose the parts

For full control, leave those props out and compose the parts, as in shadcn/ui.

```tsx live
import {
  AlertDialog,
  AlertDialogAction,
  AlertDialogCancel,
  AlertDialogContent,
  AlertDialogDescription,
  AlertDialogFooter,
  AlertDialogHeader,
  AlertDialogTitle,
  AlertDialogTrigger,
  Button,
} from 'patine-ui';

export default function Example() {
  return (
    <AlertDialog>
      <AlertDialogTrigger>
        <Button variant="destructive">Delete note</Button>
      </AlertDialogTrigger>
      <AlertDialogContent>
        <AlertDialogHeader>
          <AlertDialogTitle>Delete this note?</AlertDialogTitle>
          <AlertDialogDescription>It's removed from all your devices. This can't be undone.</AlertDialogDescription>
        </AlertDialogHeader>
        <AlertDialogFooter>
          <AlertDialogCancel>Cancel</AlertDialogCancel>
          <AlertDialogAction variant="destructive">Delete</AlertDialogAction>
        </AlertDialogFooter>
      </AlertDialogContent>
    </AlertDialog>
  );
}
```

## Example: remove a teammate

A workspace's members list: Remove asks first, names the person and what they lose, and only the destructive button removes them.

```tsx live
import { useState } from 'react';
import { AlertDialog, Avatar, Button, List, ListItem, Text } from 'patine-ui';

const MEMBERS = [
  { id: 'priya', name: 'Priya Shah', role: 'Admin', projects: 6 },
  { id: 'marco', name: 'Marco Rossi', role: 'Editor', projects: 4 },
  { id: 'lena', name: 'Lena Novak', role: 'Viewer', projects: 2 },
];

export default function Example() {
  const [members, setMembers] = useState(MEMBERS);
  const [removing, setRemoving] = useState<(typeof MEMBERS)[number] | null>(null);
  return (
    <>
      <List title="Members" footer={`${members.length} of 10 seats used`} style={{ width: '100%', maxWidth: 440 }}>
        {members.map((member) => (
          <ListItem
            key={member.id}
            leading={<Avatar name={member.name} size="sm" />}
            title={member.name}
            description={member.role}
            trailing={
              <Button size="sm" variant="ghost" onPress={() => setRemoving(member)}>
                Remove
              </Button>
            }
          />
        ))}
      </List>
      {members.length ? null : <Text color="textSecondary">Only you are left.</Text>}
      <AlertDialog
        open={removing !== null}
        onOpenChange={(open) => !open && setRemoving(null)}
        title={`Remove ${removing?.name}?`}
        description={`They lose access to ${removing?.projects} projects right away. Their comments stay, and you can invite them again later.`}
        confirmLabel="Remove"
        destructive
        onConfirm={() => setMembers((all) => all.filter((m) => m.id !== removing?.id))}
      />
    </>
  );
}
```

## Action and Cancel

`AlertDialogAction` and `AlertDialogCancel` are buttons that close the dialog. Action runs its `onPress` first; Cancel just closes. Both take [`Button`](./button)'s props.

- `variant="destructive"` on the action draws it in the theme's `danger` color, for Delete, Remove or Sign out.
- In the Patine and web designs, Action is a filled button and Cancel an outlined one. In iOS and Material they're text buttons in the primary color.
- Put Cancel first in the footer, as shadcn/ui does.

## Controlled

Pass `open` and `onOpenChange` to ask from your own code, for example when the user leaves a screen with unsaved changes. `onOpenChange` fires only for what the user does.

```tsx live
import { useState } from 'react';
import {
  AlertDialog,
  AlertDialogAction,
  AlertDialogCancel,
  AlertDialogContent,
  AlertDialogDescription,
  AlertDialogFooter,
  AlertDialogHeader,
  AlertDialogTitle,
  Button,
  Text,
  VStack,
} from 'patine-ui';

export default function Example() {
  const [open, setOpen] = useState(false);
  const [status, setStatus] = useState('Draft');
  return (
    <VStack gap="sm" align="center">
      <Button variant="outline" onPress={() => setOpen(true)}>Leave editor</Button>
      <Text color="textSecondary">{status}</Text>
      <AlertDialog open={open} onOpenChange={setOpen}>
        <AlertDialogContent>
          <AlertDialogHeader>
            <AlertDialogTitle>Discard changes?</AlertDialogTitle>
            <AlertDialogDescription>Your edits to this draft will be lost.</AlertDialogDescription>
          </AlertDialogHeader>
          <AlertDialogFooter>
            <AlertDialogCancel>Keep editing</AlertDialogCancel>
            <AlertDialogAction variant="destructive" onPress={() => setStatus('Discarded')}>
              Discard
            </AlertDialogAction>
          </AlertDialogFooter>
        </AlertDialogContent>
      </AlertDialog>
    </VStack>
  );
}
```

## Dismissing

The backdrop never closes an AlertDialog. Escape on the web, Android's back button and the iOS VoiceOver escape gesture cancel it, as Cancel would. Pass `dismissible={false}` to turn those off too.

## Designs

An alert is narrower than a dialog with a form (360 wide in brand, 384 on the web, 312dp in Material), so one asked over another dialog, such as "Discard changes?" over an edit form, reads as its own card over the dimmed form.

- **brand** and **web**: a centered card, the buttons filled and outlined at the end.
- **ios**: an iOS alert, 270pt wide, with centered text and full-width text buttons split by hairlines, side by side for two and stacked for three or more.
- **material**: a Material 3 dialog with 28dp corners and text buttons at the end.

## Accessibility

- `role="alertdialog"` with `aria-modal`, labelled by the title and described by the description.
- On the web, Cancel takes focus when it opens, Tab stays inside, and focus goes back to what opened it. Cancel's focus ring shows only when the alert was opened from the keyboard, not after a click.
- On iOS, VoiceOver stays inside the alert (`accessibilityViewIsModal`).
- Buttons keep a 44pt touch target, and reduce motion turns off the animation.

## Props

### AlertDialog

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `open` | `boolean` | — | Open, when you control it. |
| `defaultOpen` | `boolean` | `false` | Open at first, when it keeps its own state. |
| `onOpenChange` | `(open: boolean) => void` | — | Called when the user opens or closes it. |
| `trigger` | `ReactElement` | — | Shorthand: one pressable, such as a `Button`, that opens it. |
| `title` | `ReactNode` | — | Shorthand: the `AlertDialogTitle`. |
| `description` | `ReactNode` | — | Shorthand: the `AlertDialogDescription`. |
| `confirmLabel` | `string` | `'Continue'` | Shorthand: the confirm button's label. |
| `cancelLabel` | `string` | `'Cancel'` | Shorthand: the cancel button's label. |
| `destructive` | `boolean` | `false` | Shorthand: the confirm button in the danger color. |
| `onConfirm` | `() => void` | — | Shorthand: the user confirmed. It closes after. |
| `onCancel` | `() => void` | — | Shorthand: the user pressed Cancel. |
| `children` | `ReactNode` | — | The parts, or the body when you use the shorthand. |
| `dismissible` | `boolean` | `true` | Escape and Android back cancel it. The backdrop never does. |
| `theme` | `DeepPartial<Theme>` | — | Theme override, for every part. |
| `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. |

### AlertDialogAction

Takes [`Button`](./button)'s props.

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `children` | `ReactNode` | — | The label. |
| `onPress` | `(event) => void` | — | Runs before the dialog closes. |
| `variant` | `ButtonVariant` | `'default'` | `'destructive'` draws it in the danger color. |

### AlertDialogCancel

Takes [`Button`](./button)'s props.

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `children` | `ReactNode` | — | The label. |
| `onPress` | `(event) => void` | — | Runs before the dialog closes. |
| `variant` | `ButtonVariant` | `'outline'` | The look, in the Patine and web designs. |

### Other parts

`AlertDialogTrigger`, `AlertDialogContent`, `AlertDialogHeader`, `AlertDialogTitle`, `AlertDialogDescription` and `AlertDialogFooter` take the same props as their [Dialog](./dialog#api) counterparts.

## Related

- [Dialog](./dialog): a window for forms and details.
- [ActionSheet](./action-sheet): a list of actions, with a destructive one.
