Toast

    Toasts provide brief feedback about an operation through a message on the screen.

    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.

    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

    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. });

    So to create Toast we have to call:

    After that we have its initialized instance (like toast variable in example above) with useful methods and properties:

    Toast Events

    Toast will fire the following DOM events on toast element and events on app and toast instance:

    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.

    Below is the list of related (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. --f7-toast-bg-color: rgba(0, 0, 0, 0.85);
    22. --f7-toast-padding-horizontal: 10px;
    23. --f7-toast-padding-vertical: 10px;
    24. --f7-toast-border-radius: 4px;
    25. --f7-toast-button-min-width: 32px;

    Examples

    1. var app = new Framework7();
    2. var $$ = Dom7;
    3. // Create bottom toast
    4. var toastBottom = app.toast.create({
    5. text: 'This is default bottom positioned toast',
    6. closeTimeout: 2000,
    7. });
    8. // Create top toast
    9. var toastTop = app.toast.create({
    10. text: 'Top positioned toast',
    11. position: 'top',
    12. closeTimeout: 2000,
    13. });
    14. // Create center toast
    15. var toastCenter = app.toast.create({
    16. text: 'I\'m on center',
    17. position: 'center',
    18. closeTimeout: 2000,
    19. });
    20. // Create toast with icon
    21. var toastIcon = app.toast.create({
    22. icon: app.theme === 'ios' ? '<i class="f7-icons">star</i>' : '<i class="material-icons">star</i>',
    23. text: 'I\'m with icon',
    24. position: 'center',
    25. closeTimeout: 2000,
    26. });
    27. // Create toast with large message
    28. var toastLargeMessage = app.toast.create({
    29. 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.',
    30. closeTimeout: 2000,
    31. });
    32. // Create toast with button
    33. var toastWithButton = app.toast.create({
    34. text: 'Toast with additional close button',
    35. });
    36. // Create toast with custom button text
    37. text: 'Custom close button',
    38. closeButton: true,
    39. closeButtonText: 'Close Me',
    40. closeButtonColor: 'red',
    41. });
    42. // Create toast with callback on close
    43. var toastWithCallback = app.toast.create({
    44. text: 'Callback on close',
    45. closeButton: true,
    46. on: {
    47. close: function () {
    48. app.dialog.alert('Toast closed');
    49. },
    50. }
    51. });
    52. // Open toasts
    53. $$('.open-toast-bottom').on('click', function () {
    54. toastBottom.open();
    55. });
    56. $$('.open-toast-top').on('click', function () {
    57. toastTop.open();
    58. });
    59. $$('.open-toast-center').on('click', function () {
    60. toastCenter.open();
    61. });
    62. $$('.open-toast-icon').on('click', function () {
    63. toastIcon.open();
    64. });
    65. $$('.open-toast-large').on('click', function () {
    66. toastLargeMessage.open();
    67. });
    68. $$('.open-toast-button').on('click', function () {
    69. toastWithButton.open();
    70. });
    71. $$('.open-toast-custom-button').on('click', function () {
    72. toastWithCustomButton.open();
    73. });
    74. $$('.open-toast-callback').on('click', function () {
    75. });