import { cva, type VariantProps } from "class-variance-authority"; import { Children, isValidElement } from "react"; import type * as React from "react"; import { cn } from "~/lib/utils"; const alertVariants = cva("relative rounded-xl border px-3.5 py-3 text-card-foreground text-sm", { defaultVariants: { variant: "default", }, variants: { variant: { default: "bg-transparent dark:bg-input/32 [&_svg]:text-muted-foreground", error: "border-destructive/32 bg-destructive/4 text-destructive-foreground [&_[data-slot=alert-description]]:text-destructive-foreground/80 [&_svg]:text-destructive", info: "border-info/32 bg-info/4 [&_svg]:text-info", success: "border-success/32 bg-success/4 [&_svg]:text-success", warning: "border-warning/32 bg-warning/4 [&_svg]:text-warning", }, }, }); function alertChildSlot(child: React.ReactElement): string | undefined { const propsSlot = (child.props as Record)["data-slot"]; if (propsSlot) { return propsSlot; } const type = child.type as { displayName?: string; name?: string }; switch (type.displayName ?? type.name) { case "AlertAction": return "alert-action"; case "AlertTitle": return "alert-title"; case "AlertDescription": return "alert-description"; default: return undefined; } } function Alert({ className, variant, children, ...props }: React.ComponentProps<"div"> & VariantProps) { const icon: React.ReactNode[] = []; const content: React.ReactNode[] = []; const action: React.ReactNode[] = []; Children.forEach(children, (child) => { if (!isValidElement(child)) { content.push(child); return; } const slot = alertChildSlot(child); if (slot === "alert-action") { action.push(child); } else if (slot === "alert-title" || slot === "alert-description") { content.push(child); } else { icon.push(child); } }); return (
{icon.length > 0 && (
{icon}
)} {content.length > 0 && (
{content}
)} {action.length > 0 && (
{action}
)}
); } function AlertTitle({ className, ...props }: React.ComponentProps<"div">) { return
; } function AlertDescription({ className, ...props }: React.ComponentProps<"div">) { return (
); } function AlertAction({ className, ...props }: React.ComponentProps<"div">) { return
; } AlertTitle.displayName = "AlertTitle"; AlertDescription.displayName = "AlertDescription"; AlertAction.displayName = "AlertAction"; export { Alert, AlertTitle, AlertDescription, AlertAction };