-
Notifications
You must be signed in to change notification settings - Fork 91
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: global event handlers called from windows (#2195)
- Loading branch information
1 parent
d65d95c
commit 81aaff4
Showing
4 changed files
with
22 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,34 @@ | ||
import {useEffect} from "react" | ||
import {useDispatch} from "react-redux" | ||
import {useEffect, useMemo} from "react" | ||
|
||
type Listener<K extends keyof DocumentEventMap> = (event: DocumentEventMap[K]) => void | ||
export type DocumentListeners = { | ||
[K in keyof DocumentEventMap]?: Listener<K> | false | ||
} | ||
|
||
const filterEventsByContainer = (container: HTMLElement) => <K extends keyof DocumentEventMap>(listener: Listener<K>): Listener<K> => (event) => { | ||
const path = event.composedPath() || [] | ||
const rootElements = [window, document, document.documentElement, document.body] | ||
const parents = path.filter(t => ![event.target, ...rootElements].includes(t)) | ||
// allow events from root | ||
// ignore all events from outside of container (modals, overlays, etc) | ||
if (!parents.length || parents.includes(container)) { | ||
return listener(event) | ||
} | ||
} | ||
|
||
export function useDocumentListeners(listeners: DocumentListeners): void { | ||
const dispatch = useDispatch() | ||
const filterByRoot = useMemo(() => filterEventsByContainer(document.getElementById("root")), []) | ||
useEffect( | ||
() => { | ||
const entries = Object.entries(listeners).map(([type, listener]) => { | ||
if (listener) { | ||
document.addEventListener(type, listener) | ||
return () => document.removeEventListener(type, listener) | ||
const wrapped = filterByRoot(listener) | ||
document.addEventListener(type, wrapped) | ||
return () => document.removeEventListener(type, wrapped) | ||
} | ||
}) | ||
return () => entries.forEach(unbind => unbind()) | ||
}, | ||
[listeners], | ||
[filterByRoot, listeners], | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters