Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Failed to execute 'getComputedStyle' on 'Window': parameter 1 is not of type 'Element' #81

Merged
merged 1 commit into from
Sep 27, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 27 additions & 5 deletions packages/vue/src/utils/dom.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,34 @@
import type { VirtualElement } from '@floating-ui/core'
import { type ComponentPublicInstance, type Ref, ref as createRef } from 'vue'

// Reference: https://github.com/tailwindlabs/headlessui/blob/426cbf34c50252932f4e26a954226539316ca8bc/packages/%40headlessui-vue/src/utils/dom.ts#L3-L8
// Reference: https://github.com/tailwindlabs/headlessui/blob/d4a94cb5647d9e11ffc72d92033d43cbc3361da7/packages/%40headlessui-vue/src/utils/dom.ts

export function dom<T>(ref?: Ref<T | ComponentPublicInstance | null> | null): T | null {
type AsElement<T extends HTMLElement | VirtualElement | ComponentPublicInstance> =
| (T extends HTMLElement ? T : HTMLElement)
| null

export function dom<T extends HTMLElement | VirtualElement | ComponentPublicInstance>(
ref?: Ref<T | null> | null
): AsElement<T> | null {
if (ref == null) return null
if (ref.value == null) return null

const el = ((ref.value as { $el?: T }).$el ?? ref.value) as T | null
if ((el as { $el?: T }).$el) return dom(createRef(el) as any)
return el
// In this case we check for `Node` because returning `null` from a
// component renders a `Comment` which is a `Node` but not `Element`
// The types don't encode this possibility but we handle it here at runtime
if (ref.value instanceof Node) {
return ref.value as AsElement<T>
}

// Recursion call dom to get element in nested component
if ('$el' in ref.value && ref.value.$el) {
return dom(createRef(ref.value.$el))
}

// Pass `VirtualElement` for Floating UI
if ('getBoundingClientRect' in ref.value) {
return ref.value as AsElement<T>
}

return null
}
Loading