'use client' import { Text, View, XStack, YStack } from '@hanzo/gui'; import * as React from 'react'; // Inline info icon (filled circle with i) const IndicatorIcon = () => ( ); import { CloseButton } from './close-button'; import { ink } from './ink'; import { Skeleton } from './skeleton'; /* ------------------------------------------------------------------ */ /* Status type */ /* ------------------------------------------------------------------ */ type AlertStatus = 'info' | 'warning' | 'warning_table' | 'success' | 'error'; /* ------------------------------------------------------------------ */ /* Style maps (replaced CVA) */ /* ------------------------------------------------------------------ */ /** The frame every alert shares, regardless of status or size. */ const ALERT_BASE = { width: '100%', position: 'relative', alignItems: 'flex-start', borderRadius: 4, gap: 8, paddingVertical: 8, } as const; /** Foreground is constant across every status — only the background moves. */ const ALERT_FG = 'var(--color-alert-fg)' as never; const STATUS_BG: Record = { info: 'var(--color-alert-bg-info)', warning: 'var(--color-alert-bg-warning)', warning_table: 'var(--color-alert-bg-warning-table)', success: 'var(--color-alert-bg-success)', error: 'var(--color-alert-bg-error)', }; const SIZE: Record<'sm' | 'md', { paddingHorizontal: number; fontSize: number }> = { sm: { paddingHorizontal: 8, fontSize: 12 }, md: { paddingHorizontal: 12, fontSize: 16 }, }; const INDICATOR_SIZE: Record<'sm' | 'md', { marginVertical: number }> = { sm: { marginVertical: 0 }, md: { marginVertical: 2 }, }; /* ------------------------------------------------------------------ */ /* Props */ /* ------------------------------------------------------------------ */ export interface AlertProps extends Omit, 'title'> { readonly status?: AlertStatus; readonly size?: 'sm' | 'md'; readonly inline?: boolean; readonly startElement?: React.ReactNode; readonly endElement?: React.ReactNode; readonly descriptionProps?: Omit, 'ref'> & { whiteSpace?: string; alignItems?: string; flexDir?: string; flexWrap?: string; rowGap?: number | string; columnGap?: number | string; }; readonly title?: React.ReactNode; readonly icon?: React.ReactElement; readonly closable?: boolean; readonly onClose?: () => void; readonly loading?: boolean; readonly showIcon?: boolean; // Legacy Chakra style-prop shims readonly whiteSpace?: string; readonly mb?: number | string; readonly mt?: number | string; } /* ------------------------------------------------------------------ */ /* Component */ /* ------------------------------------------------------------------ */ export const Alert = React.forwardRef( function Alert(props, ref) { const { title, children, icon, closable, onClose, startElement, endElement, loading, size, inline = true, showIcon = false, descriptionProps, status = 'info', className, ...rest } = props; // Strip style prop shims const { whiteSpace: _whiteSpace, mb: _mb, mt: _mt, style: styleProp, ...alertRest } = rest; const shimStyle: React.CSSProperties = { ...styleProp }; if (_whiteSpace) shimStyle.whiteSpace = _whiteSpace as React.CSSProperties['whiteSpace']; if (_mb !== undefined) shimStyle.marginBottom = typeof _mb === 'number' ? `${ _mb * 4 }px` : _mb; if (_mt !== undefined) shimStyle.marginTop = typeof _mt === 'number' ? `${ _mt * 4 }px` : _mt; const alertStyle = Object.keys(shimStyle).length > 0 ? shimStyle : undefined; const [ isOpen, setIsOpen ] = React.useState(true); const resolvedSize = size ?? 'md'; const fontSize = SIZE[resolvedSize].fontSize; const defaultIcon = ; const iconElement = (() => { if (startElement !== undefined) { return startElement; } if (!showIcon && icon === undefined) { return null; } return ( { icon || defaultIcon } ); })(); const handleClose = React.useCallback(() => { setIsOpen(false); onClose?.(); }, [ onClose ]); if (closable && !isOpen) { return null; } const { className: descClassName, whiteSpace: descWhiteSpace, alignItems: descAlignItems, flexDir: descFlexDir, flexWrap: descFlexWrap, rowGap: descRowGap, columnGap: descColumnGap, style: descStyleProp, ...descRest } = descriptionProps ?? {}; const descStyle: React.CSSProperties = { ...descStyleProp, ...(descWhiteSpace ? { whiteSpace: descWhiteSpace as React.CSSProperties['whiteSpace'] } : {}), ...(descAlignItems ? { alignItems: descAlignItems } : {}), ...(descFlexDir ? { flexDirection: descFlexDir as React.CSSProperties['flexDirection'] } : {}), ...(descFlexWrap ? { flexWrap: descFlexWrap as React.CSSProperties['flexWrap'] } : {}), ...(descRowGap !== undefined ? { rowGap: typeof descRowGap === 'number' ? `${ descRowGap * 4 }px` : descRowGap } : {}), ...(descColumnGap !== undefined ? { columnGap: typeof descColumnGap === 'number' ? `${ descColumnGap * 4 }px` : descColumnGap } : {}), }; // Row when inline (text sits beside the title), column otherwise. const ContentFrame = inline ? XStack : YStack; return ( { iconElement } { children ? ( { title && ( { title } ) } 0 ? descStyle : undefined } { ...(descRest as object) } > { ink(children, undefined, { fontSize, color: ALERT_FG }) } ) : ( { title } ) } { endElement } { closable && ( ) } ); }, );