-
Notifications
You must be signed in to change notification settings - Fork 332
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(autocomplete-js): enable HTML templating (#920)
Co-authored-by: Haroen Viaene <hello@haroen.me>
- Loading branch information
1 parent
4131bd6
commit f5bbf34
Showing
22 changed files
with
1,448 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# Autocomplete with HTML templates example | ||
|
||
This example shows how to use HTML templates in Autocomplete. | ||
|
||
<p align="center"><img src="capture.png?raw=true" alt="A capture of the Autocomplete with HTML templates example" /></p> | ||
|
||
## Demo | ||
|
||
[Access the demo](https://codesandbox.io/s/github/algolia/autocomplete/tree/next/examples/html-templates) | ||
|
||
## How to run this example locally | ||
|
||
### 1. Clone this repository | ||
|
||
```sh | ||
git clone git@github.com:algolia/autocomplete.git | ||
``` | ||
|
||
### 2. Install the dependencies and run the server | ||
|
||
```sh | ||
yarn | ||
yarn workspace @algolia/autocomplete-example-html-templates start | ||
``` | ||
|
||
Alternatively, you may use npm: | ||
|
||
```sh | ||
cd examples/html-templates | ||
npm install | ||
npm start | ||
``` | ||
|
||
Open <http://localhost:1234> to see your app. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import { autocomplete, getAlgoliaResults } from '@algolia/autocomplete-js'; | ||
import algoliasearch from 'algoliasearch/lite'; | ||
|
||
import '@algolia/autocomplete-theme-classic'; | ||
|
||
const appId = 'latency'; | ||
const apiKey = '6be0576ff61c053d5f9a3225e2a90f76'; | ||
const searchClient = algoliasearch(appId, apiKey); | ||
|
||
autocomplete({ | ||
container: '#autocomplete', | ||
placeholder: 'Search', | ||
getSources({ query }) { | ||
return [ | ||
{ | ||
sourceId: 'products', | ||
getItems() { | ||
return getAlgoliaResults({ | ||
searchClient, | ||
queries: [ | ||
{ | ||
indexName: 'instant_search', | ||
query, | ||
}, | ||
], | ||
}); | ||
}, | ||
templates: { | ||
item({ item, components, html }) { | ||
return html`<div class="aa-ItemWrapper"> | ||
<div class="aa-ItemContent"> | ||
<div | ||
class="aa-ItemIcon aa-ItemIcon--picture aa-ItemIcon--alignTop" | ||
> | ||
<img | ||
src="${item.image}" | ||
alt="${item.name}" | ||
width="40" | ||
height="40" | ||
/> | ||
</div> | ||
<div class="aa-ItemContentBody"> | ||
<div class="aa-ItemContentTitle"> | ||
${components.Highlight({ hit: item, attribute: 'name' })} | ||
</div> | ||
<div class="aa-ItemContentDescription"> | ||
By <strong>${item.brand}</strong> in ${' '} | ||
<strong>${item.categories[0]}</strong> | ||
</div> | ||
</div> | ||
</div> | ||
</div>`; | ||
}, | ||
}, | ||
}, | ||
]; | ||
}, | ||
render({ children, render, html }, root) { | ||
render(html`<div class="aa-SomeResults">${children}</div>`, root); | ||
}, | ||
renderNoResults({ children, render, html }, root) { | ||
render(html`<div class="aa-NoResults">${children}</div>`, root); | ||
}, | ||
}); |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
// Parcel picks the `source` field of the monorepo packages and thus doesn't | ||
// apply the Babel config. We therefore need to manually override the constants | ||
// in the app. | ||
// See https://twitter.com/devongovett/status/1134231234605830144 | ||
global.__DEV__ = process.env.NODE_ENV !== 'production'; | ||
global.__TEST__ = false; |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8" /> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> | ||
|
||
<link rel="shortcut icon" href="favicon.png" type="image/x-icon" /> | ||
<link rel="stylesheet" href="style.css" /> | ||
|
||
<title>HTML Templates | Autocomplete</title> | ||
</head> | ||
|
||
<body> | ||
<div class="container"> | ||
<div id="autocomplete"></div> | ||
</div> | ||
|
||
<script src="env.js"></script> | ||
<script src="app.js"></script> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
{ | ||
"name": "@algolia/autocomplete-example-html-templates", | ||
"description": "Autocomplete example with HTML templates", | ||
"version": "1.5.7", | ||
"private": true, | ||
"license": "MIT", | ||
"scripts": { | ||
"build": "parcel build index.html", | ||
"start": "parcel index.html" | ||
}, | ||
"dependencies": { | ||
"@algolia/autocomplete-js": "1.5.7", | ||
"@algolia/autocomplete-theme-classic": "1.5.7", | ||
"algoliasearch": "4.9.1" | ||
}, | ||
"devDependencies": { | ||
"@algolia/client-search": "4.9.1", | ||
"parcel": "2.0.0-beta.2" | ||
}, | ||
"keywords": [ | ||
"algolia", | ||
"autocomplete", | ||
"javascript" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
* { | ||
box-sizing: border-box; | ||
} | ||
|
||
body { | ||
background-color: rgb(244, 244, 249); | ||
color: rgb(65, 65, 65); | ||
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen', | ||
'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue', | ||
sans-serif; | ||
-webkit-font-smoothing: antialiased; | ||
-moz-osx-font-smoothing: grayscale; | ||
padding: 1rem; | ||
} | ||
|
||
.container { | ||
margin: 0 auto; | ||
max-width: 640px; | ||
width: 100%; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.