Messages - 图1

Messages

Messages component will help you with visualisation of comments and messaging system in your app.

Messages Layout

  1. <div class="page">
  2. <div class="page-content messages-content">
  3. <div class="messages">
  4. <!-- Date stamp -->
  5. <div class="messages-title"><b>Sunday, Feb 9</b> 12:58</div>
  6. <!-- Sent message (by default - green and on right side) -->
  7. <div class="message message-sent">
  8. <div class="message-content">
  9. <!-- Bubble with text -->
  10. <div class="message-bubble">
  11. <div class="message-text">Hi, Kate</div>
  12. </div>
  13. </div>
  14. </div>
  15. <!-- Another sent message -->
  16. <div class="message message-sent">
  17. <div class="message-content">
  18. <div class="message-bubble">
  19. <div class="message-text">How are you?</div>
  20. </div>
  21. </div>
  22. </div>
  23. <!-- Received message (by default - grey on left side) -->
  24. <div class="message message-received">
  25. <!-- Sender avatar -->
  26. <div class="message-avatar" style="background-image:url(path/to/avatar.jpg)"></div>
  27. <div class="message-content">
  28. <!-- Sender name -->
  29. <div class="message-name">Blue Ninja</div>
  30. <!-- Bubble with text -->
  31. <div class="message-bubble">
  32. <div class="message-text">Hi there, I am also fine, thanks! And how are you?</div>
  33. </div>
  34. </div>
  35. </div>
  36. </div>
  37. </div>
  38. </div>

Messages page layout:

  • messages-content - required additional class for messages wrapper. Should be added to page-content

  • messages - required additional wrapper for messages bubbles. Required element.

  • messages-title - messages title

  • message - single message

Single Message Layout

Here is a full single message layout:

  1. <div class="message">
  2. <div class="message-avatar" style="background-image:url(path/to/avatar)"></div>
  3. <div class="message-content">
  4. <div class="message-name">John Doe</div>
  5. <div class="message-header">Message header</div>
  6. <div class="message-bubble">
  7. <div class="message-text-header">Text header</div>
  8. <div class="message-image">
  9. <img src="path/to/image">
  10. </div>
  11. <div class="message-text">Hello world!</div>
  12. <div class="message-text-footer">Text footer</div>
  13. </div>
  14. <div class="message-footer">Message footer</div>
  15. </div>
  16. </div>
  • message-avatar - sender avatar, optional

  • message-name - sender name, optional

  • message-header - single message header, optional

  • message-text-header - text header inside of bubble, optional

  • message-image - message image, optional

  • message-text - message text, optional

  • message-text-footer - text footer inside of bubble, optional

  • message-footer - footer text after bubble, optional

Additional classes for single message container

  • message-sent - additional class for single message which indicates that this message was sent by user. It stays on right side with green background color.

  • message-received - additional class for single message which indicates that this message was received by user. It stays on left side with grey background color.

  • message-tail - additional class for single message (received or sent) to add bubble “tail”

  • message-same-name - additional class for indicating that message has same sender name as previous message

  • message-same-avatar - additional class for indicating that message has same avatar as previous message

  • message-same-header - additional class for indicating that message has same message-header as previous message

  • message-same-footer - additional class for indicating that message has same message-footer as previous message

  • message-last - additional class for single message (received or sent) to indicate last received or last sent message in current conversation by one sender

  • message-first - additional class for single message (received or sent) to indicate first received or first sent message in current conversation by one sender

Messages App Methods

Now, when we have Messages’ HTML, we need to initialize it. We need to use related App’s method:

app.messages.create(parameters)Initialize Messages with parameters
  • parameters - object - object with Messages parameters
  • Method returns initialized Messages instance
app.messages.destroy(el)Destroy Messages instance
  • el - HTMLElement or string (with CSS Selector) or object. Messages element or Messages instance to destroy.
app.messages.get(el)Get Messages instance by HTML element
  • el - HTMLElement or string (with CSS Selector). Messages element.
  • Method returns Messages’s instance

