variantAlertTitleAlertActionAlertDismissphoneimport { useState } from "react"
import { Alert, AlertAction, AlertContent, AlertDescription, AlertDismiss, AlertIcon, AlertTitle } from "@/components/ui/alert"
export function RatesAlert() {
const [open, setOpen] = useState(true)
return (
<Alert
variant="warning"
open={open}
onOpenChange={setOpen}
>
<AlertIcon />
<AlertContent>
<AlertTitle>Sea View Double is under your floor</AlertTitle>
<AlertDescription>£162 on Wed 14 is below the £170 floor. It publishes to all channels at 18:00.</AlertDescription>
</AlertContent>
<AlertAction>Raise to £170</AlertAction>
<AlertDismiss />
</Alert>
)
}Usage
import { Alert, AlertContent, AlertDescription, AlertIcon, AlertTitle } from "@/components/ui/alert"
export function FloorWarning() {
return (
<Alert variant="warning">
<AlertIcon />
<AlertContent>
<AlertTitle>Sea View Double is under your floor</AlertTitle>
<AlertDescription>£162 on Wed 14 is below the £170 floor.</AlertDescription>
</AlertContent>
</Alert>
)
}Examples
import { RefreshCw } from 'lucide-react';
import { Alert, AlertContent, AlertDescription, AlertIcon, AlertTitle } from '@/components/ui/alert';
export function Example() {
return (
<div className="grid gap-3">
<Alert>
<AlertIcon><RefreshCw /></AlertIcon>
<AlertContent>
<AlertDescription>Rates sync every 15 minutes. Last synced 10:42.</AlertDescription>
</AlertContent>
</Alert>
<Alert variant="info">
<AlertIcon />
<AlertContent>
<AlertTitle>New cancellation policy from 1 December</AlertTitle>
<AlertDescription>Bookings made after that date get 48 hours of free cancellation.</AlertDescription>
</AlertContent>
</Alert>
<Alert variant="success">
<AlertIcon />
<AlertContent>
<AlertTitle>Rates published to 4 channels</AlertTitle>
<AlertDescription>Booking.com, Expedia, Airbnb and direct now show £148 for 12 to 18 Dec.</AlertDescription>
</AlertContent>
</Alert>
<Alert variant="warning">
<AlertIcon />
<AlertContent>
<AlertTitle>Sea View Double is under your floor</AlertTitle>
<AlertDescription>£162 on Wed 14 is below the £170 floor. It publishes to all channels at 18:00.</AlertDescription>
</AlertContent>
</Alert>
<Alert variant="destructive">
<AlertIcon />
<AlertContent>
<AlertTitle>Payout failed</AlertTitle>
<AlertDescription>Barclays declined the £12,480.00 transfer. Update the account to retry.</AlertDescription>
</AlertContent>
</Alert>
</div>
);
}import { useState } from 'react';
import {
Alert,
AlertAction,
AlertContent,
AlertDescription,
AlertDismiss,
AlertIcon,
AlertTitle,
} from '@/components/ui/alert';
export function Example() {
const [open, setOpen] = useState(true);
const [rate, setRate] = useState(162);
return (
<Alert variant="warning" open={open && rate < 170} onOpenChange={setOpen}>
<AlertIcon />
<AlertContent>
<AlertTitle>Sea View Double is under your floor</AlertTitle>
<AlertDescription>£162 on Wed 14 is below the £170 floor. It publishes to all channels at 18:00.</AlertDescription>
</AlertContent>
<AlertAction onClick={() => setRate(170)}>Raise to £170</AlertAction>
<AlertDismiss />
</Alert>
);
}- Sea View Double£162 (under floor)
- Harbour Twin£178
- Garden Single£172
import { useState } from 'react';
import { Alert, AlertAction, AlertContent, AlertDescription, AlertDismiss, AlertIcon, AlertTitle } from '@/components/ui/alert';
export function Example() {
const [open, setOpen] = useState(true);
const [rate, setRate] = useState(162);
return (
<Alert
variant="warning"
open={open && rate < 170}
onOpenChange={setOpen}
className="rounded-none border-x-0 px-[18px] pb-4 pt-3.5"
>
<AlertIcon />
<AlertContent>
<AlertTitle className="text-[15px]">Sea View Double is under your floor</AlertTitle>
<AlertDescription className="text-sm">£162 is below the £170 floor. It publishes at 18:00.</AlertDescription>
<AlertAction size="default" onClick={() => setRate(170)}>
Raise to £170
</AlertAction>
</AlertContent>
<AlertDismiss className="h-9 w-9" />
</Alert>
);
}Notes
Alert or toast
An alert is about the page in front of the person and stays until the problem is gone or they dismiss it. A toast reports something that just happened and leaves on its own. If the message still matters after five seconds, it is an alert.
Hiding it when the problem is fixed
The action does not hide the alert, because the fix may fail. Derive open from the state the alert is about, so it leaves once the fix lands:
<Alert variant="warning" open={rate < floor}>
...
<AlertAction onClick={() => setRate(floor)}>Raise to £{floor}</AlertAction>
</Alert>On a phone
Let the alert run edge to edge above the rows it is about, and move the action under the text at full height. Nothing in the component fixes the layout, so the page decides:
<Alert variant="warning" className="rounded-none border-x-0">
<AlertIcon />
<AlertContent>
<AlertTitle>...</AlertTitle>
<AlertDescription>...</AlertDescription>
<AlertAction size="default">Raise to £170</AlertAction>
</AlertContent>
<AlertDismiss className="h-9 w-9" />
</Alert>Inside AlertContent the action gets its own top margin.
Motion
It fades in and drops 4px into place over 200ms when it appears, and fades out over 150ms when dismissed, then unmounts. Re-renders never replay it. An alert in the server-rendered page skips the entrance, since it is part of the page as it loads rather than a change to it. With reduced motion it only fades. Both durations follow --motion-duration-base and --motion-duration-fast.
API
warning and destructive get role="alert", the rest role="status". The agent reads it as type.AlertDismiss or the agent dismisses it. details.cancel() keeps it shown.Takes the props of the element it renders.
sm sits beside the text. default is 40px tall, for a phone.dismiss() from the agent.Agent
Inside an AgentProvider, it appears in agent.snapshot() as alert.
Reads
labelThe title, or the description when there is no title.
valueThe description, when there is a title.
typeThe variant, such as warning.
Actions
dismiss()Dismisses it through onOpenChange. Only offered when the alert has an AlertDismiss.
It reports as an alert, so an agent can tell a message on the page from a toast. onOpenChange gets details that isAgentChange recognises; details.cancel() keeps it shown and fails the step. A disabled AlertDismiss withholds dismiss(). The action is listed separately as a button in the alert and takes press.
await agent.act({
steps: [{ ref: "Sea View Double is under your floor", action: "dismiss" }],
})Accessibility
warninganddestructivearerole="alert"and interrupt; the rest arerole="status"and wait their turn.- Only
warninganddestructive(role="alert") are reliably announced when they appear. For a message that must be announced, use Toast. - The icon is decorative. Say what is wrong in the text; the colour alone does not.
- Dismiss is a real button named Dismiss, a 44px target on phones. The alert never takes focus.
- If focus is inside the alert when it closes, it moves to the next control after it, or the one before.