Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
---
2
layout: docs
3
title: Toasts
4
description: Push notifications to your visitors with a toast, a lightweight and easily customizable alert message.
5
group: components
6
toc: true
7
---
8
 
9
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.
10
 
11
## Overview
12
 
13
Things to know when using the toast plugin:
14
 
15
- If you're building our JavaScript from source, it [requires `util.js`]({{< docsref "/getting-started/javascript#util" >}}).
16
- Toasts are opt-in for performance reasons, so **you must initialize them yourself**.
17
- **Please note that you are responsible for positioning toasts.**
18
- Toasts will automatically hide if you do not specify `autohide: false`.
19
 
20
{{< callout info >}}
21
{{< partial "callout-info-prefersreducedmotion.md" >}}
22
{{< /callout >}}
23
 
24
## Examples
25
 
26
### Basic
27
 
28
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.
29
 
30
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.
31
 
32
{{< example class="bg-light" >}}
33
<div class="toast" role="alert" aria-live="assertive" aria-atomic="true">
34
  <div class="toast-header">
35
    {{< placeholder width="20" height="20" background="#007aff" class="rounded mr-2" text=" " title=" " >}}
36
    <strong class="mr-auto">Bootstrap</strong>
37
    <small>11 mins ago</small>
38
    <button type="button" class="ml-2 mb-1 close" data-dismiss="toast" aria-label="Close">
39
      <span aria-hidden="true">&times;</span>
40
    </button>
41
  </div>
42
  <div class="toast-body">
43
    Hello, world! This is a toast message.
44
  </div>
45
</div>
46
{{< /example >}}
47
 
48
### Live
49
 
50
Click the button below to show a toast (positioned with our utilities in the lower right corner) that has been hidden by default with `.hide`.
51
 
52
<div class="position-fixed bottom-0 right-0 p-3" style="z-index: 5; right: 0; bottom: 0;">
53
  <div id="liveToast" class="toast hide" role="alert" aria-live="assertive" aria-atomic="true" data-delay="2000">
54
    <div class="toast-header">
55
      {{< placeholder width="20" height="20" background="#007aff" class="rounded mr-2" text="false" title="false" >}}
56
      <strong class="mr-auto">Bootstrap</strong>
57
      <small>11 mins ago</small>
58
      <button type="button" class="ml-2 mb-1 close" data-dismiss="toast" aria-label="Close">
59
        <span aria-hidden="true">&times;</span>
60
      </button>
61
    </div>
62
    <div class="toast-body">
63
      Hello, world! This is a toast message.
64
    </div>
65
  </div>
66
</div>
67
 
68
<div class="bd-example">
69
  <button type="button" class="btn btn-primary" id="liveToastBtn">Show live toast</button>
70
</div>
71
 
72
```html
73
<button type="button" class="btn btn-primary" id="liveToastBtn">Show live toast</button>
74
 
75
<div class="position-fixed bottom-0 right-0 p-3" style="z-index: 5; right: 0; bottom: 0;">
76
  <div id="liveToast" class="toast hide" role="alert" aria-live="assertive" aria-atomic="true" data-delay="2000">
77
    <div class="toast-header">
78
      <img src="..." class="rounded mr-2" alt="...">
79
      <strong class="mr-auto">Bootstrap</strong>
80
      <small>11 mins ago</small>
81
      <button type="button" class="ml-2 mb-1 close" data-dismiss="toast" aria-label="Close">
82
        <span aria-hidden="true">&times;</span>
83
      </button>
84
    </div>
85
    <div class="toast-body">
86
      Hello, world! This is a toast message.
87
    </div>
88
  </div>
89
</div>
90
```
91
 
92
### Translucent
93
 
94
Toasts are slightly translucent to blend in with what's below them.
95
 
96
{{< example class="bg-dark" >}}
97
<div class="toast" role="alert" aria-live="assertive" aria-atomic="true">
98
  <div class="toast-header">
99
    {{< placeholder width="20" height="20" background="#007aff" class="rounded mr-2" text=" " title=" " >}}
100
    <strong class="mr-auto">Bootstrap</strong>
101
    <small class="text-muted">11 mins ago</small>
102
    <button type="button" class="ml-2 mb-1 close" data-dismiss="toast" aria-label="Close">
103
      <span aria-hidden="true">&times;</span>
104
    </button>
105
  </div>
106
  <div class="toast-body">
107
    Hello, world! This is a toast message.
108
  </div>
109
</div>
110
{{< /example >}}
111
 
112
### Stacking
113
 
114
When you have multiple toasts, we default to vertically stacking them in a readable manner.
115
 
116
{{< example class="bg-light" >}}
117
<div class="toast" role="alert" aria-live="assertive" aria-atomic="true">
118
  <div class="toast-header">
119
    {{< placeholder width="20" height="20" background="#007aff" class="rounded mr-2" text=" " title=" " >}}
