Skip to content

Commit

Permalink
Be less aggressive in maintaining focus on search box (#4395)
Browse files Browse the repository at this point in the history
* Be less aggressive in maintaining focus on search box

* Also focus when clear clicked
  • Loading branch information
clydebarrow authored Oct 28, 2024
1 parent 7ce07b9 commit 542a91a
Showing 1 changed file with 16 additions and 8 deletions.
24 changes: 16 additions & 8 deletions _templates/searchbox.html
Original file line number Diff line number Diff line change
Expand Up @@ -157,10 +157,20 @@
resizeTarget();
}

function search_focus() {
const input_field = document.getElementById("pfmod-input-0");
if (input_field) {
requestAnimationFrame(() => input_field.focus());
}
}


function hideTargets() {
if (target.style.display !== "none") {
target.style.display = "none";
document.removeEventListener('click', clickCallback);
// keep focus on search box after clicking on search result
search_focus();
}
}

Expand Down Expand Up @@ -200,14 +210,12 @@
}
});
}
const input_field = document.getElementById("pfmod-input-0");
if (input_field) {
input_field.focus({preventScroll: true});
input_field.addEventListener("blur", () => {
requestAnimationFrame(() => {
input_field.focus({preventScroll: true});
});
})
// focus the search field on page load.
search_focus();
// focus when clicking on clear button
const clears = document.getElementsByClassName("pagefind-modular-list-clear");
for (let i = 0; i !== clears.length; i++) {
clears[i].addEventListener("click", search_focus);
}
});
</script>

0 comments on commit 542a91a

Please sign in to comment.