"use client" import * as React from "react" import { cva, type VariantProps } from "class-variance-authority" import { cn } from "@/lib/utils" /* ---------------------------------- Types ---------------------------------- */ const alertVariants = cva("relative w-full rounded-lg border p-4", { variants: { variant: { default: "bg-background text-foreground", info: "", success: "", warning: "", error: "", }, size: { default: "p-4", compact: "p-3", large: "p-6", }, }, defaultVariants: { variant: "default", size: "default", }, }) export interface AlertProps extends React.HTMLAttributes, VariantProps { onClose?: () => void } /* ---------------------------------- Context ---------------------------------- */ type AlertContextValue = { variant: AlertProps["variant"] size: AlertProps["size"] hasActions: boolean setHasActions: React.Dispatch> } const AlertContext = React.createContext(undefined) function useAlertContext() { const context = React.useContext(AlertContext) if (!context) { throw new Error("Alert compound components must be used within an Alert component") } return context } /* ---------------------------------- Components ---------------------------------- */ const Alert = React.forwardRef( ({ className, variant = "default", size = "default", onClose, ...props }, ref) => { const [hasActions, setHasActions] = React.useState(false) // Apply semantic colors for non-default variants const semanticStyle = variant !== "default" ? { backgroundColor: `var(--semantic-${variant === "destructive" ? "error" : variant}-bg)`, borderColor: `var(--semantic-${variant === "destructive" ? "error" : variant}-border)`, } : {} return (
) }, ) Alert.displayName = "Alert" interface AlertIconProps extends React.HTMLAttributes { icon: React.ReactNode } const AlertIcon = ({ icon, className, ...props }: AlertIconProps) => { const { variant } = useAlertContext() const semanticStyle = variant !== "default" ? { color: `var(--semantic-${variant === "destructive" ? "error" : variant}-text)`, } : {} return (
{icon}
) } AlertIcon.displayName = "AlertIcon" const AlertContent = React.forwardRef>( ({ className, ...props }, ref) => (
), ) AlertContent.displayName = "AlertContent" const AlertTitle = React.forwardRef>( ({ className, ...props }, ref) => { const { variant } = useAlertContext() const semanticStyle = variant !== "default" ? { color: `var(--semantic-${variant === "destructive" ? "error" : variant}-text)`, } : {} return (
) }, ) AlertTitle.displayName = "AlertTitle" const AlertDescription = React.forwardRef>( ({ className, ...props }, ref) => { const { variant, hasActions } = useAlertContext() const semanticStyle = variant !== "default" ? { color: `var(--semantic-${variant === "destructive" ? "error" : variant}-text)`, opacity: 0.8, } : {} return (

) }, ) AlertDescription.displayName = "AlertDescription" const AlertActions = React.forwardRef>( ({ className, ...props }, ref) => { const { setHasActions } = useAlertContext() React.useEffect(() => { setHasActions(true) return () => setHasActions(false) }, [setHasActions]) return

}, ) AlertActions.displayName = "AlertActions" export { Alert, AlertIcon, AlertContent, AlertTitle, AlertDescription, AlertActions }