Skip to content

Commit ee31842

Browse files
authored
Merge pull request #3249 from FlowFuse/add-a-load-more-button-to-algolia-search-boxes
Add a load more button to algolia search bars
2 parents 6e920c8 + e0087e5 commit ee31842

2 files changed

Lines changed: 43 additions & 3 deletions

File tree

src/_includes/layouts/common-js.njk

Lines changed: 29 additions & 3 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,12 +110,15 @@
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
}
110119
111120
autocomplete({
112-
debug: true,
121+
debug: false,
113122
container: '#algolia-search',
114123
placeholder,
115124
getSources({query}) {
@@ -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: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,3 +83,17 @@
8383
.aa-Panel .aa-ItemContent .aa-ItemContentBody .aa-ItemContentDescription {
8484
color: #777;
8585
}
86+
87+
.aa-Panel .aa-SourceFooter #load-more {
88+
display: block;
89+
width: 100%;
90+
text-align: center;
91+
color: #777;
92+
padding: 10px 0 5px;
93+
font-size: 12px;
94+
}
95+
96+
.aa-Panel .aa-SourceFooter #load-more:hover {
97+
cursor: pointer;
98+
color: #2563eb;
99+
}

0 commit comments

Comments
 (0)