
Toast
Toasts provide brief feedback about an operation through a message on the screen.
Toast App Methods
Let’s look at related App methods to work with Toast:
app.toast.create(parameters)- create Toast instance
- parameters - object. Object with toast parameters
Method returns created Toast’s instance
app.toast.destroy(el)- destroy Toast instance
- el - HTMLElement or string (with CSS Selector) or object. Toast element or Toast instance to destroy.
app.toast.get(el)- get Toast instance by HTML element
- el - HTMLElement or string (with CSS Selector). Toast element.
Method returns Toast’s instance
app.toast.open(el, animate)- opens Toast
- el - HTMLElement or string (with CSS Selector). Toast element to open.
- animate - boolean. Open Toast with animation.
Method returns Toast’s instance
app.toast.close(el, animate)- closes Toast
- el - HTMLElement or string (with CSS Selector). Toast element to close.
- animate - boolean. Close Toast with animation.
Method returns Toast’s instance
app.toast.show(parameters)- create Toast instance and show immediately
- parameters - object. Object with toast parameters
Method returns created Toast’s instance
Toast Parameters
Now let’s look at list of available parameters we need to create Toast:
| Parameter | Type | Default | Description |
|---|---|---|---|
| el | HTMLElement | Toast element. Can be useful if you already have Toast element in your HTML and want to create new instance using this element | |
| icon | string | Toast icon HTML layout, e.g. <i class=”f7-icons”>home</i> or image <img src=”path/to/icon.png”> |
|
| text | string | Toast inner text | |
| position | string | bottom | Toast position. Can be bottom, center or top |
| closeButton | boolean | false | Adds toast close button |
| closeButtonColor | string | One of the default color themes | |
| closeButtonText | string | Ok | Close button text |
| closeTimeout | number | Timeout delay (in ms) to close toast automatically | |
| cssClass | string | Additional css class to add | |
| destroyOnClose | boolean | false | Destroys toast instance on close |
| render | function | Custom function to render Toast. Must return toast html | |
| on | object | Object with events handlers. For example: 1. var toast = app.toast.create({ 2. text: ‘Hello, how are you?’, 3. on: { 4. opened: function () { 5. console.log(‘Toast opened’) 6. } 7. } 8. }) |
Note that all following parameters can be used in global app parameters under toast property to set defaults for all toasts. For example:
1. var app = new Framework7({
2. toast: {
3. closeTimeout: 3000,
4. closeButton: true,
5. }
6. });
Toast Methods & Properties
So to create Toast we have to call:
1. var toast = app.toast.create({ /* parameters */ })
After that we have its initialized instance (like toast variable in example above) with useful methods and properties:
| Properties | |
|---|---|
| toast.app | Link to global app instance |
| toast.el | Toast HTML element |
| toast.$el | Dom7 instance with toast HTML element |
| toast.params | Toast parameters |
| Methods | |
| toast.open() | Open toast |
| toast.close() | Close toast |
| toast.on(event, handler) | Add event handler |
| toast.once(event, handler) | Add event handler that will be removed after it was fired |
| toast.off(event, handler) | Remove event handler |
| toast.off(event) | Remove all handlers for specified event |
| toast.emit(event, …args) | Fire event on instance |
Toast Events
Toast will fire the following DOM events on toast element and events on app and toast instance:
DOM Events
| Event | Target | Description |
|---|---|---|
| toast:open | Toast Element<div class=”toast”> | Event will be triggered when Toast starts its opening animation |
| toast:opened | Toast Element<div class=”toast”> | Event will be triggered after Toast completes its opening animation |
| toast:close | Toast Element<div class=”toast”> | Event will be triggered when Toast starts its closing animation |
| toast:closed | Toast Element<div class=”toast”> | Event will be triggered after Toast completes its closing animation |
App and Toast Instance Events
Toast instance emits events on both self instance and app instance. App instance events has same names prefixed with toast.
| Event | Arguments | Target | Description |
|---|---|---|---|
| closeButtonClick | toast | toast | Event will be triggered when user clicks on Toast close button. As an argument event handler receives toast instance |
| toastCloseButtonClick | toast | app | |
| open | toast | toast | Event will be triggered when Toast starts its opening animation. As an argument event handler receives toast instance |
| toastOpen | toast | app | |
| opened | toast | toast | Event will be triggered after Toast completes its opening animation. As an argument event handler receives toast instance |
| toastOpened | toast | app | |
| close | toast | toast | Event will be triggered when Toast starts its closing animation. As an argument event handler receives toast instance |
| toastClose | toast | app | |
| closed | toast | toast | Event will be triggered after Toast completes its closing animation. As an argument event handler receives toast instance |
| toastClosed | toast | app | |
| beforeDestroy | toast | toast | Event will be triggered right before Toast instance will be destroyed. As an argument event handler receives toast instance |
| toastBeforeDestroy | toast | app |
CSS Variables
Below is the list of related CSS variables (CSS custom properties).
1. :root {
2. --f7-toast-text-color: #fff;
3. --f7-toast-font-size: 14px;
4. --f7-toast-icon-size: 48px;
5. }
6. .ios {
7. --f7-toast-bg-color: rgba(0, 0, 0, 0.75);
8. --f7-toast-translucent-bg-color-ios: rgba(0, 0, 0, 0.75);
9. --f7-toast-padding-horizontal: 15px;
10. --f7-toast-padding-vertical: 12px;
11. --f7-toast-border-radius: 8px;
12. --f7-toast-button-min-width: 64px;
13. }
14. .md {
15. --f7-toast-bg-color: #323232;
16. --f7-toast-padding-horizontal: 24px;
17. --f7-toast-padding-vertical: 14px;
18. --f7-toast-border-radius: 4px;
19. --f7-toast-button-min-width: 64px;
20. }
21. .aurora {
22. --f7-toast-bg-color: rgba(0, 0, 0, 0.85);
23. --f7-toast-padding-horizontal: 10px;
24. --f7-toast-padding-vertical: 10px;
25. --f7-toast-border-radius: 4px;
26. --f7-toast-button-min-width: 32px;
27. }
Examples
1. <div class="block">
2. <p>
3. <a href="#" class="button button-raised open-toast-bottom">Toast on Bottom</a>
4. </p>
5. <p>
6. <a href="#" class="button button-raised open-toast-top">Toast on Top</a>
7. </p>
8. <p>
9. <a href="#" class="button button-raised open-toast-center">Toast on Center</a>
10. </p>
11. <p>
12. <a href="#" class="button button-raised open-toast-icon">Toast with icon</a>
13. </p>
14. <p>
15. <a href="#" class="button button-raised open-toast-large">Toast with large message</a>
16. </p>
17. <p>
18. <a href="#" class="button button-raised open-toast-button">Toast with close button</a>
19. </p>
20. <p>
21. <a href="#" class="button button-raised open-toast-custom-button">Toast with custom button</a>
22. </p>
23. <p>
24. <a href="#" class="button button-raised open-toast-callback">Toast with callback on close</a>
25. </p>
26. </div>
1. var app = new Framework7();
3. var $$ = Dom7;
5. // Create bottom toast
6. var toastBottom = app.toast.create({
7. text: 'This is default bottom positioned toast',
8. closeTimeout: 2000,
9. });
10. // Create top toast
11. var toastTop = app.toast.create({
12. text: 'Top positioned toast',
13. position: 'top',
14. closeTimeout: 2000,
15. });
16. // Create center toast
17. var toastCenter = app.toast.create({
18. text: 'I\'m on center',
19. position: 'center',
20. closeTimeout: 2000,
21. });
22. // Create toast with icon
23. var toastIcon = app.toast.create({
24. icon: app.theme === 'ios' ? '<i class="f7-icons">star</i>' : '<i class="material-icons">star</i>',
25. text: 'I\'m with icon',
26. position: 'center',
27. closeTimeout: 2000,
28. });
29. // Create toast with large message
30. var toastLargeMessage = app.toast.create({
31. text: 'This toast contains a lot of text. Lorem ipsum dolor sit amet, consectetur adipisicing elit. Nihil, quae, ab. Delectus amet optio facere autem sapiente quisquam beatae culpa dolore.',
32. closeTimeout: 2000,
33. });
34. // Create toast with button
35. var toastWithButton = app.toast.create({
36. text: 'Toast with additional close button',
37. closeButton: true,
38. });
39. // Create toast with custom button text
40. var toastWithCustomButton = app.toast.create({
41. text: 'Custom close button',
42. closeButton: true,
43. closeButtonText: 'Close Me',
44. closeButtonColor: 'red',
45. });
46. // Create toast with callback on close
47. var toastWithCallback = app.toast.create({
48. text: 'Callback on close',
49. closeButton: true,
50. on: {
51. close: function () {
52. app.dialog.alert('Toast closed');
53. },
54. }
55. });
57. // Open toasts
58. $$('.open-toast-bottom').on('click', function () {
59. toastBottom.open();
60. });
61. $$('.open-toast-top').on('click', function () {
62. toastTop.open();
63. });
64. $$('.open-toast-center').on('click', function () {
65. toastCenter.open();
66. });
67. $$('.open-toast-icon').on('click', function () {
68. toastIcon.open();
69. });
70. $$('.open-toast-large').on('click', function () {
71. toastLargeMessage.open();
72. });
73. $$('.open-toast-button').on('click', function () {
74. toastWithButton.open();
75. });
76. $$('.open-toast-custom-button').on('click', function () {
77. toastWithCustomButton.open();
78. });
79. $$('.open-toast-callback').on('click', function () {
80. toastWithCallback.open();
81. });
