Searchbar - 图1

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.

Searchbar Layout

  1. <div class="searchbar-backdrop"></div>
  2. <form class="searchbar">
  3. <div class="searchbar-inner">
  4. <div class="searchbar-input-wrap">
  5. <input type="search" placeholder="Search">
  6. <i class="searchbar-icon"></i>
  7. <span class="input-clear-button"></span>
  8. </div>
  9. <span class="searchbar-disable-button">Cancel</span>
  10. </div>
  11. </form>

Where:

  • <div class="searchbar-input-wrap"> - 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

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 toolbar-bottom">...</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>
  • It can be placed inside of Subnavbar which is inside of the Navbar (recommended way):

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

Static Searchbar

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

  1. <div class="page">
  2. <div class="navbar">...</div>
  3. <div class="page-content">
  4. <!-- Searchbar backdrop layer -->
  5. <div class="searchbar-backdrop"></div>
  6. <!-- Searchbar is part of scrollable page content -->
  7. <form class="searchbar">...</form>
  8. <!-- page content here -->
  9. </div>
  10. </div>

Expandable Searchbar

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-bg"></div>
  4. <div class="navbar-inner">
  5. <div class="left">...</div>
  6. <div class="title">...</div>
  7. <div class="right">
  8. <!-- Link to enable searchbar -->
  9. <a class="link icon-only searchbar-enable" data-searchbar=".searchbar">
  10. <i class="icon f7-icons if-not-md">search</i>
  11. <i class="icon material-icons md-only">search</i>
  12. </a>
  13. </div>
  14. <!-- Searchbar is a direct child of "navbar-inner" -->
  15. <form class="searchbar searchbar-expandable">
  16. <div class="searchbar-inner">
  17. <div class="searchbar-input-wrap">
  18. <input type="search" placeholder="Search"/>
  19. <i class="searchbar-icon"></i>
  20. <span class="input-clear-button"></span>
  21. </div>
  22. <span class="searchbar-disable-button">Cancel</span>
  23. </div>
  24. </form>
  25. </div>
  26. </div>
  27. <!-- Scrollable page content -->
  28. <div class="page-content">...</div>
  29. </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

Inline Searchbar

Inline Searchbar is designed to be used inside of other components, specifically inside of Appbar. It has simplified layout - without .searchbar-inner, and it is recommended to use it without disable button.

We just need to add searchbar-inline class to Searchbar to make it inline

  1. <!-- Additional "searchbar-inline" class -->
  2. <div class="searchbar searchbar-inline">
  3. <div class="searchbar-input-wrap">
  4. <input type="search" placeholder="Search">
  5. <i class="searchbar-icon"></i>
  6. <span class="input-clear-button"></span>
  7. </div>
  8. </div>

Searchbar Behavior Classes

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. <li>Nothing found</li>
  23. </ul>
  24. </div>
  25. </div>
  26. </div>

Searchbar App Methods

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

app.searchbar.create(parameters)Initialize Searchbar with parameters
  • parameters - object - object with Searchbar parameters
  • Method returns initialized Searchbar instance
app.searchbar.destroy(el)Destroy Searchbar instance
  • el - HTMLElement or string (with CSS Selector) or object. Searchbar element or Searchbar instance to destroy.
app.searchbar.get(el)Get Searchbar instance by HTML element
  • el - HTMLElement or string (with CSS Selector). Searchbar element.
  • Method returns Searchbar’s instance
app.searchbar.clear(el)Clear Searchbar text input
  • el - HTMLElement or string (with CSS Selector). Searchbar element.
  • Method returns Searchbar’s instance
app.searchbar.enable(el)Enable Searchbar
  • el - HTMLElement or string (with CSS Selector). Searchbar element.
  • Method returns Searchbar’s instance
app.searchbar.disable(el)Disable Searchbar
  • el - HTMLElement or string (with CSS Selector). Searchbar element.
  • Method returns Searchbar’s instance
app.searchbar.toggle(el)Toggle Searchbar: enable if it was disabled, or disable if it was enabled
  • el - HTMLElement or string (with CSS Selector). Searchbar element.
  • Method returns Searchbar’s instance
app.searchbar.search(el, query)Trigger searchbar to search query
  • el - HTMLElement or string (with CSS Selector). Searchbar element.
  • query - string. Search query.
  • Method returns Searchbar’s instance

Searchbar Parameters

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