Messages Parameters

Let’s look on list of all available parameters:

ParameterTypeDefaultDescription
autoLayoutbooleantrueEnable Auto Layout to add all required additional classes automatically based on passed conditions
newMessagesFirstbooleanfalseEnable if you want to use new messages on top, instead of having them on bottom
scrollMessagesbooleantrueEnable/disable messages autoscrolling when adding new message
scrollMessagesOnEdgebooleantrueIf enabled then messages autoscrolling will happen only when user is on top/bottom of the messages view
messagesarrayArray with initial messages. Each message in array should be presented as an object with single message parameters
onobject

Object with events handlers. For example:

  1. var messages = app.messages.create({
  2. el: ‘.messages’,
  3. on: {
  4. change: function () {
  5. console.log(‘Textarea value changed’)
  6. }
  7. }
  8. })
renderMessagefunction(message)Function to render single message. Must return full message HTML string
Autolayout Conditions
firstMessageRulefunction(message, previousMessage, nextMessage)Function that must return boolean true or false based on required condition depending on previous and next messages. In case of match then message-first class will be added to message
lastMessageRulefunction(message, previousMessage, nextMessage)Function that must return boolean true or false based on required condition depending on previous and next messages. In case of match then message-last class will be added to message
tailMessageRulefunction(message, previousMessage, nextMessage)Function that must return boolean true or false based on required condition depending on previous and next messages. In case of match then message-tail class will be added to message
sameNameMessageRulefunction(message, previousMessage, nextMessage)Function that must return boolean true or false based on required condition depending on previous and next messages. In case of match then message-same-name class will be added to message
sameHeaderMessageRulefunction(message, previousMessage, nextMessage)Function that must return boolean true or false based on required condition depending on previous and next messages. In case of match then message-same-header class will be added to message
sameFooterMessageRulefunction(message, previousMessage, nextMessage)Function that must return boolean true or false based on required condition depending on previous and next messages. In case of match then message-same-footer class will be added to message
sameAvatarMessageRulefunction(message, previousMessage, nextMessage)Function that must return boolean true or false based on required condition depending on previous and next messages. In case of match then message-same-avatar class will be added to message
customClassMessageRulefunction(message, previousMessage, nextMessage)Function that must return additional message classes as string, based on required condition depending on previous and next messages.

Single Message Parameters

Let’s look on single message parameters object that we should use when we pass messages array:

ParameterTypeDefaultDescription
textstringMessage text
headerstringSingle message header
footerstringSingle message footer
namestringSender name
avatarstringSender avatar URL string
typestringsentMessage type - sent or received
textHeaderstringMessage text header
textFooterstringMessage text footer
imagestringMessage image HTML string, e.g. <img src=”path/to/image”>. Can be used instead of imageSrc parameter
imageSrcstringMessage image URL string. Can be used instead of image parameter
isTitlebooleanDefines whether it should be rendered as a message or as a messages title
cssClassstringAdditional CSS class to set on message HTML element
attrsobjectObject with additional HTML attributes to be set on message HTML element. For example to set extra data attributes it should look like:
  1. var message = {
  2. text: Hello!’,
  3. attrs: {
  4. data-id’: 1,
  5. data-author-id’: 150
  6. }
  7. }

Messages Methods & Properties

So to create Messages we have to call:

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

After we initialize Messages we have its initialized instance in variable (like messages variable in example above) with helpful methods and properties:

Properties
messages.paramsObject with passed initialization parameters
messages.elMessages container HTML element (<div class=”messages”>)
messages.$elDom7 element with messages HTML element
messages.messagesArray with messages
Methods
messages.showTyping(message)Show typing message indicator
messages.hideTyping()Hide typing message indicator
messages.addMessage(message, method, animate);

