33import { autocomplete } from '@algolia/autocomplete-js' ;
44import { createQuerySuggestionsPlugin } from '@algolia/autocomplete-plugin-query-suggestions' ;
55import { createLocalStorageRecentSearchesPlugin } from '@algolia/autocomplete-plugin-recent-searches' ;
6- import { h , Fragment } from 'preact ' ;
6+ import { InstantSearch } from 'instantsearch.js ' ;
77
88import {
99 debouncedSetInstantSearchUiState ,
@@ -42,29 +42,27 @@ function getItemUrl({ query, category }) {
4242 } ) ;
4343}
4444
45- function ItemWrapper ( { children, query, category } ) {
45+ function getItemWrapper ( { html , children, query, category } ) {
4646 const uiState = {
4747 query,
4848 hierarchicalMenu : {
4949 [ INSTANT_SEARCH_HIERARCHICAL_ATTRIBUTE ] : [ category ] ,
5050 } ,
5151 } ;
5252
53- return (
54- < a
55- className = "aa-ItemLink"
56- href = { getInstantSearchUrl ( uiState ) }
57- onClick = { ( event ) => {
58- if ( ! isModifierEvent ( event ) ) {
59- // Bypass the original link behavior if there's no event modifier
60- // to set the InstantSearch UI state without reloading the page.
61- event . preventDefault ( ) ;
62- }
63- } }
64- >
65- { children }
66- </ a >
67- ) ;
53+ return html `< a
54+ class ="aa-ItemLink "
55+ href =${ getInstantSearchUrl ( uiState ) }
56+ onClick =${ ( event ) => {
57+ if ( ! isModifierEvent ( event ) ) {
58+ // Bypass the original link behavior if there's no event modifier
59+ // to set the InstantSearch UI state without reloading the page.
60+ event . preventDefault ( ) ;
61+ }
62+ } }
63+ >
64+ ${ children }
65+ </ a > ` ;
6866}
6967
7068const recentSearchesPlugin = createLocalStorageRecentSearchesPlugin ( {
@@ -94,15 +92,14 @@ const recentSearchesPlugin = createLocalStorageRecentSearchesPlugin({
9492 // and plug it to the InstantSearch router.
9593 item ( params ) {
9694 const { children } = ( source . templates . item ( params ) as any ) . props ;
95+ const { item, html } = params ;
9796
98- return (
99- < ItemWrapper
100- query = { params . item . label }
101- category = { params . item . category }
102- >
103- { children }
104- </ ItemWrapper >
105- ) ;
97+ return getItemWrapper ( {
98+ query : item . label ,
99+ category : item . category ,
100+ html,
101+ children,
102+ } ) ;
106103 } ,
107104 } ,
108105 } ;
@@ -115,7 +112,7 @@ const querySuggestionsPluginInCategory = createQuerySuggestionsPlugin({
115112 getSearchParams ( ) {
116113 const currentCategory = getInstantSearchCurrentCategory ( ) ;
117114
118- return recentSearchesPlugin . data . getAlgoliaSearchParams ( {
115+ return recentSearchesPlugin . data ! . getAlgoliaSearchParams ( {
119116 hitsPerPage : 3 ,
120117 facetFilters : [
121118 `${ INSTANT_SEARCH_INDEX_NAME } .facets.exact_matches.${ INSTANT_SEARCH_HIERARCHICAL_ATTRIBUTE } .value:${ currentCategory } ` ,
@@ -152,22 +149,22 @@ const querySuggestionsPluginInCategory = createQuerySuggestionsPlugin({
152149 } ,
153150 templates : {
154151 ...source . templates ,
155- header ( ) {
156- return (
157- < Fragment >
158- < span className = "aa-SourceHeaderTitle" > In { currentCategory } </ span >
159- < div className = "aa-SourceHeaderLine" />
160- </ Fragment >
161- ) ;
152+ header ( { html } ) {
153+ return html `
154+ < span class ="aa-SourceHeaderTitle "> In ${ currentCategory } </ span >
155+ < div class ="aa-SourceHeaderLine " />
156+ ` ;
162157 } ,
163158 item ( params ) {
164159 const { children } = ( source . templates . item ( params ) as any ) . props ;
160+ const { item, html } = params ;
165161
166- return (
167- < ItemWrapper query = { params . item . query } category = { currentCategory } >
168- { children }
169- </ ItemWrapper >
170- ) ;
162+ return getItemWrapper ( {
163+ query : item . query ,
164+ category : currentCategory ,
165+ html,
166+ children,
167+ } ) ;
171168 } ,
172169 } ,
173170 } ;
@@ -181,12 +178,12 @@ const querySuggestionsPlugin = createQuerySuggestionsPlugin({
181178 const currentCategory = getInstantSearchCurrentCategory ( ) ;
182179
183180 if ( ! currentCategory ) {
184- return recentSearchesPlugin . data . getAlgoliaSearchParams ( {
181+ return recentSearchesPlugin . data ! . getAlgoliaSearchParams ( {
185182 hitsPerPage : 6 ,
186183 } ) ;
187184 }
188185
189- return recentSearchesPlugin . data . getAlgoliaSearchParams ( {
186+ return recentSearchesPlugin . data ! . getAlgoliaSearchParams ( {
190187 hitsPerPage : 3 ,
191188 facetFilters : [
192189 `${ INSTANT_SEARCH_INDEX_NAME } .facets.exact_matches.${ INSTANT_SEARCH_HIERARCHICAL_ATTRIBUTE } .value:-${ currentCategory } ` ,
@@ -229,29 +226,26 @@ const querySuggestionsPlugin = createQuerySuggestionsPlugin({
229226 } ,
230227 templates : {
231228 ...source . templates ,
232- header ( ) {
229+ header ( { html } ) {
233230 if ( ! currentCategory ) {
234231 return null ;
235232 }
236233
237- return (
238- < Fragment >
239- < span className = "aa-SourceHeaderTitle" > In other categories</ span >
240- < div className = "aa-SourceHeaderLine" />
241- </ Fragment >
242- ) ;
234+ return html `
235+ < span class ="aa-SourceHeaderTitle "> In other categories</ span >
236+ < div class ="aa-SourceHeaderLine " />
237+ ` ;
243238 } ,
244239 item ( params ) {
245240 const { children } = ( source . templates . item ( params ) as any ) . props ;
241+ const { item, html } = params ;
246242
247- return (
248- < ItemWrapper
249- query = { params . item . query }
250- category = { params . item . __autocomplete_qsCategory }
251- >
252- { children }
253- </ ItemWrapper >
254- ) ;
243+ return getItemWrapper ( {
244+ query : item . query ,
245+ category : item . __autocomplete_qsCategory ,
246+ html,
247+ children,
248+ } ) ;
255249 } ,
256250 } ,
257251 } ;
@@ -260,8 +254,10 @@ const querySuggestionsPlugin = createQuerySuggestionsPlugin({
260254
261255const searchPageState = getInstantSearchUiState ( ) ;
262256
263- export function startAutocomplete ( ) {
264- autocomplete ( {
257+ export function startAutocomplete ( searchInstance : InstantSearch ) {
258+ let skipInstantSearchStateUpdate = false ;
259+
260+ const { setQuery } = autocomplete ( {
265261 container : '#autocomplete' ,
266262 placeholder : 'Search for products' ,
267263 openOnFocus : true ,
@@ -292,9 +288,17 @@ export function startAutocomplete() {
292288 } ) ;
293289 } ,
294290 onStateChange ( { prevState, state } ) {
295- if ( prevState . query !== state . query ) {
291+ if ( ! skipInstantSearchStateUpdate && prevState . query !== state . query ) {
296292 debouncedSetInstantSearchUiState ( { query : state . query } ) ;
297293 }
294+ skipInstantSearchStateUpdate = false ;
298295 } ,
299296 } ) ;
297+
298+ window . addEventListener ( 'popstate' , ( ) => {
299+ skipInstantSearchStateUpdate = true ;
300+ setQuery (
301+ ( searchInstance . helper && searchInstance . helper . state . query ) || ''
302+ ) ;
303+ } ) ;
300304}
0 commit comments