Alert
Displays a callout for user attention.
Heads up!
You can add components to your app using the CLI.
tsx
import { Info } from "lucide-react"import { Alert, AlertDescription, AlertTitle } from "@/components/ui/alert"
export function AlertDemo() { return ( <Alert> <Info className="h-4 w-4" /> <AlertTitle>Heads up!</AlertTitle> <AlertDescription> You can add components to your app using the CLI. </AlertDescription> </Alert> )}
Installation
CLI
bash
npx fivui add alert
Manual
Copy and paste the following code into your project.
components/ui/alert.tsx
import * as React from "react"import { cva, type VariantProps } from "class-variance-authority"
import { cn } from "@/lib/utils"
const alertVariants = cva( "relative w-full rounded-lg border px-4 py-3 text-sm [&>svg+div]:translate-y-[-3px] [&>svg]:absolute [&>svg]:left-4 [&>svg]:top-4 [&>svg]:text-foreground [&>svg~*]:pl-7", { variants: { variant: { default: "bg-background text-foreground", destructive: "border-destructive/50 text-destructive dark:border-destructive [&>svg]:text-destructive", }, }, defaultVariants: { variant: "default", }, })
const Alert = React.forwardRef< HTMLDivElement, React.HTMLAttributes<HTMLDivElement> & VariantProps<typeof alertVariants>>(({ className, variant, ...props }, ref) => ( <div ref={ref} role="alert" className={cn(alertVariants({ variant }), className)} {...props} />))Alert.displayName = "Alert"
const AlertTitle = React.forwardRef< HTMLParagraphElement, React.HTMLAttributes<HTMLHeadingElement>>(({ className, ...props }, ref) => ( <h5 ref={ref} className={cn("mb-1 font-medium leading-none tracking-tight", className)} {...props} />))AlertTitle.displayName = "AlertTitle"
const AlertDescription = React.forwardRef< HTMLParagraphElement, React.HTMLAttributes<HTMLParagraphElement>>(({ className, ...props }, ref) => ( <div ref={ref} className={cn("text-sm [&_p]:leading-relaxed", className)} {...props} />))AlertDescription.displayName = "AlertDescription"
export { Alert, AlertTitle, AlertDescription }
Usage
tsx
import { Alert, AlertDescription, AlertTitle } from "@/components/ui/alert"
tsx
<Alert> <AlertTitle>Error</AlertTitle> <AlertDescription>Your session has expired. Please log in again.</AlertDescription></Alert>
Examples
Default
Heads up!
You can add components to your app using the CLI.
tsx
import { Info } from "lucide-react"import { Alert, AlertDescription, AlertTitle } from "@/components/ui/alert"
export function AlertDemo() { return ( <Alert> <Info className="h-4 w-4" /> <AlertTitle>Heads up!</AlertTitle> <AlertDescription> You can add components to your app using the CLI. </AlertDescription> </Alert> )}
Destructive
Error
Your session has expired. Please log in again.
tsx
import { AlertTriangle } from "lucide-react"import { Alert, AlertDescription, AlertTitle } from "@/components/ui/alert"
export function AlertDestructive() { return ( <Alert variant="destructive"> <AlertTriangle className="h-4 w-4" /> <AlertTitle>Error</AlertTitle> <AlertDescription> Your session has expired. Please log in again. </AlertDescription> </Alert> )}
Success
Success!
Your changes have been saved successfully.
tsx
import { CheckCircle } from "lucide-react"import { Alert, AlertDescription, AlertTitle } from "@/components/ui/alert"
export function AlertSuccess() { return ( <Alert className="border-green-200 dark:border-green-800"> <CheckCircle className="h-4 w-4 text-green-600 dark:text-green-400" /> <AlertTitle className="text-green-800 dark:text-green-200">Success!</AlertTitle> <AlertDescription className="text-green-700 dark:text-green-300"> Your changes have been saved successfully. </AlertDescription> </Alert> )}
API Reference
Alert
Prop | Type | Default | Description |
---|---|---|---|
variant | "default" | "destructive" | "default" | The visual style variant of the alert |
Alert also accepts all standard HTML div attributes and React props.