voiceSearch
instantsearch.widgets.voiceSearch({ container: string|HTMLElement, // Optional parameters searchAsYouSpeak: boolean, templates: object, cssClasses: object, });
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
instantsearch.widgets.voiceSearch({
container: '#voicesearch',
});
Options
container
|
type: string|HTMLElement
Required
The CSS Selector or |
||
Copy
|
|||
searchAsYouSpeak
|
type: boolean
default: false
Optional
Whether or not to trigger the search as you speak. If |
||
Copy
|
|||
templates
|
type: object
Optional
The templates to use for the widget. |
||
Copy
|
|||
cssClasses
|
type: object
The CSS classes to override.
|
||
Copy
|
Templates
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
|
Customize the UI - connectVoiceSearch
If you want to create your own UI of the voiceSearch
widget, you can use connectors.
It’s a 3-step process:
// 1. Create a render function
const renderVoiceSearch = (renderOptions, isFirstRender) => {
// Rendering logic
};
// 2. Create the custom widget
const customVoiceSearch = instantsearch.connectors.connectVoiceSearch(
renderVoiceSearch
);
// 3. Instantiate
search.addWidget(
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
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 = instantsearch.connectors.connectVoiceSearch(
renderVoiceSearch
);
search.addWidget(
customVoiceSearch({
// Optional parameters
searchAsYouSpeak: boolean,
})
);
Instance options
searchAsYouSpeak
|
type: boolean
default: false
Optional
Whether or not to trigger the search as you speak. If |
||
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 = instantsearch.connectors.connectVoiceSearch(
renderVoiceSearch
);
// instantiate custom widget
search.addWidget(
customVoiceSearch({
container: document.querySelector('#voicesearch'),
})
);
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>