Skip to content

Alert

Puts a message in the page that stays until the situation changes, such as a hold, a failure or a draft state.

Installation

pnpm dlx shadcn@latest add @silkware/alert

To copy the component by hand, install its dependencies:

pnpm add class-variance-authority cn

Then copy the source into components/ui/alert.tsx:

components/ui/alert.tsx
import * as React from "react"import { cva, type VariantProps } from "class-variance-authority"import { cn } from "cn"
const alertVariants = cva(  "group/alert relative grid w-full gap-0.5 rounded-lg border px-4 py-3 text-left text-sm has-data-[slot=alert-action]:relative has-data-[slot=alert-action]:pr-18 has-[>svg]:grid-cols-[auto_1fr] has-[>svg]:gap-x-2.5 *:[svg]:row-span-2 *:[svg]:translate-y-0.5 *:[svg]:text-current *:[svg:not([class*='size-'])]:size-4",  {    variants: {      variant: {        default: "bg-card text-card-foreground",        destructive:          "bg-card text-destructive *:data-[slot=alert-description]:text-destructive/90 *:[svg]:text-current",        success:          "bg-card text-success *:data-[slot=alert-description]:text-success/90 *:[svg]:text-current",        warning:          "bg-card text-warning *:data-[slot=alert-description]:text-warning/90 *:[svg]:text-current",        info:          "bg-card text-info *:data-[slot=alert-description]:text-info/90 *:[svg]:text-current",      },    },    defaultVariants: {      variant: "default",    },  })
function Alert({  className,  variant,  ...props}: React.ComponentProps<"div"> & VariantProps<typeof alertVariants>) {  return (    <div      data-slot="alert"      role="alert"      className={cn(alertVariants({ variant }), className)}      {...props}    />  )}
function AlertTitle({ className, ...props }: React.ComponentProps<"div">) {  return (    <div      data-slot="alert-title"      className={cn(        "font-medium group-has-[>svg]/alert:col-start-2 [&_a]:underline [&_a]:underline-offset-3 [&_a]:hover:text-foreground",        className      )}      {...props}    />  )}
function AlertDescription({  className,  ...props}: React.ComponentProps<"div">) {  return (    <div      data-slot="alert-description"      className={cn(        "text-sm text-balance text-muted-foreground md:text-pretty [&_a]:underline [&_a]:underline-offset-3 [&_a]:hover:text-foreground [&_p:not(:last-child)]:mb-4",        className      )}      {...props}    />  )}
function AlertAction({ className, ...props }: React.ComponentProps<"div">) {  return (    <div      data-slot="alert-action"      className={cn("absolute top-2.5 right-3", className)}      {...props}    />  )}
export { Alert, AlertTitle, AlertDescription, AlertAction }

Usage

import {  Alert,  AlertAction,  AlertDescription,  AlertTitle,} from "@/components/ui/alert"
<Alert>  <InfoIcon />  <AlertTitle>The 2026 price list is a draft</AlertTitle>  <AlertDescription>    Giulia has not approved it yet. New orders keep the 2025 prices until she does.  </AlertDescription></Alert>

Alert always renders role="alert", which screen readers announce at once and interrupt what they are reading. Keep that for messages that need attention now. For a note that can wait, pass role="status", as in Status.

Examples

Default

A neutral message about the state of the page: a hold, a draft, a change in how something works.

Destructive

Something failed or cannot continue. The text and the icon turn crimson; the title and description still say what happened, so the message does not depend on the colour.

Success

A check or a long task finished well and the result stays true while people work on the page: an inspection passed, a connection is back. The text and the icon turn pine green. To confirm an action someone just took, use a toast instead.

Inspection QI-319 passed
Marco signed off lot 7745 at 11:20. It ships to Ortelio Macchine with today's Trasvera collection.

Warning

Something will go wrong unless someone acts: a supplier has not confirmed, a limit is close, an action cannot be undone. The text and the icon turn amber. Say what to do in the description.

Info

A notice worth knowing that changes nothing by itself, when it sits among dense content or other alerts and must read as a notice. The text and the icon turn cobalt blue. Most notices need no colour: use the default alert first.

Bridge syncs the Verona site overnight
Orders placed after 18:00 reach the Brevia ERP at 06:00 the next morning.

Action

Put one AlertAction in the top corner for the step that resolves the message. It takes a small button; keep its label to one short word, because the alert reserves a fixed space for it.

Status

