Skip to content

Commit

Permalink
improve getOwnerDocument
Browse files Browse the repository at this point in the history
The `instanceof Node` ia not a good idea because `Node` is different
depending on where it runs (main document vs iframe)
  • Loading branch information
RobinMalfait committed Dec 12, 2024
1 parent 1f756a3 commit 9b84d58
Showing 1 changed file with 7 additions and 8 deletions.
15 changes: 7 additions & 8 deletions packages/@headlessui-react/src/utils/owner.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import type { MutableRefObject } from 'react'
import { env } from './env'
import type {MutableRefObject} from 'react'
import {env} from './env'

export function getOwnerDocument<T extends Element | MutableRefObject<Element | null>>(
element: T | null | undefined
) {
): Document | null {
if (env.isServer) return null
if (element instanceof Node) return element.ownerDocument
if (element?.hasOwnProperty('current')) {
if (element.current instanceof Node) return element.current.ownerDocument
}
if (!element) return document
if ('ownerDocument' in element) return element.ownerDocument
if ('current' in element) return element.current?.ownerDocument ?? document

return document
return null
}

0 comments on commit 9b84d58

Please sign in to comment.