Alert
Displays a callout for user attention.
The Alert component displays a callout for user attention with support for icons, titles, and descriptions. It supports multiple variants including success, destructive, and warning. Built from scratch using React and native HTML div elements. No UI library dependencies.
Code
TypeScript: Copy this code into components/ui/alert.tsx:
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-2 border-foreground p-4 neobrutalism-shadow",
{
variants: {
variant: {
default: "bg-background text-foreground",
success: "bg-blue-500 text-white",
destructive: "bg-destructive text-destructive-foreground",
warning: "bg-yellow-500 text-white",
},
},
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-bold 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 }JavaScript: Copy this code into components/ui/alert.jsx:
jsx
import * as React from "react"
import { cva } from "class-variance-authority"
import { cn } from "@/lib/utils"
const alertVariants = cva(
"relative w-full rounded-lg border-2 border-foreground p-4 neobrutalism-shadow",
{
variants: {
variant: {
default: "bg-background text-foreground",
success: "bg-blue-500 text-white",
destructive: "bg-destructive text-destructive-foreground",
warning: "bg-yellow-500 text-white",
},
},
defaultVariants: {
variant: "default",
},
}
)
const Alert = React.forwardRef(
({ className, variant, ...props }, ref) => (
<div
ref={ref}
role="alert"
className={cn(alertVariants({ variant }), className)}
{...props}
/>
)
)
Alert.displayName = "Alert"
const AlertTitle = React.forwardRef(
({ className, ...props }, ref) => (
<h5
ref={ref}
className={cn("mb-1 font-bold leading-none tracking-tight", className)}
{...props}
/>
)
)
AlertTitle.displayName = "AlertTitle"
const AlertDescription = React.forwardRef(
({ className, ...props }, ref) => (
<div
ref={ref}
className={cn("text-sm [&_p]:leading-relaxed", className)}
{...props}
/>
)
)
AlertDescription.displayName = "AlertDescription"
export { Alert, AlertTitle, AlertDescription }Usage
TypeScript:
tsx
import { Alert, AlertTitle, AlertDescription } from "@/components/ui/alert"
<Alert variant="success">
<AlertTitle>Success!</AlertTitle>
<AlertDescription>
Your changes have been saved.
</AlertDescription>
</Alert>JavaScript:
jsx
import { Alert, AlertTitle, AlertDescription } from "@/components/ui/alert"
<Alert variant="success">
<AlertTitle>Success!</AlertTitle>
<AlertDescription>
Your changes have been saved.
</AlertDescription>
</Alert>Make sure you also have the lib/utils.ts file with the cn helper function.
Examples
Success! Your changes have been saved
This is an alert with icon, title and description.
Default Alert
This is a default alert without any variant.
Error
Something went wrong. Please try again.
Warning
Please review your input before proceeding.