Skip to content

Commit 5613891

Browse files
docs(examples): add panel-placement example (#549)
* docs(examples): add panel-placement example Co-authored-by: François Chalifour <[email protected]>
1 parent 3d1bf26 commit 5613891

File tree

8 files changed

+217
-0
lines changed

8 files changed

+217
-0
lines changed

.codesandbox/ci.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
"/examples/query-suggestions-with-recent-searches-and-categories",
1313
"/examples/query-suggestions-with-recent-searches",
1414
"/examples/query-suggestions",
15+
"/examples/panel-placement",
1516
"/examples/react-renderer",
1617
"/examples/recently-viewed-items",
1718
"/examples/starter-algolia",

examples/panel-placement/README.md

Whitespace-only changes.

examples/panel-placement/app.tsx

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
/** @jsx h */
2+
import {
3+
autocomplete,
4+
AutocompleteComponents,
5+
AutocompleteOptions,
6+
getAlgoliaResults,
7+
GetSources,
8+
} from '@algolia/autocomplete-js';
9+
import { Hit } from '@algolia/client-search';
10+
import algoliasearch from 'algoliasearch';
11+
import { h } from 'preact';
12+
13+
import '@algolia/autocomplete-theme-classic';
14+
15+
const appId = 'latency';
16+
const apiKey = '6be0576ff61c053d5f9a3225e2a90f76';
17+
const searchClient = algoliasearch(appId, apiKey);
18+
19+
type AutocompleteItem = Hit<{
20+
brand: string;
21+
categories: string[];
22+
description: string;
23+
image: string;
24+
name: string;
25+
price: number;
26+
rating: number;
27+
type: string;
28+
url: string;
29+
}>;
30+
31+
const getSources: GetSources<AutocompleteItem> = ({ query }) => {
32+
return [
33+
{
34+
sourceId: 'products',
35+
getItems() {
36+
return getAlgoliaResults<AutocompleteItem>({
37+
searchClient,
38+
queries: [
39+
{
40+
indexName: 'instant_search',
41+
query,
42+
},
43+
],
44+
});
45+
},
46+
templates: {
47+
item({ item, components }) {
48+
return <ProductItem item={item} components={components} />;
49+
},
50+
noResults() {
51+
return 'No products matching.';
52+
},
53+
},
54+
},
55+
];
56+
};
57+
58+
const search = autocomplete<AutocompleteItem>({
59+
container: '#autocomplete',
60+
placeholder: 'Search',
61+
getSources,
62+
});
63+
64+
const searchLeft = autocomplete<AutocompleteItem>({
65+
container: '#autocomplete-left',
66+
placeholder: 'Search',
67+
getSources,
68+
});
69+
70+
const searchRight = autocomplete<AutocompleteItem>({
71+
container: '#autocomplete-right',
72+
placeholder: 'Search',
73+
getSources,
74+
});
75+
76+
type ProductItemProps = {
77+
item: AutocompleteItem;
78+
components: AutocompleteComponents;
79+
};
80+
81+
const ProductItem = ({ item, components }: ProductItemProps) => (
82+
<a href={item.url} className="aa-ItemLink">
83+
<div className="aa-ItemContent">
84+
<div className="aa-ItemIcon aa-ItemIcon--picture aa-ItemIcon--alignTop">
85+
<img src={item.image} alt={item.name} width="40" height="40" />
86+
</div>
87+
88+
<div className="aa-ItemContentBody">
89+
<div className="aa-ItemContentTitle">
90+
<components.ReverseHighlight hit={item} attribute="name" />
91+
</div>
92+
<div className="aa-ItemContentDescription">
93+
By <strong>{item.brand}</strong> in{' '}
94+
<strong>{item.categories[0]}</strong>
95+
</div>
96+
</div>
97+
</div>
98+
</a>
99+
);
100+
101+
document
102+
.querySelectorAll<HTMLInputElement>('input[name="placement"]')
103+
.forEach((radio) => {
104+
radio.addEventListener('change', (event) => {
105+
const target = event.target as HTMLInputElement;
106+
const panelPlacement = target.value as Required<
107+
AutocompleteOptions<AutocompleteItem>['panelPlacement']
108+
>;
109+
110+
search.update({ panelPlacement });
111+
searchLeft.update({ panelPlacement });
112+
searchRight.update({ panelPlacement });
113+
});
114+
});

examples/panel-placement/env.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import * as preact from 'preact';
2+
3+
// Parcel picks the `source` field of the monorepo packages and thus doesn't
4+
// apply the Babel config. We therefore need to manually override the constants
5+
// in the app, as well as the React pragmas.
6+
// See https://twitter.com/devongovett/status/1134231234605830144
7+
(global as any).__DEV__ = process.env.NODE_ENV !== 'production';
8+
(global as any).__TEST__ = false;
9+
(global as any).h = preact.h;
10+
(global as any).React = preact;

examples/panel-placement/favicon.png

228 KB
Loading

examples/panel-placement/index.html

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<html lang="en">
2+
<head>
3+
<meta charset="UTF-8" />
4+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
5+
6+
<link rel="shortcut icon" href="favicon.png" type="image/x-icon" />
7+
<link rel="stylesheet" href="style.css" />
8+
9+
<title>Panel placement | Autocomplete</title>
10+
</head>
11+
12+
<body>
13+
<form>
14+
Panel placement:
15+
<input
16+
type="radio"
17+
value="input-wrapper-width"
18+
name="placement"
19+
checked
20+
/>
21+
<label for="input-wrapper-width">input-wrapper-width</label>
22+
<input type="radio" value="start" name="placement" />
23+
<label for="start">start</label>
24+
<input type="radio" value="end" name="placement" />
25+
<label for="end">end</label>
26+
<input type="radio" value="full-width" name="placement" />
27+
<label for="full-width">full-width</label>
28+
</form>
29+
30+
<div class="container">
31+
<div id="autocomplete-left"></div>
32+
<div id="autocomplete"></div>
33+
<div id="autocomplete-right"></div>
34+
</div>
35+
36+
<script src="env.ts"></script>
37+
<script src="app.tsx"></script>
38+
</body>
39+
</html>

examples/panel-placement/package.json

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"name": "@algolia/autocomplete-example-panel-placement",
3+
"description": "Autocomplete panel placement example",
4+
"version": "1.0.0-alpha.46",
5+
"private": true,
6+
"license": "MIT",
7+
"scripts": {
8+
"build": "parcel build index.html",
9+
"start": "parcel index.html"
10+
},
11+
"dependencies": {
12+
"@algolia/autocomplete-js": "1.0.0-alpha.46",
13+
"@algolia/autocomplete-core": "1.0.0-alpha.46",
14+
"@algolia/autocomplete-theme-classic": "1.0.0-alpha.46",
15+
"algoliasearch": "4.9.0",
16+
"preact": "10.5.13"
17+
},
18+
"devDependencies": {
19+
"@algolia/client-search": "4.9.0",
20+
"parcel": "2.0.0-beta.2"
21+
},
22+
"keywords": [
23+
"algolia",
24+
"autocomplete",
25+
"javascript"
26+
]
27+
}

examples/panel-placement/style.css

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
* {
2+
box-sizing: border-box;
3+
}
4+
5+
body {
6+
background-color: rgb(244, 244, 249);
7+
color: rgb(65, 65, 65);
8+
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen',
9+
'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue',
10+
sans-serif;
11+
-webkit-font-smoothing: antialiased;
12+
-moz-osx-font-smoothing: grayscale;
13+
padding: 1rem;
14+
}
15+
16+
.container {
17+
margin: 0 auto;
18+
width: 100%;
19+
display: grid;
20+
grid-template-columns: 400px 1fr 400px;
21+
grid-gap: 10px;
22+
}
23+
24+
.action {
25+
align-content: center;
26+
}

0 commit comments

Comments
 (0)