1. Types
Each type of config/toasts.php has its colours and its icon (mapping-suggestions is cyan:
admin only). A type added there gets its template, and a button here, at once. An unknown type is shown as text.
The toasts stack in a live region (aria-live="polite"), so screen readers read them when they are done with
what they are reading; a type with 'role' => 'alert' in the config (danger) is announced at once.
The stack is above the modals.
<button type="button" data-tvt-show-toast data-tvt-toast-type="success" data-tvt-toast-message="Your changes are saved.">
Save
</button>
2. A message with markup
data-tvt-toast-target names a hidden [data-tvt-toast-content] block whose HTML becomes the message:
an id, a selector, or next for the block right after the trigger. It is looked up inside the trigger's
[data-tvt-toast-scope] first, so a card repeated on a page finds its own block. The block can set its own
data-tvt-toast-type and data-tvt-toast-icon.
<div data-tvt-toast-scope>
<button type="button" data-tvt-show-toast data-tvt-toast-target="next">Send the offer</button>
<div class="hidden" data-tvt-toast-content data-tvt-toast-type="info" data-tvt-toast-icon="fa-light fa-paper-plane" aria-hidden="true">
<strong>Offer sent!</strong>
<div class="truncate italic">Hotel Riviera Palace, 7 nights</div>
</div>
</div>
3. Add / remove: a toggle
A trigger with an -alt toast (data-tvt-toast-target-alt or -message-alt) is a toggle:
its clicks alternate between the two toasts, and its state is aria-pressed, set to "false" on load
when the markup leaves it out. Its icon becomes the icon of the toast just shown (the heart fills), keeping only the classes
of data-tvt-toast-trigger-icon-preserve.
An item that is already added is rendered with aria-pressed="true" and the filled icon: its first click removes it.
Every click fires tvt:toast-toggle on the trigger, detail: { pressed }, for the code that saves the change.
Click a heart.
<button type="button" aria-label="Favorite" aria-pressed="false"
data-tvt-show-toast
data-tvt-toast-type="favorites"
data-tvt-toast-target="favorite-add-1234"
data-tvt-toast-target-alt="favorite-remove-1234"
data-tvt-toast-trigger-icon-preserve="text-xl transition duration-300">
<i class="fa-regular fa-heart text-red-500 text-xl transition duration-300" aria-hidden="true"></i>
</button>
<div id="favorite-add-1234" class="hidden" data-tvt-toast-content aria-hidden="true">...</div>
<div id="favorite-remove-1234" class="hidden" data-tvt-toast-content data-tvt-toast-icon="fa-regular fa-heart text-red-500" aria-hidden="true">...</div>
trigger.addEventListener('tvt:toast-toggle', (e) => {
saveFavorite(hotelId, e.detail.pressed);
});
4. A toggle's icon for each state
When the trigger's icon should not be the toast's (a "+" that turns into a "−"), data-tvt-toast-trigger-icon-unpressed
and -pressed give it for each state. data-tvt-toast-trigger-icon is a selector for the icon when it is
not the trigger's first <i>.
<button type="button"
data-tvt-show-toast
data-tvt-toast-type="info"
data-tvt-toast-message="Added to the comparison."
data-tvt-toast-message-alt="Removed from the comparison."
data-tvt-toast-icon-alt="fa-light fa-circle-minus"
data-tvt-toast-trigger-icon-preserve="w-4"
data-tvt-toast-trigger-icon-unpressed="fa-light fa-plus"
data-tvt-toast-trigger-icon-pressed="fa-light fa-minus">
<i class="w-4 fa-light fa-plus" aria-hidden="true"></i> Compare
</button>
5. Closing
Each type stays its own time, from config/toasts.php: its 'duration' (ms; 0 = until closed),
else the default 'duration'. Now:
success 3 s,
warning 5 s,
danger until closed,
info 3 s,
text 3 s,
favorites 3 s,
mapping-suggestions 3 s.
A trigger changes it with data-tvt-toast-duration (ms) or data-tvt-toast-auto-dismiss="false" (until its
× is clicked); data-tvt-toast-auto-dismiss="true" gives a type that stays until closed the default time.
The countdown stops while the mouse is over the toast or the focus is in it, and goes on with the time that was left.
A new click on the same trigger replaces its toast.
'duration' => 3000, // the types that set none
'types' => [
'warning' => [ ..., 'duration' => 5000 ],
'danger' => [ ..., 'duration' => 0 ], // until closed
],
<button type="button" data-tvt-show-toast data-tvt-toast-message="This one stays 8 seconds." data-tvt-toast-duration="8000">8 seconds</button>
<button type="button" data-tvt-show-toast data-tvt-toast-message="This one stays until you close it." data-tvt-toast-auto-dismiss="false">Until closed</button>
6. Position
The toasts stack in a corner or in the middle of the top or bottom edge, and slide in from that edge; at the bottom the newest is
the lowest one. config/toasts.php sets the position for the whole site: 'position', and
'position_md' from 768px up when phones need another one (at the bottom, under the thumb). A page changes it with
tvtSetToastPosition(position, positionMd). The buttons below do that, then show a toast.
'position' => 'bottom-center', // top-right, top-left, top-center, bottom-right, bottom-left, bottom-center
'position_md' => 'top-right', // from 768px; null = 'position' everywhere
tvtSetToastPosition('bottom-center', 'top-right');
tvtSetToastPosition('top-center'); // on every width
7. From a script
tvtShowToast(content, options) shows a toast and returns its element. The content is text (shown as is) or
{ html }. Options: type, iconClasses, durationMs, autoDismiss.
tvtHideToast(toast) closes one toast (that element, or a static toast); tvtHideAllToasts()
closes the whole stack, e.g. before showing the errors of a form that is sent again.
The page that calls them includes <x-assets.tvt-toast /> itself.
tvtShowToast('Your changes are saved.', { type: 'success' });
const toast = tvtShowToast(
{ html: '<strong>Booking saved</strong><br>Reference TVT-48213' },
{ type: 'info', iconClasses: 'fa-light fa-ticket', autoDismiss: false }
);
tvtHideToast(toast);
tvtHideAllToasts();
8. A toast in the page (Blade)
<x-tools.tvt-toast> puts a toast in the markup, with the message in its slot: type (default
text) and an optional icon. It loads the module itself, which slides it in and binds its ×.
It stays where it is written and does not close by itself.
<x-tools.tvt-toast type="warning">
<strong>Prices may change</strong>
<div>The availability is checked again when you book.</div>
</x-tools.tvt-toast>
Attributes, events, functions
On the trigger
data-tvt-show-toast: click, or Enter / Space on a non-button-type,-message,-target,-icon, each with an-alt-duration,-auto-dismiss-trigger-icon,-trigger-icon-preserve,-trigger-icon-pressed/-unpressedaria-pressedon a toggle
Around it
[data-tvt-toast-content], with its own-type/-icon[data-tvt-toast-scope]: where a target is looked up first[data-tvt-toast-container]: created on the first toast
Events, functions
tvt:toast-toggle{ pressed }, on the triggertvtShowToast(content, options)→ the toasttvtHideToast(toast),tvtHideAllToasts()tvtSetToastPosition(position, positionMd)tvtInitToasts(root): static toasts and toggles added after load