Messages - 图1

Messages Component

Messages Svelte component represents Messages component.

Messages Components

There are following components included:

  • Messages - main Messages container
  • Message - single message element
  • MessagesTitle - single messages title element

Messages Properties

PropTypeDefaultDescription
<Messages> properties
initbooleantrueInitializes Messages component
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
<Message> properties
typestringsentMessage type: sent (default) or received
textstringMessage text
avatarstringMessage user’s avatar URL
namestringMessage user’s name
imagestringMessage image URL
headerstringMessage header
footerstringMessage footer
textHeaderstringMessage text header
textFooterstringMessage text footer
firstbooleanfalseDefines that the message is first in the conversation
lastbooleanfalseDefines that the message is last in the conversation
tailbooleanfalseDefines that the message has visual “tail”. Usually last message in conversation
sameNamebooleanfalseDefines that this message sender name is the same as on previous message
sameHeaderbooleanfalseDefines that this message header text is the same as on previous message
sameFooterbooleanfalseDefines that this message footer text is the same as on previous message
sameAvatarbooleanfalseDefines that this message user’s avatar URL is the same as on previous message

Messages Methods

<Messages> methods
.scroll(duration, position);Scroll messages to top/bottom depending on newMessagesFirst parameter
  • duration - number scroll duration in ms
  • position - number scroll position in px
.showTyping(message);Show typing message indicator
.hideTyping()Hide typing message indicator

Messages Events

EventDescription
<Message> events
clickEvent will be triggered when user clicks on message bubble
clickNameEvent will be triggered when user clicks on message user’s name
clickTextEvent will be triggered when user clicks on message text
clickAvatarEvent will be triggered when user clicks on message user’s avatar
clickHeaderEvent will be triggered when user clicks on message header
clickFooterEvent will be triggered when user clicks on message footer
clickBubbleEvent will be triggered when user clicks on message bubble

Messages Slots

Single message Svelte component (<Message>) has additional slots for custom elements:

  • default - element will be inserted as a child of <div class="message-bubble"> element in the end
  • start - element will be inserted in the beginning and direct child of main message element <div class="message">
  • end - element will be inserted in the end and direct child of main message element <div class="message">
  • content-start - element will be inserted in the beginning and direct child of the <div class="message-content"> element
  • content-end - element will be inserted in the end and direct child of the <div class="message-content"> element
  • bubble-start - element will be inserted in the beginning and direct child of the <div class="message-bubble"> element
  • bubble-end - element will be inserted in the end and direct child of the <div class="message-bubble"> element. Same as default slot

The following slots can be used inside of single message instead of same props if you need to pass there more complext layout:

  • header - element will be inserted in message header
  • footer - element will be inserted in message footer
  • text - element will be inserted in message text
  • name - element will be inserted in message name
  • image - element will be inserted in message image (supposed tp be an <img> element)
  • text-header - element will be inserted in message text header
  • text-footer - element will be inserted in message text footer
  1. <Message
  2. type="sent"
  3. text="Hello World"
  4. name="John Doe"
  5. avatar="path/to/image.jpg"
  6. >
  7. <div slot="start">Start</div>
  8. <div slot="end">End</div>
  9. <div slot="content-start">Content Start</div>
  10. <div slot="content-end">Content End</div>
  11. <div slot="bubble-start">Bubble Start</div>
  12. <div slot="bubble-end">Bubble End</div>
  13. </Message>
  14. <!-- Renders to: -->
  15. <div class="message message-sent">
  16. <div>Start</div>
  17. <div class="message-avatar" style="background-image: url(path/to/image.jpg);"></div>
  18. <div class="message-content">
  19. <div>Content Start</div>
  20. <div class="message-name">John Doe</div>
  21. <div class="message-bubble">
  22. <div>Bubble Start</div>
  23. <div class="message-text">Hello World</div>
  24. <div>Bubble End</div>
  25. </div>
  26. <div>Content End</div>
  27. </div>
  28. <div>End</div>
  29. </div>

Access To Messages Instance

If you use automatic initalization to init Messages (with init={true} prop) and need to use Messages API you can access its initialized instance by calling .instance() component’s method. For example:

  1. <Messages bind:this={component}>...</Messages>
  2. <script>
  3. let component;
  4. // to get instance in some method
  5. component.instance()
  6. </script>

Examples

