Abdulkader Safi

LaralCN-UI toast: from one message to a queue

The old toast took a title from a window event and removed itself after four seconds. The Figma design asked for variants, an Undo button and a stack that queues, so I rebuilt toast in place instead of adding a second component. Old calls still work, with one behaviour change around duration: 0.

5 min read

Share
Three LaralCN-UI toasts fanned out in a column after the pointer moved over the stack.

The old toast took a title from a window event and removed itself after four seconds. The Figma design asked for variants, an Undo button and a stack that queues, so I rebuilt toast in place instead of adding a second component. Old calls still work, with one behaviour change around duration: 0.

The old toast was a template and a timer

The first version cloned a <template>, filled in the title and description, appended the clone to a flex column and set a timer. That was the whole component. Fire ten events and you got ten cards in a column, each with its own timer and nothing to stop them piling up the screen.

The timer line is worth quoting, because it caused the one behaviour change in this release:

setTimeout(function() {
    dismiss(toast);
}, detail.duration || 4000);

0 is falsy in JavaScript, so duration: 0 quietly became four seconds. Nobody could make a toast that stayed open.

Variants, and updates by id

A toast now has a variant: default, success, error, info, warning or loading. Each variant shows its own icon from the template, and error gets role="alert" so screen readers announce it straight away, while the others stay role="status". Loading never times out by itself, because it is waiting for an update.

That update comes through id. When an event arrives with an id that is already on screen, the script refills that toast instead of cloning a new one, then restarts its timer with the new duration:

// 0 or Infinity means stay until dismissed; loading waits for an update.
t._sticky = variant === 'loading' || !(duration > 0) || duration === Infinity;

So the pattern for a slow request is one id used twice:

const id = toast.loading('Uploading report.pdf');
// later
toast.success('Uploaded', { id, description: 'report.pdf is ready.' });

A stack that queues and pauses

The region takes a max prop, 3 by default. A layout() function runs after every change: the newest max toasts are shown and the rest get data-state="queued" with their timers paused. When one closes, the next in line slides in.

Collapsed, older toasts sit behind the newest one, each peeking out by 14px and scaled down 5% per step. Hovering or focusing the region expands the column with an 8px gap and pauses every timer. Pausing keeps the time left, so a toast with one second to go still gets that one second after the pointer leaves:

function pauseTimer(t) {
    if (!t._timer) return;
    clearTimeout(t._timer);
    t._timer = null;
    t._remaining -= Date.now() - t._started;
}

The offsets need measured heights, which Tailwind classes cannot know, so they go in inline styles. I also avoided requestAnimationFrame for the slide-in. It stalls in a background tab, and a toast fired from a tab you are not looking at should still be there when you come back. Reading offsetHeight flushes the start position, so the transition plays without it.

Actions and the helper functions

action takes either { label, event } or { label, href }. With href the button renders as a link. With event, clicking fires a window event of that name with the toast's detail, then closes the toast. That keeps an Undo button free of any callback serialisation:

toast('Message archived', { action: { label: 'Undo', event: 'undo-archive' } });
window.addEventListener('undo-archive', (event) => restore(event.detail));

window.toast and its .success(), .error() style helpers are sugar over the same toast event, and each returns the id. toast.dismiss(id) closes one toast, and toast.dismiss() with no id closes all of them. Since the helpers only dispatch the event, the old CustomEvent('toast', ...) calls behave exactly as before.

The close button changed too. It used to be a bare &times; with a hard-coded "Dismiss" label. It is now an SVG icon with a close-label prop, so the Arabic docs can pass a translated label.

Flashing a toast from a controller

The usage snippet shows the Laravel side. The controller flashes an array to the session and the layout turns it into an event:

return back()->with('toast', ['title' => 'Saved', 'variant' => 'success']);
@if (session('toast'))
    <script>
        window.addEventListener('DOMContentLoaded', () => {
            window.dispatchEvent(new CustomEvent('toast', { detail: @json(session('toast')) }));
        });
    </script>
@endif

Any key from the JavaScript API works in that array, including id, duration and action.

The one breaking change

duration: 0 now means sticky instead of four seconds. If you passed 0 because you wanted the default, leave duration out and you get 4000ms. Infinity is sticky as well. A project that copied the old component and passes 0 will see toasts that no longer close, which is why the change is called out in the 0.6.0 release notes.

Where it is

composer require --dev safi/laralcn-ui
php artisan ui:init
php artisan ui:add toast

Place one <x-ui.toast /> near </body>. If you already have the old toast, ui:add toast asks before overwriting it and can show you the diff first.

Docs: https://laralcn-ui.abdulkadersafi.com/components/toast (Arabic at /ar/components/toast). GitHub: https://github.com/Abdulkader-Safi/LaralCN-UI. MIT licensed.

Next: the Esc bug that every dialog in the library shared, and how the command palette exposed it.

Last updated 1 Dec 2026 · filed under Laravel, blade, javascript, ui, laralcn-ui

Every entry on this project

26 build notes, in order

Including the ones where nothing worked. You are on part 19.

Read the series →

FAQ

Frequently asked questions

Do I need Alpine.js for the toast?

No. The region ships its own inline script, guarded so it runs once per page even if the markup is rendered twice.

How do I turn a loading toast into a success toast?

Pass the same id again. toast.loading() returns an id; call toast.success('Done', { id }) with it and the toast updates in place.

What happens when more toasts arrive than max?

The newest max stay on screen and the rest wait with data-state="queued". Each queued toast slides in when one of the visible ones closes, and its timer only starts then.

Can a toast stay open until the user closes it?

Yes. Pass duration: 0 or Infinity. Before this release 0 fell back to four seconds.

Written by

Abdulkader Safi

Software Engineer

Lead engineer at DSRPT, from Lebanon and based in Kuwait. I write about the tools and bugs from real client work, with the numbers I measured.

About me → GitHub LinkedIn

Need this kind of work done on your project?

Start a project →

Keep reading

More from LaralCN-UI: shadcn-style Blade components and blocks you own

All 26 entries →