v0.0.2
Demos (demo panel, opens in a new tab)

Toast

A short notification at the bottom right of the screen that reports the result of an action and disappears after a few seconds.

zb:toast-container zubazar/ui · free (MIT)

Types

Types: view
BLADE

Send the notification with a toast event and pick its color with type; when omitted, info (dark gray) is used.

blade
<div class="flex flex-wrap gap-2" x-data>
    <zb:button variant="success" x-on:click="$dispatch('toast', { type: 'success', message: 'Product saved.' })">Success</zb:button>
    <zb:button variant="danger" x-on:click="$dispatch('toast', { type: 'error', message: 'The record could not be deleted.' })">Error</zb:button>
    <zb:button variant="warning" x-on:click="$dispatch('toast', { type: 'warning', message: '2 records were skipped due to permissions.' })">Warning</zb:button>
    <zb:button variant="white" x-on:click="$dispatch('toast', { message: 'The import was queued.' })">Info</zb:button>
</div>
PropValues
typesuccess | error (danger) | warning | info
messageText to show

From Livewire

PHP

Send the same event from a Livewire component with dispatch(); the named arguments become the fields of the notification.

php
$this->dispatch('toast', type: 'success', message: 'Product saved.');

$this->dispatch('toast', type: 'error', message: 'The import failed.', link: $reportUrl, linkLabel: 'Details');

Shortcut: WithToasts

PHP

Add the WithToasts trait to the Livewire component; toast() writes the same event more briefly and uses success when no type is given.

php
use Zubazar\Ui\Livewire\Concerns\WithToasts;

class EditProduct extends Component
{
    use WithToasts;

    public function save(): void
    {
        // …
        $this->toast('Product saved.');
        $this->toast('The import failed.', 'error', link: $reportUrl, linkLabel: 'Details');
        $this->toast('The export is ready.', 'info', link: $fileUrl, linkLabel: 'Download', download: true);
    }
}

After a redirect

PHP

When the action redirects to another page, leave the notification in the session: use toastAfterRedirect() in Livewire and Toast::flash() in a controller.

php
// Livewire (WithToasts)
$this->toastAfterRedirect('Product added.');
$this->redirectRoute('products.index', navigate: true);

// Controller
use Zubazar\Ui\Support\Toast;

Toast::flash('Product added.');

return redirect()->route('products.index');

Props

The values the component takes. What are props and slots?

PropTypeDefaultDescription
type—infosuccess · error · warning · info (event field); danger counts as error, case doesn't matter
message— — Notification text
link—nullLink below the message; when given, the notification stays until it is closed
linkLabel—nullLink text
downloadbooleanfalsetrue: the link downloads a file (download attribute); in a JavaScript event, a string gives the file name

Slots: The container takes no props; the fields above are the content of the toast event. The container is an aria-live="polite" region: new notifications are read without interrupting the user.

Installation

The ready-made layouts (zb:layout.sidebar, zb:layout.horizontal, zb:layout.auth) include the container; in your own layout, add zb:toast-container once, before body closes.

blade
<body>
    {{ $slot }}

    <zb:toast-container />
</body>

Advanced

1 example

From JavaScript

JS

The container listens for the event on window. Use $dispatch inside Alpine and CustomEvent in plain JavaScript; the fields are sent in detail.

js
// Alpine: x-on:click="$dispatch('toast', { type: 'success', message: 'Copied.' })"

window.dispatchEvent(new CustomEvent('toast', {
    detail: { type: 'warning', message: 'The connection is slow, retrying.' },
}));

Developer notes

Events, hooks and details
  • A notification closes on its own after 5 seconds; one with a link stays until it is closed, so keyboard and screen reader users can reach the link.
  • The type is read regardless of case and surrounding spaces; danger counts as error, color names such as green, amber and blue map to the matching type, and an unknown type shows as info.
  • On a page with Livewire, a link on the same site opens without a reload (Livewire.navigate); clicking it closes the notification.
  • For a link that downloads a file, pass download: true: the link gets the download attribute and opens as a regular link. From JavaScript you can also give the file name: download: 'report.csv'.
  • The WithToasts methods are protected on purpose: they cannot be called from the browser. In toast(), an unknown type throws an error.
  • A notification left in the session is shown once on the next page; several are queued. toastAfterRedirect() and Toast::flash() take the same arguments as toast().
  • To show the application's own session messages (redirect()->with('status', …)) as toasts, set toast_session_keys in config/zubazar-ui.php, e.g. ['status' => 'success', 'error' => 'error']; it is empty by default.

Events

EventdetailDescription
toast (listens){ type, message, link?, linkLabel?, download? }Each toast event dispatched on window adds a notification. type is success, error, warning or info (danger counts as error; anything else shows as info). From Livewire: $this->dispatch('toast', type: 'success', message: 'Saved.').
Zubazar UI · ZubazarDesigned for people.