Skip to content

Commit

Permalink
fix: hide modal upon keyboard blur
Browse files Browse the repository at this point in the history
  • Loading branch information
David Walsh committed Oct 12, 2023
1 parent 5fee3c6 commit 08a46a2
Showing 1 changed file with 3 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/hooks/useOnClickOutside.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export function useOnClickOutside<T extends HTMLElement>(
}, [handler])

useEffect(() => {
const handleClickOutside = (e: MouseEvent) => {
const handleClickOutside = (e: MouseEvent | KeyboardEvent) => {
const nodeClicked = node.current?.contains(e.target as Node)
const ignoredNodeClicked = ignoredNodes.reduce(
(reducer, val) => reducer || !!val.current?.contains(e.target as Node),
Expand All @@ -27,9 +27,11 @@ export function useOnClickOutside<T extends HTMLElement>(
}

document.addEventListener('mousedown', handleClickOutside)
document.addEventListener('keyup', handleClickOutside)

return () => {
document.removeEventListener('mousedown', handleClickOutside)
document.removeEventListener('keyup', handleClickOutside)
}
}, [node, ignoredNodes])
}

0 comments on commit 08a46a2

Please sign in to comment.