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
Send the notification with a toast event and pick its color with type; when omitted, info (dark gray) is used.
<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>
| Prop | Values |
|---|---|
type | success | error (danger) | warning | info |
message | Text to show |
Notification with a link
link and linkLabel add a link below the message; such a notification stays until it is closed.
<zb:button variant="white" x-data x-on:click="$dispatch('toast', { type: 'info', message: 'Import complete: 148 products added.', link: '#', linkLabel: 'Show the report' })">
Notification with a link
</zb:button>
| Prop | Values |
|---|---|
link | Address to open |
linkLabel | Link text |
download | true: a file download link; in JavaScript, a file name works too |
From Livewire
Send the same event from a Livewire component with dispatch(); the named arguments become the fields of the notification.
$this->dispatch('toast', type: 'success', message: 'Product saved.');
$this->dispatch('toast', type: 'error', message: 'The import failed.', link: $reportUrl, linkLabel: 'Details');
Shortcut: WithToasts
Add the WithToasts trait to the Livewire component; toast() writes the same event more briefly and uses success when no type is given.
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
When the action redirects to another page, leave the notification in the session: use toastAfterRedirect() in Livewire and Toast::flash() in a controller.
// 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?
| Prop | Type | Default | Description |
|---|---|---|---|
type | — | info | success · error · warning · info (event field); danger counts as error, case doesn't matter |
message | — | — | Notification text |
link | — | null | Link below the message; when given, the notification stays until it is closed |
linkLabel | — | null | Link text |
download | boolean | false | true: 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.
<body>
{{ $slot }}
<zb:toast-container />
</body>
Advanced
From JavaScript
The container listens for the event on window. Use $dispatch inside Alpine and CustomEvent in plain JavaScript; the fields are sent in detail.
// 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
- 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;
dangercounts aserror, color names such asgreen,amberandbluemap to the matching type, and an unknown type shows asinfo. - 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 thedownloadattribute and opens as a regular link. From JavaScript you can also give the file name:download: 'report.csv'. - The
WithToastsmethods areprotectedon purpose: they cannot be called from the browser. Intoast(), an unknown type throws an error. - A notification left in the session is shown once on the next page; several are queued.
toastAfterRedirect()andToast::flash()take the same arguments astoast(). - To show the application's own session messages (
redirect()->with('status', …)) as toasts, settoast_session_keysinconfig/zubazar-ui.php, e.g.['status' => 'success', 'error' => 'error']; it is empty by default.
Events
| Event | detail | Description |
|---|---|---|
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.'). |