Skip to content

Commit e9794f9

Browse files
committed
Add a load more button to algolia search bars
1 parent 846dd66 commit e9794f9

2 files changed

Lines changed: 37 additions & 2 deletions

File tree

src/_includes/layouts/common-js.njk

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,14 @@
3939
'webinars': 'Webinars',
4040
}
4141
const placeholder = Object.prototype.hasOwnProperty.call(scopeTitles, searchScope) ? `Search in ${scopeTitles[searchScope]}...` : 'Search...'
42-
42+
const initialHitsPerPage = 5;
43+
44+
let hitsPerPage = initialHitsPerPage
45+
let initialQuery = '';
46+
4347
const searchQuery = (searchClient, query, scope) => {
48+
initialQuery = query
49+
4450
return {
4551
sourceId : scope,
4652
getItems : () => getAlgoliaResults({
@@ -50,7 +56,7 @@
5056
indexName : 'netlify_00f8cf60-997f-4c4d-9427-a97924358648_live_all',
5157
params : {
5258
query,
53-
hitsPerPage: 5,
59+
hitsPerPage
5460
},
5561
attributesToHighlight: '*',
5662
filters: scope.length === 0 ? undefined : `category:${scope}`
@@ -104,6 +110,9 @@
104110
</div>
105111
</a>`;
106112
},
113+
footer({html}) {
114+
return html`<button id="load-more" class="aa-LoadMore">Load more...</button>`;
115+
}
107116
}
108117
};
109118
}
@@ -127,6 +136,23 @@
127136
document.querySelectorAll('.aa-Panel img').forEach(img => {
128137
img.src = img.getAttribute('data-src');
129138
});
139+
140+
// reset the number of results per page if the query changes
141+
if (state.query !== initialQuery) {
142+
hitsPerPage = initialHitsPerPage
143+
}
144+
145+
const loadMoreBtn = document.querySelector('#load-more');
146+
if (loadMoreBtn) {
147+
loadMoreBtn.onclick = () => {
148+
hitsPerPage += initialHitsPerPage;
149+
150+
const input = document.querySelector('#algolia-search input');
151+
152+
// Trick Autocomplete into refreshing and retrieving more hits per page
153+
input.dispatchEvent(new Event('input', { bubbles: true }));
154+
};
155+
}
130156
}
131157
});
132158
}

src/css/algolia-theme.css

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,3 +80,12 @@
8080
.aa-Panel .aa-ItemContent .aa-ItemContentBody .aa-ItemContentDescription {
8181
color: #777;
8282
}
83+
84+
.aa-Panel .aa-SourceFooter #load-more {
85+
display: block;
86+
width: 100%;
87+
text-align: center;
88+
color: #777;
89+
padding: 10px 0 5px;
90+
font-size: 12px;
91+
}

0 commit comments

Comments
 (0)