Messagebar - 图1

Messagebar

Framework7 comes with special resizealbe toolbar for usage with Messages

Messagebar Layout

Messagebar layour is pretty simple:

  1. <div class="toolbar messagebar">
  2. <div class="toolbar-inner">
  3. <div class="messagebar-area">
  4. <!-- messagebar attachments -->
  5. <div class="messagebar-attachments">...</div>
  6. <!-- messagebar resizable textarea -->
  7. <textarea class="resizable" placeholder="Message"></textarea>
  8. </div>
  9. <a href="#" class="link">Send</a>
  10. </div>
  11. <!-- messagebar sheet -->
  12. <div class="messagebar-sheet">...</div>
  13. </div>

Where

  • messagebar-attachments - block with messagebar attachments. Optional
  • messagebar-sheet - block with messagebar sheet. Optional

Messagebar place is very important, it should be inside of page and right before page-content:

  1. <div class="page">
  2. <!-- navbar -->
  3. <div class="navbar">...</div>
  4. <!-- messagebar -->
  5. <div class="toolbar messagebar">...</div>
  6. <!-- page-content/messages-content -->
  7. <div class="page-content messages-content">
  8. ... messages
  9. </div>
  10. </div>

Messagebar Sheet Layout

If need additional sheet with, for example, images that we can attach to message then we use extra block designed for this:

  1. <div class="messagebar-sheet">
  2. <!-- selectable sheet image -->
  3. <label class="checkbox messagebar-sheet-image" style="background-image:url(path/to/image1.png)">
  4. <input type="checkbox">
  5. <i class="icon icon-checkbox"></i>
  6. </label>
  7. <!-- another selectable sheet image -->
  8. <label class="checkbox messagebar-sheet-image" style="background-image:url(path/to/image2.png)">
  9. <input type="checkbox">
  10. <i class="icon icon-checkbox"></i>
  11. </label>
  12. <!-- some custom sheet item -->
  13. <div class="messagebar-sheet-item">
  14. <!-- any custom content here -->
  15. </div>
  16. </div>

Messagebar Attachments Layout

Messages attachments block is designed to display currently attached message items/images:

  1. <div class="messagebar-attachments">
  2. <!-- image attachment -->
  3. <div class="messagebar-attachment">
  4. <img src="path/to/image1.png">
  5. </div>
  6. <!-- deletable image attachment -->
  7. <div class="messagebar-attachment">
  8. <img src="path/to/image2.png">
  9. <!-- attachment delete button -->
  10. <span class="messagebar-attachment-delete"></span>
  11. </div>
  12. </div>

Messagebar App Methods

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

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

Messagebar Parameters

Let’s look on list of all available parameters:

ParameterTypeDefaultDescription
elstring
HTMLElement
CSS selector or HTML element of messagebar element (div class=”messagebar”)
textareaElstring
HTMLElement
CSS selector or HTML element of messagebar textarea element. By default (if not passed) will try to look for textarea inside of messagebar
maxHeightnumbernullMax height of textarea when it resized depending on amount of its text
attachmentsarray[]Array with attachments. For example [‘path/to/image1.png’, ‘path/to/image2.png’]
resizePagebooleantrueDisable if you don’t want to resize messages page when messagebar textarea size changed
onobject

Object with events handlers. For example:

  1. var messagebar = app.messagebar.create({
  2. el: ‘.messagebar’,
  3. on: {
  4. change: function () {
  5. console.log(‘Textarea value changed’)
  6. }
  7. }
  8. })
Render functions
renderAttachmentsfunction(attachments)Function to render attachments block. Must return full attachments HTML string
renderAttachmentfunction(attachment)Function to render single attachment. Must return full attachment HTML string

Messagebar Methods & Properties

So to create Messagebar we have to call:

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

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

