Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

alert.tsx

140 lines4.1 KB

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