feat(blog): / focuses search, Esc clears and blurs

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Josh Bairstow 2026-06-02 01:32:08 +10:00
parent 468d4e460d
commit 963ea56314

View file

@ -140,5 +140,30 @@
} }
input.addEventListener("input", applyFilter); input.addEventListener("input", applyFilter);
// `/` from anywhere on the page focuses the search input — unless the user
// is already typing somewhere editable (text input, textarea, or any
// contenteditable element). `Esc` while the input is focused clears the
// query, blurs, and restores the full archive.
document.addEventListener("keydown", (event) => {
if (event.key === "/" && !event.metaKey && !event.ctrlKey && !event.altKey) {
const target = event.target as HTMLElement | null;
const tag = target?.tagName;
const editable =
tag === "INPUT" ||
tag === "TEXTAREA" ||
target?.isContentEditable === true;
if (!editable) {
event.preventDefault();
input!.focus();
}
} else if (event.key === "Escape" && document.activeElement === input) {
if (input!.value !== "") {
input!.value = "";
applyFilter();
}
input!.blur();
}
});
} }
</script> </script>