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 size: "default",
10 surface: "default",
11 variant: "default",
12 },
13 variants: {
14 size: {
15 default: "",
16 compact: "px-3 py-2 text-xs [&_[data-slot=alert-description]]:gap-2",
17 },
18 surface: {
19 default: "",
20 glass: "alert-glass",
21 },
22 variant: {
23 default: "bg-transparent dark:bg-input/32 [&_svg]:text-muted-foreground",
24 sidebar:
25 "rounded-lg border-sidebar-border bg-sidebar-control-surface px-2 py-1.5 text-[11px] leading-4 [&_[data-slot=alert-description]]:block [&_[data-slot=alert-description]]:text-sidebar-muted-foreground",
26 error:
27 "border-error/32 bg-error-surface text-error-foreground [&_[data-slot=alert-description]]:text-error-foreground/80 [&_svg]:text-error",
28 info: "border-info/32 bg-info/4 [&_svg]:text-info",
29 success: "border-success/32 bg-success/4 [&_svg]:text-success",
30 warning:
31 "border-warning/32 bg-warning-surface text-warning-foreground [&_[data-slot=alert-description]]:text-warning-foreground/80 [&_svg]:text-warning",
32 },
33 },
34});
35
36function alertChildSlot(child: React.ReactElement): string | undefined {
37 const propsSlot = (child.props as Record<string, string | undefined>)["data-slot"];
38 if (propsSlot) {
39 return propsSlot;
40 }
41
42 const type = child.type as { displayName?: string; name?: string };
43 switch (type.displayName ?? type.name) {
44 case "AlertAction":
45 return "alert-action";
46 case "AlertTitle":
47 return "alert-title";
48 case "AlertDescription":
49 return "alert-description";
50 default:
51 return undefined;
52 }
53}
54
55function Alert({
56 className,
57 variant,
58 size,
59 surface,
60 controlAlignment = "center",
61 children,
62 ...props
63}: React.ComponentProps<"div"> &
64 VariantProps<typeof alertVariants> & {
65 controlAlignment?: "center" | "first-line";
66 }) {
67 const icon: React.ReactNode[] = [];
68 const content: React.ReactNode[] = [];
69 const action: React.ReactNode[] = [];
70
71 Children.forEach(children, (child) => {
72 if (!isValidElement(child)) {
73 content.push(child);
74 return;
75 }
76 const slot = alertChildSlot(child);
77 if (slot === "alert-action") {
78 action.push(child);
79 } else if (slot === "alert-title" || slot === "alert-description") {
80 content.push(child);
81 } else {
82 icon.push(child);
83 }
84 });
85
86 return (
87 <div
88 className={cn(alertVariants({ size, surface, variant }), className)}
89 data-slot="alert"
90 data-variant={variant ?? "default"}
91 role="alert"
92 {...props}
93 >
94 <div
95 className={cn(
96 "flex flex-wrap gap-2",
97 controlAlignment === "first-line" ? "items-start" : "items-center",
98 controlAlignment === "first-line" &&
99 action.length > 0 &&
100 "min-h-7 pt-1 sm:min-h-6 sm:pt-0.5",
101 )}
102 >
103 {icon.length > 0 && (
104 <div
105 className={cn(
106 "flex shrink-0 items-center justify-center",
107 controlAlignment === "first-line"
108 ? "h-lh w-4 [&>svg]:size-4"
109 : "size-4 [&>svg]:size-full",
110 )}
111 >
112 {icon}
113 </div>
114 )}
115 {content.length > 0 && (
116 <div className="flex min-w-0 grow basis-[min(10rem,calc(100%-1.5rem))] flex-col gap-0.5">
117 {content}
118 </div>
119 )}
120 {action.length > 0 && (
121 <div
122 className={cn(
123 "ml-auto flex shrink-0 items-center",
124 controlAlignment === "first-line" ? "h-lh self-start" : "self-center",
125 )}
126 >
127 {action}
128 </div>
129 )}
130 </div>
131 </div>
132 );
133}
134
135function AlertTitle({ className, ...props }: React.ComponentProps<"div">) {
136 return <div className={cn("font-medium", className)} data-slot="alert-title" {...props} />;
137}
138
139function AlertDescription({ className, ...props }: React.ComponentProps<"div">) {
140 return (
141 <div
142 className={cn("flex flex-col gap-2.5 text-muted-foreground", className)}
143 data-slot="alert-description"
144 {...props}
145 />
146 );
147}
148
149function AlertAction({ className, ...props }: React.ComponentProps<"div">) {
150 return <div className={cn("flex gap-1", className)} data-slot="alert-action" {...props} />;
151}
152
153AlertTitle.displayName = "AlertTitle";
154AlertDescription.displayName = "AlertDescription";
155AlertAction.displayName = "AlertAction";
156
157export { Alert, AlertTitle, AlertDescription, AlertAction };