diff --git a/src/components/molecules/BlogSearch.astro b/src/components/molecules/BlogSearch.astro index 991d398..c7b461e 100644 --- a/src/components/molecules/BlogSearch.astro +++ b/src/components/molecules/BlogSearch.astro @@ -140,5 +140,30 @@ } 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(); + } + }); }