-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve outside click support (#1175)
* improve outside click support We used to use `pointerdown`, but some older devices with iOS 12 didn't have support for that. Instead we used `mousedown`. But now it turns out that some devices only properly use `pointerdown` and not the `mousedown` event. Instead, we will listen to both, but make sure to only handle the event once. * update changelog
- Loading branch information
1 parent
1b3837b
commit cefb899
Showing
13 changed files
with
150 additions
and
60 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
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
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 |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import { MutableRefObject, useMemo, useRef } from 'react' | ||
import { useLatestValue } from './use-latest-value' | ||
import { useWindowEvent } from './use-window-event' | ||
|
||
// Polyfill | ||
function microTask(cb: () => void) { | ||
if (typeof queueMicrotask === 'function') { | ||
queueMicrotask(cb) | ||
} else { | ||
Promise.resolve() | ||
.then(cb) | ||
.catch((e) => | ||
setTimeout(() => { | ||
throw e | ||
}) | ||
) | ||
} | ||
} | ||
|
||
export function useOutsideClick( | ||
containers: | ||
| HTMLElement | ||
| MutableRefObject<HTMLElement | null> | ||
| (MutableRefObject<HTMLElement | null> | HTMLElement | null)[] | ||
| Set<HTMLElement>, | ||
cb: (event: MouseEvent | PointerEvent, target: HTMLElement) => void | ||
) { | ||
let _containers = useMemo(() => { | ||
if (Array.isArray(containers)) { | ||
return containers | ||
} | ||
|
||
if (containers instanceof Set) { | ||
return containers | ||
} | ||
|
||
return [containers] | ||
}, [containers]) | ||
|
||
let called = useRef(false) | ||
let handler = useLatestValue((event: MouseEvent | PointerEvent) => { | ||
if (called.current) return | ||
called.current = true | ||
microTask(() => { | ||
called.current = false | ||
}) | ||
|
||
let target = event.target as HTMLElement | ||
|
||
for (let container of _containers) { | ||
if (container === null) continue | ||
let domNode = container instanceof HTMLElement ? container : container.current | ||
if (domNode?.contains(target)) { | ||
return | ||
} | ||
} | ||
|
||
return cb(event, target) | ||
}) | ||
|
||
useWindowEvent('pointerdown', (...args) => handler.current(...args)) | ||
useWindowEvent('mousedown', (...args) => handler.current(...args)) | ||
} |
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
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
Oops, something went wrong.