Saltar al contenido

Crear UISin intentar

Construye toasts que se anuncien

HTML / CSS / JSDificultad: fácil

«Guardar» muestra un toast de éxito; «Provocar error» muestra un toast de error. Ambos se descartan solos a los 4 segundos y los lectores de pantalla deben anunciarlos.

Vista previa: tu documento HTML tal cual. localStorage e IndexedDB se reinician cuando se recarga la vista previa.

Requisitos · 0 / 5

Marca cada requisito después de comprobarlo en la vista previa.
Ver solución de referencia
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<style>
.toast-stack { position: fixed; bottom: 16px; right: 16px; display: flex; flex-direction: column; gap: 8px; }
.toasts { display: grid; gap: 8px; }
.toast { padding: 10px 14px; border-radius: 8px; color: white; background: #15803d; box-shadow: 0 6px 20px rgb(0 0 0 / 0.2); }
.toast.error { background: #c4312b; }
button { font: inherit; padding: 8px 14px; margin-right: 8px; }
</style>
</head>
<body>
<button id="save">Save</button>
<button id="fail">Trigger error</button>
<!-- Live regions must exist in the DOM BEFORE content is injected, or many screen readers miss it. -->
<div class="toast-stack">
<div class="toasts" id="assertive" role="alert"></div>
<!-- Not aria-atomic: otherwise every visible toast is re-read when a new one is added. -->
<div class="toasts" id="polite" role="status"></div>
</div>
<script>
function toast(message, { error = false } = {}) {
const region = document.getElementById(error ? 'assertive' : 'polite');
const el = document.createElement('div');
el.className = 'toast' + (error ? ' error' : '');
el.textContent = message;
region.append(el);
setTimeout(() => el.remove(), 4000);
}
document.getElementById('save').onclick = () => toast('Settings saved');
document.getElementById('fail').onclick = () => toast('Could not save: network error', { error: true });
</script>
</body>
</html>
Cargando editor…