Notification - 图1

Notification

With Notification component you can show required messages that looks like Push (or Local) system notifications.

Notification App Methods

Let’s look at related App methods to work with Notification:

app.notification.create(parameters)- create Notification instance

  • parameters - object. Object with notification parameters

Method returns created Notification’s instance

app.notification.destroy(el)- destroy Notification instance

  • el - HTMLElement or string (with CSS Selector) or object. Notification element or Notification instance to destroy.

app.notification.get(el)- get Notification instance by HTML element

  • el - HTMLElement or string (with CSS Selector). Notification element.

Method returns Notification’s instance

app.notification.open(el, animate)- opens Notification

  • el - HTMLElement or string (with CSS Selector). Notification element to open.
  • animate - boolean. Open Notification with animation.

Method returns Notification’s instance

app.notification.close(el, animate)- closes Notification

  • el - HTMLElement or string (with CSS Selector). Notification element to close.
  • animate - boolean. Close Notification with animation.

Method returns Notification’s instance

Notification Parameters

Now let’s look at list of available parameters we need to create Notification:

ParameterTypeDefaultDescription
elHTMLElementNotification element. Can be useful if you already have Notification element in your HTML and want to create new instance using this element
iconstringNotification icon HTML layout, e.g. <i class=”f7-icons”>home</i> or image <img src=”path/to/icon.png”>
titlestringNotification title
titleRightTextstringAdditional text on the right side of title
subtitlestringNotification subtitle
textstringNotification inner text
closeButtonbooleanfalseAdds notification close button
closeTimeoutnumberTimeout delay (in ms) to close notification automatically
closeOnClickbooleanfalseIf enabled, notification will be closed on notification click
swipeToClosebooleantrueIf enabled, notification can be closed by swipe gesture
cssClassstringAdditional css class to add
renderfunctionCustom function to render Notification. Must return notification html
onobject

Object with events handlers. For example:

  1. var notification = app.notification.create({
  2. title: John Doe’,
  3. text: Hello, how are you?’,
  4. on: {
  5. opened: function () {
  6. console.log(‘Notification opened’)
  7. }
  8. }
  9. })

Note that all following parameters can be used in global app parameters under notification property to set defaults for all notifications. For example:

  1. var app = new Framework7({
  2. notification: {
  3. title: 'My App',
  4. closeTimeout: 3000,
  5. }
  6. });

Notification Methods & Properties

So to create Notification we have to call:

  1. var notification = app.notification.create({ /* parameters */ })

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

Properties
notification.appLink to global app instance
notification.elNotification HTML element
notification.$elDom7 instance with notification HTML element
notification.paramsNotification parameters
Methods
notification.open()Open notification
notification.close()Close notification
notification.on(event, handler)Add event handler
notification.once(event, handler)Add event handler that will be removed after it was fired
notification.off(event, handler)Remove event handler
notification.off(event)Remove all handlers for specified event
notification.emit(event, …args)Fire event on instance

Notification Events

Notification will fire the following DOM events on notification element and events on app and notification instance:

DOM Events

EventTargetDescription
notification:openNotification Element<div class=”notification”>Event will be triggered when Notification starts its opening animation
notification:openedNotification Element<div class=”notification”>Event will be triggered after Notification completes its opening animation
notification:closeNotification Element<div class=”notification”>Event will be triggered when Notification starts its closing animation
notification:closedNotification Element<div class=”notification”>Event will be triggered after Notification completes its closing animation

App and Notification Instance Events

Notification instance emits events on both self instance and app instance. App instance events has same names prefixed with notification.

EventArgumentsTargetDescription
clicknotificationnotificationEvent will be triggered when user clicks on Notification element. As an argument event handler receives notification instance
notificationClicknotificationapp
opennotificationnotificationEvent will be triggered when Notification starts its opening animation. As an argument event handler receives notification instance
notificationOpennotificationapp
openednotificationnotificationEvent will be triggered after Notification completes its opening animation. As an argument event handler receives notification instance
notificationOpenednotificationapp
closenotificationnotificationEvent will be triggered when Notification starts its closing animation. As an argument event handler receives notification instance
notificationClosenotificationapp
closednotificationnotificationEvent will be triggered after Notification completes its closing animation. As an argument event handler receives notification instance
notificationClosednotificationapp
beforeDestroynotificationnotificationEvent will be triggered right before Notification instance will be destroyed. As an argument event handler receives notification instance
notificationBeforeDestroynotificationapp

