Closed devYuraKim closed 2 months ago
document.activeElement
with 1)case-insensitive 2)tag comparisondocument.activeElement
returns the element itself, not a name or a tag.document.activeElement.tagName
returns the tag name in all capital letters. With a lowercase string, the comparison will fail.useEffect(
function () {
function useBackspace(e) {
const activeElement = document.activeElement.tagName.toLowerCase();
if (
activeElement !== "textarea" &&
activeElement !== "input" &&
e.code === "Backspace"
)
navigate(-1);
}
document.addEventListener("keydown", useBackspace);
return () => document.removeEventListener("keydown", useBackspace);
},
[navigate]
);
The Backspace key triggers navigation even when I'm editing text in an input or textarea field!