Skip to content
npm ↗ GitHub ↗

Documentation

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

ClassDescription
.cb-toasterFixed container. --top and --left for other corners.
.cb-toastNotification with a colored side line.
.cb-toast--success|warning|danger|infoVariants.
.cb-toast__icon, .cb-toast__title, .cb-toast__message, .cb-toast__action, .cb-toast__closeParts.

data-cb-* attributes

AttributeDescription
data-cb-toastOn a button: JSON with the options or plain text.

JavaScript

EventWhen it fires
cb:open / cb:closeOn 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 use role="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: 0 and let the user close it.
  • The close button uses "Close notification"; change it with the closeLabel option or with cobalto.setMessages({ toastClose }).