_toast() htmx
Add a Bootstrap 5 toast to an htmx response (HX-Trigger event yf-toast); the response goes on.
_class('htmx')->_toast($text, $extra = [])Options
| Option | Meaning |
|---|---|
$text | Body, shown as text (never HTML); cut at TOAST_MAX_BODY (500). |
title, type | Title (cut at TOAST_MAX_TITLE, 100); success, info, warning, danger... |
autohide, delay | false keeps it until closed; delay in ms. |
Example
Code
$hx = _class('htmx');
if ($hx->_wants_fragment() && ($_GET['hx'] ?? '') === 'toast') {
$hx->_toast('Saved at ' . date('H:i:s') . '.', ['title' => 'htmx', 'type' => 'success']);
$hx->_toast('A second toast in the same header.', ['type' => 'info', 'autohide' => false]);
return $hx->_result('<span id="docs-ref-hx-toast-result">Last answer: ' . date('H:i:s') . '</span>');
}
asset('htmx');
return html()->toast_container(['position' => 'bottom-end'])
. '<button type="button" class="btn btn-success" hx-get="' . _prepare_html(url('/@object/htmx/toast')) . '" hx-vals=\'{"hx": "toast"}\' hx-target="#docs-ref-hx-toast-result" hx-swap="outerHTML">Save through htmx</button> '
. '<span id="docs-ref-hx-toast-result"></span>';Notes
- All toasts of one response travel in one
HX-Triggerheader (it replaces anHX-Triggerset elsewhere); pastTOAST_MAX_COUNT(10) they are dropped. - The
yf_bs5_toastsasset shows them in the first toast_container() of the page, as text.
Bootstrap 3 and 5
Toasts are Bootstrap 5; under Bootstrap 3 the event arrives but no toast is shown.
See also: html/toast, demo:html/bs5_components