zasuiv0.1
GitHub
Menú

Avisos

Alert Message

Aviso flotante arriba al centro, de error (recarga por defecto) o de información.

Información

Instalación

Con la CLI de zasui (ver Introducción): instala el componente con sus dependencias y queda listo para usar.

pnpm dlx @medialityc/zasui add alert-message
Ver el código
// Base: components/alert/alert-message.tsx de ZasExpress, sin cambios.
import React from 'react';

export type AlertVariant = 'error' | 'info';

export interface AlertMessageProps {
	variant?: AlertVariant;
	title?: string;
	message?: string;
	buttonLabel?: string;
	className?: string;
	showButton?: boolean; // only relevant for error variant by default
	onButtonClick?: () => void; // error variant: reload fallback
	onClose?: () => void; // info variant: close handler
}

export const AlertMessage: React.FC<AlertMessageProps> = ({
	variant = 'error',
	title,
	message,
	buttonLabel,
	className = '',
	showButton = true,
	onButtonClick,
	onClose,
}) => {
	const isError = variant === 'error';

	const computedTitle = title ?? (isError ? 'Ocurrió un error' : 'Información');
	const computedMessage =
		message ??
		(isError
			? 'Ha ocurrido un error inesperado. Por favor, intente nuevamente.'
			: 'Esta es un mensaje informativo.');
	const computedButtonLabel =
		buttonLabel ?? (isError ? 'Recargar página' : 'Aceptar');

	const handlePrimaryAction = () => {
		if (isError) {
			if (onButtonClick) return onButtonClick();
			// Default error action: reload
			if (typeof window !== 'undefined') window.location.reload();
			return;
		}
		// info variant defaults to onClose if provided
		if (onClose) onClose();
	};

	const colors = isError
		? {
				border: 'border-red-200 dark:border-red-700',
				iconBg: 'bg-red-100 dark:bg-red-900/30',
				iconColor: 'text-red-600 dark:text-red-300',
				buttonBg:
					'bg-red-600 hover:bg-red-700 dark:bg-red-500 dark:hover:bg-red-600',
			}
		: {
				border: 'border-blue-200 dark:border-blue-700',
				iconBg: 'bg-blue-100 dark:bg-blue-900/30',
				iconColor: 'text-blue-600 dark:text-blue-300',
				buttonBg:
					'bg-blue-600 hover:bg-blue-700 dark:bg-blue-600 dark:hover:bg-blue-700',
			};

	const iconPath = isError
		? 'M12 9v2m0 4h.01M12 17a5 5 0 100-10 5 5 0 000 10zm0 0v.01'
		: 'M13 16h-1v-4h-1m1-4h.01M21 12a9 9 0 11-18 0 9 9 0 0118 0z';

	const shouldShowButton = isError
		? showButton && !!computedButtonLabel
		: !!computedButtonLabel;

	return (
		<div
			className={`fixed top-4 left-1/2 z-50 w-full max-w-md -translate-x-1/2 transform px-4 ${className}`}
		>
			<div
				className={`border bg-white dark:bg-slate-800 ${colors.border} animate-slide-down flex items-center justify-between rounded-lg p-4 shadow-lg`}
			>
				<div className='flex items-center gap-3'>
					<div
						className={`h-10 w-10 ${colors.iconBg} flex items-center justify-center rounded-full`}
					>
						<svg
							className={`h-6 w-6 ${colors.iconColor}`}
							fill='none'
							stroke='currentColor'
							viewBox='0 0 24 24'
						>
							<path
								strokeLinecap='round'
								strokeLinejoin='round'
								strokeWidth={2}
								d={iconPath}
							/>
						</svg>
					</div>
					<div>
						<p className='text-sm font-medium text-gray-900 dark:text-white'>
							{computedTitle}
						</p>
						<p className='text-sm text-gray-600 dark:text-slate-300'>
							{computedMessage}
						</p>
					</div>
				</div>
				{shouldShowButton && (
					<button
						onClick={handlePrimaryAction}
						className={`ml-4 ${colors.buttonBg} rounded-md px-3 py-1.5 text-sm font-medium text-white transition-colors`}
					>
						{computedButtonLabel}
					</button>
				)}
			</div>
		</div>
	);
};

export default AlertMessage;