components / feedback
Alert
A terminal-style log line for information, success, warnings, or errors.
alert-default.tsx
import { Alert, AlertAction, AlertDescription, AlertTitle } from "@/components/ui/alert";
import { Button } from "@/components/ui/button";
export default function AlertDefault() {
return (
<div className="flex w-full max-w-xl flex-col gap-2">
<Alert variant="info" time="09:41:07">
<AlertTitle>deploy queued</AlertTitle>
<AlertDescription>build 482 waits behind 2 others</AlertDescription>
</Alert>
<Alert variant="success" time="09:41:52">
<AlertTitle>deploy finished</AlertTitle>
<AlertDescription>live on cd-ui.dev in 38s</AlertDescription>
</Alert>
<Alert variant="warn" time="09:42:15">
<AlertTitle>disk almost full</AlertTitle>
<AlertDescription>14 GB left on /dev/sda1</AlertDescription>
<AlertAction>
<Button size="sm" variant="outline">
Delete old builds
</Button>
</AlertAction>
</Alert>
<Alert variant="error" tag="fatal" time="09:43:01">
<AlertTitle>payment failed</AlertTitle>
<AlertDescription>card declined, retry in 3 attempts</AlertDescription>
</Alert>
</div>
);
}Installation
$ npx shadcn@latest add https://cd-ui.vercel.app/r/alert.jsonfirst time? set up components.json and the theme first: see installation.
Usage
import { Alert, AlertDescription, AlertTitle } from "@/components/ui/alert"
<Alert variant="warn">
<AlertTitle>disk almost full</AlertTitle>
<AlertDescription>14 GB left on /dev/sda1</AlertDescription>
</Alert>Examples
Close, actions and auto-hide
alert-dismissible.tsx
"use client";
import { useState } from "react";
import { Alert, AlertAction, AlertDescription, AlertTitle } from "@/components/ui/alert";
import { Button } from "@/components/ui/button";
export default function AlertDismissible() {
const [round, setRound] = useState(0);
return (
<div className="flex w-full max-w-xl flex-col items-start gap-2">
<Alert key={round} onDismiss={() => {}} variant="warn">
<AlertTitle>disk almost full</AlertTitle>
<AlertDescription>14 GB left</AlertDescription>
<AlertAction>
<Button size="sm" variant="outline">
Delete old builds
</Button>
</AlertAction>
</Alert>
<Alert autoHide={5000} key={`auto-${round}`} variant="success">
<AlertTitle>settings saved</AlertTitle>
<AlertDescription>this one closes by itself in 5s</AlertDescription>
</Alert>
<Button onClick={() => setRound((n) => n + 1)} size="sm" variant="outline">
Show again
</Button>
</div>
);
}Compact
alert-compact.tsx
import { Alert, AlertDescription, AlertTitle } from "@/components/ui/alert";
export default function AlertCompact() {
return (
<div className="flex w-full max-w-xl flex-col gap-1.5">
<Alert compact variant="info">
<AlertTitle>3 files changed</AlertTitle>
</Alert>
<Alert compact variant="success">
<AlertTitle>saved</AlertTitle>
<AlertDescription>just now</AlertDescription>
</Alert>
<Alert compact icon={false} variant="error">
<AlertTitle>email already in use</AlertTitle>
</Alert>
</div>
);
}API
Alert
One log line: [icon] [tag] time title - description, with a colored rule on the left. Each level has its own icon. Close it with a button (onDismiss) or let it close by itself (autoHide).
| prop | type | default |
|---|---|---|
variantLevel. | "info" | "success" | "warn" | "error" | "info" |
tagText of the level chip. Defaults to the level name. | ReactNode | variant name |
timeOptional timestamp shown after the tag. | ReactNode | - |
iconIcon before the line. Each level has one by default. Pass your own, or | ReactNode | false | icon of the level |
compactOne tight line, without tag and time. For short messages next to a form. | boolean | false |
onDismissShows a close button. Runs when the alert closes, by click or by | () => void | - |
autoHideCloses after this many milliseconds and shows a progress bar. Pauses while the pointer or focus is on it. | number | - |
dismissLabelAccessible name of the close button. | string | "Dismiss" |
classNameExtra classes, merged with | string | - |
AlertTitle · AlertDescription · AlertAction
AlertTitle and AlertDescription share the line, joined by a dash. AlertAction pins one or more buttons or links to the right end.
| prop | type | default |
|---|---|---|
classNameExtra classes, merged with | string | - |
Accessibility
warnanderroruserole="alert", announced right away.infoandsuccessuserole="status", announced politely. Passroleto override.