Add new message to the end or to the beginning depending on method parameter

  • message - object - parameters of message to add. Required.
  • method - string - (append or prepend) dictates to add new message in the end or in the beginning of messages container. Optional, if not specified, then it will add message depending on newMessagesFirst parameter
  • animate - boolean - (by default true) You may pass here false and message will be added immediately without any transiton and scrolling animation. Optional.
  • Method returns Messages instance
messages.addMessages(messages, method, animate);Add multiple messages per once.
  • messages - array with messages to add. Each message in array should be presented as an object with message parameters Required.
  • Method returns Messages instance
messages.removeMessage(message);Remove message
  • message - HTMLElement or string (with CSS Selector) or number (with index number of message from messages array) of message to remove
  • Method returns Messages instance
messages.removeMessages(messages);Remove multiple messages
  • messages - array with messages to remove
  • Method returns Messages instance
messages.scroll(duration, position);Scroll messages to top/bottom depending on newMessagesFirst parameter
  • duration - number scroll duration in ms
  • position - number scroll position in px
messages.renderMessages()Render messages HTML depending on messages array
messages.layout();Force messages auto layout
messages.clear();Clear/remove all the messages
messages.destroy();Destroy messages instance

Messages Events

Messages will fire the following DOM events on messages element and events on app and messages instance:

DOM Events

EventTargetDescription
messages:beforedestroyMessages Element<div class=”messages”>Event will be triggered right before Messages instance will be destroyed

App and Messages Instance Events

Messages instance emits events on both self instance and app instance. App instance events has same names prefixed with messages.

EventTargetArgumentsDescription
beforeDestroymessages(messages)Event will be triggered right before Messages instance will be destroyed
messagesBeforeDestroyapp

Messages Auto Initialization

If you don’t need to use Messages API and your Messages is inside of the page and presented in DOM on moment of page initialization then it can be auto initialized with just adding additional messages-init class to messages element, and all required parameters can be passed using data- attributes:

  1. <div class="messages messages-init" data-new-messages-first="true">
  2. ...
  3. </div>

Parameters that used in camelCase, for example newMessagesFirst, in data- attributes should be used in kebab-case as data-new-messages-first

CSS Variables

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

