Alert
A message block for information, success, warnings and errors. It renders as plain content by default; make it a live region only when it appears in response to something the user did.
Installation
Add Alert to your project with the CK-UI CLI. It copies the source into your project, so the code is yours to change.
- Adds
Alert.tsxto your CK-UI folder (src/components/ck-ui by default)- Installs
@radix-ui/react-slotreact-iconsif your project does not have them yet
First time with CK-UI in this project? Run npx ck-ui-cli@latest init once before adding components. The installation guide explains what it sets up.
Manual installationInstall the packages and copy the files yourself.
Install the following dependencies
Run these commands in your terminal to install the required packages
Create or update your Alert component
Copy this code to create your Alert component
Test Your Component
- 1. Add the component to a test page
- 2. Check all props and variants work as expected
- 3. Verify responsive behavior
- 4. Test with different content/data
- 5. Ensure accessibility features work correctly
Colors
Use color for intent: info, success, warning and destructive. The default is neutral.
With Icon
Alert.Icon shows the default icon for the color, or your own icon passed as children (or with as inside a client component). Title and description are both optional.
With Action
Alert.Actions holds buttons or links under the text.
Announcing an alert
When an alert appears because of something the user just did (a failed save), add role="alert" so screen readers announce it immediately. Use role="status" for news that can wait.
Accessibility
- No role by default: alerts that are part of the page when it loads are read in document order, not announced. Adding
role="alert"to every alert would interrupt users on every page load. - Add
role="alert"(assertive) orrole="status"(polite) to an alert that is inserted after a user action. Render the alert into the page at that moment; changing an existing alert's role later is not announced reliably. - The icon is decorative (aria-hidden). The title and description carry the meaning, so color is never the only signal.
- Text uses the foreground and muted-foreground tokens; icons use the
-strongintent tokens, which keep at least 4.5:1 contrast on the tinted backgrounds in light and dark mode. - Use
Alert.Title asChildwith a heading element when the alert starts a new section of the page.
API Reference
Import the parts with import * as Alert from "@/components/ui/Alert".
Alert.Root
The container. Other HTML attributes, including role, are passed to the div.
| Prop | Type | Default | Description |
|---|---|---|---|
color | "neutral""info""success""warning""destructive" | "neutral" | Intent: sets the surface, border and icon color. |
role | "alert""status" | "undefined" | Only for alerts inserted after a user action. Omit for static content. |
className | "string" | "undefined" | Additional classes, merged last. |
Alert.Icon
Decorative icon in the first column.
| Prop | Type | Default | Description |
|---|---|---|---|
as | "React.ElementType" | "color icon" | Icon component to render, e.g. RiRocketLine. Client components only: from a Server Component, pass the icon as children. |
children | "ReactNode" | "undefined" | Custom icon element. Takes precedence over as. |
className | "string" | "undefined" | Additional classes, merged last. |
Alert.Title
| Prop | Type | Default | Description |
|---|---|---|---|
asChild | "boolean" | "false" | Render a heading (or other element) instead of a div. |
className | "string" | "undefined" | Additional classes, merged last. |
Alert.Description
| Prop | Type | Default | Description |
|---|---|---|---|
asChild | "boolean" | "false" | Render the single child element instead, merging this part's props and behaviour onto it. |
className | "string" | "undefined" | Additional classes, merged last. |
Alert.Actions
A wrapping row for buttons and links.
| Prop | Type | Default | Description |
|---|---|---|---|
className | "string" | "undefined" | Additional classes, merged last. |