Signature
Import
About this widget
ThesearchBox
widget is used to let users perform a text-based query.
This usually is the main entry point to start the search in an InstantSearch context.
It’s usually placed at the top of a search experience, so that users can start searching right away.
Examples
JavaScript
Options
The CSS Selector or
HTMLElement
to insert the widget into.The placeholder text of the input.
JavaScript
Whether the input should be autofocused.
JavaScript
Whether to update the search state in the middle of a composition session.
This is useful when users need to search using non-latin characters.
JavaScript
If
false
, triggers the search only on submit.JavaScript
Whether to show the reset button.
JavaScript
Whether to show the submit button.
JavaScript
Whether to show the loading indicator (replaces the submit button if the search is stalled).
JavaScript
A function that is called just before the search is triggered. It takes two parameters:
query: string
: the current query stringsearch: function
: a function to trigger the search.
search
method is not called, no search is made to Algolia and the UI doesn’t refresh. If the search
method is called, the widget is rendered.This can be useful if you need to:- Debounce the number of searches done from the
searchBox
. You can find more information in the guide on slow network. - Programmatically alter the query.
JavaScript
The CSS classes you can override:
root
: the root element of the widget.form
: the form element.input
: the input element.reset
: the reset button element.resetIcon
: the reset button icon.loadingIndicator
: the loading indicator element.loadingIcon
: the loading indicator icon.submit
: the submit button element.submitIcon
: the submit button icon.
JavaScript
Templates
You can customize parts of a widget’s UI using the Templates API. Each template includes anhtml
function,
which you can use as a tagged template.
This function safely renders templates as HTML strings and works directly in the browser—no build step required.
For details, see Templating your UI.
The
html
function is available in InstantSearch.js version 4.46.0 or later.The template used for displaying the submit button.
The template used for displaying the reset button.
The template used for displaying the loading indicator.
HTML output
HTML
Customize the UI with connectSearchBox
If you want to create your own UI of the searchBox
widget, you can use connectors.
To use connectSearchBox
, you can import it with the declaration relevant to how you installed InstantSearch.js.
JavaScript
Create a render function
This rendering function is called before the first search (init
lifecycle step)
and each time results come back from Algolia (render
lifecycle step).
JavaScript
Render options
The query from the current search.
JavaScript
Sets a new query and triggers a new search.
JavaScript
Removes the query and triggers a new search.
JavaScript
Returns
true
if the search results take more than a certain time to come back from Algolia servers.
This can be configured on the instantsearch
constructor with the attribute stalledSearchDelay
.JavaScript
All original widget options forwarded to the render function.
JavaScript
Create and instantiate the custom widget
First, create your custom widgets using a rendering function. Then, instantiate them with parameters. There are two kinds of parameters you can pass:- Instance parameters. Predefined options that configure Algolia’s behavior.
- Custom parameters. Parameters you define to make the widget reusable and adaptable.
renderFunction
, both instance and custom parameters are accessible through connector.widgetParams
.
JavaScript
Instance options
A function that is called just before the search is triggered. It takes two parameters
query: string
: the current query stringsearch: function
: a function to trigger the search.
search
method is not called, no search is made to Algolia and the UI doesn’t refresh.
If the search
method is called, the widget is rendered.This can be useful if you need to:- debounce the number of searches done from the
searchBox
. You can find more information in the guide on slow network. - programmatically alter the query.
JavaScript