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 ad62617
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 14 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 ?? document

return document
return null
}
14 changes: 6 additions & 8 deletions packages/@headlessui-vue/src/utils/owner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,13 @@ import type { Ref } from 'vue'
import { dom } from './dom'
import { env } from './env'

export function getOwnerDocument<T extends HTMLElement | Ref<HTMLElement | null>>(
export function getOwnerDocument<T extends Element | Ref<Element | null>>(
element: T | null | undefined
) {
): Document | null {
if (env.isServer) return null
if (element instanceof Node) return element.ownerDocument
if (element?.hasOwnProperty('value')) {
let domElement = dom(element as any)
if (domElement) return domElement.ownerDocument
}
if (!element) return document
if ('ownerDocument' in element) return element.ownerDocument
if ('value' in element) return dom(element as any)?.ownerDocument ?? document

return document
return null
}

0 comments on commit ad62617

Please sign in to comment.