CSS Variables

Below is the list of related CSS variables (CSS custom properties).

  1. :root {
  2. --f7-notification-max-width: 568px;
  3. --f7-notification-subtitle-text-transform: none;
  4. --f7-notification-subtitle-line-height: 1.35;
  5. --f7-notification-text-text-transform: none;
  6. --f7-notification-text-font-weight: 400;
  7. }
  8. :root .theme-dark,
  9. :root.theme-dark {
  10. --f7-notification-subtitle-color: #fff;
  11. }
  12. .ios {
  13. --f7-notification-margin: 8px;
  14. --f7-notification-padding: 10px;
  15. --f7-notification-border-radius: 12px;
  16. --f7-notification-box-shadow: 0px 5px 25px -10px rgba(0, 0, 0, 0.7);
  17. --f7-notification-icon-size: 20px;
  18. --f7-notification-title-font-size: 13px;
  19. --f7-notification-title-text-transform: uppercase;
  20. --f7-notification-title-line-height: 1.4;
  21. --f7-notification-title-font-weight: 400;
  22. --f7-notification-title-letter-spacing: 0.02em;
  23. --f7-notification-title-right-font-size: 13px;
  24. --f7-notification-subtitle-font-size: 15px;
  25. --f7-notification-subtitle-font-weight: 600;
  26. --f7-notification-text-font-size: 15px;
  27. --f7-notification-text-line-height: 1.2;
  28. --f7-notification-bg-color: rgba(250, 250, 250, 0.95);
  29. --f7-notification-bg-color-rgb: 255, 255, 255;
  30. --f7-notification-title-color: #000;
  31. --f7-notification-title-right-color: rgba(0, 0, 0, 0.45);
  32. --f7-notification-subtitle-color: #000;
  33. --f7-notification-text-color: #000;
  34. }
  35. .ios .theme-dark,
  36. .ios.theme-dark {
  37. --f7-notification-bg-color: rgba(30, 30, 30, 0.95);
  38. --f7-notification-bg-color-rgb: 30, 30, 30;
  39. --f7-notification-title-color: #fff;
  40. --f7-notification-text-color: #fff;
  41. --f7-notification-title-right-color: rgba(255, 255, 255, 0.55);
  42. }
  43. .md {
  44. --f7-notification-margin: 0px;
  45. --f7-notification-padding: 16px;
  46. --f7-notification-border-radius: 0px;
  47. --f7-notification-box-shadow: 0 2px 4px rgba(0, 0, 0, 0.22), 0 1px 2px rgba(0, 0, 0, 0.24);
  48. --f7-notification-icon-size: 16px;
  49. --f7-notification-title-color: var(--f7-theme-color);
  50. --f7-notification-title-font-size: 12px;
  51. --f7-notification-title-text-transform: none;
  52. --f7-notification-title-line-height: 1;
  53. --f7-notification-title-font-weight: 400;
  54. --f7-notification-title-right-font-size: 12px;
  55. --f7-notification-subtitle-font-size: 14px;
  56. --f7-notification-subtitle-font-weight: 400;
  57. --f7-notification-text-font-size: 14px;
  58. --f7-notification-text-line-height: 1.35;
  59. --f7-notification-bg-color: #fff;
  60. --f7-notification-title-right-color: rgba(0, 0, 0, 0.54);
  61. --f7-notification-subtitle-color: #212121;
  62. --f7-notification-text-color: rgba(0, 0, 0, 0.54);
  63. }
  64. .md .theme-dark,
  65. .md.theme-dark {
  66. --f7-notification-bg-color: #242424;
  67. --f7-notification-title-right-color: rgba(255, 255, 255, 0.54);
  68. --f7-notification-text-color: rgba(255, 255, 255, 0.54);
  69. }
  70. .aurora {
  71. --f7-notification-margin: 10px;
  72. --f7-notification-padding: 10px;
  73. --f7-notification-border-radius: 4px;
  74. --f7-notification-box-shadow: 0px 5px 25px -10px rgba(0, 0, 0, 0.7);
  75. --f7-notification-icon-size: 18px;
  76. --f7-notification-title-font-size: 13px;
  77. --f7-notification-title-text-transform: uppercase;
  78. --f7-notification-title-line-height: 1.4;
  79. --f7-notification-title-font-weight: 500;
  80. --f7-notification-title-letter-spacing: 0.02em;
  81. --f7-notification-title-right-font-size: 13px;
  82. --f7-notification-subtitle-font-size: 12px;
  83. --f7-notification-subtitle-font-weight: 600;
  84. --f7-notification-text-font-size: 12px;
  85. --f7-notification-text-line-height: 1.2;
  86. --f7-notification-bg-color: #fff;
  87. --f7-notification-title-color: #000;
  88. --f7-notification-title-right-color: rgba(255, 255, 255, 0.6);
  89. --f7-notification-subtitle-color: #000;
  90. --f7-notification-text-color: #000;
  91. }
  92. .aurora .theme-dark,
  93. .aurora.theme-dark {
  94. --f7-notification-bg-color: #242424;
  95. --f7-notification-title-color: #fff;
  96. --f7-notification-text-color: #fff;
  97. --f7-notification-title-right-color: rgba(255, 255, 255, 0.54);
  98. }

