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 642061a
Showing 1 changed file with 5 additions and 6 deletions.
11 changes: 5 additions & 6 deletions packages/@headlessui-react/src/utils/owner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@ 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 ?? null

return document
return null
}

0 comments on commit 642061a

Please sign in to comment.