Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(experiences): implement custom element #6364

Merged
merged 5 commits into from
Sep 19, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 89 additions & 0 deletions examples/js/algolia-experiences/custom-element.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Algolia Experiences demo</title>
<link
rel="stylesheet"
href="/packages/instantsearch.css/themes/satellite-min.css"
/>
<style>
body {
font-family: system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI',
Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue',
sans-serif;
}
main {
max-width: 80em;
margin: 0 auto;
}
.ais-Hits-list,
.ais-TrendingItems-list {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
gap: 1em;
}
.ais-TrendingItems-item,
.ais-Hits-item {
padding: 0;
}
.ais-TrendingItems-item a,
.ais-Hits-item a {
width: 100%;
padding: 1.5em;
text-decoration: none;
color: inherit;
display: flex;
flex-direction: column;
}
.ais-TrendingItems-item img,
.ais-Hits-item img {
width: 100%;
height: 10em;
border-radius: 4px;
object-fit: contain;
}
.__flex {
display: flex;
gap: 1em;
justify-content: space-between;
}
.__bold {
font-weight: bold;
}
div:has(> .ais-Pagination) {
display: flex;
}
.ais-Pagination {
margin: 1em auto;
}
</style>
<script src="/packages/algolia-experiences/dist/algolia-experiences.development.js"></script>
<meta
name="algolia-configuration"
content='{"appId":"F4T6CUV2AH","apiKey":"72d500963987bc97ff899d350a00f7a8"}'
/>
<script>
let isMounted = false;
function toggle() {
if (isMounted) {
document.querySelector('algolia-experience').remove();
isMounted = false;
return;
}
const element = document.createElement('template');
document.querySelector('main').appendChild(element);
element.outerHTML = `
<algolia-experience experience-id="category:historical" />
`;
isMounted = true;
}
</script>
</head>
<body>
<main>
<h2>Buy historical books now!</h2>
<button onclick="toggle()">Toggle</button>
</main>
</body>
</html>
6 changes: 3 additions & 3 deletions examples/js/algolia-experiences/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
"version": "1.1.0",
"private": true,
"scripts": {
"start": "BABEL_ENV=parcel parcel index.html local.html",
"build": "BABEL_ENV=parcel parcel build index.html local.html --public-url .",
"website:examples": "BABEL_ENV=parcel parcel build index.html local.html --public-url . --dist-dir=../../../website/examples/js/algolia-experiences"
"start": "BABEL_ENV=parcel parcel index.html local.html custom-element.html",
aymeric-giraudet marked this conversation as resolved.
Show resolved Hide resolved
"build": "BABEL_ENV=parcel parcel build index.html local.html custom-element.html --public-url .",
"website:examples": "BABEL_ENV=parcel parcel build index.html local.html custom-element.html --public-url . --dist-dir=../../../website/examples/js/algolia-experiences"
},
"browserslist": "firefox 68, chrome 78, IE 11",
"devDependencies": {
Expand Down
6 changes: 5 additions & 1 deletion packages/algolia-experiences/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { setupInstantSearch } from './setup-instantsearch';

if (typeof window === 'object') {
document.addEventListener('DOMContentLoaded', setupInstantSearch);
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', setupInstantSearch);
} else {
setupInstantSearch();
}
}
37 changes: 37 additions & 0 deletions packages/algolia-experiences/src/setup-instantsearch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ import { getElements, getSettings } from './get-information';
import { configToIndex, injectStyles } from './render';
import { error } from './util';

import type { Settings } from './get-information';
import type { IndexWidget } from 'instantsearch.js';

declare global {
interface Window {
__search: InstantSearch;
Expand All @@ -24,6 +27,8 @@ export function setupInstantSearch() {
});
window.__search = search;

registerComponents(search, settings);

const elements = getElements();

injectStyles();
Expand All @@ -39,3 +44,35 @@ export function setupInstantSearch() {
error((err as Error).message);
}
}

function registerComponents(search: InstantSearch, settings: Settings) {
class AlgoliaExperience extends HTMLElement {
static observedAttributes = ['experience-id'];

widgets: IndexWidget[] = [];

connectedCallback() {
const experienceId = this.getAttribute('experience-id');
Haroenv marked this conversation as resolved.
Show resolved Hide resolved
if (!experienceId) {
error('Experience ID is required');
return;
}

fetchConfiguration([experienceId], settings).then((configuration) => {
this.widgets = configToIndex(
configuration[0],
new Map([[experienceId, this]])
);
search.addWidgets(this.widgets);
if (!search.started) {
search.start();
}
});
}

disconnectedCallback() {
search.removeWidgets(this.widgets);
}
}
customElements.define('algolia-experience', AlgoliaExperience);
}