Here is how the full example of Messages page where it can be used together with Messagebar:

  1. <Page>
  2. <Navbar title="Messages"></Navbar>
  3. <Messagebar
  4. placeholder={placeholder}
  5. bind:this={messagebarComponent}
  6. attachmentsVisible={attachmentsVisible}
  7. sheetVisible={sheetVisible}
  8. value={messageText}
  9. onInput={(e) => messageText = e.target.value}
  10. >
  11. <a class="link icon-only" slot="inner-start" on:click={() => sheetVisible = !sheetVisible}>
  12. <Icon
  13. ios="f7:camera_fill"
  14. aurora="f7:camera_fill"
  15. md="material:camera_alt"
  16. />
  17. </a>
  18. <a class="link icon-only" slot="inner-end" on:click={sendMessage}>
  19. <Icon
  20. ios="f7:arrow_up_circle_fill"
  21. aurora="f7:arrow_up_circle_fill"
  22. md="material:send"
  23. />
  24. </a>
  25. <MessagebarAttachments>
  26. {#each attachments as image, index (index)}
  27. <MessagebarAttachment
  28. key={index}
  29. image={image}
  30. onAttachmentDelete={() => deleteAttachment(image)}
  31. ></MessagebarAttachment>
  32. {/each}
  33. </MessagebarAttachments>
  34. <MessagebarSheet>
  35. {#each images as image, index (index)}
  36. <MessagebarSheetImage
  37. key={index}
  38. image={image}
  39. checked={attachments.indexOf(image) >= 0}
  40. onChange={handleAttachment}
  41. ></MessagebarSheetImage>
  42. {/each}
  43. </MessagebarSheet>
  44. </Messagebar>
  45. <Messages>
  46. <MessagesTitle><b>Sunday, Feb 9,</b> 12:58</MessagesTitle>
  47. {#each messagesData as message, index (index)}
  48. <Message
  49. type={message.type}
  50. image={message.image}
  51. name={message.name}
  52. avatar={message.avatar}
  53. first={isFirstMessage(message, index)}
  54. last={isLastMessage(message, index)}
  55. tail={isTailMessage(message, index)}
  56. htmlText={message.text}
  57. />
  58. {/each}
  59. {#if typingMessage}
  60. <Message
  61. type="received"
  62. typing={true}
  63. first={true}
  64. last={true}
  65. tail={true}
  66. header={`${typingMessage.name} is typing`}
  67. avatar={typingMessage.avatar}
  68. ></Message>
  69. {/if}
  70. </Messages>
  71. </Page>
  72. <script>
  73. import { onMount, onDestroy } from 'svelte';
  74. import { f7, f7ready, Navbar, Page, Messages, MessagesTitle, Message, Messagebar, Icon, MessagebarAttachments, MessagebarAttachment, MessagebarSheet, MessagebarSheetImage } from 'framework7-svelte';
  75. let messagebarComponent;
  76. let messagebarInstance;
  77. let attachments = [];
  78. let sheetVisible = false;
  79. let typingMessage = null;
  80. let messageText = '';
  81. let messagesData = [
  82. {
  83. type: 'sent',
  84. text: 'Hi, Kate',
  85. },
  86. {
  87. type: 'sent',
  88. text: 'How are you?',
  89. },
  90. {
  91. name: 'Kate',
  92. type: 'received',
  93. text: 'Hi, I am good!',
  94. avatar: 'https://cdn.framework7.io/placeholder/people-100x100-9.jpg',
  95. },
  96. {
  97. name: 'Blue Ninja',
  98. type: 'received',
  99. text: 'Hi there, I am also fine, thanks! And how are you?',
  100. avatar: 'https://cdn.framework7.io/placeholder/people-100x100-7.jpg',
  101. },
  102. {
  103. type: 'sent',
  104. text: 'Hey, Blue Ninja! Glad to see you ;)',
  105. },
  106. {
  107. type: 'sent',
  108. text: 'Hey, look, cutest kitten ever!',
  109. },
  110. {
  111. type: 'sent',
  112. image: 'https://cdn.framework7.io/placeholder/cats-200x260-4.jpg',
  113. },
  114. {
  115. name: 'Kate',
  116. type: 'received',
  117. text: 'Nice!',
  118. avatar: 'https://cdn.framework7.io/placeholder/people-100x100-9.jpg',
  119. },
  120. {
  121. name: 'Kate',
  122. type: 'received',
  123. text: 'Like it very much!',
  124. avatar: 'https://cdn.framework7.io/placeholder/people-100x100-9.jpg',
  125. },
  126. {
  127. name: 'Blue Ninja',
  128. type: 'received',
  129. text: 'Awesome!',
  130. avatar: 'https://cdn.framework7.io/placeholder/people-100x100-7.jpg',
  131. },
  132. ];
  133. let images = [
  134. 'https://cdn.framework7.io/placeholder/cats-300x300-1.jpg',
  135. 'https://cdn.framework7.io/placeholder/cats-200x300-2.jpg',
  136. 'https://cdn.framework7.io/placeholder/cats-400x300-3.jpg',
  137. 'https://cdn.framework7.io/placeholder/cats-300x150-4.jpg',
  138. 'https://cdn.framework7.io/placeholder/cats-150x300-5.jpg',
  139. 'https://cdn.framework7.io/placeholder/cats-300x300-6.jpg',
  140. 'https://cdn.framework7.io/placeholder/cats-300x300-7.jpg',
  141. 'https://cdn.framework7.io/placeholder/cats-200x300-8.jpg',
  142. 'https://cdn.framework7.io/placeholder/cats-400x300-9.jpg',
  143. 'https://cdn.framework7.io/placeholder/cats-300x150-10.jpg',
  144. ];
  145. let people = [
  146. {
  147. name: 'Kate Johnson',
  148. avatar: 'https://cdn.framework7.io/placeholder/people-100x100-9.jpg',
  149. },
  150. {
  151. name: 'Blue Ninja',
  152. avatar: 'https://cdn.framework7.io/placeholder/people-100x100-7.jpg',
  153. },
  154. ];
  155. let answers = [
  156. 'Yes!',
  157. 'No',
  158. 'Hm...',
  159. 'I am not sure',
  160. 'And what about you?',
  161. 'May be ;)',
  162. 'Lorem ipsum dolor sit amet, consectetur',
  163. 'What?',
  164. 'Are you sure?',
  165. 'Of course',
  166. 'Need to think about it',
  167. 'Amazing!!!',
  168. ];
  169. let responseInProgress = false;
  170. $: attachmentsVisible = attachments.length > 0;
  171. $: placeholder = attachments.length > 0 ? 'Add comment or Send' : 'Message';
  172. onMount(() => {
  173. f7ready(() => {
  174. messagebarInstance = messagebarComponent.instance();
  175. });
  176. });
  177. function isFirstMessage(message, index) {
  178. const previousMessage = messagesData[index - 1];
  179. if (message.isTitle) return false;
  180. if (!previousMessage || previousMessage.type !== message.type || previousMessage.name !== message.name) return true;
  181. return false;
  182. }
  183. function isLastMessage(message, index) {
  184. const nextMessage = messagesData[index + 1];
  185. if (message.isTitle) return false;
  186. if (!nextMessage || nextMessage.type !== message.type || nextMessage.name !== message.name) return true;
  187. return false;
  188. }
  189. function isTailMessage(message, index) {
  190. const nextMessage = messagesData[index + 1];
  191. if (message.isTitle) return false;
  192. if (!nextMessage || nextMessage.type !== message.type || nextMessage.name !== message.name) return true;
  193. return false;
  194. }
  195. function deleteAttachment(image) {
  196. const index = attachments.indexOf(image);
  197. attachments.splice(index, 1);
  198. attachments = attachments;
  199. }
  200. function handleAttachment(e) {
  201. const index = f7.$(e.target).parents('label.checkbox').index();
  202. const image = images[index];
  203. if (e.target.checked) {
  204. // Add to attachments
  205. attachments.unshift(image);
  206. } else {
  207. // Remove from attachments
  208. attachments.splice(attachments.indexOf(image), 1);
  209. }
  210. attachments = attachments;
  211. }
  212. function sendMessage() {
  213. const text = messageText.replace(/\n/g, '<br>').trim();
  214. const messagesToSend = [];
  215. attachments.forEach((attachment) => {
  216. messagesToSend.push({
  217. type: 'sent',
  218. image: attachment,
  219. });
  220. });
  221. if (text.length) {
  222. messagesToSend.push({
  223. text,
  224. type: 'sent',
  225. });
  226. }
  227. if (messagesToSend.length === 0) {
  228. return;
  229. }
  230. // Reset attachments
  231. attachments = [];
  232. // Hide sheet
  233. sheetVisible = false;
  234. // Send message
  235. messagesData = [...messagesData, ...messagesToSend];
  236. // Clear
  237. messageText = '';
  238. messagebarInstance.clear();
  239. // Focus area
  240. if (text.length) messagebarInstance.focus();
  241. // Mock response
  242. if (responseInProgress) return;
  243. responseInProgress = true;
  244. setTimeout(() => {
  245. const answer = answers[Math.floor(Math.random() * answers.length)];
  246. const person = people[Math.floor(Math.random() * people.length)];
  247. typingMessage = {
  248. name: person.name,
  249. avatar: person.avatar,
  250. };
  251. setTimeout(() => {
  252. messagesData = [...messagesData, {
  253. text: answer,
  254. type: 'received',
  255. name: person.name,
  256. avatar: person.avatar,
  257. }];
  258. typingMessage = null;
  259. responseInProgress = false;
  260. }, 4000);
  261. }, 1000);
  262. }
  263. </script>

← Messagebar

Navbar →