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