Open History to see the last commit
1import { cva, type VariantProps } from "class-variance-authority";
2import { Children, isValidElement } from "react";
3import type * as React from "react";
4
5import { cn } from "~/lib/utils";
6
7const alertVariants = cva("relative rounded-xl border px-3.5 py-3 text-card-foreground text-sm", {
8 defaultVariants: {
9 variant: "default",
10 },
11 variants: {
12 variant: {
13 default: "bg-transparent dark:bg-input/32 [&_svg]:text-muted-foreground",
14 error:
15 "border-destructive/32 bg-destructive/4 text-destructive-foreground [&_[data-slot=alert-description]]:text-destructive-foreground/80 [&_svg]:text-destructive",
16 info: "border-info/32 bg-info/4 [&_svg]:text-info",
17 success: "border-success/32 bg-success/4 [&_svg]:text-success",
18 warning: "border-warning/32 bg-warning/4 [&_svg]:text-warning",
19 },
20 },
21});
22
23function alertChildSlot(child: React.ReactElement): string | undefined {
24 const propsSlot = (child.props as Record<string, string | undefined>)["data-slot"];
25 if (propsSlot) {
26 return propsSlot;
27 }
28
29 const type = child.type as { displayName?: string; name?: string };
30 switch (type.displayName ?? type.name) {
31 case "AlertAction":
32 return "alert-action";
33 case "AlertTitle":
34 return "alert-title";
35 case "AlertDescription":
36 return "alert-description";
37 default:
38 return undefined;
39 }
40}
41
42function Alert({
43 className,
44 variant,
45 controlAlignment = "center",
46 children,
47 ...props
48}: React.ComponentProps<"div"> &
49 VariantProps<typeof alertVariants> & {
50 controlAlignment?: "center" | "first-line";
51 }) {
52 const icon: React.ReactNode[] = [];
53 const content: React.ReactNode[] = [];
54 const action: React.ReactNode[] = [];
55
56 Children.forEach(children, (child) => {
57 if (!isValidElement(child)) {
58 content.push(child);
59 return;
60 }
61 const slot = alertChildSlot(child);
62 if (slot === "alert-action") {
63 action.push(child);
64 } else if (slot === "alert-title" || slot === "alert-description") {
65 content.push(child);
66 } else {
67 icon.push(child);
68 }
69 });
70
71 return (
72 <div
73 className={cn(alertVariants({ variant }), className)}
74 data-slot="alert"
75 role="alert"
76 {...props}
77 >
78 <div
79 className={cn(
80 "flex gap-2",
81 controlAlignment === "first-line" ? "items-start" : "items-center",
82 controlAlignment === "first-line" &&
83 action.length > 0 &&
84 "min-h-7 pt-1 sm:min-h-6 sm:pt-0.5",
85 )}
86 >
87 {icon.length > 0 && (
88 <div
89 className={cn(
90 "flex shrink-0 items-center justify-center",
91 controlAlignment === "first-line"
92 ? "h-lh w-4 [&>svg]:size-4"
93 : "size-4 [&>svg]:size-full",
94 )}
95 >
96 {icon}
97 </div>
98 )}
99 {content.length > 0 && (
100 <div className="flex min-w-0 flex-1 flex-col gap-0.5">{content}</div>
101 )}
102 {action.length > 0 && (
103 <div
104 className={cn(
105 "flex shrink-0 items-center",
106 controlAlignment === "first-line" ? "h-lh self-start" : "self-center",
107 )}
108 >
109 {action}
110 </div>
111 )}
112 </div>
113 </div>
114 );
115}
116
117function AlertTitle({ className, ...props }: React.ComponentProps<"div">) {
118 return <div className={cn("font-medium", className)} data-slot="alert-title" {...props} />;
119}
120
121function AlertDescription({ className, ...props }: React.ComponentProps<"div">) {
122 return (
123 <div
124 className={cn("flex flex-col gap-2.5 text-muted-foreground", className)}
125 data-slot="alert-description"
126 {...props}
127 />
128 );
129}
130
131function AlertAction({ className, ...props }: React.ComponentProps<"div">) {
132 return <div className={cn("flex gap-1", className)} data-slot="alert-action" {...props} />;
133}
134
135AlertTitle.displayName = "AlertTitle";
136AlertDescription.displayName = "AlertDescription";
137AlertAction.displayName = "AlertAction";
138
139export { Alert, AlertTitle, AlertDescription, AlertAction };