zasuiv0.1
GitHub
Menú

Controles

Toggle Switch

Interruptor con check, estado de carga y tamaño sm para filas densas.

Estados

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 toggle-switch
Ver el código
// Base: components/switch/toggle-switch.tsx de ZasExpress, sin cambios.
interface Props {
	isEnabled: boolean;
	isLoading?: boolean;
	onToggle: () => void;
	disabled?: boolean;
	/**
	 * Nombre accesible del interruptor. Solo hace falta cuando el switch va sin
	 * etiqueta de texto asociada —en una fila o una celda de tabla, donde el
	 * significado lo da la columna—: sin él el lector de pantalla anuncia
	 * «conmutador» y nada más.
	 *
	 * `aria-label` no llegaba al botón por sí solo: el componente no propaga el
	 * resto de props, así que ponerlo en la etiqueta JSX se descartaba en
	 * silencio (TypeScript acepta los `aria-*` en cualquier componente).
	 */
	ariaLabel?: string;
	/** `sm` para filas densas, donde el interruptor normal pesa demasiado. */
	size?: 'sm' | 'md';
}
const ToggleSwitch = ({
	isEnabled,
	isLoading = false,
	onToggle,
	disabled = false,
	ariaLabel,
	size = 'md',
}: Props) => {
	const isInert = isLoading || disabled;
	const track = size === 'sm' ? 'h-5 w-9' : 'h-6 w-11';
	const knob = size === 'sm' ? 'h-3.5 w-3.5' : 'h-5 w-5';
	const knobShift = size === 'sm' ? 'translate-x-4' : 'translate-x-5';
	const mark = size === 'sm' ? 'h-2.5 w-2.5' : 'h-3 w-3';
	return (
		<button
			type='button'
			role='switch'
			aria-checked={isEnabled}
			aria-busy={isLoading}
			aria-disabled={isInert}
			aria-label={ariaLabel}
			disabled={isInert}
			onClick={(e) => {
				e.preventDefault();
				e.stopPropagation();
				if (isInert) return;
				onToggle();
			}}
			className={`relative inline-flex ${track} shrink-0 rounded-full border-2 border-transparent transition-colors duration-200 ease-in-out focus:ring-2 focus:ring-blue-500/40 focus:ring-offset-2 focus:outline-none dark:focus:ring-offset-gray-900 ${
				isEnabled ? 'bg-blue-500' : 'bg-gray-300 dark:bg-gray-600'
			} ${isInert ? 'cursor-not-allowed opacity-60' : 'cursor-pointer'}`}
		>
			<span
				className={`pointer-events-none relative inline-flex ${knob} transform items-center justify-center rounded-full bg-white shadow ring-0 transition duration-200 ease-in-out ${isEnabled ? knobShift : 'translate-x-0'}`}
			>
				{isLoading ? (
					<span
						className={`${mark} animate-spin rounded-full border-2 border-blue-500 border-t-transparent`}
					/>
				) : (
					<svg
						className={`${mark} text-blue-500 transition-all duration-200 ${
							isEnabled ? 'scale-100 opacity-100' : 'scale-75 opacity-0'
						}`}
						fill='none'
						stroke='currentColor'
						strokeWidth='3'
						viewBox='0 0 24 24'
					>
						<path
							strokeLinecap='round'
							strokeLinejoin='round'
							d='M5 13l4 4L19 7'
						/>
					</svg>
				)}
			</span>
		</button>
	);
};

export default ToggleSwitch;