Skip to content

Commit e34bde4

Browse files
dhayabsarahdayan
andauthored
feat(hooks): document query suggestions (#396)
Co-authored-by: Sarah Dayan <5370675+sarahdayan@users.noreply.github.com>
1 parent 2a3342c commit e34bde4

14 files changed

Lines changed: 2025 additions & 0 deletions

File tree

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
root = true
2+
3+
[*]
4+
indent_style = space
5+
indent_size = 2
6+
end_of_line = lf
7+
insert_final_newline = true
8+
trim_trailing_whitespace = true
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Logs
2+
logs
3+
*.log
4+
npm-debug.log*
5+
yarn-debug.log*
6+
yarn-error.log*
7+
pnpm-debug.log*
8+
lerna-debug.log*
9+
10+
node_modules
11+
dist
12+
.parcel-cache
13+
*.local
14+
15+
# Editor directories and files
16+
.vscode
17+
.idea
18+
.DS_Store
19+
*.suo
20+
*.ntvs*
21+
*.njsproj
22+
*.sln
23+
*.sw?
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"singleQuote": true,
3+
"proseWrap": "never",
4+
"trailingComma": "es5"
5+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# query-suggestions
2+
3+
_This project was generated with [create-instantsearch-app](https://github.com/algolia/create-instantsearch-app) by [Algolia](https://algolia.com)._
4+
5+
## Get started
6+
7+
To run this project locally, install the dependencies and run the local server:
8+
9+
```sh
10+
npm install
11+
npm start
12+
```
13+
14+
Alternatively, you may use [Yarn](https://http://yarnpkg.com/):
15+
16+
```sh
17+
yarn
18+
yarn start
19+
```
1.88 KB
Loading
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf-8" />
5+
<meta
6+
name="viewport"
7+
content="width=device-width, initial-scale=1, shrink-to-fit=no"
8+
/>
9+
10+
<link rel="shortcut icon" href="favicon.png">
11+
12+
<!--
13+
Do not use @7 in production, use a complete version like x.x.x, see website for latest version:
14+
https://www.algolia.com/doc/guides/building-search-ui/installation/react/#load-the-style
15+
-->
16+
<link
17+
rel="stylesheet"
18+
href="https://cdn.jsdelivr.net/npm/instantsearch.css@7/themes/algolia-min.css"
19+
/>
20+
21+
<title>query-suggestions</title>
22+
</head>
23+
24+
<body>
25+
<noscript> You need to enable JavaScript to run this app. </noscript>
26+
27+
<div id="root"></div>
28+
29+
<script type="module" src="src/index.tsx"></script>
30+
</body>
31+
</html>
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"name": "query-suggestions",
3+
"version": "1.0.0",
4+
"private": true,
5+
"scripts": {
6+
"start": "parcel index.html",
7+
"build": "parcel build index.html"
8+
},
9+
"dependencies": {
10+
"@algolia/autocomplete-js": "1.7.0",
11+
"@algolia/autocomplete-plugin-query-suggestions": "1.7.0",
12+
"@algolia/autocomplete-theme-classic": "1.7.0",
13+
"algoliasearch": "4",
14+
"instantsearch.js": "4.40.5",
15+
"react": "17.0.2",
16+
"react-dom": "17.0.2",
17+
"react-instantsearch-hooks-web": "6.29.0"
18+
},
19+
"devDependencies": {
20+
"@types/react": "17.0.45",
21+
"parcel": "2.5.0"
22+
}
23+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
body,
2+
h1 {
3+
margin: 0;
4+
padding: 0;
5+
}
6+
7+
body {
8+
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica,
9+
Arial, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol';
10+
}
11+
12+
.container {
13+
max-width: 1200px;
14+
margin: 0 auto;
15+
padding: 1rem;
16+
}
17+
18+
.current-refinements {
19+
display: flex;
20+
align-items: center;
21+
padding: 1rem 0;
22+
min-height: 64px;
23+
}
24+
25+
@media screen and (max-width: 768px) {
26+
.ais-Hits .ais-Hits-item {
27+
width: calc(50% - 1rem);
28+
}
29+
}
30+
31+
@media screen and (max-width: 480px) {
32+
.ais-Hits .ais-Hits-item {
33+
width: 100%;
34+
}
35+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import {
2+
Configure,
3+
CurrentRefinements,
4+
Highlight,
5+
Hits,
6+
InstantSearch,
7+
} from 'react-instantsearch-hooks-web';
8+
9+
import './App.css';
10+
11+
import type { Hit } from 'instantsearch.js';
12+
13+
import { SearchBoxWithSuggestions } from './SearchBoxWithSuggestions';
14+
import { searchClient } from './searchClient';
15+
16+
export function App() {
17+
return (
18+
<div className="container">
19+
<InstantSearch searchClient={searchClient} indexName="instant_search">
20+
<Configure hitsPerPage={16} />
21+
22+
<SearchBoxWithSuggestions />
23+
24+
<CurrentRefinements
25+
className="current-refinements"
26+
includedAttributes={['query', 'categories']}
27+
/>
28+
29+
<Hits hitComponent={Hit} />
30+
</InstantSearch>
31+
</div>
32+
);
33+
}
34+
35+
type HitProps = {
36+
hit: Hit;
37+
};
38+
39+
function Hit({ hit }: HitProps) {
40+
return (
41+
<article>
42+
<Highlight attribute="name" hit={hit} />
43+
</article>
44+
);
45+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { createElement, Fragment, useEffect, useRef } from 'react';
2+
import { render } from 'react-dom';
3+
4+
import { autocomplete } from '@algolia/autocomplete-js';
5+
6+
import type { AutocompleteOptions } from '@algolia/autocomplete-js';
7+
import type { BaseItem } from '@algolia/autocomplete-core';
8+
9+
import '@algolia/autocomplete-theme-classic';
10+
11+
type AutocompleteProps = Partial<AutocompleteOptions<BaseItem>>;
12+
13+
export function Autocomplete(props: AutocompleteProps) {
14+
const containerRef = useRef<HTMLDivElement>(null);
15+
16+
useEffect(() => {
17+
if (!containerRef.current) {
18+
return;
19+
}
20+
21+
const search = autocomplete({
22+
...props,
23+
container: containerRef.current,
24+
renderer: { createElement, Fragment, render },
25+
});
26+
27+
return () => search.destroy();
28+
}, [props]);
29+
30+
return <div ref={containerRef} />;
31+
}

0 commit comments

Comments
 (0)