Skip to content

Commit 31ce373

Browse files
committed
Fix focus ghosting in search results O_o
1 parent 15c2b93 commit 31ce373

File tree

1 file changed

+20
-16
lines changed

1 file changed

+20
-16
lines changed

assets/js/fastsearch.js

+20-16
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import * as params from '@params';
33
var fuse; // holds our search engine
44
var resList = document.getElementById('searchResults');
55
var sInput = document.getElementById('searchInput');
6-
var first, last = null
6+
var first, last, current_elem = null
77
var resultsAvailable = false;
88

99
// load our search index
@@ -38,8 +38,18 @@ window.onload = function () {
3838
xhr.send();
3939
}
4040

41-
function activeToggle() {
42-
document.activeElement.parentElement.classList.toggle("focus")
41+
function activeToggle(ae) {
42+
document.querySelectorAll('.focus').forEach(function (element) {
43+
// rm focus class
44+
element.classList.remove("focus")
45+
});
46+
if (ae) {
47+
ae.focus()
48+
document.activeElement = current_elem = ae;
49+
ae.parentElement.classList.add("focus")
50+
} else {
51+
document.activeElement.parentElement.classList.add("focus")
52+
}
4353
}
4454

4555
function reset() {
@@ -82,44 +92,38 @@ sInput.addEventListener('search', function (e) {
8292
// kb bindings
8393
document.onkeydown = function (e) {
8494
let key = e.key;
85-
let ae = document.activeElement;
95+
var ae = document.activeElement;
96+
8697
let inbox = document.getElementById("searchbox").contains(ae)
8798

8899
if (ae === sInput) {
89100
var elements = document.getElementsByClassName('focus');
90101
while (elements.length > 0) {
91102
elements[0].classList.remove('focus');
92103
}
93-
}
104+
} else if (current_elem) ae = current_elem;
94105

95106
if (key === "ArrowDown" && resultsAvailable && inbox) {
96107
e.preventDefault();
97108
if (ae == sInput) {
98109
// if the currently focused element is the search input, focus the <a> of first <li>
99-
activeToggle(); // rm focus class
100-
resList.firstChild.lastChild.focus();
101-
activeToggle(); // add focus class
110+
activeToggle(resList.firstChild.lastChild);
102111
} else if (ae.parentElement == last) {
103112
// if the currently focused element's parent is last, do nothing
104113
} else {
105114
// otherwise select the next search result
106-
activeToggle(); // rm focus class
107-
ae.parentElement.nextSibling.lastChild.focus();
108-
activeToggle(); // add focus class
115+
activeToggle(ae.parentElement.nextSibling.lastChild);
109116
}
110117
} else if (key === "ArrowUp" && resultsAvailable && inbox) {
111118
e.preventDefault();
112119
if (ae == sInput) {
113120
// if the currently focused element is input box, do nothing
114121
} else if (ae.parentElement == first) {
115122
// if the currently focused element is first item, go to input box
116-
activeToggle(); // rm focus class
117-
sInput.focus();
123+
activeToggle(sInput);
118124
} else {
119125
// otherwise select the previous search result
120-
activeToggle(); // rm focus class
121-
ae.parentElement.previousSibling.lastChild.focus();
122-
activeToggle(); // add focus class
126+
activeToggle(ae.parentElement.previousSibling.lastChild);
123127
}
124128
} else if (key === "ArrowRight" && resultsAvailable && inbox) {
125129
ae.click(); // click on active link

0 commit comments

Comments
 (0)