The searchBox widget creates an input field where users can type their search queries. This is typically the main entry point for search interactions.
Usage
const searchBox = instantsearch.widgets.searchBox({
container: '#search-box',
placeholder: 'Search for products...',
searchAsYouType: true,
showReset: true,
showSubmit: true,
showLoadingIndicator: true,
});
search.addWidget(searchBox);
Examples
Basic Search Box
instantsearch.widgets.searchBox({
container: '#search-box',
});
With Custom Placeholder
instantsearch.widgets.searchBox({
container: '#search-box',
placeholder: 'Search our catalog...',
autofocus: true,
});
With Debounced Search
instantsearch.widgets.searchBox({
container: '#search-box',
queryHook(query, search) {
clearTimeout(searchTimeout);
searchTimeout = setTimeout(() => search(query), 500);
},
});
Search on Enter Only
instantsearch.widgets.searchBox({
container: '#search-box',
searchAsYouType: false,
});
Options
container
string | HTMLElement
required
CSS Selector or HTMLElement to insert the widget.
The placeholder text shown in the input field.
Whether the input should be autofocused when the page loads.
If set to false, the search will only be triggered when pressing Enter.
Whether to update the search state during IME composition (useful for Asian languages).
Whether to show the reset button to clear the query.
Whether to show the submit button.
Whether to show a loading indicator when the search is stalled.
A function called on each keystroke to transform or debounce the query.(query: string, search: (query: string) => void) => void
Templates to customize the widget rendering.
Template for the submit button.
Template for the reset button.
Template for the loading indicator.
CSS classes to add to the widget elements.
CSS class for the root element.
CSS class for the form element.
CSS class for the input element.
CSS class for the submit button.
CSS class for the submit icon.
CSS class for the reset button.
CSS class for the reset icon.
CSS class for the loading indicator.
CSS class for the loading icon.
HTML Output
<div class="ais-SearchBox">
<form class="ais-SearchBox-form" novalidate>
<input
class="ais-SearchBox-input"
autocomplete="off"
autocorrect="off"
autocapitalize="off"
placeholder="Search for products"
spellcheck="false"
maxlength="512"
type="search"
value=""
/>
<button class="ais-SearchBox-submit" type="submit" title="Submit">
<svg class="ais-SearchBox-submitIcon">...</svg>
</button>
<button class="ais-SearchBox-reset" type="reset" title="Clear">
<svg class="ais-SearchBox-resetIcon">...</svg>
</button>
<span class="ais-SearchBox-loadingIndicator" hidden>
<svg class="ais-SearchBox-loadingIcon">...</svg>
</span>
</form>
</div>