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

alert.tsx

113 lines3.4 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-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 }
41​
42function Alert({
43 className,
44 variant,
45 children,
46 ...props
47}: React.ComponentProps<"div"> & VariantProps<typeof alertVariants>) {
48 const icon: React.ReactNode[] = [];
49 const content: React.ReactNode[] = [];
50 const action: React.ReactNode[] = [];
51​
52 Children.forEach(children, (child) => {
53 if (!isValidElement(child)) {
54 content.push(child);
55 return;
56 }
57 const slot = alertChildSlot(child);
58 if (slot === "alert-action") {
59 action.push(child);
60 } else if (slot === "alert-title" || slot === "alert-description") {
61 content.push(child);
62 } else {
63 icon.push(child);
64 }
65 });
66​
67 return (
68 <div
69 className={cn(alertVariants({ variant }), className)}
70 data-slot="alert"
71 role="alert"
72 {...props}
73 >
74 <div className="flex items-center gap-2">
75 {icon.length > 0 && (
76 <div className="flex size-4 shrink-0 items-center justify-center [&>svg]:size-full">
77 {icon}
78 </div>
79 )}
80 {content.length > 0 && (
81 <div className="flex min-w-0 flex-1 flex-col gap-0.5">{content}</div>
82 )}
83 {action.length > 0 && (
84 <div className="flex shrink-0 items-center self-center">{action}</div>
85 )}
86 </div>
87 </div>
88 );
90​
91function AlertTitle({ className, ...props }: React.ComponentProps<"div">) {
92 return <div className={cn("font-medium", className)} data-slot="alert-title" {...props} />;
94​
95function AlertDescription({ className, ...props }: React.ComponentProps<"div">) {
96 return (
97 <div
98 className={cn("flex flex-col gap-2.5 text-muted-foreground", className)}
99 data-slot="alert-description"
100 {...props}
101 />
102 );
104​
105function AlertAction({ className, ...props }: React.ComponentProps<"div">) {
106 return <div className={cn("flex gap-1", className)} data-slot="alert-action" {...props} />;
108​
109AlertTitle.displayName = "AlertTitle";
110AlertDescription.displayName = "AlertDescription";
111AlertAction.displayName = "AlertAction";
112​
113export { Alert, AlertTitle, AlertDescription, AlertAction };