Skip to content

Commit 25f5c04

Browse files
bdong8bossdonganzh
andauthored
[MT-3568] EPCC Ref Store - Global search: Enable voice search (#50)
* [[MT-3568]add voicesearch functionality and adjust styles * fully working voicesearch implementation with styled components method * using classic css on voicesearch instead of styled component, and remove styled component from package.json * change voicesearch icon animation name * resolve isListening console error message * revert change for form labelling Co-authored-by: Barton Dong <barton97daz@gmail.com>
1 parent 19639d4 commit 25f5c04

9 files changed

+111
-1
lines changed

src/SearchBar.scss

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
max-width: 500px;
77

88
#{$block}__hints {
9+
top: 50px;
910
position: absolute;
1011
z-index: 2;
1112
background-color: $mainBackgroundColor;
@@ -60,6 +61,7 @@
6061
#{$block}__input {
6162
position: relative;
6263
margin-left: 40px;
64+
display: flex;
6365

6466
input {
6567
height: 40px;
@@ -80,6 +82,10 @@
8082
}
8183
}
8284

85+
.ais-SearchBox{
86+
width: 100%;
87+
}
88+
8389
.ais-SearchBox-form {
8490
position: relative;
8591
}
@@ -107,7 +113,55 @@
107113
.ais-SearchBox-loadingIndicator {
108114
padding-top: 3px;
109115
}
116+
117+
.VoiceSearchButton {
118+
all: unset;
119+
display: block;
120+
width: 36px;
121+
height: 36px;
122+
border-radius: 36px;
123+
margin-left: 8px;
124+
cursor: pointer;
125+
background-image: url(./images/icons/icon-search-mic.svg);
126+
background-repeat: no-repeat;
127+
background-position: center;
128+
129+
&:hover {
130+
background-image: url(./images/icons/icon-search-mic-hover.svg);
131+
}
132+
133+
&:active {
134+
background-image: url(./images/icons/icon-search-mic-active.svg);
135+
}
136+
}
137+
138+
@keyframes breatheAnimation {
139+
100% { opacity: 1 }
140+
50% { opacity: 0.65 }
141+
}
142+
143+
.VoiceSearchButtonBreathing {
144+
background-repeat: no-repeat;
145+
background-position: center;
146+
all: unset;
147+
display: block;
148+
width: 36px;
149+
height: 36px;
150+
border-radius: 36px;
151+
margin-left: 8px;
152+
cursor: pointer;
153+
background-image: url(./images/icons/icon-search-mic-active.svg);
154+
animation-name: breatheAnimation;
155+
animation-duration: 0.9s;
156+
animation-iteration-count: infinite;
157+
}
158+
110159
}
160+
161+
.ais-VoiceSearch-status {
162+
display: none;
163+
}
164+
111165
}
112166

113167
@media (max-width: $mobileWidth - 1px) {

src/SearchBar.tsx

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React, { useState } from 'react';
22
import { Link, useHistory } from 'react-router-dom';
33
import useOnclickOutside from 'react-cool-onclickoutside';
4-
import { SearchBox, Hits, PoweredBy } from 'react-instantsearch-dom';
4+
import { SearchBox, Hits, PoweredBy, VoiceSearch } from 'react-instantsearch-dom';
55
import { useTranslation } from './app-state';
66
import { createSearchUrl } from './routes';
77

@@ -13,6 +13,14 @@ import './SearchBar.scss';
1313
interface SearchBoxProps {
1414
}
1515

16+
const SearchButtonMic = (props: any) => (
17+
props.islistening==="true" ?
18+
<span className="VoiceSearchButtonBreathing"
19+
{...props} /> :
20+
<span className="VoiceSearchButton"
21+
{...props} />
22+
)
23+
1624
export const SearchBar: React.FC<SearchBoxProps> = () => {
1725
const { t } = useTranslation();
1826
const [ inputVisible, setInputVisible] = useState(false);
@@ -52,6 +60,22 @@ export const SearchBar: React.FC<SearchBoxProps> = () => {
5260
setInputVisible(false);
5361
};
5462

63+
const VoiceSearchButtonText = ({
64+
isListening,
65+
isBrowserSupported
66+
}: {isListening:any, isBrowserSupported:any}) => (
67+
isBrowserSupported ? (
68+
isListening ?
69+
<SearchButtonMic
70+
islistening={isListening.toString()}
71+
/> :
72+
<SearchButtonMic
73+
islistening={isListening.toString()}
74+
onClick={() => handleFocus()}
75+
/>
76+
) : null
77+
)
78+
5579
const Hit = ({ hit }: any) => {
5680
return (
5781
<Link className="searchbar__hint" to={`/product/${hit.slug}`} onClick={handleLinkClick}>
@@ -88,6 +112,10 @@ export const SearchBar: React.FC<SearchBoxProps> = () => {
88112
reset={<ClearIcon />}
89113
translations={translations}
90114
/>
115+
<VoiceSearch
116+
searchAsYouSpeak={true}
117+
buttonTextComponent={VoiceSearchButtonText}
118+
/>
91119
<button
92120
className={`searchbar__close ${searchValue && '--show'}`}
93121
onClick={onCancel}>
Lines changed: 5 additions & 0 deletions
Loading
Lines changed: 5 additions & 0 deletions
Loading
Lines changed: 3 additions & 0 deletions
Loading
Lines changed: 4 additions & 0 deletions
Loading
Lines changed: 4 additions & 0 deletions
Loading
Lines changed: 4 additions & 0 deletions
Loading
Lines changed: 3 additions & 0 deletions
Loading

0 commit comments

Comments
 (0)