ParameterTypeDefaultDescription
elstring
HTMLElement
CSS selector or HTML element of searchbar element (form class=”searchbar”)
inputElstring
HTMLElement
CSS selector or HTML element of searchbar input element. By default (if not passed) will try to look for input type=”search” inside of searchbar
disableButtonbooleantrueEnables disable button
disableButtonElstring
HTMLElement
CSS selector or HTML element of searchbar disable button. By default (if not passed) will try to look for element with search-disable-button class inside of searchbar
searchContainerstring
HTMLElement
CSS selector or HTML element of list block to search in
searchInstringCSS selector of List View element’s field where we need to search. Usually we search through element titles, in this case we need to pass .item-title. It is also possible to pass few elements for search like .item-title, .item-text
searchItemstringliCSS selector of single search item. If we do a search in List View, then it must be a single list element li
searchGroupstring.list-groupCSS selector of group element. Used when hideGroups enabled to hide groups. If we do a search in List View, then it usually a list group.
searchGroupTitlestring.list-group-title, .item-dividerCSS selector of group titles and dividers. Used when hideDividers enabled to hide group titles and dividers. If we do a search in List View, then it usually a list group title or list item divider.
foundElstring
HTMLElement
.searchbar-foundCSS selector or HTMLElement of searchbar “found” element to make it hidden when there is no search results
notFoundElstring
HTMLElement
.searchbar-not-foundCSS selector or HTMLElement of searchbar “not-found” element to make it visible when there is no search results
hideOnEnableElstring
HTMLElement
.searchbar-hide-on-enableCSS selector or HTMLElement of elements to be hidden when searchbar enabled
hideOnSearchElstring
HTMLElement
.searchbar-hide-on-searchCSS selector or HTMLElement of elements to be hidden on searchbar search
backdropbooleanEnables searchbar backdrop element. By default, disabled for Aurora theme or for inline searchbar
backdropElstring
HTMLElement
CSS selector or HTMLElement of searchbar backdrop element. If not passed and backdrop parameter is true then it will look for .searchbar-backdrop element. In case none found it will create one automatically
ignorestring.searchbar-ignoreCSS selector for items to be ignored by searchbar and always present in search results
customSearchbooleanfalseWhen enabled searchbar will not search through any of list blocks specified by searchContainer and you will be able to use custom search functionality, for example, for calling external APIs with search results and for displaying them manually
removeDiacriticsbooleanfalseEnable to remove/replace diacritics (á, í, ó, etc.) during search
hideDividersbooleantrueIf enabled, then search will consider item dividers and group titles and hide them if there are no found items right after them
hideGroupsbooleantrueIf enabled, then search will consider list view groups hide them if there are no found items inside of these groups
onobject

Object with events handlers. For example:

  1. var searchbar = app.searchbar.create({
  2. el: ‘.searchbar’,
  3. on: {
  4. enable: function () {
  5. console.log(‘Searchbar enabled’)
  6. }
  7. }
  8. })

Searchbar Methods & Properties

To create a Searchbar 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:

Properties
searchbar.paramsObject with passed initialization parameters
searchbar.queryCurrent search query (search input value)
searchbar.previousQueryPrevious search query (search input value)
searchbar.searchContainerSearchbar search container
searchbar.$searchContainerDom7 element with searchbar search container
searchbar.elSearchbar HTML element.
searchbar.$elDom7 element with searchbar HTML element.
searchbar.inputElSearchbar input HTML element
searchbar.$inputElDom7 element with searchbar input HTML element
searchbar.enabledBoolean value that represents is searchbar enabled or disabled
searchbar.expandableBoolean value that represents is searchbar expandable or not
Methods
searchbar.search(query);Force searchbar to search passed query
searchbar.enable();Enable/activate searchbar
searchbar.disable();Disable/deactivate searchbar
searchbar.toggle();Toggle searchbar
searchbar.clear();Clear search query and update results
searchbar.destroy();Destroy searchbar instance
searchbar.on(event, handler)Add event handler
searchbar.once(event, handler)Add event handler that will be removed after it was fired
searchbar.off(event, handler)Remove event handler
searchbar.off(event)Remove all handlers for specified event
searchbar.emit(event, …args)Fire event on instance

Searchbar Events

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

DOM Events