120
    <strong class="mr-auto">Bootstrap</strong>
121
    <small class="text-muted">just now</small>
122
    <button type="button" class="ml-2 mb-1 close" data-dismiss="toast" aria-label="Close">
123
      <span aria-hidden="true">&times;</span>
124
    </button>
125
  </div>
126
  <div class="toast-body">
127
    See? Just like this.
128
  </div>
129
</div>
130
 
131
<div class="toast" role="alert" aria-live="assertive" aria-atomic="true">
132
  <div class="toast-header">
133
    {{< placeholder width="20" height="20" background="#007aff" class="rounded mr-2" text=" " title=" " >}}
134
    <strong class="mr-auto">Bootstrap</strong>
135
    <small class="text-muted">2 seconds ago</small>
136
    <button type="button" class="ml-2 mb-1 close" data-dismiss="toast" aria-label="Close">
137
      <span aria-hidden="true">&times;</span>
138
    </button>
139
  </div>
140
  <div class="toast-body">
141
    Heads up, toasts will stack automatically
142
  </div>
143
</div>
144
{{< /example >}}
145
 
146
## Placement
147
 
148
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`.
149
 
150
{{< example class="bg-dark" >}}
151
<div aria-live="polite" aria-atomic="true" style="position: relative; min-height: 200px;">
152
  <div class="toast" style="position: absolute; top: 0; right: 0;">
153
    <div class="toast-header">
154
      {{< placeholder width="20" height="20" background="#007aff" class="rounded mr-2" text=" " title=" " >}}
155
      <strong class="mr-auto">Bootstrap</strong>
156
      <small>11 mins ago</small>
157
      <button type="button" class="ml-2 mb-1 close" data-dismiss="toast" aria-label="Close">
158
        <span aria-hidden="true">&times;</span>
159
      </button>
160
    </div>
161
    <div class="toast-body">
162
      Hello, world! This is a toast message.
163
    </div>
164
  </div>
165
</div>
166
{{< /example >}}
167
 
168
For systems that generate more notifications, consider using a wrapping element so they can easily stack.
169
 
170
{{< example class="bg-dark" >}}
171
<div aria-live="polite" aria-atomic="true" style="position: relative; min-height: 200px;">
172
  <!-- Position it -->
173
  <div style="position: absolute; top: 0; right: 0;">
174
 
175
    <!-- Then put toasts within -->
176
    <div class="toast" role="alert" aria-live="assertive" aria-atomic="true">
177
      <div class="toast-header">
178
        {{< placeholder width="20" height="20" background="#007aff" class="rounded mr-2" text=" " title=" " >}}
179
        <strong class="mr-auto">Bootstrap</strong>
180
        <small class="text-muted">just now</small>
181
        <button type="button" class="ml-2 mb-1 close" data-dismiss="toast" aria-label="Close">
182
          <span aria-hidden="true">&times;</span>
183
        </button>
184
      </div>
185
      <div class="toast-body">
186
        See? Just like this.
187
      </div>
188
    </div>
189
 
190
    <div class="toast" role="alert" aria-live="assertive" aria-atomic="true">
191
      <div class="toast-header">
192
        {{< placeholder width="20" height="20" background="#007aff" class="rounded mr-2" text=" " title=" " >}}
193
        <strong class="mr-auto">Bootstrap</strong>
194
        <small class="text-muted">2 seconds ago</small>
195
        <button type="button" class="ml-2 mb-1 close" data-dismiss="toast" aria-label="Close">
196
          <span aria-hidden="true">&times;</span>
197
        </button>
198
      </div>
199
      <div class="toast-body">
200
        Heads up, toasts will stack automatically
201
      </div>
202
    </div>
203
  </div>
204
</div>
205
{{< /example >}}
206
 
207
You can also get fancy with flexbox utilities to align toasts horizontally and/or vertically.
208
 
209
{{< example class="bg-dark" >}}
210
<!-- Flexbox container for aligning the toasts -->
211
<div aria-live="polite" aria-atomic="true" class="d-flex justify-content-center align-items-center" style="height: 200px;">
212
 
213
  <!-- Then put toasts within -->
214
  <div class="toast" role="alert" aria-live="assertive" aria-atomic="true">
215
    <div class="toast-header">
216
      {{< placeholder width="20" height="20" background="#007aff" class="rounded mr-2" text=" " title=" " >}}
217
      <strong class="mr-auto">Bootstrap</strong>
218
      <small>11 mins ago</small>
219
      <button type="button" class="ml-2 mb-1 close" data-dismiss="toast" aria-label="Close">
220
        <span aria-hidden="true">&times;</span>
221
      </button>
222
    </div>
223
    <div class="toast-body">
224
      Hello, world! This is a toast message.
225
    </div>
226
  </div>
227
</div>
228
{{< /example >}}
229
 
230
## Accessibility
231
 
232
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 just 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]({{< docsref "/components/alerts" >}}) instead of toast.
233
 
234
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.
235
 
236
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.
237
 
238
As the content you're displaying changes, be sure to update the [`delay` timeout](#options) so that users have enough time to read the toast.
239
 
240
```html
241
<div class="toast" role="alert" aria-live="polite" aria-atomic="true" data-delay="10000">
242
  <div role="alert" aria-live="assertive" aria-atomic="true">...</div>
