feat(App): add global portal prop (#3688)

Co-authored-by: Benjamin Canac <canacb1@gmail.com>
This commit is contained in:
Nathanaël Louison
2025-04-21 17:42:55 +02:00
committed by GitHub
parent 7a35baebc7
commit 29fa46276d
15 changed files with 82 additions and 28 deletions

View File

@@ -0,0 +1,24 @@
import { inject, provide, computed, type Ref, type InjectionKey } from 'vue'
export const portalTargetInjectionKey: InjectionKey<Ref<string | HTMLElement>> = Symbol('nuxt-ui.portal-target')
export function usePortal(portal: Ref<string | HTMLElement | boolean | undefined>) {
const portalTarget = inject(portalTargetInjectionKey, undefined)
const to = computed(() => {
if (typeof portal.value === 'string' || portal.value instanceof HTMLElement) {
return portal.value
}
return portalTarget?.value ?? 'body'
})
const disabled = computed(() => typeof portal.value === 'boolean' ? !portal.value : false)
provide(portalTargetInjectionKey, computed(() => to.value))
return computed(() => ({
to: to.value,
disabled: disabled.value
}))
}