Alert
Displays a callout for user attention with contextual feedback messages and optional icons.
Installation
npx shadcn@latest add alert
Usage
import { Terminal } from "lucide-react"
import { Alert, AlertDescription, AlertTitle } from "@/components/ui/alert"
export function AlertDemo() {
return (
<Alert>
<Terminal />
<AlertTitle>Heads up!</AlertTitle>
<AlertDescription>
You can add components and dependencies to your app using the cli.
</AlertDescription>
</Alert>
)
}
API Reference
Alert
The root alert container that provides visual feedback to users.
Prop | Type | Default | Description |
---|---|---|---|
variant | "default" | "destructive" | "default" | The visual style variant of the alert. |
className | string | - | Additional CSS classes to apply to the alert. |
children | React.ReactNode | - | The content to display inside the alert. |
AlertTitle
The title component for the alert message.
Prop | Type | Default | Description |
---|---|---|---|
className | string | - | Additional CSS classes to apply to the title. |
children | React.ReactNode | - | The title text content. |
AlertDescription
The description component that provides additional context.
Prop | Type | Default | Description |
---|---|---|---|
className | string | - | Additional CSS classes to apply to the description. |
children | React.ReactNode | - | The description content, can include formatted text and lists. |
Examples
Informational
Success States
Warning Messages
With Actions
Implementation Details
The Alert component uses a sophisticated grid-based layout system:
Grid Layout System
The component employs CSS Grid with dynamic column configuration:
- Without icon: Single column layout (
grid-cols-[0_1fr]
) - With icon: Two-column layout (
grid-cols-[calc(var(--spacing)*4)_1fr]
) - Uses the
:has()
CSS selector to detect icon presence - Icons automatically sized to 16x16px (
size-4
)
Styling Architecture
Built with class-variance-authority
for variant management:
- Base styles: Rounded corners, border, padding, responsive text
- Default variant: Uses card background and foreground colors
- Destructive variant: Destructive text color with modified description opacity
Component Composition
<Alert>
<Icon /> {/* Optional: Positioned in first grid column */}
<AlertTitle /> {/* Positioned in second column, single line */}
<AlertDescription /> {/* Positioned in second column, below title */}
</Alert>
Accessibility
The Alert component follows WAI-ARIA guidelines for status messages:
ARIA Attributes
role="alert"
: Announces important messages to screen readers- Semantic HTML structure for proper content hierarchy
- Focus management considerations for interactive content
Screen Reader Support
- Alert content is automatically announced when rendered
- Title and description maintain proper heading hierarchy
- Icons are decorative and don't interfere with screen reader flow
Keyboard Navigation
While alerts are typically non-interactive, any interactive content within follows standard keyboard patterns:
- Links and buttons within alerts remain keyboard accessible
- Tab order flows naturally through interactive elements
Best Practices
Content Guidelines
- Keep titles concise: Single line, clear and actionable
- Provide context: Use descriptions to explain what happened and what to do
- Choose appropriate variants: Use destructive only for errors/warnings
- Include icons: Visual indicators improve scannability
Usage Patterns
Success Messages
<Alert>
<CheckCircle2Icon />
<AlertTitle>Success!</AlertTitle>
<AlertDescription>
Your changes have been saved successfully.
</AlertDescription>
</Alert>
Error Handling
<Alert variant="destructive">
<AlertCircleIcon />
<AlertTitle>Payment Failed</AlertTitle>
<AlertDescription>
<p>We couldn't process your payment.</p>
<ul className="list-disc list-inside text-sm mt-2">
<li>Check your card details</li>
<li>Ensure sufficient funds</li>
<li>Try a different payment method</li>
</ul>
</AlertDescription>
</Alert>
Information Notices
<Alert>
<InfoIcon />
<AlertTitle>New Feature Available</AlertTitle>
<AlertDescription>
We've added dark mode support. Enable it in your settings.
</AlertDescription>
</Alert>
Customization
Custom Variants
Extend the alert with custom variants:
// Custom warning variant
<Alert className="border-yellow-200 bg-yellow-50 text-yellow-900">
<AlertTriangleIcon className="text-yellow-600" />
<AlertTitle>Warning</AlertTitle>
<AlertDescription className="text-yellow-800">
This action cannot be undone.
</AlertDescription>
</Alert>
Icon Positioning
The grid system automatically handles icon alignment:
- Icons are vertically aligned with the title
- Consistent spacing maintained across all variants
- Responsive sizing adapts to content
SEO Considerations
For better SEO and accessibility:
- Semantic Structure: Alert content uses proper HTML elements
- Meaningful Content: Clear, descriptive messages improve user experience
- Status Indicators: Visual and semantic indicators for message types
- Progressive Enhancement: Alerts remain readable without CSS/JavaScript