import { useEffect, useRef, useState, type ComponentProps, type ReactNode } from 'react' import { CircleAlert, CircleCheck, CircleX, Info, TriangleAlert, X } from 'lucide-react' import { cn } from '../lib/cn' import { useControllableState } from '../lib/useControllableState' import { IconButton } from './Button' import { ALERT_CLOSE_STYLES, ALERT_ICON_STYLES, ALERT_STYLES, ALERT_URGENT_COLORS, type AlertColor, type AlertVariant, } from './alert.tokens' export type AlertProps = Omit, 'color' | 'title'> & { /** Semantic colour. `danger` and `warning` use `role="alert"`; the rest are polite `role="status"`. */ color?: AlertColor /** Visual weight. */ variant?: AlertVariant /** Short bold heading. */ title?: ReactNode /** The message. */ children?: ReactNode /** Replaces the colour's default icon. */ icon?: ReactNode /** Hides the icon. */ hideIcon?: boolean /** Buttons or links shown under the message. */ actions?: ReactNode /** Shows a close button labelled "Dismiss". */ dismissible?: boolean /** Controlled visibility. */ open?: boolean /** Initial visibility when uncontrolled. */ defaultOpen?: boolean /** Called when the alert is dismissed or auto-closes. */ onOpenChange?: (open: boolean) => void /** Closes the alert after this many milliseconds. Pauses while hovered or focused. `0` disables. */ autoClose?: number } const DEFAULT_ICONS: Record = { info: , success: , warning: , danger: , neutral: , primary: , } /** Counts down `delay` ms, pausing while `paused` is true, then calls `onDone`. */ function useAutoClose(delay: number, paused: boolean, active: boolean, onDone: () => void) { const remaining = useRef(delay) const onDoneRef = useRef(onDone) useEffect(() => { onDoneRef.current = onDone }) useEffect(() => { remaining.current = delay }, [delay]) useEffect(() => { if (!active || delay <= 0 || paused) return const started = Date.now() const timer = setTimeout(() => onDoneRef.current(), remaining.current) return () => { clearTimeout(timer) remaining.current = Math.max(0, remaining.current - (Date.now() - started)) } }, [active, delay, paused]) } /** An inline status message, optionally dismissible or auto-closing. */ export function Alert({ color = 'info', variant = 'soft', title, children, icon, hideIcon = false, actions, dismissible = false, open: openProp, defaultOpen = true, onOpenChange, autoClose = 0, className, onMouseEnter, onMouseLeave, onFocus, onBlur, ...props }: AlertProps) { const [open, setOpen] = useControllableState(openProp, defaultOpen, onOpenChange) const [hovered, setHovered] = useState(false) const [focused, setFocused] = useState(false) useAutoClose(autoClose, hovered || focused, open, () => setOpen(false)) if (!open) return null const urgent = ALERT_URGENT_COLORS.includes(color) return (
{ setHovered(true) onMouseEnter?.(e) }} onMouseLeave={(e) => { setHovered(false) onMouseLeave?.(e) }} onFocus={(e) => { setFocused(true) onFocus?.(e) }} onBlur={(e) => { if (!e.currentTarget.contains(e.relatedTarget as Node | null)) setFocused(false) onBlur?.(e) }} {...props} > {!hideIcon && ( )}
{title != null &&

{title}

} {children != null &&
{children}
} {actions != null &&
{actions}
}
{dismissible && ( } onClick={() => setOpen(false)} className={cn('-my-1.5 -mr-1.5 size-8', ALERT_CLOSE_STYLES[variant])} /> )}
) }