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): call handler if click event is fired by a keypress #2426

Merged
merged 2 commits into from Nov 13, 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
13 changes: 9 additions & 4 deletions packages/core/onClickOutside/index.ts
@@ -1,5 +1,4 @@
import type { Fn } from '@vueuse/shared'
import { ref } from 'vue-demi'
import type { MaybeElementRef } from '../unrefElement'
import { unrefElement } from '../unrefElement'
import { useEventListener } from '../useEventListener'
Expand Down Expand Up @@ -43,7 +42,7 @@ export function onClickOutside<T extends OnClickOutsideOptions>(
if (!window)
return

const shouldListen = ref(true)
let shouldListen = true

let fallback: number

Expand All @@ -52,9 +51,14 @@ export function onClickOutside<T extends OnClickOutsideOptions>(

const el = unrefElement(target)

if (!el || el === event.target || event.composedPath().includes(el) || !shouldListen.value)
if (!el || el === event.target || event.composedPath().includes(el))
return

if (!shouldListen) {
shouldListen = true
return
}

handler(event)
}

Expand All @@ -69,7 +73,8 @@ export function onClickOutside<T extends OnClickOutsideOptions>(
useEventListener(window, 'click', listener, { passive: true, capture }),
useEventListener(window, 'pointerdown', (e) => {
const el = unrefElement(target)
shouldListen.value = !!el && !e.composedPath().includes(el) && !shouldIgnore(e)
if (el)
shouldListen = !e.composedPath().includes(el) && !shouldIgnore(e)
}, { passive: true }),
useEventListener(window, 'pointerup', (e) => {
if (e.button === 0) {
Expand Down