Files
headlessui/packages/@headlessui-react/src/utils/owner.ts
T
Robin Malfait c219d87a69 Use ownerDocument instead of document (#1158)
* use `ownerDocument` instead of `document`

This should ensure that in iframes and new windows the correct document
is being used.

* update changelog
2022-03-10 13:37:50 +01:00

14 lines
422 B
TypeScript

import { MutableRefObject } from 'react'
export function getOwnerDocument<T extends Element | MutableRefObject<Element | null>>(
element: T | null | undefined
) {
if (typeof window === 'undefined') return null
if (element instanceof Node) return element.ownerDocument
if (element?.hasOwnProperty('current')) {
if (element.current instanceof Node) return element.current.ownerDocument
}
return document
}