Messagebar - 图1

Messagebar Svelte Component

Messagebar Svelte component represents Framework7’s Messagebar component.

Messagebar Components

There are following components included:

  • Messagebar - main messagebar element
  • MessagebarSheet - messagebar sheet element
  • MessagebarSheetImage - messagebar sheet image item
  • MessagebarSheetItem - messagebar sheet item
  • MessagebarAttachments - messagebar attachments element
  • MessagebarAttachment - single messagebar attachment element

Messagebar - 图2

Messagebar Properties

PropTypeDefaultDescription
<Messagebar> properties
initbooleantrueInitializes Messagebar
namestringTextarea “name” attribute
textareaIdstring
number
Textarea “id” attribute
placeholderstringMessageTextarea placeholder text
valuestring
number
Textarea value
readonlybooleanfalseSets “readonly” textarea attribute
disabledbooleanfalseSets “disbled” textarea attribute
sendLinkstringEnables Send link and specifies its text. This property will be ignored in case you use send-link slot
maxHeightnumberDefines resizeable textarea max height
resizableboleantrueEnables resizeable textarea
sheetVisiblebooleanfalseMakes messagebar sheet visible/active
attachmentsVisiblebooleanfalseMakes messagebar attachments visible/active
resizePagebooleantrueWhen enabled, it will resize messages page when messagebar textarea size changed
<MessagebarSheetImage> properties
imagestringSheet image URL
checkedbooleanfalseIndicates whether this sheet image-item is checked or not
<MessagebarAttachment> properties
imagestringAttachment image URL
deletablebooleantrueDefines whether the attachment is deletable or not. In case of deletable the additional delete button will be rendered

Messagebar Methods

<Messagebar> methods
.clear()Clear textarea and update/reset its size
.setValue(newValue)Set messagebar textarea value/text
.getValue()Return messagebar textarea value
.resize()Force Messagebar to resize messages page depending on messagebar height/size
.focus()Focus messagebar textarea
.blur()Remove focus from messagebar textarea

Messagebar Events

EventArgumentsDescription
<Messagebar> events
change(event)Event will be triggered when “change” event occurs on messagebar textarea element
input(event)Event will be triggered when “input” event occurs on messagebar textarea element
focus(event)Event will be triggered when “focus” event occurs on messagebar textarea element
blur(event)Event will be triggered when “blur” event occurs on messagebar textarea element
submit
send
(value, clear)Event will be triggered when user clicks on messagebar “send link”
<MessagebarSheetImage> events
change(event)Event will be triggered on sheet item checkbox change
<MessagebarAttachment> events
attachmentClick(event)Event will be triggered on attachment click
attachmentDelete(event)Event will be triggered on attachment delete button click

Messagebar Slots

Messagebar Svelte component has additional slots for custom elements:

  • default - element will be inserted in the end of <div class="toolbar-inner">. element
  • before-area - element will be inserted right before textarea. Messagebar attachments go here
  • after-area - element will be inserted right after textarea
  • send-link - element will be inserted inside of send link
  • before-inner - element will be inserted right before <div class="toolbar-inner">. element
  • after-inner - element will be inserted right after <div class="toolbar-inner">. element. Messagebar sheet goes here
  • inner-start - element will be inserted in the beginning of <div class="toolbar-inner">. element
  • inner-end - element will be inserted in the end of <div class="toolbar-inner">. element. Same as default slot
  1. <Messagebar placeholder="Message">
  2. <div slot="before-inner">Before inner</div>
  3. <div slot="after-inner">After inner</div>
  4. <div slot="before-area">Before textarea</div>
  5. <div slot="after-area">After textarea</div>
  6. <i class="f7-icons" slot="send-link">arrow_up_fill</i>
  7. <div>Default slot</div>
  8. </Messagebar>

Renders to:

  1. <div class="toolbar messagebar">
  2. <div>Before inner</div>
  3. <div class="toolbar-inner">
  4. <div class="messagebar-area">
  5. <div>Before textarea</div>
  6. <textarea placeholder="Message"></textarea>
  7. <div>After textarea</div>
  8. </div>
  9. <a href="#" class="link">
  10. <i class="f7-icons" slot="send-link">arrow_up_fill</i>
  11. </a>
  12. <div>Default slot</div>
  13. </div>
  14. <div>After inner</div>
  15. </div>

Access To Messagebar Instance

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

  1. <Messagebar bind:this={component}>...</Messagebar>
  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 Messages:

  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>

← Menu

Messages →