Skip to content
shadcn.io

shadcn.io is not affiliated with official shadcn/ui

Alert examples · Error

Error with Title Alert for React

A destructive alert with only a title, for a message that needs no explanation.

Framework
UI library

Sometimes an error needs no essay, just a heads-up. This React alert sets variant="destructive" on Alert and holds a single AlertTitle, so the message reads in the destructive color without a description or an icon. Use it for a quick failure notice where the context is already obvious, such as a save that didn't go through next to the form that sent it.

Preview

Preview width

Installation

Sign in to get your install command
Free with an account. It carries your token, so the CLI installs everything in one go.

FAQ

When should I use a title-only error alert?

When one line is the whole message. This demo is an Alert with variant="destructive" and a single AlertTitle. Add AlertDescription when the reader still needs the reason or the next step.

Where do the error colors come from?

From variant="destructive": the alert takes the theme's destructive color for its text and icon, so it follows light and dark mode and any theme you install. There are no color classes in the demo; change the --destructive token to restyle every destructive alert at once.

What does the title render as?

AlertTitle is a div with the text Error. It is font-medium and inherits the destructive text color. className="max-w-lg" on Alert caps the width.

Should error alerts be dismissible?

Usually not: an error should stay until its cause is fixed, then go away because your code stops rendering it. The Alert has no built-in close button. If you want one, keep its visibility in your own state and put a button in AlertAction that hides it.

How do I add an icon to this alert?

Put an svg first, before AlertTitle. The alert then adds an icon column, spans the icon across the rows, and sizes an unsized icon. This demo leaves the icon out, so that column is not there.

Are error alerts accessible to screen readers?

Yes. Alert renders with role="alert", so a screen reader announces the title as soon as the alert appears. The color is not the only signal, since the title names the problem in text.