toasts.md 12.9 KB
Newer Older
Mark Otto's avatar
Mark Otto committed
1
2
3
4
5
6
7
8
9
10
---
layout: docs
title: Toasts
description: Push notifications to your visitors with a toast, a lightweight and easily customizable alert message.
group: components
toc: 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.

Johann-S's avatar
Johann-S committed
11
12
13
14
15
## 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).
Mark Otto's avatar
Mark Otto committed
16
- Toasts are opt-in for performance reasons, so **you must initialize them yourself**.
17
- **Please note that you are responsible for positioning toasts.**
Mark Otto's avatar
Mark Otto committed
18
- Toasts will automatically hide if you do not specify `autohide: false`.
Johann-S's avatar
Johann-S committed
19

Mark Otto's avatar
Mark Otto committed
20
21
## Examples

Mark Otto's avatar
Mark Otto committed
22
23
24
25
26
### Basic

To encourage extensible and predictable toasts, we recommend a header and body. Toast headers use `display: flex`, allowing easy alignment of content thanks to our margin and flexbox utilities.

Toasts are as flexible as you need and have very little required markup. At a minimum, we require a single element to contain your "toasted" content and strongly encourage a dismiss button.
Mark Otto's avatar
Mark Otto committed
27
28

{% capture example %}
29
<div class="toast" role="alert" aria-live="assertive" aria-atomic="true">
Mark Otto's avatar
Mark Otto committed
30
  <div class="toast-header">
31
    {% include icons/placeholder.svg width="20" height="20" background="#007aff" class="rounded mr-2" text=" " title=" " %}
Mark Otto's avatar
Mark Otto committed
32
33
    <strong class="mr-auto">Bootstrap</strong>
    <small>11 mins ago</small>
34
35
36
    <button type="button" class="ml-2 mb-1 close" data-dismiss="toast" aria-label="Close">
      <span aria-hidden="true">&times;</span>
    </button>
Mark Otto's avatar
Mark Otto committed
37
38
39
40
41
42
  </div>
  <div class="toast-body">
    Hello, world! This is a toast message.
  </div>
</div>
{% endcapture %}
XhmikosR's avatar
XhmikosR committed
43
{% include example.html content=example class="bg-light" %}
Mark Otto's avatar
Mark Otto committed
44

Mark Otto's avatar
Mark Otto committed
45
46
47
### Translucent

Toasts are slightly translucent, too, so they blend over whatever they might appear over. For browsers that support the `backdrop-filter` CSS property, we'll also attempt to blur the elements under a toast.
Mark Otto's avatar
Mark Otto committed
48
49

{% capture example %}
50
<div class="toast" role="alert" aria-live="assertive" aria-atomic="true">
Mark Otto's avatar
Mark Otto committed
51
  <div class="toast-header">
52
    {% include icons/placeholder.svg width="20" height="20" background="#007aff" class="rounded mr-2" text=" " title=" " %}
Mark Otto's avatar
Mark Otto committed
53
54
    <strong class="mr-auto">Bootstrap</strong>
    <small class="text-muted">11 mins ago</small>
55
56
57
    <button type="button" class="ml-2 mb-1 close" data-dismiss="toast" aria-label="Close">
      <span aria-hidden="true">&times;</span>
    </button>
Mark Otto's avatar
Mark Otto committed
58
59
60
61
62
63
  </div>
  <div class="toast-body">
    Hello, world! This is a toast message.
  </div>
</div>
{% endcapture %}
XhmikosR's avatar
XhmikosR committed
64
{% include example.html content=example class="bg-dark" %}
Mark Otto's avatar
Mark Otto committed
65

Mark Otto's avatar
Mark Otto committed
66
67
### Stacking

sabruss's avatar
sabruss committed
68
When you have multiple toasts, we default to vertically stacking them in a readable manner.
Mark Otto's avatar
Mark Otto committed
69
70

{% capture example %}
71
<div class="toast" role="alert" aria-live="assertive" aria-atomic="true">
Mark Otto's avatar
Mark Otto committed
72
  <div class="toast-header">
73
    {% include icons/placeholder.svg width="20" height="20" background="#007aff" class="rounded mr-2" text=" " title=" " %}
Mark Otto's avatar
Mark Otto committed
74
75
    <strong class="mr-auto">Bootstrap</strong>
    <small class="text-muted">just now</small>
Mark Otto's avatar
Mark Otto committed
76
77
78
    <button type="button" class="ml-2 mb-1 close" data-dismiss="toast" aria-label="Close">
      <span aria-hidden="true">&times;</span>
    </button>
Mark Otto's avatar
Mark Otto committed
79
80
81
82
83
84
  </div>
  <div class="toast-body">
    See? Just like this.
  </div>
</div>

85
<div class="toast" role="alert" aria-live="assertive" aria-atomic="true">
Mark Otto's avatar
Mark Otto committed
86
  <div class="toast-header">
87
    {% include icons/placeholder.svg width="20" height="20" background="#007aff" class="rounded mr-2" text=" " title=" " %}
