33/Interaction and state
Toast
Temporary notifications with the cobalto.toast() API. They pause on hover or focus.
Variants
The buttons use data-cb-toast with the configuration as JSON, so you can try it without writing JS.
HTML
<button class="cb-btn cb-btn--outline" type="button" data-cb-toast='{"title": "Cambios guardados", "variant": "success"}'>Éxito</button>
<button class="cb-btn cb-btn--outline" type="button" data-cb-toast='{"title": "Sincronizando", "message": "Puede tardar unos segundos.", "variant": "info"}'>Info</button>
<button class="cb-btn cb-btn--outline" type="button" data-cb-toast='{"title": "Factura por vencer", "message": "Vence en 3 días.", "variant": "warning"}'>Aviso</button>
<button class="cb-btn cb-btn--outline" type="button" data-cb-toast='{"title": "No se pudo guardar", "message": "Revisa tu conexión.", "variant": "danger", "duration": 0}'>Error fijo</button>
<button class="cb-btn cb-btn--outline" type="button" data-cb-toast="Mensaje simple">Texto</button>
Classes and modifiers
| Class | Description |
|---|---|
.cb-toaster | Fixed container. --top and --left for other corners. |
.cb-toast | Notification with a colored side line. |
.cb-toast--success|warning|danger|info | Variants. |
.cb-toast__icon, .cb-toast__title, .cb-toast__message, .cb-toast__action, .cb-toast__close | Parts. |
data-cb-* attributes
| Attribute | Description |
|---|---|
data-cb-toast | On a button: JSON with the options or plain text. |
JavaScript
| Event | When it fires |
|---|---|
cb:open / cb:close | On the toast when it appears and when it closes. |
Full API:
JS
const notice = cobalto.toast({
title: 'Cliente creado',
message: 'María Ramírez ya puede iniciar sesión.',
variant: 'success', // default, info, success, warning, danger
duration: 5000, // 0 para que no se cierre sola
position: 'bottom-right', // bottom-left, top-right, top-left
action: { label: 'Deshacer', onClick: () => deshacer() },
});
notice.close();
From Livewire 4, with an event dispatched using $this->dispatch():
JS
document.addEventListener('livewire:init', () => {
Livewire.on('toast', (event) => cobalto.toast(event));
});
Accessibility
- Toasts use
role="status"; error toasts userole="alert"so they are announced immediately. - The timer pauses on hover and focus, to give time to read or use the action.
- For important messages use
duration: 0and let the user close it. - The close button uses "Close notification"; change it with the
closeLabeloption or withcobalto.setMessages({ toastClose }).