mirror of
https://github.com/ArthurDanjou/ui.git
synced 2026-01-22 07:50:36 +01:00
feat(App): add global portal prop (#3688)
Co-authored-by: Benjamin Canac <canacb1@gmail.com>
This commit is contained in:
committed by
GitHub
parent
7a35baebc7
commit
29fa46276d
24
src/runtime/composables/usePortal.ts
Normal file
24
src/runtime/composables/usePortal.ts
Normal 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
|
||||
}))
|
||||
}
|
||||
Reference in New Issue
Block a user