Alert
A component for displaying alerts.
Follow the installation and quick start below to use Alert in your project, or read the complete setup guide.
On this page
Installation
1. Initialize once
Run from an existing React project with Node.js 18 or later. Skip this step if the project already has u-lib.json. Initialization copies the shared stylesheet and adds its import to your application entry.
npx @scriptwithahmad/u-lib@0.2.2 init2. Add Alert and example components
This command includes the UI components used by this page's usage examples. The CLI copies editable source into your components directory and installs each component's dependencies. Existing source files are preserved.
npx @scriptwithahmad/u-lib@0.2.2 add alert button3. Install example dependencies
These packages are imported directly by the examples. Some may already have been installed by the CLI.
npm install lucide-reactChoose the tab for your package manager. Yarn commands require modern Yarn; Yarn Classic users can use npx. Use --dry-run to preview changes. See the installation guide for custom paths, JavaScript output and package-import alternatives.
Quick start
Copy this example into src/Example.tsx (or Example.tsx at the project root when you do not use src). Imports assume the default CLI paths. From a nested file such as src/app/page.tsx, adjust ./components to ../components. Keep the client directive in Next.js when using state or event handlers.
"use client";
import { useState } from "react";
import { Alert, AlertDescription, AlertTitle } from "./components/ui/alert";
import { Button } from "./components/ui/button";
import { AlertCircle, AlertTriangle, CheckCircle2, Info, Loader2, X } from "lucide-react";
export default function AlertUi() {
const [dismissed, setDismissed] = useState(false);
const [refreshed, setRefreshed] = useState(false);
const [connected, setConnected] = useState(false);
return (
<div className="flex w-full max-w-xl flex-col gap-3 py-4">
<Alert>
<Info aria-hidden="true" />
<AlertTitle>New features available</AlertTitle>
<AlertDescription>Explore the latest updates, including improved accessibility and dark mode support.</AlertDescription>
</Alert>
<Alert variant="info">
<Info aria-hidden="true" />
<div className="flex flex-col items-start gap-3 sm:flex-row sm:justify-between sm:gap-4">
<div className="min-w-0">
<AlertTitle>{refreshed ? "You're up to date" : "Update available"}</AlertTitle>
<AlertDescription>{refreshed ? "The latest updates are ready. You're all set." : "A new version is ready. Refresh to get the latest features and fixes."}</AlertDescription>
</div>
<Button variant="outline" size="sm" className="shrink-0 rounded-full" onClick={() => setRefreshed(value => !value)}>{refreshed ? "Reset demo" : "Refresh"}</Button>
</div>
</Alert>
<Alert variant={connected ? "success" : "error"}>
{connected ? <CheckCircle2 aria-hidden="true" /> : <AlertCircle aria-hidden="true" />}
<div className="flex flex-col items-start gap-3 sm:flex-row sm:justify-between sm:gap-4">
<div className="min-w-0">
<AlertTitle>{connected ? "Connection restored" : "Unable to connect to server"}</AlertTitle>
<AlertDescription>
{connected ? "You're back online. Your work is ready to sync." : <>
We're experiencing connection issues. Try the following:
<ul className="mt-2 list-disc space-y-1 pl-4">
<li>Check your internet connection</li>
<li>Refresh the page</li>
<li>Try again in a moment</li>
</ul>
</>}
</AlertDescription>
</div>
<Button variant="outline" size="sm" className="shrink-0 rounded-full" onClick={() => setConnected(value => !value)}>{connected ? "Reset demo" : "Retry"}</Button>
</div>
</Alert>
{!dismissed ? <Alert variant="success">
<CheckCircle2 aria-hidden="true" />
<div className="flex items-center justify-between gap-3">
<AlertTitle className="mb-0">Profile updated successfully</AlertTitle>
<Button variant="ghost" size="icon" className="-my-1 size-7 shrink-0 rounded-full" aria-label="Dismiss success notification" onClick={() => setDismissed(true)}><X className="size-4" aria-hidden="true" /></Button>
</div>
</Alert> : <Button variant="ghost" size="sm" className="self-start" onClick={() => setDismissed(false)}>Show success alert again</Button>}
<Alert variant="loading">
<Loader2 className="animate-spin motion-reduce:animate-none" aria-hidden="true" />
<AlertTitle>Processing your request</AlertTitle>
<AlertDescription>Please wait while we sync your data. This may take a few moments.</AlertDescription>
</Alert>
<Alert variant="warning">
<AlertTriangle aria-hidden="true" />
<AlertTitle>Scheduled maintenance</AlertTitle>
<AlertDescription>Our services will be briefly unavailable during scheduled maintenance. Save your work before the maintenance window begins.</AlertDescription>
</Alert>
</div>
);
}Keep the shared stylesheet imported once. Replace sample image URLs and data with your own. Examples may use layout utilities beyond the precompiled component styles; compile added Tailwind classes in your app or replace them with your own CSS. Customize local source or className, and preserve accessible names and keyboard behavior.
Usage examples
Try each example, then switch to Code to copy its complete implementation. These catalog snippets use the website's @/components/ui alias; replace it with your local generated path as shown in Quick start.
Basic Usage
A complete alert example. Copy this into a client component.
Loading example…
Props & configuration
Everything you need to fine-tune this component.
| Prop | Type | Default | Description |
|---|---|---|---|
variant | "default" | "info" | "success" | "warning" | "error" | "loading" | "destructive" | default | Semantic alert style. Destructive remains available as an alias for error. Error alerts use an assertive announcement; other variants use a polite status announcement. Loading also sets aria-busy. |
className | string | — | Additional classes on the alert. |
