Skip to content

Commit

Permalink
improve search
Browse files Browse the repository at this point in the history
    - fix menu element becoming inactive
      change class active -> focus
    - set input type seach adityatelange#198
    - add a reset func
    - add script to clear searchbox
      when clicked on X sign
  • Loading branch information
adityatelange committed Feb 7, 2021
1 parent 7170eda commit e03348c
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 18 deletions.
2 changes: 1 addition & 1 deletion assets/css/search.css
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
outline: none;
}

#searchResults .active {
#searchResults .focus {
transform: scale(.98);
border: 2px solid var(--tertiary)
}
39 changes: 24 additions & 15 deletions assets/js/fastsearch.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,13 @@ function itemGen(name, link) {
}

function activeToggle() {
document.activeElement.parentElement.classList.toggle("active")
document.activeElement.parentElement.classList.toggle("focus")
}

function reset() {
resultsAvailable = false;
resList.innerHTML = sInput.value = ''; // clear inputbox and searchResults
sInput.focus(); // shift focus to input box
}

// execute search as each character is typed
Expand All @@ -59,63 +65,66 @@ sInput.onkeyup = function (e) {
resultSet = resultSet + itemGen(results[item].item.title, results[item].item.permalink)
}

document.getElementById("searchResults").innerHTML = resultSet;
resList.innerHTML = resultSet;
resultsAvailable = true;
first = resList.firstChild;
last = resList.lastChild;
} else {
resultsAvailable = false;
document.getElementById("searchResults").innerHTML = '';
resList.innerHTML = '';
}
}

sInput.addEventListener('search', function (e) {
// clicked on x
if (!this.value) reset()
})

// kb bindings
document.onkeydown = function (e) {
let key = e.key;
let ae = document.activeElement;
let inbox = document.getElementById("searchbox").contains(ae)

if (ae === sInput) {
var elements = document.getElementsByClassName('active');
var elements = document.getElementsByClassName('focus');
while (elements.length > 0) {
elements[0].classList.remove('active');
elements[0].classList.remove('focus');
}
}

if (key === "ArrowDown" && resultsAvailable && inbox) {
e.preventDefault();
if (ae == sInput) {
// if the currently focused element is the search input, focus the <a> of first <li>
activeToggle(); // rm active class
activeToggle(); // rm focus class
resList.firstChild.lastChild.focus();
activeToggle(); // add active class
activeToggle(); // add focus class
} else if (ae.parentElement == last) {
// if the currently focused element's parent is last, do nothing
} else {
// otherwise select the next search result
activeToggle(); // rm active class
activeToggle(); // rm focus class
ae.parentElement.nextSibling.lastChild.focus();
activeToggle(); // add active class
activeToggle(); // add focus class
}
} else if (key === "ArrowUp" && resultsAvailable && inbox) {
e.preventDefault();
if (ae == sInput) {
// if the currently focused element is input box, do nothing
} else if (ae.parentElement == first) {
// if the currently focused element is first item, go to input box
activeToggle(); // rm active class
activeToggle(); // rm focus class
sInput.focus();
} else {
// otherwise select the previous search result
activeToggle(); // rm active class
activeToggle(); // rm focus class
ae.parentElement.previousSibling.lastChild.focus();
activeToggle(); // add active class
activeToggle(); // add focus class
}
} else if (key === "ArrowRight" && resultsAvailable && inbox) {
ae.click(); // click on active link
} else if (key === "Escape") {
resultsAvailable = false;
resList.innerHTML = sInput.value = ''; // clear inputbox and searchResults
sInput.focus(); // shift focus to input box
reset()
}
}
4 changes: 2 additions & 2 deletions layouts/_default/search.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ <h1>{{ .Title }}
</header>

<div id="searchbox">
<input id="searchInput" autofocus placeholder="{{.Title}} ↵" aria-label="search">
<input id="searchInput" autofocus placeholder="{{.Title}} ↵" aria-label="search" type="search">
<ul id="searchResults" aria-label="search results"></ul>
</div>

{{- end }}{{/* end main */}}
{{- end }}{{/* end main */}}

0 comments on commit e03348c

Please sign in to comment.