|
1 | 1 | import { cva, type VariantProps } from "class-variance-authority"; |
| 2 | +import { Children, isValidElement } from "react"; |
2 | 3 | import type * as React from "react"; |
3 | 4 |
|
4 | 5 | import { cn } from "~/lib/utils"; |
5 | 6 |
|
6 | | -const alertVariants = cva( |
7 | | - "relative grid w-full items-start gap-x-2 gap-y-0.5 rounded-xl border px-3.5 py-3 text-card-foreground text-sm has-[>svg]:has-data-[slot=alert-action]:grid-cols-[calc(var(--spacing)*4)_1fr_auto] has-[>svg]:grid-cols-[calc(var(--spacing)*4)_1fr] has-data-[slot=alert-action]:grid-cols-[1fr_auto] has-[>svg]:gap-x-2 [&>svg]:h-lh [&>svg]:w-4", |
8 | | - { |
9 | | - defaultVariants: { |
10 | | - variant: "default", |
11 | | - }, |
12 | | - variants: { |
13 | | - variant: { |
14 | | - default: "bg-transparent dark:bg-input/32 [&>svg]:text-muted-foreground", |
15 | | - error: "border-destructive/32 bg-destructive/4 [&>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 | | - }, |
| 7 | +const 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", |
20 | 19 | }, |
21 | 20 | }, |
22 | | -); |
| 21 | +}); |
| 22 | + |
| 23 | +function 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 | +} |
23 | 41 |
|
24 | 42 | function Alert({ |
25 | 43 | className, |
26 | 44 | variant, |
| 45 | + children, |
27 | 46 | ...props |
28 | 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 | + |
29 | 67 | return ( |
30 | 68 | <div |
31 | 69 | className={cn(alertVariants({ variant }), className)} |
32 | 70 | data-slot="alert" |
33 | 71 | role="alert" |
34 | 72 | {...props} |
35 | | - /> |
| 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> |
36 | 88 | ); |
37 | 89 | } |
38 | 90 |
|
39 | 91 | function AlertTitle({ className, ...props }: React.ComponentProps<"div">) { |
40 | | - return ( |
41 | | - <div |
42 | | - className={cn("font-medium [svg~&]:col-start-2", className)} |
43 | | - data-slot="alert-title" |
44 | | - {...props} |
45 | | - /> |
46 | | - ); |
| 92 | + return <div className={cn("font-medium", className)} data-slot="alert-title" {...props} />; |
47 | 93 | } |
48 | 94 |
|
49 | 95 | function AlertDescription({ className, ...props }: React.ComponentProps<"div">) { |
50 | 96 | return ( |
51 | 97 | <div |
52 | | - className={cn("flex flex-col gap-2.5 text-muted-foreground [svg~&]:col-start-2", className)} |
| 98 | + className={cn("flex flex-col gap-2.5 text-muted-foreground", className)} |
53 | 99 | data-slot="alert-description" |
54 | 100 | {...props} |
55 | 101 | /> |
56 | 102 | ); |
57 | 103 | } |
58 | 104 |
|
59 | 105 | function AlertAction({ className, ...props }: React.ComponentProps<"div">) { |
60 | | - return ( |
61 | | - <div |
62 | | - className={cn( |
63 | | - "flex gap-1 max-sm:col-start-2 max-sm:mt-2 sm:row-start-1 sm:row-end-3 sm:self-center sm:[[data-slot=alert-description]~&]:col-start-2 sm:[[data-slot=alert-title]~&]:col-start-2 sm:[svg~&]:col-start-2 sm:[svg~[data-slot=alert-description]~&]:col-start-3 sm:[svg~[data-slot=alert-title]~&]:col-start-3", |
64 | | - className, |
65 | | - )} |
66 | | - data-slot="alert-action" |
67 | | - {...props} |
68 | | - /> |
69 | | - ); |
| 106 | + return <div className={cn("flex gap-1", className)} data-slot="alert-action" {...props} />; |
70 | 107 | } |
71 | 108 |
|
| 109 | +AlertTitle.displayName = "AlertTitle"; |
| 110 | +AlertDescription.displayName = "AlertDescription"; |
| 111 | +AlertAction.displayName = "AlertAction"; |
| 112 | + |
72 | 113 | export { Alert, AlertTitle, AlertDescription, AlertAction }; |
0 commit comments