Skip to content

Commit 642061a

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 642061a

File tree

1 file changed

+5
-6
lines changed
  • packages/@headlessui-react/src/utils

1 file changed

+5
-6
lines changed

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

Lines changed: 5 additions & 6 deletions
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 ?? null
1211

13-
return document
12+
return null
1413
}

0 commit comments

Comments
 (0)