An alert that is already on the page when it loads, or that does not need to interrupt, should be polite. Replace the default role with role="status".

Stock counts update every hour
The next refresh for the Verona warehouse is at 14:00.

Accessibility

An alert is not interactive, so it has no keyboard behaviour of its own. Anything you place inside it, such as the action button or a link, is reached with Tab, in reading order.

KeyAction
TabMoves focus to the action button or a link inside the alert, in reading order.

The component provides:

  • role="alert" on the root. Browsers expose it as an assertive live region: a screen reader announces the alert when it is inserted into the page, without moving focus.
  • A layout that places the icon beside the title and keeps the description aligned with the title.
  • Underlined links inside the title and the description, so a link does not rely on colour.

You provide:

  • Text that carries the meaning. The icon is decorative and the status variants add colour, so the title must say what happened.
  • An icon that matches the variant: CircleCheckIcon for success, TriangleAlertIcon for warning, InfoIcon for info, CircleAlertIcon for destructive. Colour is never the only signal.
  • The right politeness. An alert that appears on first load is often not announced; an alert that appears after an action is. Use role="status" for a message that should not interrupt.
  • A visible way forward when there is one: a button in AlertAction or a link in the description.
  • A labelled icon only if it adds meaning. Icons from lucide-react are hidden from assistive technology by default.

Guidelines

When to use

  • To explain a state of the page that lasts: a supplier on hold, a draft price list, a sync that failed.
  • To report a failure that the person must fix before they can continue.
  • To tell people something before they act, when a dialog would be too much.

When not to use

  • To confirm an action that just succeeded, such as "Saved". Use a Toast, which disappears by itself.
  • To ask for a decision before something irreversible. Use an Alert Dialog.
  • To show an error under one input. Use the invalid state of a Field.
  • To fill an empty list or a first-use screen. Use Empty.
  • To show that something is loading. Use a Spinner or a Skeleton.

Content

  • The title says what happened or what is true, in a few words and in sentence case, with no full stop and no exclamation mark: "The supplier sync failed".
  • The description says why it matters and what to do next, in one or two sentences. Name the object: "Add a price to rows 14 and 22, then save again."
  • Do not write "Error", "Warning" or "Oops" as a title. The icon and the variant already say it.
  • Show one alert per cause. If three things are wrong, list them in the description or fix them in the fields.

Status and colour

Most alerts are neutral. Reach for a colour only when the alert reports an outcome, and pick the one that matches it:

VariantUse it whenExample title
defaultThe page is in a state worth knowing: a hold, a draft, a change in how something works.The 2026 price list is a draft
successA check or a task finished well and the result stays on the page.Inspection QI-319 passed
warningSomething goes wrong unless someone acts, or an action cannot be undone.Ferrolva Acciai has not confirmed PO-2418
infoA neutral notice that must stand out from the content around it.Bridge syncs the Verona site overnight
destructiveSomething failed or cannot continue.The supplier sync failed

One coloured alert per screen is usually enough; three colours at once read as noise. Crimson stays for failures, and the state square, not an alert colour, says where an order or a task stands. Do not add a coloured class of your own; the variants hold 4.5:1 in both modes, a custom class may not.

Do

Say what failed and the step that fixes it.

Don’t

Don’t write "Error!" and "Something went wrong". The reader learns nothing and has no next step.

Do

Use an alert for a state that stays true while people work on the page.

Don’t

Don’t use an alert to confirm a save. The alert stays in the layout and the next save adds another. Use a toast.

API reference

Alert

Renders a <div> with role="alert". It accepts every prop of a div, including role, which replaces the default.

PropTypeDefaultDescription
variant?"default" | "destructive" | "success" | "warning" | "info""default"Neutral by default. destructive, success, warning and info set the text and the icon to that status colour on the card ground.
role?string"alert"The ARIA role. Use status for a message that should not interrupt.
className?stringNoneClasses merged after the variant classes.

AlertTitle

Renders a <div> with the title. When the alert has an icon, the title sits beside it.

PropTypeDefaultDescription
className?stringNoneClasses merged after the title classes.

AlertDescription

Renders a <div> with the supporting text in the muted colour. Paragraphs inside it are spaced apart.

PropTypeDefaultDescription
className?stringNoneClasses merged after the description classes.

AlertAction

Renders a <div> positioned in the top corner at the end of the alert. When it is present the alert reserves space on that side.

PropTypeDefaultDescription
className?stringNoneClasses merged after the position classes.