Examples

  1. <div class="block">
  2. <p>
  3. <a class="button button-raised open-full" href="#">Full-layout notification</a>
  4. </p>
  5. <p>
  6. <a class="button button-raised open-with-button" href="#">With close button</a>
  7. </p>
  8. <p>
  9. <a class="button button-raised open-click-to-close" href="#">Click to close</a>
  10. </p>
  11. <p>
  12. <a class="button button-raised open-callback-on-close" href="#">Callback on close</a>
  13. </p>
  14. </div>
  1. var app = new Framework7();
  2. var $$ = Dom7;
  3. // Create full-layout notification
  4. var notificationFull = app.notification.create({
  5. icon: '<i class="icon demo-icon">7</i>',
  6. title: 'Framework7',
  7. titleRightText: 'now',
  8. subtitle: 'This is a subtitle',
  9. text: 'This is a simple notification message',
  10. closeTimeout: 3000,
  11. });
  12. // Create notification with close button
  13. var notificationWithButton = app.notification.create({
  14. icon: '<i class="icon demo-icon">7</i>',
  15. title: 'Framework7',
  16. subtitle: 'Notification with close button',
  17. text: 'Click (x) button to close me',
  18. closeButton: true,
  19. });
  20. // Create notification with click to close
  21. var notificationClickToClose = app.notification.create({
  22. icon: '<i class="icon demo-icon">7</i>',
  23. title: 'Framework7',
  24. titleRightText: 'now',
  25. subtitle: 'Notification with close on click',
  26. text: 'Click me to close',
  27. closeOnClick: true,
  28. })
  29. // With callback on close
  30. var notificationCallbackOnClose = app.notification.create({
  31. icon: '<i class="icon demo-icon">7</i>',
  32. title: 'Framework7',
  33. titleRightText: 'now',
  34. subtitle: 'Notification with close on click',
  35. text: 'Click me to close',
  36. closeOnClick: true,
  37. on: {
  38. close: function () {
  39. app.dialog.alert('Notification closed');
  40. },
  41. },
  42. });
  43. // Open Notifications
  44. $$('.open-full').on('click', function () {
  45. notificationFull.open();
  46. });
  47. $$('.open-with-button').on('click', function () {
  48. notificationWithButton.open();
  49. });
  50. $$('.open-click-to-close').on('click', function () {
  51. notificationClickToClose.open();
  52. });
  53. $$('.open-callback-on-close').on('click', function () {
  54. notificationCallbackOnClose.open();
  55. });

← Navbar

Page →