Properties
messagebar.elMessagebar HTML element.
messagebar.$elDom7 element with messagebar HTML element.
messagebar.textareaElMessagebar textarea HTML element
messagebar.$textareaElDom7 element with messagebar textarea HTML element
messagebar.paramsObject with passed initialization parameters
messagebar.attachmentsArray with messagebar attachments
Methods
messagebar.getValue();Get messagebar textarea value
messagebar.setValue(value);Set messagebar textarea value/text
messagebar.clear();Clear textarea and update/reset its size
messagebar.focus();Focus messagebar textarea
messagebar.blur();Remove focus from messagebar textarea
messagebar.setPlaceholder(placeholder)Set/change messagebar placeholder text
messagebar.resizePage()Force Messagebar to resize messages page depending on messagebar height/size
messagebar.attachmentsCreate()Dynamically create attachments block HTML element
messagebar.attachmentsShow()Show attachments block
messagebar.attachmentsHide()Hide attachments block
messagebar.attachmentsToggle()Toggle attachments block
messagebar.renderAttachments()Render attachments block based on attachments data
messagebar.sheetCreate()Dynamically create messagebar sheet block HTML element
messagebar.sheetShow()Show messagebar sheet
messagebar.sheetHide()Hide messagebar sheet
messagebar.sheetToggle()Toggle messagebar sheet
messagebar.destroy();Destroy messagebar instance

Messagebar Events

Messagebar will fire the following DOM events on messagebar element and events on app and messagebar instance:

DOM Events

EventTargetDescription
messagebar:changeMessagebar Element<div class=”messagebar”>Event will be triggered after messagebar textarea value changed
messagebar:focusMessagebar Element<div class=”messagebar”>Event will be triggered when messagebar textarea gets focus
messagebar:blurMessagebar Element<div class=”messagebar”>Event will be triggered when messagebar textarea loses focus
messagebar:resizepageMessagebar Element<div class=”messagebar”>Event will be triggered when messagebar resizes messages page
messagebar:attachmentdeleteMessagebar attachment element<div class=”messagebar-attachment”>Event will be triggered after click on messagebar attachment delete button
messagebar:attachmentclickMessagebar attachment element<div class=”messagebar-attachment”>Event will be triggered on messagebar attachment click
messagebar:beforedestroyMessagebar Element<div class=”messagebar”>Event will be triggered right before Messagebar instance will be destroyed

App and Messagebar Instance Events

Messagebar instance emits events on both self instance and app instance. App instance events has same names prefixed with messagebar.

EventTargetArgumentsDescription
changemessagebar(messagebar)Event will be triggered after messagebar textarea value changed. As an argument event handler receives messagebar instance
messagebarChangeapp
focusmessagebar(messagebar)Event will be triggered when messagebar textarea gets focus. As an argument event handler receives messagebar instance
messagebarFocusapp
blurmessagebar(messagebar)Event will be triggered when messagebar textarea loses focus. As an argument event handler receives messagebar instance
messagebarBlurapp
resizePagemessagebar(messagebar)Event will be triggered when messagebar resizes messages page. As an argument event handler receives messagebar instance
messagebarResizePageapp
attachmentDeletemessagebar(messagebar, attachmentEl, attachmentIndex)Event will be triggered after click on messagebar attachment delete button. As an argument event handler receives messagebar instance, clicked attachment HTML element and attachment index number
messagebarAttachmentDeleteapp
attachmentClickmessagebar(messagebar, attachmentEl, attachmentIndex)Event will be triggered on messagebar attachment click. As an argument event handler receives messagebar instance, clicked attachment HTML element and attachment index number
messagebarAttachmentClickapp
beforeDestroymessagebar(messagebar)Event will be triggered right before Messagebar instance will be destroyed
messagebarBeforeDestroyapp

Messagebar Auto Initialization

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

  1. <div class="toolbar messagebar messagebar-init" data-max-height="200">
  2. <div class="toolbar-inner">
  3. <div class="messagebar-area">
  4. <textarea placeholder="Message"></textarea>
  5. </div>
  6. <a href="#" class="link">Send</a>
  7. </div>
  8. </div>