Note that commented variables are not specified by default and their values is what they fallback to in this case.

  1. :root {
  2. --f7-message-text-header-text-color: inherit;
  3. --f7-message-text-header-opacity: 0.65;
  4. --f7-message-text-header-font-size: 12px;
  5. --f7-message-text-footer-text-color: inherit;
  6. --f7-message-text-footer-opacity: 0.65;
  7. --f7-message-text-footer-font-size: 12px;
  8. --f7-message-bubble-line-height: 1.2;
  9. --f7-message-header-font-size: 12px;
  10. --f7-message-footer-font-size: 11px;
  11. --f7-message-name-font-size: 12px;
  12. --f7-message-name-font-weight: inherit;
  13. --f7-message-avatar-border-radius: 50%;
  14. --f7-messages-title-font-weight: inherit;
  15. /*
  16. --f7-message-sent-bg-color: var(--f7-theme-color);
  17. */
  18. --f7-message-sent-text-color: #fff;
  19. --f7-messages-content-bg-color: #fff;
  20. --f7-message-typing-indicator-bg-color: #000;
  21. --f7-message-received-bg-color: #e5e5ea;
  22. --f7-message-received-text-color: #000;
  23. }
  24. :root .theme-dark,
  25. :root.theme-dark {
  26. --f7-messages-title-text-color: rgba(255, 255, 255, 0.54);
  27. --f7-message-header-text-color: rgba(255, 255, 255, 0.54);
  28. --f7-message-name-text-color: rgba(255, 255, 255, 0.54);
  29. --f7-message-footer-text-color: rgba(255, 255, 255, 0.54);
  30. --f7-messages-content-bg-color: transparent;
  31. --f7-message-received-bg-color: #252525;
  32. --f7-message-received-text-color: #fff;
  33. --f7-message-typing-indicator-bg-color: #fff;
  34. }
  35. .ios {
  36. --f7-messages-title-text-color: rgba(0, 0, 0, 0.45);
  37. --f7-messages-title-font-size: 11px;
  38. --f7-message-header-text-color: rgba(0, 0, 0, 0.45);
  39. --f7-message-footer-text-color: rgba(0, 0, 0, 0.45);
  40. --f7-message-name-text-color: rgba(0, 0, 0, 0.45);
  41. --f7-message-avatar-size: 29px;
  42. --f7-message-margin: 10px;
  43. --f7-message-bubble-min-height: 32px;
  44. --f7-message-bubble-font-size: 17px;
  45. --f7-message-bubble-border-radius: 16px;
  46. --f7-message-bubble-padding-vertical: 6px;
  47. --f7-message-bubble-padding-horizontal: 16px;
  48. --f7-message-typing-indicator-opacity: 0.35;
  49. }
  50. .md {
  51. --f7-messages-title-font-size: 12px;
  52. --f7-message-avatar-size: 32px;
  53. --f7-message-margin: 16px;
  54. --f7-message-bubble-min-height: 32px;
  55. --f7-message-bubble-font-size: 16px;
  56. --f7-message-bubble-border-radius: 4px;
  57. --f7-message-bubble-padding-vertical: 6px;
  58. --f7-message-bubble-padding-horizontal: 8px;
  59. --f7-message-typing-indicator-opacity: 0.6;
  60. --f7-messages-title-text-color: rgba(0, 0, 0, 0.51);
  61. --f7-message-header-text-color: rgba(0, 0, 0, 0.51);
  62. --f7-message-footer-text-color: rgba(0, 0, 0, 0.51);
  63. --f7-message-name-text-color: rgba(0, 0, 0, 0.51);
  64. }
  65. .aurora {
  66. --f7-messages-title-font-size: 12px;
  67. --f7-message-avatar-size: 24px;
  68. --f7-message-margin: 10px;
  69. --f7-message-bubble-min-height: 28px;
  70. --f7-message-bubble-font-size: 13px;
  71. --f7-message-bubble-line-height: 1.4;
  72. --f7-message-bubble-border-radius: 14px;
  73. --f7-message-bubble-padding-vertical: 5px;
  74. --f7-message-bubble-padding-horizontal: 10px;
  75. --f7-message-typing-indicator-opacity: 0.5;
  76. --f7-messages-title-text-color: rgba(0, 0, 0, 0.51);
  77. --f7-message-header-text-color: rgba(0, 0, 0, 0.51);
  78. --f7-message-footer-text-color: rgba(0, 0, 0, 0.51);
  79. --f7-message-name-text-color: rgba(0, 0, 0, 0.51);
  80. }

