Alert

An inline message about this page that stays put, with one optional action.

Source
$npx @dinachi/cli@latest add alert
variant
AlertTitle
AlertAction
AlertDismiss
phone

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

Rates sync every 15 minutes. Last synced 10:42.
New cancellation policy from 1 December
Bookings made after that date get 48 hours of free cancellation.
Rates published to 4 channels
Booking.com, Expedia, Airbnb and direct now show £148 for 12 to 18 Dec.
One per variant. The first has no title, so its description is the message.
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>
  );
}

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

variant
agent
"default" | "info" | "success" | "warning" | "destructive" = "default"
The tone. warning and destructive get role="alert", the rest role="status". The agent reads it as type.
openboolean
Shown or dismissed, when the parent holds it.
defaultOpenboolean = true
Whether it starts shown.
onOpenChange
agent
(open: boolean, details) => void
Runs when AlertDismiss or the agent dismisses it. details.cancel() keeps it shown.
rolestring
Overrides the role the variant sets.

Agent

Inside an AgentProvider, it appears in agent.snapshot() as alert.

Reads

label

The title, or the description when there is no title.

value

The description, when there is a title.

type

The 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

  • warning and destructive are role="alert" and interrupt; the rest are role="status" and wait their turn.
  • Only warning and destructive (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.