Toast
A short message that appears in the corner and goes away on its own.
Default
{var $url = $page->url()}
<div class="flex flex-wrap gap-2">
{ui button,
title: 'Save',
class: 'button-outline',
attrs: [
'hx-get' => "{$url}:toast?variant=success",
'hx-swap' => 'none',
],
/}
{ui button,
title: 'Check for updates',
class: 'button-outline',
attrs: [
'hx-get' => "{$url}:toast?variant=info",
'hx-swap' => 'none',
],
/}
{ui button,
title: 'Fail',
class: 'button-outline',
attrs: [
'hx-get' => "{$url}:toast?variant=destructive",
'hx-swap' => 'none',
],
/}
</div>
A toast is a fragment the server adds to a fixed region of the layout, the same way a modal is filled in. The layout holds one <div id="toasts" class="toasts" role="status"></div>. Because the region is already on the page and is a live region, a screen reader announces each toast as it arrives.
A toast is usually the answer to an action, so any response can carry one. Wrap it in a {response} that targets the region with a selector and adds to the end of it. A response is never part of the page: named, it renders only when its own :block is requested, and without a name it rides along in whichever block's response renders it, such as a save that also refreshes a form:
{response 'toast', target: '#toasts', swap: 'beforeend'}
{component toast,
title: 'Saved',
class: 'toast-success',
icon: 'circle-check',
/}
{/response}
{* inside another block, sent with its response *}
{response target: '#toasts', swap: 'beforeend'}
{component toast, title: 'Saved' /}
{/response}
A POST handler that saves can return the same markup, so a form shows its result without a reload.
Timing and closing
The shared ui.js removes a toast after duration milliseconds, 5000 by default. duration: 0 keeps it until it is closed. The close button is always there, and several toasts stack in the order they arrive.
The class toast-success, toast-warning, toast-info or toast-destructive colours the border and the icon, and the text stays the normal colour so it reads in both themes.