Skip to content

Commit

Permalink
fix: check unmounted even targets (COR-000) (#36)
Browse files Browse the repository at this point in the history
  • Loading branch information
z4o4z authored Sep 3, 2024
1 parent d437aab commit aebd4ef
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"path": "./node_modules/cz-conventional-changelog"
}
},
"version": "0.3.3",
"version": "0.4.2",
"description": "React dismissable context and hook with layers (nesting) support",
"exports": {
".": {
Expand Down
7 changes: 6 additions & 1 deletion src/useDismissable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,12 @@ const useDismissable = (
cache.current.preventClose?.(event) ||
!cache.current.isOpened ||
(cache.current.skipDefaultPrevented && event?.defaultPrevented) ||
(event?.target && ref?.current?.contains?.(event.target as Element));
(ref?.current &&
event?.target &&
event.target instanceof Node &&
// sometimes event target can be a unmounted due to state changes in the same event loop
// so .contains will return false, but we can check it by composedPath
(event.target.isConnected ? ref.current.contains?.(event.target) : event.composedPath().some((target) => ref.current === target)));

if (skipEvent) {
return;
Expand Down

0 comments on commit aebd4ef

Please sign in to comment.