# Alert dialog: Controlled

> Pass `open` and `onOpenChange` to open it from code, without a trigger. Focus still lands on Cancel.

Example: https://hextaui.com/docs/alert-dialog/examples/controlled
Component docs: https://hextaui.com/docs/alert-dialog.md

## Add this example

```bash
npx shadcn@latest add https://hextaui.com/r/alert-dialog-controlled.json
```

This adds `components/examples/alert-dialog/controlled.tsx` with the Alert dialog component and the HextaUI theme tokens.

## Source

```tsx title="components/examples/alert-dialog/controlled.tsx"
"use client"

import * as React from "react"

import {
  AlertDialog,
  AlertDialogAction,
  AlertDialogCancel,
  AlertDialogContent,
  AlertDialogDescription,
  AlertDialogFooter,
  AlertDialogHeader,
  AlertDialogTitle,
} from "@/components/ui/alert-dialog"
import { Button } from "@/components/ui/button"

export function AlertDialogControlled() {
  const [open, setOpen] = React.useState(false)

  return (
    <>
      <Button
        variant="outline"
        onClick={() => window.setTimeout(() => setOpen(true), 600)}
      >
        Open in 600ms
      </Button>
      <AlertDialog open={open} onOpenChange={setOpen}>
        <AlertDialogContent>
          <AlertDialogHeader>
            <AlertDialogTitle>Session expiring</AlertDialogTitle>
            <AlertDialogDescription>
              You’ve been inactive for a while. Stay signed in?
            </AlertDialogDescription>
          </AlertDialogHeader>
          <AlertDialogFooter>
            <AlertDialogCancel>Sign out</AlertDialogCancel>
            <AlertDialogAction>Stay signed in</AlertDialogAction>
          </AlertDialogFooter>
        </AlertDialogContent>
      </AlertDialog>
    </>
  )
}
```

## Props in this example

### AlertDialog

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `open` | `boolean` | – |  |
| `onOpenChange` | `(open: boolean, details) => void` | – | Not called for closes blocked by a pending action. |

## More Alert dialog examples

- [Async action](https://hextaui.com/docs/alert-dialog/examples/async-action.md)
- [Media](https://hextaui.com/docs/alert-dialog/examples/media.md)
- [Small](https://hextaui.com/docs/alert-dialog/examples/small.md)
- [Detached triggers](https://hextaui.com/docs/alert-dialog/examples/detached-triggers.md)
- [Nested](https://hextaui.com/docs/alert-dialog/examples/nested.md)
- [Long content](https://hextaui.com/docs/alert-dialog/examples/long-content.md)
- [Right to left](https://hextaui.com/docs/alert-dialog/examples/rtl.md)
