Searchbar

    Searchbar allows user to search through List View elements. Or it can be used as a visual UI component for your custom search realization.

    Where:

    • - wrapper for search field and clear button
      • **<input type="search">** - search field
      • **<i class="searchbar-icon">** - search icon
      • **<span class="input-clear-button">** - button to clear field value and reset search results. Optional element
    • **<span class="searchbar-disable-button">** - Searchbar “Cancel” button that will deactivate Searchbar, reset search results and clear search field. Optional element
    • **<div class="searchbar-backdrop">** - semi transparent searchbar backdrop that becomes visible when we enable searchbar. It is recommended to place this element inside of scrollable page’s page-content

    Searchbar Type

    Now let’s see where we can place Searchbar in page structure. There are few options:

    Fixed Searchbar is always visible on screen not depending on page scroll. In this case it must be placed according to one of the following rules:

    • It can be a direct child of page and if page has also fixed Navbar and/or Toolbar then it must be AFTER Navbar and Toolbar:

      1. <div class="page">
      2. <div class="navbar">...</div>
      3. <div class="toolbar">...</div>
      4. <!-- Searchbar goes after Navbar and Toolbar -->
      5. <form class="searchbar">...</form>
      6. <div class="page-content">
      7. <!-- Searchbar backdrop layer -->
      8. <div class="searchbar-backdrop"></div>
      9. <!-- page content here -->
      10. </div>
      11. </div>
      1. <div class="page page-with-subnavbar">
      2. <div class="navbar">
      3. <div class="navbar-inner">
      4. ...
      5. <div class="subnavbar">
      6. <!-- Searchbar inside of Subnavbar -->
      7. <form class="searchbar">...</form>
      8. </div>
      9. </div>
      10. </div>
      11. <div class="page-content">
      12. <!-- Searchbar backdrop layer -->
      13. <div class="searchbar-backdrop"></div>
      14. <!-- page content here -->
      15. </div>
      16. </div>

    In this case Searchbar is just part of the scrollable page content:

    Expandable Searchbar is hidden when disabled and becomes visible when we enable it. Its layout is pretty strict, it must be placed inside of Navbar:

    1. <div class="page">
    2. <div class="navbar">
    3. <div class="navbar-inner">
    4. <div class="left">...</div>
    5. <div class="title">...</div>
    6. <!-- Link to enable searchbar -->
    7. <a class="link icon-only searchbar-enable" data-searchbar=".searchbar">
    8. <i class="icon f7-icons ios-only">search_strong</i>
    9. <i class="icon material-icons md-only">search</i>
    10. </a>
    11. </div>
    12. <!-- Searchbar is a direct child of "navbar-inner" -->
    13. <form class="searchbar searchbar-expandable">
    14. <div class="searchbar-inner">
    15. <div class="searchbar-input-wrap">
    16. <i class="searchbar-icon"></i>
    17. <span class="input-clear-button"></span>
    18. </div>
    19. <span class="searchbar-disable-button">Cancel</span>
    20. </div>
    21. </form>
    22. </div>
    23. </div>
    24. <!-- Scrollable page content -->
    25. <div class="page-content">...</div>
    26. </div>

    Where:

    • **<a class="link icon-only searchbar-enable" data-searchbar=".searchbar">** - link to enable/expand Searchbar. Optional or can be placed in any other place. **data-searchbar** attribute contains CSS selector of Searchbar to enable.
    • Searchbar has additional **searchbar-expandable** class. It is required for expandable Searchbar to work

    There are also several CSS classes that can be added to elements that will define their behavior when Searchbar is active:

    • **searchbar-hide-on-enable** - elements with such class on page will be hidden when searchbar is enabled
    • **searchbar-hide-on-search** - elements with such class on page will be hidden during search
    • **searchbar-not-found** - elements with such class are hidden by default and become visible when there is not any search results
    • **searchbar-found** - elements with such class are visible by default and become hidden when there is not any search results
    • **searchbar-ignore** - searchbar will not consider this elements in search results

    For example:

    1. <div class="page">
    2. <div class="navbar">...</div>
    3. <div class="page-content">
    4. <div class="searchbar-backdrop"></div>
    5. <form class="searchbar">...</form>
    6. <!-- Following block title and block will be hidden on search -->
    7. <div class="block-title searchbar-hide-on-search">Some block title</div>
    8. <div class="block">Lorem ipsum dolor sit amet...</div>
    9. <!-- We do search in super heroes list so the following title and list must be visible on search -->
    10. <div class="block-title searchbar-found">Super Heroes</div>
    11. <div class="list simple-list searchbar-found">
    12. <ul>
    13. <li>Hulk</li>
    14. <li>Batman</li>
    15. <li>Superman</li>
    16. ...
    17. </ul>
    18. </div>
    19. <!-- This list will be visible when there is not any search results -->
    20. <div class="list simple-list searchbar-not-found">
    21. <ul>
    22. </ul>
    23. </div>
    24. </div>
    25. </div>

    Searchbar App Methods

    Let’s look on list of all available parameters we need to create/init Searchbar:

    Searchbar Methods & Properties

    So to create Popup we have to call:

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

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

    Searchbar will fire the following DOM events on searchbar element and events on app and searchbar instance:

    Searchbar instance emits events on both self instance and app instance. App instance events has same names prefixed with searchbar.

    Searchbar Auto Initialization

    1. <div class="page">
    2. <div class="navbar">...</div>
    3. <div class="page-content">
    4. <div class="searchbar-backdrop"></div>
    5. <!-- Searchbar with "searchbar-init" class for auto initialization and searchContainer, searchIn parameters passed in data- attributes -->
    6. <form class="searchbar searchbar-init" data-search-container=".search-here">
    7. ...
    8. </form>
    9. <div class="list simple-list search-list searchbar-found">
    10. <ul>
    11. <li>Hulk</li>
    12. <li>Batman</li>
    13. <li>Superman</li>
    14. ...
    15. </ul>
    16. </div>
    17. ...
    18. </div>
    19. </div>

    Parameters that used in camelCase, for example searchContainer, in data- attributes should be used in kebab-case as data-search-container

    1. var app = new Framework7();
    2. // create searchbar
    3. var searchbar = app.searchbar.create({
    4. el: '.searchbar',
    5. searchContainer: '.list',
    6. searchIn: '.item-title',
    7. on: {
    8. search(sb, query, previousQuery) {
    9. console.log(query, previousQuery);
    10. }