ais-voice-search
<ais-voice-search // Optional parameters [searchAsYouSpeak]="boolean" ></ais-voice-search>
About this widget
The ais-voice-search
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
<ais-voice-search></ais-voice-search>
Props
searchAsYouSpeak
|
type: boolean
default: false
Optional
Whether to trigger the search as you speak. If |
||
Copy
|
|||
buttonTitle
|
type: string
default: 'Search by voice'
Optional
The |
||
Copy
|
|||
disabledButtonTitle
|
type: string
default: 'Search by voice (not supported on this browser)'
Optional
The |
||
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 ais-voice-search
widget, you can combine the connectVoiceSearch
connector with the TypedBaseWidget
class.
1. Extend the TypedBaseWidget
class
First of all, you will need to write some boilerplate code to initialize correctly the TypedBaseWidget
class. This happens in the constructor()
of your class extending the TypedBaseWidget
class.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import { Component, Inject, forwardRef, Optional } from '@angular/core';
import { TypedBaseWidget, NgAisInstantSearch, NgAisIndex } from 'angular-instantsearch';
@Component({
selector: 'app-voice-search',
template: '<p>It works!</p>'
})
export class VoiceSearch extends TypedBaseWidget {
constructor(
@Inject(forwardRef(() => NgAisIndex))
@Optional()
public parentIndex: NgAisIndex,
@Inject(forwardRef(() => NgAisInstantSearch))
public instantSearchInstance: NgAisInstantSearch
) {
super('VoiceSearch');
}
}
There are a couple of things happening in this boilerplate:
- create a
VoiceSearch
class extendingTypedBaseWidget
- reference the
<ais-instantsearch>
parent component instance on theVoiceSearch
widget class - set
app-voice-search
as a selector, so we can use our component as<app-voice-search></app-voice-search>
2. Connect your custom widget
The TypedBaseWidget
class has a method called createWidget()
which takes two arguments: the connector to use and an object of options
(instance options)
for this connector. We call this method at ngOnInit
. This component now implements OnInit
.
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
import { Component, Inject, forwardRef, Optional } from '@angular/core';
import { TypedBaseWidget, NgAisInstantSearch, NgAisIndex } from 'angular-instantsearch';
import connectVoiceSearch, {
VoiceSearchWidgetDescription,
VoiceSearchConnectorParams
} from 'instantsearch.js/es/connectors/voice-search/connectVoiceSearch';
@Component({
selector: 'app-voice-search',
template: '<p>It works!</p>'
})
export class VoiceSearch extends TypedBaseWidget<VoiceSearchWidgetDescription, VoiceSearchConnectorParams> {
public state: VoiceSearchWidgetDescription['renderState']; // Rendering options
constructor(
@Inject(forwardRef(() => NgAisIndex))
@Optional()
public parentIndex: NgAisIndex,
@Inject(forwardRef(() => NgAisInstantSearch))
public instantSearchInstance: NgAisInstantSearch
) {
super('VoiceSearch');
}
ngOnInit() {
this.createWidget(connectVoiceSearch, {
// instance options
});
super.ngOnInit();
}
}
3. Render from the state
Your component instance has access to a this.state
property which holds the rendering options of the widget.
public state: VoiceSearchWidgetDescription['renderState'];
// {
// isBrowserSupported: boolean;
// isListening: boolean;
// toggleListening: Function;
// voiceListeningState: object;
// widgetParams: object;
// }
1
2
3
4
5
6
7
8
9
10
11
12
13
<div>
<button
type="button"
(click)="this.state.toggleListening()"
>
{{ state.isListening ? 'Stop' : 'Start' }}
</button>
<p>isBrowserSupported: {{ this.state.isBrowserSupported }}</p>
<p>isListening: {{ this.state.isListening }}</p>
<pre>
{{ this.state.voiceListeningState | json }}
</pre>
</div>
Rendering options
isBrowserSupported
|
type: boolean
|
isListening
|
type: boolean
|
toggleListening
|
type: function
Starts listening to user’s speech, or stops it if already listening. |
voiceListeningState
|
type: object
An object containing the following states regarding speech recognition:
|
widgetParams
|
type: object
All original widget options forwarded to the render function. |
Instance options
searchAsYouSpeak
|
type: boolean
Optional
Whether to trigger the search as you speak. If |
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
36
37
38
39
40
41
42
43
44
import { Component, Inject, forwardRef, Optional } from '@angular/core';
import { TypedBaseWidget, NgAisInstantSearch, NgAisIndex } from 'angular-instantsearch';
import connectVoiceSearch, {
VoiceSearchWidgetDescription,
VoiceSearchConnectorParams
} from 'instantsearch.js/es/connectors/voice-search/connectVoiceSearch';
@Component({
selector: 'app-voice-search',
template: `
<div>
<button
type="button"
(click)="this.state.toggleListening()"
>
{{ state.isListening ? 'Stop' : 'Start' }}
</button>
<p>isBrowserSupported: {{ this.state.isBrowserSupported }}</p>
<p>isListening: {{ this.state.isListening }}</p>
<pre>
{{ this.state.voiceListeningState | json }}
</pre>
</div>
`
})
export class VoiceSearch extends TypedBaseWidget<VoiceSearchWidgetDescription, VoiceSearchConnectorParams> {
public state: VoiceSearchWidgetDescription['renderState']; // Rendering options
constructor(
@Inject(forwardRef(() => NgAisIndex))
@Optional()
public parentIndex: NgAisIndex,
@Inject(forwardRef(() => NgAisInstantSearch))
public instantSearchInstance: NgAisInstantSearch
) {
super('VoiceSearch');
}
ngOnInit() {
this.createWidget(connectVoiceSearch, {
// instance options
});
super.ngOnInit();
}
}