Also remove close button everywhere just let it when autohide is set to false
11 KiB
layout | title | description | group | toc |
---|---|---|---|---|
docs | Toasts | Push notifications to your visitors with a toast, a lightweight and easily customizable alert message. | components | true |
Toasts are lightweight notifications designed to mimic the push notifications that have been popularized by mobile and desktop operating systems. They're built with flexbox, so they're easy to align and position.
Overview
Things to know when using the toast plugin:
- If you're building our JavaScript from source, it [requires
util.js
]({{ site.baseurl }}/docs/{{ site.docs_version }}/getting-started/javascript/#util). - Toast are opt-in for performance reasons, so you must initialize them yourself.
- Toast will auto hide if you do not specify
autohide: false
Got all that? Great, let's see how they work with some examples.
Examples
A basic toast can include a header (though it doesn't strictly need one) with whatever contents you like. The header is also display: flex
, so .mr-auto
and .ml-auto
can be used for easy pushing of content, as well as all our flexbox utilities.
They're slightly translucent, too, so they blend over whatever they might appear over. For browsers that support backdrop-filter
, we'll also attempt to blur the elements under a toast.
Plus, they'll easily stack.
Accessibility
Toasts are intended to be small interruptions to your visitors or users, so to help those on screen readers, you should wrap your toasts in an aria-live
region. This allows screen readers the ability to see suggested interruptions without any visual cues. If the information needed is important for the process, e.g. for a list of errors in a form, then use the [alert component]({{ site.baseurl }}/docs/{{ site.docs_version }}/components/alerts/) instead of toast.
You also need to adapt the role
and aria-live
level depending on the content. If it's an important message like an error, use role="alert" aria-live="assertive"
, otherwise use role="status" aria-live="polite"
attributes.
As the content you're displaying changes, be sure to update the delay
timeout to ensure people have enough time to read the toast.
{% highlight html %}
When using autohide: false
, you must add a close button to allow users to dismiss the toast.
Placement
Place toasts with custom CSS as you need them. The top right is often used for notifications, as is the top middle. If you're only ever going to show one toast at a time, put the positioning styles right on the .toast.
For systems that generate more notifications, consider using a wrapping element so they can easily stack.
<!-- Then put toasts within -->
<div class="toast" role="alert" aria-live="assertive" aria-atomic="true">
<div class="toast-header">
<img class="rounded mr-2" data-src="holder.js/20x20?size=1&text=.&bg=#007aff" alt="">
<strong class="mr-auto">Bootstrap</strong>
<small class="text-muted">just now</small>
</div>
<div class="toast-body">
See? Just like this.
</div>
</div>
<div class="toast" role="alert" aria-live="assertive" aria-atomic="true">
<div class="toast-header">
<img class="rounded mr-2" data-src="holder.js/20x20?size=1&text=.&bg=#007aff" alt="">
<strong class="mr-auto">Bootstrap</strong>
<small class="text-muted">2 seconds ago</small>
</div>
<div class="toast-body">
Heads up, toasts will stack automatically
</div>
</div>
You can also get fancy with flexbox utilities.
<!-- Then put toasts within -->
<div class="toast" role="alert" aria-live="assertive" aria-atomic="true">
<div class="toast-header">
<img class="rounded mr-2" data-src="holder.js/20x20?size=1&text=.&bg=#007aff" alt="">
<strong class="mr-auto">Bootstrap</strong>
<small>11 mins ago</small>
</div>
<div class="toast-body">
Hello, world! This is a toast message.
</div>
</div>
JavaScript behavior
Usage
Initialize toasts via JavaScript:
{% highlight js %} $('.toast').toast(option) {% endhighlight %}
Options
Options can be passed via data attributes or JavaScript. For data attributes, append the option name to data-
, as in data-animation=""
.
Name | Type | Default | Description |
---|---|---|---|
animation | boolean | true | Apply a CSS fade transition to the toast |
autohide | boolean | true | Auto hide the toast |
delay | number |
500
|
Delay hiding the toast (ms) |
Methods
{% include callout-danger-async-methods.md %}
$().toast(options)
Attaches a toast handler to an element collection.
.toast('show')
Reveals an element's toast. Returns to the caller before the toast has actually been shown (i.e. before the shown.bs.toast
event occurs).
You have to manually call this method, instead your toast won't show.
{% highlight js %}$('#element').toast('show'){% endhighlight %}
.toast('hide')
Hides an element's toast. Returns to the caller before the toast has actually been hidden (i.e. before the hidden.bs.toast
event occurs). You have to manually call this method if you made autohide
to false
.
{% highlight js %}$('#element').toast('hide'){% endhighlight %}
.toast('dispose')
Hides an element's toast. Your toast will remain on the DOM but won't show anymore.
{% highlight js %}$('#element').toast('dispose'){% endhighlight %}
Events
Event Type | Description |
---|---|
show.bs.toast | This event fires immediately when the show instance method is called. |
shown.bs.toast | This event is fired when the toast has been made visible to the user. |
hide.bs.toast | This event is fired immediately when the hide instance method has been called. |
hidden.bs.toast | This event is fired when the toast has finished being hidden from the user. |
{% highlight js %} $('#myToast').on('hidden.bs.toast', function () { // do something… }) {% endhighlight %}