Skip to content

Commit afd2be1

Browse files
Add a spinner when search is in progress
1 parent 78485f4 commit afd2be1

File tree

5 files changed

+47
-13
lines changed

5 files changed

+47
-13
lines changed

src/theme/css/chrome.css

+26-1
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/theme/css/variables.css

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

1516
/* Themes */

src/theme/index.hbs

+6-1
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,12 @@
170170
{{#if search_enabled}}
171171
<div id="search-wrapper" class="hidden">
172172
<form id="searchbar-outer" class="searchbar-outer">
173-
<input type="search" id="searchbar" name="searchbar" placeholder="Search this book ..." aria-controls="searchresults-outer" aria-describedby="searchresults-header" disabled>
173+
<div class="search-wrapper">
174+
<input type="search" id="searchbar" name="searchbar" placeholder="Search this book ..." aria-controls="searchresults-outer" aria-describedby="searchresults-header">
175+
<div class="spinner-wrapper">
176+
<i class="fa fa-spinner fa-spin"></i>
177+
</div>
178+
</div>
174179
</form>
175180
<div id="searchresults-outer" class="searchresults-outer hidden">
176181
<div id="searchresults-header" class="searchresults-header"></div>

src/theme/searcher/searcher.js

+13-7
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'),
@@ -267,12 +268,13 @@ window.search = window.search || {};
267268
doc_urls = config.doc_urls;
268269
searchindex = elasticlunr.Index.load(config.index);
269270

270-
searchbar.removeAttribute("disabled");
271+
searchbar_outer.classList.remove('searching');
272+
271273
searchbar.focus();
272274

273275
const searchterm = searchbar.value.trim();
274-
if (searchterm !== "") {
275-
searchbar.classList.add("active");
276+
if (searchterm !== '') {
277+
searchbar.classList.add('active');
276278
doSearch(searchterm);
277279
}
278280
}
@@ -409,10 +411,12 @@ window.search = window.search || {};
409411
}
410412
}
411413

412-
function loadScript(url, id) {
414+
function loadSearchScript(url, id) {
413415
if (document.getElementById(id)) {
414416
return;
415417
}
418+
searchbar_outer.classList.add('searching');
419+
416420
const script = document.createElement('script');
417421
script.src = url;
418422
script.id = id;
@@ -425,7 +429,7 @@ window.search = window.search || {};
425429

426430
function showSearch(yes) {
427431
if (yes) {
428-
loadScript(path_to_root + '{{ resource "searchindex.js" }}', 'search-index');
432+
loadSearchScript(path_to_root + '{{ resource "searchindex.js" }}', 'search-index');
429433
search_wrap.classList.remove('hidden');
430434
searchicon.setAttribute('aria-expanded', 'true');
431435
} else {
@@ -506,10 +510,11 @@ window.search = window.search || {};
506510

507511
function doSearch(searchterm) {
508512
// Don't search the same twice
509-
if (current_searchterm == searchterm) {
513+
if (current_searchterm === searchterm) {
510514
return;
511515
}
512-
if (searchindex == null) {
516+
searchbar_outer.classList.add('searching');
517+
if (searchindex === null) {
513518
return;
514519
}
515520

@@ -533,6 +538,7 @@ window.search = window.search || {};
533538

534539
// Display results
535540
showResults(true);
541+
searchbar_outer.classList.remove('searching');
536542
}
537543

538544
// Exported functions

tests/gui/search.goml

+1-4
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)