EventTargetDescription
searchbar:searchSearchbar Element<form class=”searchbar”>Event will be triggered during search (search field change). Event detail (e.detail) contains search query
searchbar:clearSearchbar Element<form class=”searchbar”>Event will be triggered when user clicks on Searchbar’s “clear” element (a href=”#” class=”searchbar-clear”). Event detail (e.detail) contains previous (before clear) search query
searchbar:enableSearchbar Element<form class=”searchbar”>Event will be triggered when Searchbar becomes active/enabled
searchbar:disableSearchbar Element<form class=”searchbar”>Event will be triggered when Searchbar becomes disabled/inactive - when user clicks on “Cancel” button (a href=”searchbar-cancel”) or on “searchbar-overlay” element
searchbar:beforedestroySearchbar Element<form class=”searchbar”>Event will be triggered right before Searchbar instance will be destroyed

App and Searchbar Instance Events

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

EventTargetArgumentsDescription
searchsearchbar(searchbar, query, previousQuery)Event will be triggered during search (search field change). As an argument event handler receives searchbar instance, current query and previous query
searchbarSearchapp
clearsearchbar(searchbar, previousQuery)Event will be triggered when user clicks on Searchbar’s “clear” element . As an argument event handler receives searchbar instance and previous (before clear) query
searchbarClearapp
enablesearchbar(searchbar)Event will be triggered when Searchbar becomes active/enabled. As an argument event handler receives searchbar instance
searchbarEnableapp
disablesearchbar(searchbar)Event will be triggered when Searchbar becomes inactive/disabled. As an argument event handler receives searchbar instance
searchbarDisableapp
beforeDestroysearchbar(searchbar)Event will be triggered right before Searchbar instance will be destroyed
searchbarBeforeDestroyapp

Searchbar Auto Initialization

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

  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

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. /*
  3. --f7-searchbar-link-color: var(--f7-bars-link-color);
  4. --f7-searchbar-inline-input-font-size: var(--f7-searchbar-input-font-size);
  5. --f7-searchbar-inline-input-height: var(--f7-searchbar-input-height);
  6. */
  7. --f7-searchbar-input-border-width: 0px;
  8. --f7-searchbar-input-border-color: transparent;
  9. --f7-searchbar-input-text-color: #000;
  10. --f7-searchbar-placeholder-color: rgba(0, 0, 0, 0.4);
  11. }
  12. :root .theme-dark,
  13. :root.theme-dark {
  14. --f7-searchbar-input-text-color: #fff;
  15. --f7-searchbar-placeholder-color: rgba(255, 255, 255, 0.4);
  16. }
  17. .ios {
  18. /*
  19. --f7-searchbar-bg-image: var(--f7-bars-bg-image);
  20. --f7-searchbar-bg-color: var(--f7-bars-bg-color);
  21. --f7-searchbar-bg-color-rgb: var(--f7-bars-bg-color-rgb);
  22. --f7-searchbar-border-color: var(--f7-bars-border-color);
  23. */
  24. --f7-searchbar-height: 44px;
  25. --f7-searchbar-inner-padding-left: 8px;
  26. --f7-searchbar-inner-padding-right: 8px;
  27. /*
  28. --f7-searchbar-link-color: var(--f7-bars-link-color, var(--f7-theme-color));
  29. */
  30. --f7-searchbar-input-font-size: 17px;
  31. --f7-searchbar-input-border-radius: 8px;
  32. --f7-searchbar-input-height: 32px;
  33. --f7-searchbar-input-padding-horizontal: 28px;
  34. /*
  35. --f7-searchbar-inline-input-padding-horizontal: var(--f7-searchbar-input-padding-horizontal);
  36. --f7-searchbar-input-clear-button-color: var(--f7-input-clear-button-color);
  37. */
  38. --f7-searchbar-backdrop-bg-color: rgba(0, 0, 0, 0.4);
  39. --f7-searchbar-shadow-image: none;
  40. --f7-searchbar-in-page-content-margin: 0px;
  41. --f7-searchbar-in-page-content-box-shadow: none;
  42. --f7-searchbar-in-page-content-border-radius: 0;
  43. --f7-searchbar-in-page-content-input-border-radius: 0;
  44. --f7-searchbar-search-icon-color: rgba(0, 0, 0, 0.4);
  45. --f7-searchbar-input-bg-color: #e4e4e4;
  46. }
  47. .ios .theme-dark,
  48. .ios.theme-dark {
  49. --f7-searchbar-search-icon-color: rgba(255, 255, 255, 0.4);
  50. --f7-searchbar-input-bg-color: #2a2a2a;
  51. }
  52. .md {
  53. --f7-searchbar-border-color: transparent;
  54. --f7-searchbar-height: 48px;
  55. --f7-searchbar-inner-padding-left: 0px;
  56. --f7-searchbar-inner-padding-right: 0px;
  57. --f7-searchbar-link-color: #737373;
  58. --f7-searchbar-search-icon-color: #737373;
  59. --f7-searchbar-input-font-size: 20px;
  60. --f7-searchbar-input-border-radius: 0px;
  61. --f7-searchbar-input-height: 100%;
  62. --f7-searchbar-input-padding-horizontal: 48px;
  63. --f7-searchbar-inline-input-padding-horizontal: 24px;
  64. --f7-searchbar-input-clear-button-color: #737373;
  65. --f7-searchbar-backdrop-bg-color: rgba(0, 0, 0, 0.25);
  66. --f7-searchbar-shadow-image: var(--f7-bars-shadow-bottom-image);
  67. --f7-searchbar-in-page-content-margin: 8px;
  68. --f7-searchbar-in-page-content-box-shadow: var(--f7-elevation-1);
  69. --f7-searchbar-in-page-content-border-radius: 4px;
  70. --f7-searchbar-in-page-content-input-border-radius: 4px;
  71. --f7-searchbar-bg-color: #fff;
  72. --f7-searchbar-input-bg-color: #fff;
  73. }
  74. .md .theme-dark,
  75. .md.theme-dark {
  76. --f7-searchbar-bg-color: #202020;
  77. --f7-searchbar-input-bg-color: #202020;
  78. }
  79. .aurora {
  80. /*
  81. --f7-searchbar-bg-image: var(--f7-bars-bg-image);
  82. --f7-searchbar-bg-color: var(--f7-bars-bg-color);
  83. --f7-searchbar-border-color: var(--f7-bars-border-color);
  84. */
  85. --f7-searchbar-height: 38px;
  86. --f7-searchbar-inner-padding-left: 8px;
  87. --f7-searchbar-inner-padding-right: 8px;
  88. /*
  89. --f7-searchbar-link-color: var(--f7-bars-link-color, var(--f7-theme-color));
  90. */
  91. --f7-searchbar-input-font-size: 13px;
  92. --f7-searchbar-input-border-radius: 4px;
  93. --f7-searchbar-input-height: 24px;
  94. --f7-searchbar-input-padding-horizontal: 24px;
  95. /*
  96. --f7-searchbar-inline-input-padding-horizontal: var(--f7-searchbar-input-padding-horizontal;
  97. --f7-searchbar-input-clear-button-color: var(--f7-input-clear-button-color);
  98. */
  99. --f7-searchbar-backdrop-bg-color: rgba(0, 0, 0, 0.4);
  100. --f7-searchbar-shadow-image: none;
  101. --f7-searchbar-in-page-content-margin: 0px;
  102. --f7-searchbar-in-page-content-box-shadow: none;
  103. --f7-searchbar-in-page-content-border-radius: 0;
  104. /*
  105. --f7-searchbar-in-page-content-input-border-radius: var(--f7-searchbar-input-border-radius);
  106. */
  107. --f7-searchbar-search-icon-color: rgba(0, 0, 0, 0.4);
  108. --f7-searchbar-input-bg-color: #fff;
  109. }
  110. .aurora .theme-dark,
  111. .aurora.theme-dark {
  112. --f7-searchbar-input-bg-color: #333;
  113. --f7-searchbar-search-icon-color: rgba(255, 255, 255, 0.4);
  114. }

