main
tsx 43 lines 1.51 KB
Raw
1 import * as React from "react";
2 import { cva, type VariantProps } from "class-variance-authority";
3
4 import { cn } from "@/lib/utils";
5
6 const alertVariants = cva(
7 "relative w-full rounded-lg border p-4 [&>svg~*]:pl-7 [&>svg+div]:translate-y-[-3px] [&>svg]:absolute [&>svg]:left-4 [&>svg]:top-4 [&>svg]:text-foreground",
8 {
9 variants: {
10 variant: {
11 default: "bg-background text-foreground",
12 destructive: "border-destructive/50 text-destructive dark:border-destructive [&>svg]:text-destructive",
13 },
14 },
15 defaultVariants: {
16 variant: "default",
17 },
18 },
19 );
20
21 const Alert = React.forwardRef<
22 HTMLDivElement,
23 React.HTMLAttributes<HTMLDivElement> & VariantProps<typeof alertVariants>
24 >(({ className, variant, ...props }, ref) => (
25 <div ref={ref} role="alert" className={cn(alertVariants({ variant }), className)} {...props} />
26 ));
27 Alert.displayName = "Alert";
28
29 const AlertTitle = React.forwardRef<HTMLParagraphElement, React.HTMLAttributes<HTMLHeadingElement>>(
30 ({ className, ...props }, ref) => (
31 <h5 ref={ref} className={cn("mb-1 font-medium leading-none tracking-tight", className)} {...props} />
32 ),
33 );
34 AlertTitle.displayName = "AlertTitle";
35
36 const AlertDescription = React.forwardRef<HTMLParagraphElement, React.HTMLAttributes<HTMLParagraphElement>>(
37 ({ className, ...props }, ref) => (
38 <div ref={ref} className={cn("text-sm [&_p]:leading-relaxed", className)} {...props} />
39 ),
40 );
41 AlertDescription.displayName = "AlertDescription";
42
43 export { Alert, AlertTitle, AlertDescription };