voiceSearch
voiceSearch({ container: string|HTMLElement, // Optional parameters searchAsYouSpeak: boolean, language: string, additionalQueryParameters: object, templates: object, cssClasses: object, });
1
import { voiceSearch } from 'instantsearch.js/es/widgets';
About this widget
The voiceSearch
widget lets the user perform a voice-based query.
It uses the Web Speech API, which only Chrome (from version 25) has implemented so far. This means the voiceSearch
widget only works on desktop Chrome and Android Chrome. It doesn’t work on iOS Chrome, which uses the iOS WebKit.
Examples
1
2
3
voiceSearch({
container: '#voicesearch',
});
Options
Parameter | Description | ||
---|---|---|---|
container
|
type: string|HTMLElement
Required
The CSS Selector or |
||
Copy
|
|||
searchAsYouSpeak
|
type: boolean
default: false
Optional
Whether to trigger the search as you speak. If |
||
Copy
|
|||
language
|
type: string
default: all languages
Optional
The language you want your |
||
Copy
|
|||
additionalQueryParameters
|
type: function
Optional
A function that receives the current query and returns the list of search parameters you want to enable for voice search. By default, we set the following query parameters:
To disable this behavior, override the return value of |
||
Copy
|
|||
templates
|
type: object
Optional
The templates to use for the widget. |
||
Copy
|
|||
cssClasses
|
type: object
The CSS classes you can override:
|
||
Copy
|
Templates
You can customize parts of the widget’s UI using the Templates API.
Every template provides an html
function you can use as a tagged template. Using html
lets you safely provide templates as an HTML string. It works directly in the browser without a build step. See Templating your UI for more information.
The html
function is available starting from v4.46.0.
Parameter | Description | ||
---|---|---|---|
buttonText
|
type: string|function
Optional
The template used for displaying the button. |
||
Copy
|
|||
status
|
type: string|function
Optional
The template used for displaying the status. |
||
Copy
|
HTML output
1
2
3
4
5
6
7
8
<div class="ais-VoiceSearch">
<button class="ais-VoiceSearch-button" type="button" title="Search by voice">
...
</button>
<div class="ais-VoiceSearch-status">
...
</div>
</div>
Customize the UI with connectVoiceSearch
If you want to create your own UI of the voiceSearch
widget, you can use connectors.
To use connectVoiceSearch
, you can import it with the declaration relevant to how you installed InstantSearch.js.
1
import { connectVoiceSearch } from 'instantsearch.js/es/connectors';
Then it’s a 3-step process:
// 1. Create a render function
const renderVoiceSearch = (renderOptions, isFirstRender) => {
// Rendering logic
};
// 2. Create the custom widget
const customVoiceSearch = connectVoiceSearch(
renderVoiceSearch
);
// 3. Instantiate
search.addWidgets([
customVoiceSearch({
// instance params
})
]);
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).
const renderVoiceSearch = (renderOptions, isFirstRender) => {
const {
boolean isBrowserSupported,
boolean isListening,
function toggleListening,
object voiceListeningState,
object widgetParams,
} = renderOptions;
if (isFirstRender) {
// Do some initial rendering and bind events
}
// Render the widget
}
Render options
Parameter | Description | ||
---|---|---|---|
isBrowserSupported
|
type: boolean
|
||
Copy
|
|||
toggleListening
|
type: function
Starts listening to user’s speech, or stops it if already listening. |
||
Copy
|
|||
isListening
|
type: boolean
|
||
Copy
|
|||
voiceListeningState
|
type: boolean
An object containing the following states regarding speech recognition:
|
||
Copy
|
|||
widgetParams
|
type: object
All original widget options forwarded to the render function. |
||
Copy
|
Create and instantiate the custom widget
We first create custom widgets from our rendering function, then we instantiate them. When doing that, there are two types of parameters you can give:
- Instance parameters: they are predefined parameters that you can use to configure the behavior of Algolia.
- Your own parameters: to make the custom widget generic.
Both instance and custom parameters are available in connector.widgetParams
, inside the renderFunction
.
const customVoiceSearch = connectVoiceSearch(
renderVoiceSearch
);
search.addWidgets([
customVoiceSearch({
// Optional parameters
searchAsYouSpeak: boolean,
})
]);
Instance options
Parameter | Description | ||
---|---|---|---|
searchAsYouSpeak
|
type: boolean
default: false
Optional
Whether to trigger the search as you speak. If |
||
Copy
|
|||
language
|
type: string
default: all languages
Optional
The language you want your |
||
Copy
|
|||
additionalQueryParameters
|
type: function
Optional
A function that receives the current query and returns the list of search parameters you want to enable for voice search. By default, we set the following query parameters:
To disable this behavior, override the return value of |
||
Copy
|
Full example
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
// Create a render function
const renderVoiceSearch = (renderOptions, isFirstRender) => {
const { isListening, toggleListening, voiceListeningState } = renderOptions;
const container = document.querySelector('#voicesearch');
if (isFirstRender) {
const button = document.createElement('button');
button.addEventListener('click', event => {
toggleListening();
})
container.appendChild(button);
const state = document.createElement('pre');
container.appendChild(state)
}
container.querySelector('button').textContent =
isListening ? 'Stop' : 'Start';
container.querySelector('pre').textContent =
JSON.stringify(voiceListeningState, null, 2);
};
// create custom widget
const customVoiceSearch = connectVoiceSearch(
renderVoiceSearch
);
// instantiate custom widget
search.addWidgets([
customVoiceSearch({
container: document.querySelector('#voicesearch'),
})
]);