1 |
efrain |
1 |
---
|
|
|
2 |
layout: docs
|
|
|
3 |
title: "Toast"
|
|
|
4 |
date: 2021-12-09T14:48:00+08:00
|
|
|
5 |
draft: false
|
|
|
6 |
tags:
|
|
|
7 |
- MDL-66828
|
|
|
8 |
- MDL-67074
|
|
|
9 |
- MDL-72544
|
|
|
10 |
- 3.8
|
|
|
11 |
---
|
|
|
12 |
|
|
|
13 |
## How it works
|
|
|
14 |
|
|
|
15 |
Toasts are lightweight notifications designed to mimic push notifications.
|
|
|
16 |
Moodle toasts are based upon core the Bootstrap notification feature, but with a Moodle Javascript module wrapper.
|
|
|
17 |
|
|
|
18 |
## Source files
|
|
|
19 |
|
|
|
20 |
* `lib/amd/src/toast.js` ({{< jsdoc module="core/toast" >}})
|
|
|
21 |
* `lib/templates/local/toast/message.mustache`
|
|
|
22 |
|
|
|
23 |
## Examples
|
|
|
24 |
|
|
|
25 |
Toasts can only be applied from JavaScript, and the most basic form just takes the message to be displayed.
|
|
|
26 |
|
|
|
27 |
### Displaying a simple message
|
|
|
28 |
|
|
|
29 |
{{< example >}}
|
|
|
30 |
<button type="button" class="btn btn-info" data-example-name="basic">Basic example</button>
|
|
|
31 |
|
|
|
32 |
{{#js}}
|
|
|
33 |
require(['core/toast'], Toast => {
|
|
|
34 |
const button = document.querySelector("[data-example-name='basic']")
|
|
|
35 |
button.addEventListener('click', () => {
|
|
|
36 |
Toast.add('This is the message for the toast');
|
|
|
37 |
});
|
|
|
38 |
});
|
|
|
39 |
{{/js}}
|
|
|
40 |
{{< /example >}}
|
|
|
41 |
|
|
|
42 |
### Applying semantic styles
|
|
|
43 |
|
|
|
44 |
The standard semantic Bootstrap styles can be applied.
|
|
|
45 |
|
|
|
46 |
{{< example >}}
|
|
|
47 |
<button type="button" class="btn btn-success" data-example-name="semantic" data-type="success">Success</button>
|
|
|
48 |
<button type="button" class="btn btn-danger" data-example-name="semantic" data-type="danger">Danger</button>
|
|
|
49 |
<button type="button" class="btn btn-warning" data-example-name="semantic" data-type="warning">Warning</button>
|
|
|
50 |
<button type="button" class="btn btn-info" data-example-name="semantic" data-type="info">Info</button>
|
|
|
51 |
|
|
|
52 |
{{#js}}
|
|
|
53 |
require(['core/toast'], Toast => {
|
|
|
54 |
const container = document.querySelector("[data-example-name='semantic']").parentNode;
|
|
|
55 |
container.addEventListener('click', e => {
|
|
|
56 |
if (!e.target.closest('[data-type]')) {
|
|
|
57 |
return;
|
|
|
58 |
}
|
|
|
59 |
Toast.add(`This toast will be displayed with the ${e.target.dataset.type} type.`, {
|
|
|
60 |
type: e.target.dataset.type,
|
|
|
61 |
});
|
|
|
62 |
});
|
|
|
63 |
});
|
|
|
64 |
{{/js}}
|
|
|
65 |
{{< /example >}}
|
|
|
66 |
|
|
|
67 |
### Auto-hide, and close buttons
|
|
|
68 |
|
|
|
69 |
The standard behaviour of the toast is to auto-hide after a short period which
|
|
|
70 |
can be configured or disabled. A close button can also be displayed, which is
|
|
|
71 |
recommended when a longer period is used.
|
|
|
72 |
|
|
|
73 |
| Name | Description |
|
|
|
74 |
| ------------- | ----------------------------------------------------------------------------- |
|
|
|
75 |
| `delay` | An auto-hide delay can be configured by providing a millisecond setting |
|
|
|
76 |
| `autohide` | The auto-hide can be entirely disabled using this boolean setting |
|
|
|
77 |
| `closeButton` | The presence of the close button can be controlled using this boolean setting |
|
|
|
78 |
|
|
|
79 |
{{< example >}}
|
|
|
80 |
<button type="button" class="btn btn-primary" data-example-name="autohide-long">Auto-hide long</button>
|
|
|
81 |
<button type="button" class="btn btn-primary" data-example-name="autohide-disabled">Auto-hide disabled</button>
|
|
|
82 |
|
|
|
83 |
{{#js}}
|
|
|
84 |
require(['core/toast'], Toast => {
|
|
|
85 |
document.querySelector("[data-example-name='autohide-long']").addEventListener('click', e => {
|
|
|
86 |
Toast.add('This message will be displayed for 30 seconds with a closeButton', {
|
|
|
87 |
delay: 30000,
|
|
|
88 |
closeButton: true,
|
|
|
89 |
});
|
|
|
90 |
});
|
|
|
91 |
|
|
|
92 |
document.querySelector("[data-example-name='autohide-disabled']").addEventListener('click', e => {
|
|
|
93 |
Toast.add('This message will be displayed until closed using the closeButton.', {
|
|
|
94 |
autohide: false,
|
|
|
95 |
closeButton: true,
|
|
|
96 |
});
|
|
|
97 |
});
|
|
|
98 |
});
|
|
|
99 |
{{/js}}
|
|
|
100 |
{{< /example >}}
|
|
|
101 |
|
|
|
102 |
|
|
|
103 |
### Using a Language String
|
|
|
104 |
|
|
|
105 |
The standard behaviour of the toast is to auto-hide after a short period which
|
|
|
106 |
can be configured or disabled. A close button can also be displayed, which is
|
|
|
107 |
recommended when a longer period is used.
|
|
|
108 |
|
|
|
109 |
{{< example >}}
|
|
|
110 |
<button type="button" class="btn btn-primary" data-example-name="langstring">Language string</button>
|
|
|
111 |
|
|
|
112 |
{{#js}}
|
|
|
113 |
require(['core/toast', 'core/str'], (Toast, Str) => {
|
|
|
114 |
document.querySelector("[data-example-name='langstring']").addEventListener('click', e => {
|
|
|
115 |
Toast.add(Str.get_string('ok'));
|
|
|
116 |
});
|
|
|
117 |
});
|
|
|
118 |
{{/js}}
|
|
|
119 |
{{< /example >}}
|
|
|
120 |
|