Example

  1. <div class="page">
  2. <div class="navbar">
  3. <div class="navbar-bg"></div>
  4. <div class="navbar-inner">
  5. <div class="title">Search Bar</div>
  6. <div class="subnavbar">
  7. <!-- Searchbar with auto init -->
  8. <form class="searchbar">
  9. <div class="searchbar-inner">
  10. <div class="searchbar-input-wrap">
  11. <input type="search" placeholder="Search">
  12. <i class="searchbar-icon"></i>
  13. <span class="input-clear-button"></span>
  14. </div>
  15. <span class="searchbar-disable-button if-not-aurora">Cancel</span>
  16. </div>
  17. </form>
  18. </div>
  19. </div>
  20. </div>
  21. <div class="page-content">
  22. <!-- Searchbar backdrop -->
  23. <div class="searchbar-backdrop"></div>
  24. <!-- hide-on-search element -->
  25. <div class="block searchbar-hide-on-search">
  26. <p>This block will be hidden on search. Lorem ipsum dolor sit amet, consectetur adipisicing elit.</p>
  27. </div>
  28. <!-- search target list -->
  29. <div class="list searchbar-found">
  30. <ul>
  31. <li class="item-content">
  32. <div class="item-inner">
  33. <div class="item-title">Acura</div>
  34. </div>
  35. </li>
  36. <li class="item-content">
  37. <div class="item-inner">
  38. <div class="item-title">Audi</div>
  39. </div>
  40. </li>
  41. <li class="item-content">
  42. <div class="item-inner">
  43. <div class="item-title">BMW</div>
  44. </div>
  45. </li>
  46. ...
  47. <li class="item-content">
  48. <div class="item-inner">
  49. <div class="item-title">Volvo</div>
  50. </div>
  51. </li>
  52. </ul>
  53. </div>
  54. <!-- Nothing found message -->
  55. <div class="block searchbar-not-found">
  56. <div class="block-inner">Nothing found</div>
  57. </div>
  58. </div>
  59. </div>
  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. }
  11. }
  12. });

← Range Slider

Sheet Modal →