Examples

  1. <div class="page">
  2. <div class="navbar">
  3. <div class="navbar-bg"></div>
  4. <div class="navbar-inner">
  5. <div class="title">Messages</div>
  6. </div>
  7. </div>
  8. <div class="toolbar messagebar">
  9. <div class="toolbar-inner">
  10. <div class="messagebar-area">
  11. <textarea class="resizable" placeholder="Message"></textarea>
  12. </div><a class="link send-link" href="#">Send</a>
  13. </div>
  14. </div>
  15. <div class="page-content messages-content">
  16. <div class="messages">
  17. <!-- Messages title -->
  18. <div class="messages-title"><b>Sunday, Feb 9,</b> 12:58</div>
  19. <!-- Full layout sent message -->
  20. <div class="message message-sent">
  21. <div class="message-avatar" style="background-image:url(https://cdn.framework7.io/placeholder/people-100x100-7.jpg);"></div>
  22. <div class="message-content">
  23. <div class="message-name">John Doe</div>
  24. <div class="message-header">Message header</div>
  25. <div class="message-bubble">
  26. <div class="message-text-header">Text header</div>
  27. <div class="message-text">Lorem ipsum dolor sit amet, consectetur adipisicing elit.</div>
  28. <div class="message-text-footer">Text footer</div>
  29. </div>
  30. <div class="message-footer">Message footer</div>
  31. </div>
  32. </div>
  33. <!-- Full layout received message -->
  34. <div class="message message-received">
  35. <div class="message-avatar" style="background-image:url(https://cdn.framework7.io/placeholder/people-100x100-7.jpg);"></div>
  36. <div class="message-content">
  37. <div class="message-name">John Doe</div>
  38. <div class="message-header">Message header</div>
  39. <div class="message-bubble">
  40. <div class="message-text-header">Text header</div>
  41. <div class="message-text">Lorem ipsum dolor sit amet, consectetur adipisicing elit.</div>
  42. <div class="message-text-footer">Text footer</div>
  43. </div>
  44. <div class="message-footer">Message footer</div>
  45. </div>
  46. </div>
  47. <!-- Messages -->
  48. <div class="message message-sent">
  49. <div class="message-content">
  50. <div class="message-bubble">
  51. <div class="message-text">Hi, Kate</div>
  52. </div>
  53. </div>
  54. </div>
  55. <div class="message message-sent">
  56. <div class="message-content">
  57. <div class="message-bubble">
  58. <div class="message-text">How are you?</div>
  59. </div>
  60. </div>
  61. </div>
  62. <div class="message message-received">
  63. <div class="message-avatar" style="background-image:url(https://cdn.framework7.io/placeholder/people-100x100-9.jpg);"></div>
  64. <div class="message-content">
  65. <div class="message-name">Kate</div>
  66. <div class="message-bubble">
  67. <div class="message-text">Hi, I am good!</div>
  68. </div>
  69. </div>
  70. </div>
  71. <div class="message message-received">
  72. <div class="message-avatar" style="background-image:url(https://cdn.framework7.io/placeholder/people-100x100-7.jpg);"></div>
  73. <div class="message-content">
  74. <div class="message-name">Blue Ninja</div>
  75. <div class="message-bubble">
  76. <div class="message-text">Hi there, I am also fine, thanks! And how are you?</div>
  77. </div>
  78. </div>
  79. </div>
  80. <div class="message message-sent">
  81. <div class="message-content">
  82. <div class="message-bubble">
  83. <div class="message-text">Hey, Blue Ninja! Glad to see you ;)</div>
  84. </div>
  85. </div>
  86. </div>
  87. <div class="message message-sent">
  88. <div class="message-content">
  89. <div class="message-bubble">
  90. <div class="message-text">Hey, look, cutest kitten ever!</div>
  91. </div>
  92. </div>
  93. </div>
  94. <div class="message message-sent">
  95. <div class="message-content">
  96. <div class="message-bubble">
  97. <div class="message-image"><img src="https://cdn.framework7.io/placeholder/cats-200x260-4.jpg" style="width:200px; height: 260px;"></div>
  98. </div>
  99. </div>
  100. </div>
  101. <div class="message message-received">
  102. <div class="message-avatar" style="background-image:url(https://cdn.framework7.io/placeholder/people-100x100-9.jpg);"></div>
  103. <div class="message-content">
  104. <div class="message-name">Kate</div>
  105. <div class="message-bubble">
  106. <div class="message-text">Nice!</div>
  107. </div>
  108. </div>
  109. </div>
  110. <div class="message message-received">
  111. <div class="message-avatar" style="background-image:url(https://cdn.framework7.io/placeholder/people-100x100-9.jpg);"></div>
  112. <div class="message-content">
  113. <div class="message-name">Kate</div>
  114. <div class="message-bubble">
  115. <div class="message-text">Like it very much!</div>
  116. </div>
  117. </div>
  118. </div>
  119. <div class="message message-received">
  120. <div class="message-avatar" style="background-image:url(https://cdn.framework7.io/placeholder/people-100x100-7.jpg);"></div>
  121. <div class="message-content">
  122. <div class="message-name">Blue Ninja</div>
  123. <div class="message-bubble">
  124. <div class="message-text">Awesome!</div>
  125. </div>
  126. </div>
  127. </div>
  128. </div>
  129. </div>
  130. </div>
  1. var app = new Framework7();
  2. var $$ = Dom7;
  3. // Init Messages
  4. var messages = app.messages.create({
  5. el: '.messages',
  6. // First message rule
  7. firstMessageRule: function (message, previousMessage, nextMessage) {
  8. // Skip if title
  9. if (message.isTitle) return false;
  10. /* if:
  11. - there is no previous message
  12. - or previous message type (send/received) is different
  13. - or previous message sender name is different
  14. */
  15. if (!previousMessage || previousMessage.type !== message.type || previousMessage.name !== message.name) return true;
  16. return false;
  17. },
  18. // Last message rule
  19. lastMessageRule: function (message, previousMessage, nextMessage) {
  20. // Skip if title
  21. if (message.isTitle) return false;
  22. /* if:
  23. - there is no next message
  24. - or next message type (send/received) is different
  25. - or next message sender name is different
  26. */
  27. if (!nextMessage || nextMessage.type !== message.type || nextMessage.name !== message.name) return true;
  28. return false;
  29. },
  30. // Last message rule
  31. tailMessageRule: function (message, previousMessage, nextMessage) {
  32. // Skip if title
  33. if (message.isTitle) return false;
  34. /* if (bascially same as lastMessageRule):
  35. - there is no next message
  36. - or next message type (send/received) is different
  37. - or next message sender name is different
  38. */
  39. if (!nextMessage || nextMessage.type !== message.type || nextMessage.name !== message.name) return true;
  40. return false;
  41. }
  42. });
  43. // Init Messagebar
  44. var messagebar = app.messagebar.create({
  45. el: '.messagebar'
  46. });
  47. // Response flag
  48. var responseInProgress = false;
  49. // Send Message
  50. $$('.send-link').on('click', function () {
  51. var text = messagebar.getValue().replace(/\n/g, '<br>').trim();
  52. // return if empty message
  53. if (!text.length) return;
  54. // Clear area
  55. messagebar.clear();
  56. // Return focus to area
  57. messagebar.focus();
  58. // Add message to messages
  59. messages.addMessage({
  60. text: text,
  61. });
  62. if (responseInProgress) return;
  63. // Receive dummy message
  64. receiveMessage();
  65. });
  66. // Dummy response
  67. var answers = [
  68. 'Yes!',
  69. 'No',
  70. 'Hm...',
  71. 'I am not sure',
  72. 'And what about you?',
  73. 'May be ;)',
  74. 'Lorem ipsum dolor sit amet, consectetur',
  75. 'What?',
  76. 'Are you sure?',
  77. 'Of course',
  78. 'Need to think about it',
  79. 'Amazing!!!'
  80. ]
  81. var people = [
  82. {
  83. name: 'Kate Johnson',
  84. avatar: 'https://cdn.framework7.io/placeholder/people-100x100-9.jpg'
  85. },
  86. {
  87. name: 'Blue Ninja',
  88. avatar: 'https://cdn.framework7.io/placeholder/people-100x100-7.jpg'
  89. }
  90. ];
  91. function receiveMessage() {
  92. responseInProgress = true;
  93. setTimeout(function () {
  94. // Get random answer and random person
  95. var answer = answers[Math.floor(Math.random() * answers.length)];
  96. var person = people[Math.floor(Math.random() * people.length)];
  97. // Show typing indicator
  98. messages.showTyping({
  99. header: person.name + ' is typing',
  100. avatar: person.avatar
  101. });
  102. setTimeout(function () {
  103. // Add received dummy message
  104. messages.addMessage({
  105. text: answer,
  106. type: 'received',
  107. name: person.name,
  108. avatar: person.avatar
  109. });
  110. // Hide typing indicator
  111. messages.hideTyping();
  112. responseInProgress = false;
  113. }, 4000);
  114. }, 1000);
  115. }

← Messagebar

Navbar →