Parameters that used in camelCase, for example maxHeight, in data- attributes should be used in kebab-case as data-max-height

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-messagebar-shadow-image: none;
  3. --f7-messagebar-textarea-bg-color: transparent;
  4. --f7-messagebar-attachments-height: 155px;
  5. --f7-messagebar-attachment-height: 155px;
  6. --f7-messagebar-attachment-landscape-height: 120px;
  7. --f7-messagebar-sheet-height: 252px;
  8. --f7-messagebar-sheet-landscape-height: 192px;
  9. /*
  10. --f7-messagebar-inner-padding-left: var(--f7-toolbar-inner-padding-left);
  11. --f7-messagebar-inner-padding-right: var(--f7-toolbar-inner-padding-right);
  12. */
  13. --f7-messagebar-bg-color: #fff;
  14. --f7-messagebar-bg-color-rgb: 255, 255, 255;
  15. }
  16. :root .theme-dark,
  17. :root.theme-dark {
  18. --f7-messagebar-bg-color: var(--f7-bars-bg-color);
  19. --f7-messagebar-bg-color-rgb: var(--f7-bars-bg-color-rgb);
  20. }
  21. .ios {
  22. --f7-messagebar-height: 44px;
  23. --f7-messagebar-font-size: 17px;
  24. /*
  25. --f7-messagebar-link-color: var(--f7-theme-color);
  26. */
  27. --f7-messagebar-border-color: transparent;
  28. --f7-messagebar-textarea-border-radius: 17px;
  29. --f7-messagebar-textarea-padding: 6px 16px;
  30. --f7-messagebar-textarea-height: 34px;
  31. --f7-messagebar-textarea-font-size: 17px;
  32. --f7-messagebar-textarea-line-height: 20px;
  33. --f7-messagebar-sheet-bg-color: #d1d5da;
  34. --f7-messagebar-attachment-border-radius: 12px;
  35. --f7-messagebar-textarea-text-color: #000;
  36. --f7-messagebar-textarea-border: 1px solid #c8c8cd;
  37. --f7-messagebar-attachments-border-color: #c8c8cd;
  38. }
  39. .ios .theme-dark,
  40. .ios.theme-dark {
  41. --f7-messagebar-textarea-text-color: #fff;
  42. --f7-messagebar-textarea-border: 1px solid var(--f7-bars-border-color);
  43. --f7-messagebar-attachments-border-color: var(--f7-bars-border-color);
  44. }
  45. .md {
  46. --f7-messagebar-height: 48px;
  47. --f7-messagebar-font-size: 16px;
  48. --f7-messagebar-textarea-border-radius: 0px;
  49. --f7-messagebar-textarea-padding: 5px 8px;
  50. --f7-messagebar-textarea-height: 32px;
  51. --f7-messagebar-textarea-font-size: 16px;
  52. --f7-messagebar-textarea-line-height: 22px;
  53. --f7-messagebar-textarea-border: 1px solid transparent;
  54. --f7-messagebar-sheet-bg-color: #fff;
  55. --f7-messagebar-attachment-border-radius: 4px;
  56. --f7-messagebar-link-color: #333;
  57. --f7-messagebar-border-color: #d1d1d1;
  58. --f7-messagebar-textarea-text-color: #333;
  59. --f7-messagebar-attachments-border-color: #ddd;
  60. }
  61. .md .theme-dark,
  62. .md.theme-dark {
  63. --f7-messagebar-border-color: transparent;
  64. --f7-messagebar-link-color: rgba(255, 255, 255, 0.87);
  65. --f7-messagebar-textarea-text-color: rgba(255, 255, 255, 0.87);
  66. --f7-messagebar-attachments-border-color: rgba(255, 255, 255, 0.2);
  67. }
  68. .aurora {
  69. --f7-messagebar-height: 38px;
  70. --f7-messagebar-font-size: 14px;
  71. /*
  72. --f7-messagebar-link-color: var(--f7-theme-color);
  73. */
  74. --f7-messagebar-border-color: transparent;
  75. --f7-messagebar-textarea-border-radius: 12px;
  76. --f7-messagebar-textarea-padding: 3px 10px;
  77. --f7-messagebar-textarea-height: 24px;
  78. --f7-messagebar-textarea-font-size: 13px;
  79. --f7-messagebar-textarea-line-height: 16px;
  80. --f7-messagebar-sheet-bg-color: #fff;
  81. --f7-messagebar-attachment-border-radius: 12px;
  82. --f7-messagebar-textarea-text-color: #000;
  83. --f7-messagebar-textarea-border: 1px solid #c8c8cd;
  84. --f7-messagebar-attachments-border-color: #c8c8cd;
  85. }
  86. .aurora .theme-dark,
  87. .aurora.theme-dark {
  88. --f7-messagebar-textarea-text-color: #fff;
  89. --f7-messagebar-textarea-border: 1px solid var(--f7-bars-border-color);
  90. --f7-messagebar-attachments-border-color: var(--f7-bars-border-color);
  91. }

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. <a class="link toggle-sheet" href="#">
  11. <i class="icon f7-icons if-not-md">camera_fill</i>
  12. <i class="icon material-icons md-only">camera_alt</i>
  13. </a>
  14. <div class="messagebar-area">
  15. <textarea placeholder="Message"></textarea>
  16. </div>
  17. <a class="link" href="#">Send</a>
  18. </div>
  19. <div class="messagebar-sheet"></div>
  20. </div>
  21. <div class="page-content messages-content">
  22. <div class="messages">
  23. ...
  24. </div>
  25. </div>
  26. </div>
  1. var app = new Framework7();
  2. var $$ = Dom7;
  3. // Init Messages
  4. var messages = app.messages.create({
  5. ...
  6. });
  7. // Init messagebar
  8. var messagebar = app.messagebar.create({
  9. el: '.messagebar',
  10. attachments: []
  11. });
  12. // Available Images
  13. var images = [
  14. 'https://cdn.framework7.io/placeholder/cats-300x300-1.jpg',
  15. 'https://cdn.framework7.io/placeholder/cats-200x300-2.jpg',
  16. 'https://cdn.framework7.io/placeholder/cats-400x300-3.jpg',
  17. 'https://cdn.framework7.io/placeholder/cats-300x150-4.jpg',
  18. 'https://cdn.framework7.io/placeholder/cats-150x300-5.jpg',
  19. 'https://cdn.framework7.io/placeholder/cats-300x300-6.jpg',
  20. 'https://cdn.framework7.io/placeholder/cats-300x300-7.jpg',
  21. 'https://cdn.framework7.io/placeholder/cats-200x300-8.jpg',
  22. 'https://cdn.framework7.io/placeholder/cats-400x300-9.jpg',
  23. 'https://cdn.framework7.io/placeholder/cats-300x150-10.jpg'
  24. ]
  25. // Create sheet items
  26. var sheetHtml = '';
  27. images.forEach(function (image) {
  28. sheetHtml +=
  29. '<label class="checkbox messagebar-sheet-image" style="background-image:url(' + image + ')">' +
  30. '<input type="checkbox">' +
  31. '<i class="icon icon-checkbox"></i>' +
  32. '</label>';
  33. });
  34. $$('.messagebar-sheet').html(sheetHtml);
  35. /*========================
  36. Handle Attachments
  37. ========================*/
  38. function checkAttachments() {
  39. if (messagebar.attachments.length > 0) {
  40. messagebar.attachmentsShow();
  41. messagebar.setPlaceholder('Add comment or Send');
  42. } else {
  43. messagebar.attachmentsHide();
  44. messagebar.setPlaceholder('Message');
  45. }
  46. }
  47. $$('.messagebar-sheet-image input').on('change', function (e) {
  48. var index = $$(e.target).parents('.messagebar-sheet-image').index();
  49. var image = images[index];
  50. if (e.target.checked) {
  51. // Add to attachments
  52. messagebar.attachments.unshift(image)
  53. } else {
  54. // Remove from attachments
  55. messagebar.attachments.splice(messagebar.attachments.indexOf(image), 1);
  56. }
  57. messagebar.renderAttachments();
  58. checkAttachments();
  59. });
  60. messagebar.on('attachmentDelete', function (messagebar, attachmentEl, attachmentIndex) {
  61. var image = messagebar.attachments.splice(attachmentIndex, 1)[0];
  62. messagebar.renderAttachments();
  63. checkAttachments();
  64. // Uncheck in sheet
  65. var imageIndex = images.indexOf(image);
  66. messagebar.$el.find('.messagebar-sheet .checkbox').eq(imageIndex).find('input').prop('checked', false);
  67. });
  68. /*========================
  69. Toggle Sheet
  70. ========================*/
  71. $$('a.toggle-sheet').on('click', function () {
  72. messagebar.sheetToggle();
  73. });

← Menu

Messages →