243
</div>
244
```
245
 
246
When using `autohide: false`, you must add a close button to allow users to dismiss the toast.
247
 
248
{{< example class="bg-light" >}}
249
<div role="alert" aria-live="assertive" aria-atomic="true" class="toast" data-autohide="false">
250
  <div class="toast-header">
251
    {{< placeholder width="20" height="20" background="#007aff" class="rounded mr-2" text=" " title=" " >}}
252
    <strong class="mr-auto">Bootstrap</strong>
253
    <small>11 mins ago</small>
254
    <button type="button" class="ml-2 mb-1 close" data-dismiss="toast" aria-label="Close">
255
      <span aria-hidden="true">&times;</span>
256
    </button>
257
  </div>
258
  <div class="toast-body">
259
    Hello, world! This is a toast message.
260
  </div>
261
</div>
262
{{< /example >}}
263
 
264
While technically it's possible to add focusable/actionable controls (such as additional buttons or links) in your toast, you should avoid doing this for autohiding toasts. Even if you give the toast a long [`delay` timeout](#options), keyboard and assistive technology users may find it difficult to reach the toast in time to take action (since toasts don't receive focus when they are displayed). If you absolutely must have further controls, we recommend using a toast with `autohide: false`.
265
 
266
## JavaScript behavior
267
 
268
### Usage
269
 
270
Initialize toasts via JavaScript:
271
 
272
```js
273
$('.toast').toast(option)
274
```
275
 
276
### Options
277
 
278
Options can be passed via data attributes or JavaScript. For data attributes, append the option name to `data-`, as in `data-animation=""`.
279
 
280
<table class="table table-bordered table-striped">
281
  <thead>
282
    <tr>
283
      <th style="width: 100px;">Name</th>
284
      <th style="width: 100px;">Type</th>
285
      <th style="width: 50px;">Default</th>
286
      <th>Description</th>
287
    </tr>
288
  </thead>
289
  <tbody>
290
    <tr>
291
      <td>animation</td>
292
      <td>boolean</td>
293
      <td>true</td>
294
      <td>Apply a CSS fade transition to the toast</td>
295
    </tr>
296
    <tr>
297
      <td>autohide</td>
298
      <td>boolean</td>
299
      <td>true</td>
300
      <td>Auto hide the toast</td>
301
    </tr>
302
    <tr>
303
      <td>delay</td>
304
      <td>number</td>
305
      <td>
306
        <code>500</code>
307
      </td>
308
      <td>Delay hiding the toast (ms)</td>
309
    </tr>
310
  </tbody>
311
</table>
312
 
313
### Methods
314
 
315
{{< callout danger >}}
316
{{< partial "callout-danger-async-methods.md" >}}
317
{{< /callout >}}
318
 
319
#### `$().toast(options)`
320
 
321
Attaches a toast handler to an element collection.
322
 
323
#### `.toast('show')`
324
 
325
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).
326
You have to manually call this method, instead your toast won't show.
327
 
328
```js
329
$('#element').toast('show')
330
```
331
 
332
#### `.toast('hide')`
333
 
334
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`.
335
 
336
```js
337
$('#element').toast('hide')
338
```
339
 
340
#### `.toast('dispose')`
341
 
342
Hides an element's toast. Your toast will remain on the DOM but won't show anymore.
343
 
344
```js
345
$('#element').toast('dispose')
346
```
347
 
348
### Events
349
 
350
<table class="table table-bordered table-striped">
351
  <thead>
352
    <tr>
353
      <th style="width: 150px;">Event Type</th>
354
      <th>Description</th>
355
    </tr>
356
  </thead>
357
  <tbody>
358
    <tr>
359
      <td>show.bs.toast</td>
360
      <td>This event fires immediately when the <code>show</code> instance method is called.</td>
361
    </tr>
362
    <tr>
363
      <td>shown.bs.toast</td>
364
      <td>This event is fired when the toast has been made visible to the user.</td>
365
    </tr>
366
    <tr>
367
      <td>hide.bs.toast</td>
368
      <td>This event is fired immediately when the <code>hide</code> instance method has been called.</td>
369
    </tr>
370
    <tr>
371
      <td>hidden.bs.toast</td>
372
      <td>This event is fired when the toast has finished being hidden from the user.</td>
373
    </tr>
374
  </tbody>
375
</table>
376
 
377
```js
378
$('#myToast').on('hidden.bs.toast', function () {
379
  // do something...
380
})
381
```