Skip to content

Commit ad62617

Browse files
committed
improve getOwnerDocument
The `instanceof Node` ia not a good idea because `Node` is different depending on where it runs (main document vs iframe)
1 parent 1f756a3 commit ad62617

File tree

2 files changed

+11
-14
lines changed

2 files changed

+11
-14
lines changed

packages/@headlessui-react/src/utils/owner.ts

+5-6
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,11 @@ import { env } from './env'
33

44
export function getOwnerDocument<T extends Element | MutableRefObject<Element | null>>(
55
element: T | null | undefined
6-
) {
6+
): Document | null {
77
if (env.isServer) return null
8-
if (element instanceof Node) return element.ownerDocument
9-
if (element?.hasOwnProperty('current')) {
10-
if (element.current instanceof Node) return element.current.ownerDocument
11-
}
8+
if (!element) return document
9+
if ('ownerDocument' in element) return element.ownerDocument
10+
if ('current' in element) return element.current?.ownerDocument ?? document
1211

13-
return document
12+
return null
1413
}

packages/@headlessui-vue/src/utils/owner.ts

+6-8
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,13 @@ import type { Ref } from 'vue'
22
import { dom } from './dom'
33
import { env } from './env'
44

5-
export function getOwnerDocument<T extends HTMLElement | Ref<HTMLElement | null>>(
5+
export function getOwnerDocument<T extends Element | Ref<Element | null>>(
66
element: T | null | undefined
7-
) {
7+
): Document | null {
88
if (env.isServer) return null
9-
if (element instanceof Node) return element.ownerDocument
10-
if (element?.hasOwnProperty('value')) {
11-
let domElement = dom(element as any)
12-
if (domElement) return domElement.ownerDocument
13-
}
9+
if (!element) return document
10+
if ('ownerDocument' in element) return element.ownerDocument
11+
if ('value' in element) return dom(element as any)?.ownerDocument ?? document
1412

15-
return document
13+
return null
1614
}

0 commit comments

Comments
 (0)