Mark Otto's avatar
Mark Otto committed
88
89
    <strong class="mr-auto">Bootstrap</strong>
    <small class="text-muted">2 seconds ago</small>
90
91
92
93
94
    <button type="button" class="ml-2 mb-1 close" data-dismiss="toast" aria-label="Close">
      <span aria-hidden="true">&times;</span>
    </button>
  </div>
  <div class="toast-body">
Mark Otto's avatar
Mark Otto committed
95
    Heads up, toasts will stack automatically
96
97
98
  </div>
</div>
{% endcapture %}
XhmikosR's avatar
XhmikosR committed
99
{% include example.html content=example class="bg-light" %}
100

Mark Otto's avatar
Mark Otto committed
101
102
## Placement

Mark Otto's avatar
Mark Otto committed
103
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`.
Mark Otto's avatar
Mark Otto committed
104
105

{% capture example %}
Mark Otto's avatar
Mark Otto committed
106
<div aria-live="polite" aria-atomic="true" style="position: relative; min-height: 200px;">
Mark Otto's avatar
Mark Otto committed
107
108
  <div class="toast" style="position: absolute; top: 0; right: 0;">
    <div class="toast-header">
109
      {% include icons/placeholder.svg width="20" height="20" background="#007aff" class="rounded mr-2" text=" " title=" " %}
Mark Otto's avatar
Mark Otto committed
110
111
      <strong class="mr-auto">Bootstrap</strong>
      <small>11 mins ago</small>
Mark Otto's avatar
Mark Otto committed
112
113
114
      <button type="button" class="ml-2 mb-1 close" data-dismiss="toast" aria-label="Close">
        <span aria-hidden="true">&times;</span>
      </button>
Mark Otto's avatar
Mark Otto committed
115
116
117
118
119
120
121
    </div>
    <div class="toast-body">
      Hello, world! This is a toast message.
    </div>
  </div>
</div>
{% endcapture %}
XhmikosR's avatar
XhmikosR committed
122
{% include example.html content=example class="bg-dark" %}
Mark Otto's avatar
Mark Otto committed
123
124
125
126

For systems that generate more notifications, consider using a wrapping element so they can easily stack.

{% capture example %}
Mark Otto's avatar
Mark Otto committed
127
<div aria-live="polite" aria-atomic="true" style="position: relative; min-height: 200px;">
Mark Otto's avatar
Mark Otto committed
128
129
130
131
  <!-- Position it -->
  <div style="position: absolute; top: 0; right: 0;">

    <!-- Then put toasts within -->
132
    <div class="toast" role="alert" aria-live="assertive" aria-atomic="true">
Mark Otto's avatar
Mark Otto committed
133
      <div class="toast-header">
134
        {% include icons/placeholder.svg width="20" height="20" background="#007aff" class="rounded mr-2" text=" " title=" " %}
Mark Otto's avatar
Mark Otto committed
135
136
        <strong class="mr-auto">Bootstrap</strong>
        <small class="text-muted">just now</small>
Mark Otto's avatar
Mark Otto committed
137
138
139
        <button type="button" class="ml-2 mb-1 close" data-dismiss="toast" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
Mark Otto's avatar
Mark Otto committed
140
141
142
143
144
145
      </div>
      <div class="toast-body">
        See? Just like this.
      </div>
    </div>

146
    <div class="toast" role="alert" aria-live="assertive" aria-atomic="true">
Mark Otto's avatar
Mark Otto committed
147
      <div class="toast-header">
148
        {% include icons/placeholder.svg width="20" height="20" background="#007aff" class="rounded mr-2" text=" " title=" " %}
Mark Otto's avatar
Mark Otto committed
149
150
        <strong class="mr-auto">Bootstrap</strong>
        <small class="text-muted">2 seconds ago</small>
Mark Otto's avatar
Mark Otto committed
151
152
153
        <button type="button" class="ml-2 mb-1 close" data-dismiss="toast" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
Mark Otto's avatar
Mark Otto committed
154
155
156
157
158
159
160
161
      </div>
      <div class="toast-body">
        Heads up, toasts will stack automatically
      </div>
    </div>
  </div>
</div>
{% endcapture %}
XhmikosR's avatar
XhmikosR committed
162
{% include example.html content=example class="bg-dark" %}
Mark Otto's avatar
Mark Otto committed
163

Mark Otto's avatar
Mark Otto committed
164
You can also get fancy with flexbox utilities to align toasts horizontally and/or vertically.
Mark Otto's avatar
Mark Otto committed
165

166
{% capture example %}
Mark Otto's avatar
Mark Otto committed
167
168
<!-- Flexbox container for aligning the toasts -->
<div aria-live="polite" aria-atomic="true" class="d-flex justify-content-center align-items-center" style="min-height: 200px;">
Mark Otto's avatar
Mark Otto committed
169

Mark Otto's avatar
Mark Otto committed
170
171
172
  <!-- Then put toasts within -->
  <div class="toast" role="alert" aria-live="assertive" aria-atomic="true">
    <div class="toast-header">
173
      {% include icons/placeholder.svg width="20" height="20" background="#007aff" class="rounded mr-2" text=" " title=" " %}
Mark Otto's avatar
Mark Otto committed
174
175
176
177
178
179
180
181
      <strong class="mr-auto">Bootstrap</strong>
      <small>11 mins ago</small>
      <button type="button" class="ml-2 mb-1 close" data-dismiss="toast" aria-label="Close">
        <span aria-hidden="true">&times;</span>
      </button>
    </div>
    <div class="toast-body">
      Hello, world! This is a toast message.
Mark Otto's avatar
Mark Otto committed
182
183
184
185
    </div>
  </div>
</div>
{% endcapture %}
XhmikosR's avatar
XhmikosR committed
186
{% include example.html content=example class="bg-dark" %}
Johann-S's avatar
Johann-S committed
187

Mark Otto's avatar
Mark Otto committed
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
## Accessibility

Toasts are intended to be small interruptions to your visitors or users, so to help those with screen readers and similar assistive technologies, you should wrap your toasts in an [`aria-live` region](https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/ARIA_Live_Regions). Changes to live regions (such as injecting/updating a toast component) are automatically announced by screen readers without needing to move the user's focus or otherwise interrupt the user. Additionally, include `aria-atomic="true"` to ensure that the entire toast is always announced as a single (atomic) unit, rather than announcing what was changed (which could lead to problems if you only update part of the toast's content, or if displaying the same toast content at a later point in time). 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.

Note that the live region needs to be present in the markup *before* the toast is generated or updated. If you dynamically generate both at the same time and inject them into the page, they will generally not be announced by assistive technologies.

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](#options) to ensure people have enough time to read the toast.

{% highlight html %}
<div class="toast" role="alert" aria-live="polite" aria-atomic="true" data-delay="10000">
  <div role="alert" aria-live="assertive" aria-atomic="true">...</div>
</div>
{% endhighlight %}

When using `autohide: false`, you must add a close button to allow users to dismiss the toast.

{% capture example %}
<div role="alert" aria-live="assertive" aria-atomic="true" class="toast" data-autohide="false">
  <div class="toast-header">
209
    {% include icons/placeholder.svg width="20" height="20" background="#007aff" class="rounded mr-2" text=" " title=" " %}
Mark Otto's avatar
Mark Otto committed
210
211
212
213
214
215
216
217
218
219
220
    <strong class="mr-auto">Bootstrap</strong>
    <small>11 mins ago</small>
    <button type="button" class="ml-2 mb-1 close" data-dismiss="toast" aria-label="Close">
      <span aria-hidden="true">&times;</span>
    </button>
  </div>
  <div class="toast-body">
    Hello, world! This is a toast message.
  </div>
</div>
{% endcapture %}
XhmikosR's avatar
XhmikosR committed
221
{% include example.html content=example class="bg-light" %}
Mark Otto's avatar
Mark Otto committed
222

Johann-S's avatar
Johann-S committed
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
## 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=""`.

<table class="table table-bordered table-striped">
  <thead>
    <tr>
      <th style="width: 100px;">Name</th>
      <th style="width: 100px;">Type</th>
      <th style="width: 50px;">Default</th>
      <th>Description</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>animation</td>
      <td>boolean</td>
      <td>true</td>
      <td>Apply a CSS fade transition to the toast</td>
    </tr>
    <tr>
      <td>autohide</td>
      <td>boolean</td>
      <td>true</td>
      <td>Auto hide the toast</td>
    </tr>
    <tr>
      <td>delay</td>
261
      <td>number</td>
Johann-S's avatar
Johann-S committed
262
      <td>
263
        <code>500</code>
Johann-S's avatar
Johann-S committed
264
      </td>
265
      <td>Delay hiding the toast (ms)</td>
Johann-S's avatar
Johann-S committed
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
    </tr>
  </tbody>
</table>

### 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

<table class="table table-bordered table-striped">
  <thead>
    <tr>
      <th style="width: 150px;">Event Type</th>
      <th>Description</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>show.bs.toast</td>
      <td>This event fires immediately when the <code>show</code> instance method is called.</td>
    </tr>
    <tr>
      <td>shown.bs.toast</td>
      <td>This event is fired when the toast has been made visible to the user.</td>
    </tr>
    <tr>
      <td>hide.bs.toast</td>
      <td>This event is fired immediately when the <code>hide</code> instance method has been called.</td>
    </tr>
    <tr>
      <td>hidden.bs.toast</td>
      <td>This event is fired when the toast has finished being hidden from the user.</td>
    </tr>
  </tbody>
</table>

{% highlight js %}
$('#myToast').on('hidden.bs.toast', function () {
XhmikosR's avatar
XhmikosR committed
328
  // do something...
Johann-S's avatar
Johann-S committed
329
330
})
{% endhighlight %}