feat(blog): / focuses search, Esc clears and blurs
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
parent
468d4e460d
commit
963ea56314
1 changed files with 25 additions and 0 deletions
|
|
@ -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();
|
||||
}
|
||||
});
|
||||
}
|
||||
</script>
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue