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 all 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
1 change: 1 addition & 0 deletions packages/@headlessui-vue/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Ensure hidden `TabPanel` components are hidden from the accessibility tree ([#2708](https://github.com/tailwindlabs/headlessui/pull/2708))
- Add support for `role="alertdialog"` to `<Dialog>` component ([#2709](https://github.com/tailwindlabs/headlessui/pull/2709))
- Ensure blurring the `ComboboxInput` component closes the `Combobox` ([#2712](https://github.com/tailwindlabs/headlessui/pull/2712))
- Allow `<button>` to be in nested components in `<PopoverButton>` ([#2715](https://github.com/tailwindlabs/headlessui/pull/2715))

## [1.7.16] - 2023-08-17

Expand Down
6 changes: 4 additions & 2 deletions packages/@headlessui-vue/src/components/popover/popover.ts
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
}