Skip to content

Commit 9b84d58

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 9b84d58

File tree

1 file changed

+7
-8
lines changed
  • packages/@headlessui-react/src/utils

1 file changed

+7
-8
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
1-
import type { MutableRefObject } from 'react'
2-
import { env } from './env'
1+
import type {MutableRefObject} from 'react'
2+
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
}

0 commit comments

Comments
 (0)