Skip to content

Commit

Permalink
fix(onClickOutside): apply ignore list on keyboard clicks (#2438)
Browse files Browse the repository at this point in the history
  • Loading branch information
sibbng committed Dec 20, 2022
1 parent f40a021 commit 12e21d3
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions packages/core/onClickOutside/index.ts
Expand Up @@ -37,7 +37,7 @@ export function onClickOutside<T extends OnClickOutsideOptions>(
handler: OnClickOutsideHandler<{ detectIframe: T['detectIframe'] }>,
options: T = {} as T,
) {
const { window = defaultWindow, ignore, capture = true, detectIframe = false } = options
const { window = defaultWindow, ignore = [], capture = true, detectIframe = false } = options

if (!window)
return
Expand All @@ -46,6 +46,13 @@ export function onClickOutside<T extends OnClickOutsideOptions>(

let fallback: number

const shouldIgnore = (event: PointerEvent) => {
return ignore.some((target) => {
const el = unrefElement(target)
return el && (event.target === el || event.composedPath().includes(el))
})
}

const listener = (event: PointerEvent) => {
window.clearTimeout(fallback)

Expand All @@ -54,6 +61,9 @@ export function onClickOutside<T extends OnClickOutsideOptions>(
if (!el || el === event.target || event.composedPath().includes(el))
return

if (event.detail === 0)
shouldListen = !shouldIgnore(event)

if (!shouldListen) {
shouldListen = true
return
Expand All @@ -62,13 +72,6 @@ export function onClickOutside<T extends OnClickOutsideOptions>(
handler(event)
}

const shouldIgnore = (event: PointerEvent) => {
return ignore && ignore.some((target) => {
const el = unrefElement(target)
return el && (event.target === el || event.composedPath().includes(el))
})
}

const cleanup = [
useEventListener(window, 'click', listener, { passive: true, capture }),
useEventListener(window, 'pointerdown', (e) => {
Expand Down

0 comments on commit 12e21d3

Please sign in to comment.