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

Allow <button> to be in nested components in <PopoverButton> #2715

Merged
merged 5 commits into from
Aug 30, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 4 additions & 2 deletions packages/@headlessui-vue/src/components/popover/popover.ts
Expand Up @@ -14,6 +14,7 @@ import {
// Types
InjectionKey,
Ref,
ComponentPublicInstance,
} from 'vue'

import { match } from '../../utils/match'
Expand Down Expand Up @@ -315,12 +316,13 @@ export let PopoverButton = defineComponent({
panelContext === null ? false : panelContext.value === api.panelId.value
)

let elementRef = ref(null)
let elementRef = ref<HTMLElement | ComponentPublicInstance | null>(null)
let sentinelId = `headlessui-focus-sentinel-${useId()}`

if (!isWithinPanel.value) {
watchEffect(() => {
api.button.value = elementRef.value
// `elementRef` could be a Vue component in which case we want to grab the DOM element from it
api.button.value = dom(elementRef)
})
}

Expand Down
4 changes: 2 additions & 2 deletions packages/@headlessui-vue/src/hooks/use-resolve-button-type.ts
@@ -1,4 +1,4 @@
import { ref, onMounted, watchEffect, Ref } from 'vue'
import { ref, onMounted, watchEffect, Ref, ComponentPublicInstance } from 'vue'
import { dom } from '../utils/dom'

function resolveType(type: unknown, as: string | object) {
Expand All @@ -12,7 +12,7 @@ function resolveType(type: unknown, as: string | object) {

export function useResolveButtonType(
data: Ref<{ as: string | object; type?: unknown }>,
refElement: Ref<HTMLElement | null>
refElement: Ref<HTMLElement | ComponentPublicInstance | null>
) {
let type = ref(resolveType(data.value.type, data.value.as))

Expand Down
19 changes: 17 additions & 2 deletions packages/@headlessui-vue/src/utils/dom.ts
@@ -1,8 +1,23 @@
import { Ref, ComponentPublicInstance } from 'vue'

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

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

return (ref.value as { $el?: T }).$el ?? ref.value
let el = (ref.value as { $el?: T }).$el ?? ref.value

// 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 (el instanceof Node) {
return el as AsElement<T>
}

return null
}