Toast
A toast is a small message shown for a few seconds after an action, for feedback that doesn't need a response.
Default markup
The default toast has a header with the sender and the time, a body with the message, and a close button.
<div class="toast show" role="status" aria-live="polite" aria-atomic="true" data-bs-autohide="false" data-bs-toggle="toast">
<div class="toast-header">
<span class="avatar avatar-xs me-2" style="background-image: url(/static/avatars/018f.jpg)"></span>
<strong class="me-auto">Mallory Hulme</strong>
<small>11 mins ago</small>
<button type="button" class="ms-2 btn-close" data-bs-dismiss="toast" aria-label="Close"></button>
</div>
<div class="toast-body">Hello, world! This is a toast message.</div>
</div>Translucent
Toasts are translucent, so the page shows through them. In browsers that support backdrop-filter, the content under a toast is blurred.
<div class="toast show" role="status" aria-live="polite" aria-atomic="true" data-bs-autohide="false" data-bs-toggle="toast">
<div class="toast-header">
<span class="avatar avatar-xs me-2" style="background-image: url(/static/avatars/029m.jpg)"></span>
<strong class="me-auto">Mallory Hulme</strong>
<small>11 mins ago</small>
<button type="button" class="ms-2 btn-close" data-bs-dismiss="toast" aria-label="Close"></button>
</div>
<div class="toast-body">Hello, world! This is a toast message.</div>
</div>Stacking toasts
Stack multiple toasts together by putting them within one .toast-container.
<div class="toast-container">
<div class="toast show" role="status" aria-live="polite" aria-atomic="true" data-bs-autohide="false" data-bs-toggle="toast">
<div class="toast-header">
<span class="avatar avatar-xs me-2" style="background-image: url(/static/avatars/008m.jpg)"></span>
<strong class="me-auto">Dunn Slane</strong>
<small>11 mins ago</small>
<button type="button" class="ms-2 btn-close" data-bs-dismiss="toast" aria-label="Close"></button>
</div>
<div class="toast-body">Hello, world! This is a toast message.</div>
</div>
<div class="toast show" role="status" aria-live="polite" aria-atomic="true" data-bs-autohide="false" data-bs-toggle="toast">
<div class="toast-header">
<span class="avatar avatar-xs me-2" style="background-image: url(/static/avatars/020m.jpg)"></span>
<strong class="me-auto">Mallory Hulme</strong>
<small>7 mins ago</small>
<button type="button" class="ms-2 btn-close" data-bs-dismiss="toast" aria-label="Close"></button>
</div>
<div class="toast-body">This is another toast message.</div>
</div>
</div>JavaScript
Tabler binds every element with data-bs-toggle="toast" to show the toast from its data-bs-target on click. This is the code that runs:
document.addEventListener('click', (event: MouseEvent) => {
const trigger = event.target instanceof Element ? event.target.closest<HTMLElement>(SELECTOR_DATA_TOGGLE) : null
if (!trigger) {
return
}
if (['A', 'AREA'].includes(trigger.tagName)) {
event.preventDefault()
}
if (isDisabled(trigger)) {
return
}
const toastEl = SelectorEngine.getElementFromSelector(trigger)
if (toastEl) {
void (Toast.getOrCreateInstance(toastEl) as InstanceType<typeof Toast>).show()
}
})Promise return values
show() and hide() return a Promise that resolves after the transition ends, right after shown.bs.toast or hidden.bs.toast fires. Use it to run code after the toast has finished opening or closing:
const toast = tabler.Toast.getOrCreateInstance(el);
await toast.show();
console.log('Toast is visible');
The Promise never rejects. It also resolves, without doing anything, when the call has no effect: hide() is called on a toast that is not shown, or a listener called preventDefault() on show.bs.toast or hide.bs.toast. To act only when the toast has changed state, listen for shown.bs.toast or hidden.bs.toast instead.
Accessibility
- Use
role="status"witharia-live="polite"for most toasts, such as a success message. The reader announces it when it is idle. - Use
role="alert"witharia-live="assertive"only for urgent toasts, such as an error. Assertive interrupts the reader, so keep it for things that matter. - The live region has to be in the DOM before the message arrives. Render an empty toast container on page load and fill it, rather than inserting the whole toast at the moment of the event.
- Toasts hide themselves after
data-bs-delay. That is too fast to read for many people - setdata-bs-autohide="false"for anything the user has to act on. - A toast does not take focus. Never put the only copy of an action inside one.
- Label the close button with
aria-label="Close".
Class reference
Every class this component ships, grouped by what it changes. A name in braces stands for a family — {color} is any base color, {breakpoint} any responsive step.
toast | component | Container element |
toast-container | part | Positions the toasts on the page |
toast-header | part | Title bar with the close button |
toast-body | part | Message area |
toast-{color} | color | Any base color, for example toast-success or toast-azure |
CSS
Variables
The component uses local CSS variables for real-time customization. Values for the CSS variables are set via Sass, so Sass customization is still supported, too.
On .toast. Source: core/scss/bootstrap/_toasts.scss.
--toast-zindex: #{$zindex-toast};
--toast-padding-x: #{$toast-padding-x};
--toast-padding-y: #{$toast-padding-y};
--toast-spacing: #{$toast-spacing};
--toast-max-width: #{$toast-max-width};
@include rfs($toast-font-size, --toast-font-size);
--toast-color: #{$toast-color};
--toast-bg: #{$toast-background-color};
--toast-border-width: #{$toast-border-width};
--toast-border-color: #{$toast-border-color};
--toast-border-radius: #{$toast-border-radius};
--toast-box-shadow: #{$toast-box-shadow};
--toast-header-color: #{$toast-header-color};
--toast-header-bg: #{$toast-header-background-color};
--toast-header-border-color: #{$toast-header-border-color};Sass variables
Override these variables before importing Tabler to change the defaults.
Source: core/scss/_variables.scss.
$toast-max-width: 350px;
$toast-padding-x: 0.75rem;
$toast-padding-y: 0.5rem;
$toast-font-size: 0.875rem;
$toast-color: null;
$toast-background-color: var(--bg-surface);
$toast-border-width: var(--border-width);
$toast-border-color: var(--border-color);
$toast-border-radius: var(--border-radius);
$toast-box-shadow: var(--box-shadow);
$toast-spacing: $container-padding-x;
$toast-header-color: var(--secondary-color);
$toast-header-background-color: color-transparent(var(--body-bg), 0.85);
$toast-header-border-color: $toast-border-color;
