Alert
An Alert shows users a short message in the page about a status, such as a payment that failed.
When to Use
- for a status that lasts as long as its cause, for example “INV-2026-012 is overdue”
- when one click can fix the cause, with an action like “Retry” next to “The payment failed”
When Not to Use
- for the result of an action, like “Invoice sent” (use a toast from Sonner)
- for a decision that users have to make before anything else (use an Alert Dialog)
- for an error in one field of a form (use the
FieldErrorof a Field) - for the status of one record in a list (use a Badge)
| Alert | Toast | Alert Dialog | |
|---|---|---|---|
| Purpose | A status that lasts | The result of an action | A decision before an action |
| Position | In the page, where it is written | A corner of the screen, the bottom right by default | The center of the screen |
| Duration | Until the page removes it | 4 seconds, or until it is closed | Until it is answered |
| Page Behind | Interactive | Interactive | Blocked |
| Focus | Stays where it is | Stays where it is | Moves into the popup |
| Role | alert | A list item in a live region set to polite | alertdialog |
Anatomy
| Element | Usage |
|---|---|
| Icon | Optional |
| Title* | Optional |
| Action | Optional |
| Container | Required |
| Description* | Optional |
* An Alert has a title, a description, or both.
The icon is any svg that is a direct child of Alert.
Variants
| Variant | Purpose |
|---|---|
default | A notice that is neither good nor bad, such as a trial that ends soon |
success | A good outcome, such as a payment received |
warning | A state that needs attention, such as an overdue invoice |
destructive | An error or a loss, such as a failed payment |
The variant changes only the color of the icon. Alert doesn’t render an icon, so you pass one. The preview uses InfoIcon, CheckCircleIcon, WarningIcon, and XCircleIcon, which are the icons that a toast shows for the same statuses.
Add an icon and a title that states the status in words to every Alert, as the variants look the same when there is no icon.
default one, and users who scan the page will miss the failure.Behavior
An Alert is as wide as its container. It has no close button and isn’t dismissed automatically, so render it for as long as its condition is true.
Content
| String | Rule | Example | Counterexample |
|---|---|---|---|
| Title | Say what happened or what is due, with no period at the end | “The payment failed” | “Error” |
| Description | Give the cause and the next step | “The card was declined. Ask the client for another payment method.” | “Something went wrong.” |
| Action | One verb | “Retry” | “Click here” |
Accessibility
Alert renders a div with role="alert", which is a live region that is assertive and atomic. AlertTitle is a div, not a heading. An Alert can’t receive focus and has no keys; users reach the Button in its action with Tab.
Most screen readers announce an Alert immediately when it is added after the page has loaded. If an Alert is already in the page when it loads, it isn’t announced, according to the ARIA Authoring Practices Guide. If the message can wait its turn, pass role="status", which is polite.
Set aria-hidden on the icon. See Iconography for the rule.
The title must state the status in words so that users who can’t tell colors apart can read it, as WCAG 2.2 SC 1.4.1 Use of Color requires. The variant changes only the color of the icon.
Installation
npx shadcn@latest add @summit/alertUsage
import { Alert, AlertDescription, AlertTitle } from '@/components/ui/alert';
import { InfoIcon } from '@phosphor-icons/react';
<Alert>
<InfoIcon aria-hidden />
<AlertTitle>Your trial ends in 5 days</AlertTitle>
<AlertDescription>Add a payment method to keep sending invoices.</AlertDescription>
</Alert>;Show a Status
To show a status, set variant and pass the icon for that status.
import { WarningIcon } from '@phosphor-icons/react';
<Alert variant="warning">
<WarningIcon aria-hidden />
<AlertTitle>INV-2026-012 is overdue</AlertTitle>
<AlertDescription>It was due 12 days ago.</AlertDescription>
</Alert>;Add an Action
To add an action, write AlertAction last and place an xs Button in it.
import { Alert, AlertAction, AlertDescription, AlertTitle } from '@/components/ui/alert';
import { Button } from '@/components/ui/button';
import { XCircleIcon } from '@phosphor-icons/react';
<Alert variant="destructive">
<XCircleIcon aria-hidden />
<AlertTitle>The payment failed</AlertTitle>
<AlertDescription>The card was declined. Ask the client for another payment method.</AlertDescription>
<AlertAction>
<Button variant="outline" size="xs">
Retry
</Button>
</AlertAction>
</Alert>;Change the Role
By default, the role is alert. If the message can wait its turn, pass another role.
import { CheckCircleIcon } from '@phosphor-icons/react';
<Alert role="status" variant="success">
<CheckCircleIcon aria-hidden />
<AlertTitle>Payment received</AlertTitle>
</Alert>;API Reference
The parts accept the props of a div.
Alert
| Prop | Type | Default | Description |
|---|---|---|---|
variant | 'default' | 'destructive' | 'success' | 'warning' | 'default' | The color of the icon |