Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(onClickOutside): apply ignore list on keyboard clicks #2438

Merged
merged 1 commit into from Dec 20, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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