Skip to content

Commit fe322bd

Browse files
authored
feat(OnClickOutside): support component with fragments (#4313)
1 parent 9e78eda commit fe322bd

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

packages/core/onClickOutside/index.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { Fn, MaybeRefOrGetter } from '@vueuse/shared'
2+
import type { ComponentPublicInstance, VNode } from 'vue-demi'
23
import type { ConfigurableWindow } from '../_configurable'
34
import type { MaybeElementRef } from '../unrefElement'
45
import { isIOS, noop, toValue } from '@vueuse/shared'
@@ -69,9 +70,35 @@ export function onClickOutside<T extends OnClickOutsideOptions>(
6970
})
7071
}
7172

73+
/**
74+
* Determines if the given target has multiple root elements.
75+
* Referenced from: https://github.com/vuejs/test-utils/blob/ccb460be55f9f6be05ab708500a41ec8adf6f4bc/src/vue-wrapper.ts#L21
76+
*/
77+
function hasMultipleRoots(target: MaybeElementRef): boolean {
78+
const vm = toValue(target) as ComponentPublicInstance
79+
return vm && vm.$.subTree.shapeFlag === 16
80+
}
81+
82+
function checkMultipleRoots(target: MaybeElementRef, event: PointerEvent): boolean {
83+
const vm = toValue(target) as ComponentPublicInstance
84+
const children = vm.$.subTree && vm.$.subTree.children
85+
86+
if (children == null || !Array.isArray(children))
87+
return false
88+
89+
// @ts-expect-error should be VNode
90+
return children.some((child: VNode) => child.el === event.target || event.composedPath().includes(child.el))
91+
}
92+
7293
const listener = (event: PointerEvent) => {
7394
const el = unrefElement(target)
7495

96+
if (event.target == null)
97+
return
98+
99+
if (!(el instanceof Element) && hasMultipleRoots(target) && checkMultipleRoots(target, event))
100+
return
101+
75102
if (!el || el === event.target || event.composedPath().includes(el))
76103
return
77104

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy