Be less aggressive in maintaining focus on search box (#4395)

* Be less aggressive in maintaining focus on search box

* Also focus when clear clicked
This commit is contained in:
Clyde Stubbs 2024-10-28 22:32:09 +11:00 committed by GitHub
parent 7ce07b9512
commit 542a91a42d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -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();
}
}
@ -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>