Alert
A flexible and accessible alert component for displaying contextual messages, notifications, or warnings. Supports multiple variants (info, success, warning, error), optional closable behavior, and clear accessibility cues.
tsx
import { Alert } from '@nofinite/nui';Interactive Preview
Loading preview...
Usage
Basic Alert
tsx
import { Alert } from '@nofinite/nui';
<Alert>This is an informational message.</Alert>;Alert with a title
tsx
<Alert title="Network Error">
Unable to connect to the server. Please try again.
</Alert>Variants
tsx
<Alert variant="success" title="Success">
Your data has been saved successfully.
</Alert>
<Alert variant="warning" title="Warning">
Your subscription will expire in 3 days.
</Alert>
<Alert variant="error" title="Error">
Failed to process your request.
</Alert>Closable Alert
tsx
<Alert
variant="info"
title="Reminder"
closable
onClose={() => console.log('Alert closed')}
>
Don’t forget to check your inbox.
</Alert>Variants
| Variant | Background | Border | Text Color | Description |
|---|---|---|---|---|
info | var(--brand-50) | var(--brand-100) | var(--color-primary-hover) | Informational message |
success | #f0fdf4 | #dcfce7 | var(--color-success) | Positive feedback / success message |
warning | #fffbeb | #fef3c7 | #b45309 | Warning or caution |
error | #fef2f2 | #fee2e2 | var(--color-danger) | Error / critical alert |
Colors can be overridden with design tokens for theming.
Accessibility
-
Uses semantic roles:
role="alert"forerrorandwarning(automatically announced by screen readers)role="status"forinfoandsuccess
-
Headers are optional but recommended.
-
Close button is keyboard accessible (
Enter/SpacetriggersonClose). -
Focus styles visible via
:focus-visibleon the close button.
Behavior
- Alerts are controlled/uncontrolled: content always renders; visibility is handled via parent component logic.
closabletriggersonClosecallback but does not remove alert automatically — parent should handle removal.- Alerts are visually distinct per variant for immediate recognition.
- Close button shows hover and focus states for better UX.
Best Practices
Do
- Provide a title for clarity, especially for
errororwarningalerts. - Keep messages concise.
- Use
closablefor temporary notifications.
Don’t
- Overload with too much text — keep alerts scannable.
- Use only color to convey status; combine with icons or text.
- Nest alerts inside other alerts — avoid confusion.
API Reference
| Prop | Type | Default | Description |
|---|---|---|---|
variant | enum | info | — |
title | ReactNode | — | — |
closable | boolean | false | Determines if the close icon button is rendered |
onClose | (() => void) | — | Callback fired when the close button is clicked. Note: The component is controlled; it does not unmount itself. |
className | string | — | — |
asChild | boolean | — | Renders the component using its child element |