Skip to content

Commit 55fd7ad

Browse files
Add a spinner when search is in progress
1 parent 35c8615 commit 55fd7ad

File tree

5 files changed

+47
-13
lines changed

5 files changed

+47
-13
lines changed

src/front-end/css/chrome.css

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -344,9 +344,34 @@ mark.fade-out {
344344
max-width: var(--content-max-width);
345345
}
346346

347+
#searchbar-outer.searching #searchbar {
348+
padding-right: 30px;
349+
}
350+
#searchbar-outer .spinner-wrapper {
351+
display: none;
352+
}
353+
#searchbar-outer.searching .spinner-wrapper {
354+
display: block;
355+
}
356+
357+
.search-wrapper {
358+
position: relative;
359+
}
360+
361+
.spinner-wrapper {
362+
--spinner-margin: 2px;
363+
position: absolute;
364+
margin-block-start: calc(var(--searchbar-margin-block-start) + var(--spinner-margin));
365+
right: var(--spinner-margin);
366+
top: 0;
367+
bottom: var(--spinner-margin);
368+
padding: 6px;
369+
background-color: var(--bg);
370+
}
371+
347372
#searchbar {
348373
width: 100%;
349-
margin-block-start: 5px;
374+
margin-block-start: var(--searchbar-margin-block-start);
350375
margin-block-end: 0;
351376
margin-inline-start: auto;
352377
margin-inline-end: auto;

src/front-end/css/variables.css

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
--menu-bar-height: 50px;
1212
--mono-font: "Source Code Pro", Consolas, "Ubuntu Mono", Menlo, "DejaVu Sans Mono", monospace, monospace;
1313
--code-font-size: 0.875em; /* please adjust the ace font size accordingly in editor.js */
14+
--searchbar-margin-block-start: 5px;
1415
}
1516

1617
/* Themes */

src/front-end/searcher/searcher.js

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ window.search = window.search || {};
2222
}
2323

2424
const search_wrap = document.getElementById('search-wrapper'),
25+
searchbar_outer = document.getElementById('searchbar-outer'),
2526
searchbar = document.getElementById('searchbar'),
2627
searchresults = document.getElementById('searchresults'),
2728
searchresults_outer = document.getElementById('searchresults-outer'),
@@ -262,12 +263,13 @@ window.search = window.search || {};
262263
doc_urls = config.doc_urls;
263264
searchindex = elasticlunr.Index.load(config.index);
264265

265-
searchbar.removeAttribute("disabled");
266+
searchbar_outer.classList.remove('searching');
267+
266268
searchbar.focus();
267269

268270
const searchterm = searchbar.value.trim();
269-
if (searchterm !== "") {
270-
searchbar.classList.add("active");
271+
if (searchterm !== '') {
272+
searchbar.classList.add('active');
271273
doSearch(searchterm);
272274
}
273275
}
@@ -414,10 +416,12 @@ window.search = window.search || {};
414416
}
415417
}
416418

417-
function loadScript(url, id) {
419+
function loadSearchScript(url, id) {
418420
if (document.getElementById(id)) {
419421
return;
420422
}
423+
searchbar_outer.classList.add('searching');
424+
421425
const script = document.createElement('script');
422426
script.src = url;
423427
script.id = id;
@@ -430,7 +434,7 @@ window.search = window.search || {};
430434

431435
function showSearch(yes) {
432436
if (yes) {
433-
loadScript(path_to_root + '{{ resource "searchindex.js" }}', 'search-index');
437+
loadSearchScript(path_to_root + '{{ resource "searchindex.js" }}', 'search-index');
434438
search_wrap.classList.remove('hidden');
435439
searchicon.setAttribute('aria-expanded', 'true');
436440
} else {
@@ -511,10 +515,11 @@ window.search = window.search || {};
511515

512516
function doSearch(searchterm) {
513517
// Don't search the same twice
514-
if (current_searchterm == searchterm) {
518+
if (current_searchterm === searchterm) {
515519
return;
516520
}
517-
if (searchindex == null) {
521+
searchbar_outer.classList.add('searching');
522+
if (searchindex === null) {
518523
return;
519524
}
520525

@@ -538,6 +543,7 @@ window.search = window.search || {};
538543

539544
// Display results
540545
showResults(true);
546+
searchbar_outer.classList.remove('searching');
541547
}
542548

543549
// Exported functions

src/front-end/templates/index.hbs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,12 @@
186186
{{#if search_enabled}}
187187
<div id="search-wrapper" class="hidden">
188188
<form id="searchbar-outer" class="searchbar-outer">
189-
<input type="search" id="searchbar" name="searchbar" placeholder="Search this book ..." aria-controls="searchresults-outer" aria-describedby="searchresults-header" disabled>
189+
<div class="search-wrapper">
190+
<input type="search" id="searchbar" name="searchbar" placeholder="Search this book ..." aria-controls="searchresults-outer" aria-describedby="searchresults-header">
191+
<div class="spinner-wrapper">
192+
<i class="fa fa-spinner fa-spin"></i>
193+
</div>
194+
</div>
190195
</form>
191196
<div id="searchresults-outer" class="searchresults-outer hidden">
192197
<div id="searchresults-header" class="searchresults-header"></div>

tests/gui/search.goml

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,13 @@ wait-for-text: ("#searchresults-header", "2 search results for 'strikethrough':"
3232
// Now we test search shortcuts and more page changes.
3333
go-to: |DOC_PATH| + "index.html"
3434

35-
// First we ensure that the search input is disabled and hidden.
36-
assert: "#searchbar:disabled"
3735
// This check is to ensure that the search bar is inside the search wrapper.
3836
assert: "#search-wrapper #searchbar"
3937
assert-css: ("#search-wrapper", {"display": "none"})
4038

4139
// Now we make the search input appear with the `S` shortcut.
4240
press-key: 'S'
43-
wait-for: "#searchbar:not(:disabled)"
44-
assert-css: ("#search-wrapper", {"display": "block"})
41+
wait-for-css: ("#search-wrapper", {"display": "block"})
4542
// We ensure the search bar has the focus.
4643
assert: "#searchbar:focus"
4744

0 commit comments

Comments
 (0)