Alerts provide important messages to users with different severity levels. They support dismissible behavior, custom icons, and multiple size options.
Import
import { Alert } from '@/components/ui/Alert';
Usage
Basic Alert
<Alert>
This is a default info alert with important information.
</Alert>
Variants
Alert supports four different severity variants:
<Alert variant="info">
This is an informational alert with helpful details.
</Alert>
<Alert variant="success" title="Success!">
Your changes have been saved successfully.
</Alert>
<Alert variant="warning" title="Warning">
Please review your information before proceeding.
</Alert>
<Alert variant="error" title="Error">
Something went wrong. Please try again.
</Alert>
Sizes
Three sizes are available:
<Alert variant="info" size="sm">
Small alert message with compact spacing.
</Alert>
<Alert variant="success" size="md">
Medium alert message with default spacing.
</Alert>
<Alert variant="warning" size="lg">
Large alert message with generous spacing.
</Alert>
With Title
Add a title to provide context:
<Alert variant="info" title="Information">
This is an informational alert with helpful details.
</Alert>
Icon Variations
Control icon display:
import { CheckCircle } from 'lucide-react';
// Default icon (automatic based on variant)
<Alert variant="info" icon={true}>
Alert with default icon
</Alert>
// Custom icon
<Alert variant="success" icon={<CheckCircle />}>
Alert with custom icon
</Alert>
// No icon
<Alert variant="warning" icon={false}>
Alert without icon
</Alert>
Dismissible Alerts
Make alerts dismissible with a close button:
<Alert
variant="warning"
title="Dismissible Alert"
dismissible
onDismiss={() => console.log('Alert dismissed!')}
>
This alert can be dismissed by clicking the X button.
</Alert>
Example with state management:
const [alerts, setAlerts] = useState([
{ id: 1, variant: 'info', message: 'Welcome to the dashboard!' },
{ id: 2, variant: 'success', message: 'Profile updated successfully' },
]);
const removeAlert = (id) => {
setAlerts(alerts.filter((alert) => alert.id !== id));
};
return (
<div className="space-y-4">
{alerts.map((alert) => (
<Alert
key={alert.id}
variant={alert.variant}
dismissible
onDismiss={() => removeAlert(alert.id)}
>
{alert.message}
</Alert>
))}
</div>
);
Long Content
Alerts automatically expand to accommodate longer content:
<Alert
variant="info"
title="Detailed Information"
dismissible
>
This is a longer alert message that demonstrates how the component handles
multiple lines of text. The alert should expand to accommodate the content
while maintaining proper spacing and readability. You can include detailed
information, instructions, or context to help users understand important
system messages or updates that require their attention.
</Alert>
Examples
Status Messages
<div className="space-y-4">
<Alert variant="info" title="Information">
This is an informational alert with helpful details.
</Alert>
<Alert variant="success" title="Success" dismissible>
Operation completed successfully! You can dismiss this.
</Alert>
<Alert variant="warning" title="Warning">
Please be careful with this action.
</Alert>
<Alert variant="error" title="Error" dismissible>
An error occurred. Please check your input and try again.
</Alert>
</div>
<Alert variant="error" title="Validation Failed">
Please correct the following errors:
<ul className="mt-2 list-disc list-inside">
<li>Email address is required</li>
<li>Password must be at least 8 characters</li>
<li>Terms and conditions must be accepted</li>
</ul>
</Alert>
System Notifications
<div className="space-y-3">
<Alert variant="info" size="sm" dismissible>
New updates available. Click to refresh.
</Alert>
<Alert variant="success" size="sm" dismissible>
File uploaded successfully.
</Alert>
<Alert variant="warning" size="sm">
Your session will expire in 5 minutes.
</Alert>
</div>
Props
The severity level of the alert. Options: info, success, warning, error. Each variant has distinct colors and a default icon.
The size of the alert. Options: sm, md, lg. Affects padding and font size.
Optional title displayed above the alert content.
icon
ReactNode | boolean
default:"true"
Icon to display. When true, shows the default icon for the variant. Pass a custom React element for a custom icon, or false to hide the icon.
Whether the alert can be dismissed. When true, displays a close button.
Callback function called when the close button is clicked. Only works when dismissible is true.
ARIA role for the alert. Use "alert" for critical messages that need immediate attention, or "status" for less urgent updates.
Additional CSS classes to apply to the alert container.
The content to display inside the alert.
Default Icons
Each variant has a default icon from lucide-react:
- info:
Info icon
- success:
CheckCircle icon
- warning:
AlertTriangle icon
- error:
AlertCircle icon
Semantic Tokens
Alert uses the following semantic tokens from the Stride Design System:
--status-success, --status-warning, --status-danger (and their -bg and -text variants)
--border-focus
--radius-card
--transition-normal, --transition-fast
Accessibility
- Alert uses
role="alert" by default for screen readers (can be customized via the role prop)
- Use
role="alert" for critical messages requiring immediate attention
- Use
role="status" for informational updates that are less urgent
- Add
aria-live="polite" for non-critical success messages
- Close button has
aria-label="Dismiss alert" for screen readers
- Close button includes visible focus ring for keyboard navigation
- All color combinations meet WCAG AA contrast requirements
- Icons are automatically sized based on the alert size for readability
- Alert layout adapts for single-element (title-only or content-only) vs. multi-element configurations
Dark Mode
Alert automatically adapts to dark mode with adjusted background colors, borders, and text colors that maintain proper contrast ratios. All variants are tested for readability in both light and dark themes.