Skip to content

Commit af4cda6

Browse files
committed
move search from regular to infinite
1 parent bc83ca2 commit af4cda6

File tree

2 files changed

+102
-66
lines changed

2 files changed

+102
-66
lines changed

layouts/index.html

Lines changed: 101 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -11,58 +11,64 @@
1111
{{ partial "overview_block.html" .}}
1212

1313
<style>
14-
.algolia-search {
15-
.search-panel {
16-
display: flex;
17-
}
14+
.algolia-search {
15+
.search-panel {
16+
display: flex;
17+
}
1818

19-
.search-panel__results {
20-
flex: 3;
21-
}
19+
.search-panel__results {
20+
flex: 3;
21+
}
2222

23-
.ais-Highlight-highlighted {
24-
color: inherit;
25-
font-size: inherit;
26-
}
23+
.ais-Highlight-highlighted {
24+
color: inherit;
25+
font-size: inherit;
26+
}
2727

28-
#searchbox {
29-
margin-bottom: 2rem;
30-
}
28+
#searchbox {
29+
margin-bottom: 2rem;
30+
}
3131

32-
#pagination {
33-
margin: 2rem auto;
34-
text-align: center;
35-
}
36-
.search-result-row {
37-
@media (min-width: 992px) {
38-
height: 220px;
32+
#pagination {
33+
margin: 2rem auto;
34+
text-align: center;
3935
}
40-
}
41-
.ais-Hits-item {
42-
padding: 0;
43-
margin-top: 10px;
44-
margin-bottom: 10px;
45-
}
46-
.search-result-image {
47-
height: auto;
48-
max-height: 175px;
49-
max-width: 100%;
50-
margin: auto;
51-
}
52-
.search-result-title {
53-
margin-top: 40px;
54-
}
5536

56-
ol.ais-Hits-list {
57-
list-style: none;
58-
}
59-
.ais-Highlight-highlighted, .ais-Snippet-highlighted {
60-
background-color: #FFFF92;
37+
.search-result-row {
38+
@media (min-width: 992px) {
39+
height: 220px;
40+
}
41+
}
42+
43+
.ais-Hits-item {
44+
padding: 0;
45+
margin-top: 10px;
46+
margin-bottom: 10px;
47+
}
48+
49+
.search-result-image {
50+
height: auto;
51+
max-height: 175px;
52+
max-width: 100%;
53+
margin: auto;
54+
}
55+
56+
.search-result-title {
57+
margin-top: 40px;
58+
}
59+
60+
ol.ais-Hits-list {
61+
list-style: none;
62+
}
63+
64+
.ais-Highlight-highlighted,
65+
.ais-Snippet-highlighted {
66+
background-color: #FFFF92;
67+
}
6168
}
62-
}
6369
</style>
6470
<!-- Search -->
65-
<section class="section color-blue">
71+
<section class="section color-blue">
6672
<div class="container algolia-search rounded-lg bg-lightblue">
6773
<div class="row">
6874
<div class="col">
@@ -99,39 +105,69 @@ <h2 class="pt-5 color-blue">Search</h2>
99105
future: { preserveSharedStateOnUnmount: true },
100106
});
101107

108+
let lastRenderArgs;
109+
110+
const infiniteHits = instantsearch.connectors.connectInfiniteHits(
111+
(renderArgs, isFirstRender) => {
112+
const { items, showMore, widgetParams } = renderArgs;
113+
const { container } = widgetParams;
114+
115+
lastRenderArgs = renderArgs;
116+
117+
if (isFirstRender) {
118+
const sentinel = document.createElement('div');
119+
container.appendChild(document.createElement('ul'));
120+
container.appendChild(sentinel);
121+
122+
const observer = new IntersectionObserver(entries => {
123+
entries.forEach(entry => {
124+
if (entry.isIntersecting && !lastRenderArgs.isLastPage) {
125+
showMore();
126+
}
127+
});
128+
});
129+
130+
observer.observe(sentinel);
131+
132+
return;
133+
}
134+
135+
container.querySelector('ul').innerHTML = items
136+
.map(
137+
item => {
138+
return `
139+
<a href=${item.url} class="container">
140+
<div class="row search-result-row bg-white">
141+
<div class="col-12 col-lg-2">
142+
<div class="my-3">
143+
<img class="image-fluid search-result-image d-flex" src=${item.image} alt=${item.title} />
144+
</div>
145+
</div>
146+
<div class="col-12 col-lg-10">
147+
<h3 class="color-blue search-result-title">${item.title}</h3>
148+
<p class="color-gray search-result-subtitle">${item.subtitle}</p>
149+
</div>
150+
</div>
151+
</a>
152+
`;
153+
})
154+
.join('');
155+
}
156+
);
102157
search.addWidgets([
103158
instantsearch.widgets.searchBox({
104159
container: '#searchbox',
105160
}),
106-
instantsearch.widgets.hits({
107-
container: '#hits',
108-
templates: {
109-
item: (hit, { html, components }) => html`
110-
<a href=${hit.url} class="container">
111-
<div class="row search-result-row">
112-
<div class="col-12 col-lg-2">
113-
<div class="my-3">
114-
<img class="image-fluid search-result-image d-flex" src=${hit.image} alt=${hit.title} />
115-
</div>
116-
</div>
117-
<div class="col-12 col-lg-10">
118-
<h3 class="color-blue search-result-title">${components.Highlight({ hit, attribute: 'title' })}</h3>
119-
<p class="color-gray search-result-subtitle">${components.Highlight({ hit, attribute: 'subtitle' })}</p>
120-
</div>
121-
</div>
122-
</a>
123-
`,
124-
},
125-
}),
126161
instantsearch.widgets.configure({
127162
hitsPerPage: 8,
128163
filters: "language:{{$.Site.Language.Lang}}"
129164
}),
130-
instantsearch.widgets.pagination({
131-
container: '#pagination',
165+
infiniteHits({
166+
container: document.querySelector('#hits'),
132167
}),
133168
]);
134169

170+
135171
search.start();
136172
</script>
137173
<!-- /Search -->

tina/tina-lock.json

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)