mirror of
https://github.com/ArthurDanjou/ui.git
synced 2026-01-14 12:14:41 +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
@@ -6,6 +6,7 @@ export interface AppProps<T extends Messages = Messages> extends Omit<ConfigProv
|
||||
tooltip?: TooltipProviderProps
|
||||
toaster?: ToasterProps | null
|
||||
locale?: Locale<T>
|
||||
portal?: string | HTMLElement
|
||||
}
|
||||
|
||||
export interface AppSlots {
|
||||
@@ -22,10 +23,14 @@ import { toRef, useId, provide } from 'vue'
|
||||
import { ConfigProvider, TooltipProvider, useForwardProps } from 'reka-ui'
|
||||
import { reactivePick } from '@vueuse/core'
|
||||
import { localeContextInjectionKey } from '../composables/useLocale'
|
||||
import { portalTargetInjectionKey } from '../composables/usePortal'
|
||||
import UToaster from './Toaster.vue'
|
||||
import UOverlayProvider from './OverlayProvider.vue'
|
||||
|
||||
const props = defineProps<AppProps<T>>()
|
||||
const props = withDefaults(defineProps<AppProps<T>>(), {
|
||||
portal: 'body'
|
||||
})
|
||||
|
||||
defineSlots<AppSlots>()
|
||||
|
||||
const configProviderProps = useForwardProps(reactivePick(props, 'scrollBody'))
|
||||
@@ -34,6 +39,9 @@ const toasterProps = toRef(() => props.toaster)
|
||||
|
||||
const locale = toRef(() => props.locale)
|
||||
provide(localeContextInjectionKey, locale)
|
||||
|
||||
const portal = toRef(() => props.portal)
|
||||
provide(portalTargetInjectionKey, portal)
|
||||
</script>
|
||||
|
||||
<template>
|
||||
|
||||
@@ -66,7 +66,7 @@ export interface ContextMenuProps<T extends ArrayOrNested<ContextMenuItem> = Arr
|
||||
* Render the menu in a portal.
|
||||
* @defaultValue true
|
||||
*/
|
||||
portal?: boolean
|
||||
portal?: boolean | string | HTMLElement
|
||||
/**
|
||||
* The key used to get the label from the item.
|
||||
* @defaultValue 'label'
|
||||
|
||||
@@ -9,7 +9,7 @@ type ContextMenu = ComponentConfig<typeof theme, AppConfig, 'contextMenu'>
|
||||
|
||||
interface ContextMenuContentProps<T extends ArrayOrNested<ContextMenuItem>> extends Omit<RekaContextMenuContentProps, 'as' | 'asChild' | 'forceMount'> {
|
||||
items?: T
|
||||
portal?: boolean
|
||||
portal?: boolean | string | HTMLElement
|
||||
sub?: boolean
|
||||
labelKey: keyof NestedItem<T>
|
||||
/**
|
||||
@@ -33,12 +33,13 @@ interface ContextMenuContentEmits extends RekaContextMenuContentEmits {}
|
||||
</script>
|
||||
|
||||
<script setup lang="ts" generic="T extends ArrayOrNested<ContextMenuItem>">
|
||||
import { computed } from 'vue'
|
||||
import { computed, toRef } from 'vue'
|
||||
import { ContextMenu } from 'reka-ui/namespaced'
|
||||
import { useForwardPropsEmits } from 'reka-ui'
|
||||
import { reactiveOmit, createReusableTemplate } from '@vueuse/core'
|
||||
import { useAppConfig } from '#imports'
|
||||
import { useLocale } from '../composables/useLocale'
|
||||
import { usePortal } from '../composables/usePortal'
|
||||
import { omit, get, isArrayOfArray } from '../utils'
|
||||
import { pickLinkProps } from '../utils/link'
|
||||
import ULinkBase from './LinkBase.vue'
|
||||
@@ -56,6 +57,7 @@ const slots = defineSlots<ContextMenuSlots<T>>()
|
||||
const { dir } = useLocale()
|
||||
const appConfig = useAppConfig()
|
||||
|
||||
const portalProps = usePortal(toRef(() => props.portal))
|
||||
const contentProps = useForwardPropsEmits(reactiveOmit(props, 'sub', 'items', 'portal', 'labelKey', 'checkedIcon', 'loadingIcon', 'externalIcon', 'class', 'ui', 'uiOverride'), emits)
|
||||
const proxySlots = omit(slots, ['default'])
|
||||
|
||||
@@ -103,7 +105,7 @@ const groups = computed<ContextMenuItem[][]>(() =>
|
||||
</slot>
|
||||
</DefineItemTemplate>
|
||||
|
||||
<ContextMenu.Portal :disabled="!portal">
|
||||
<ContextMenu.Portal v-bind="portalProps">
|
||||
<component :is="sub ? ContextMenu.SubContent : ContextMenu.Content" :class="props.class" v-bind="contentProps">
|
||||
<ContextMenu.Group v-for="(group, groupIndex) in groups" :key="`group-${groupIndex}`" :class="ui.group({ class: uiOverride?.group })">
|
||||
<template v-for="(item, index) in group" :key="`group-${groupIndex}-${index}`">
|
||||
|
||||
@@ -36,7 +36,7 @@ export interface DrawerProps extends Pick<DrawerRootProps, 'activeSnapPoint' | '
|
||||
* Render the drawer in a portal.
|
||||
* @defaultValue true
|
||||
*/
|
||||
portal?: boolean
|
||||
portal?: boolean | string | HTMLElement
|
||||
class?: any
|
||||
ui?: Drawer['slots']
|
||||
}
|
||||
@@ -60,6 +60,7 @@ import { useForwardPropsEmits } from 'reka-ui'
|
||||
import { DrawerRoot, DrawerTrigger, DrawerPortal, DrawerOverlay, DrawerContent, DrawerTitle, DrawerDescription, DrawerHandle } from 'vaul-vue'
|
||||
import { reactivePick } from '@vueuse/core'
|
||||
import { useAppConfig } from '#imports'
|
||||
import { usePortal } from '../composables/usePortal'
|
||||
import { tv } from '../utils/tv'
|
||||
|
||||
const props = withDefaults(defineProps<DrawerProps>(), {
|
||||
@@ -76,6 +77,7 @@ const slots = defineSlots<DrawerSlots>()
|
||||
const appConfig = useAppConfig() as Drawer['AppConfig']
|
||||
|
||||
const rootProps = useForwardPropsEmits(reactivePick(props, 'activeSnapPoint', 'closeThreshold', 'shouldScaleBackground', 'setBackgroundColorOnScale', 'scrollLockTimeout', 'fixed', 'dismissible', 'modal', 'open', 'defaultOpen', 'nested', 'direction', 'noBodyStyles', 'handleOnly', 'preventScrollRestoration', 'snapPoints'), emits)
|
||||
const portalProps = usePortal(toRef(() => props.portal))
|
||||
const contentProps = toRef(() => props.content)
|
||||
const contentEvents = {
|
||||
closeAutoFocus: (e: Event) => e.preventDefault()
|
||||
@@ -93,7 +95,7 @@ const ui = computed(() => tv({ extend: tv(theme), ...(appConfig.ui?.drawer || {}
|
||||
<slot />
|
||||
</DrawerTrigger>
|
||||
|
||||
<DrawerPortal :disabled="!portal">
|
||||
<DrawerPortal v-bind="portalProps">
|
||||
<DrawerOverlay v-if="overlay" :class="ui.overlay({ class: props.ui?.overlay })" />
|
||||
|
||||
<DrawerContent :class="ui.content({ class: [!slots.default && props.class, props.ui?.content] })" v-bind="contentProps" v-on="contentEvents">
|
||||
|
||||
@@ -74,7 +74,7 @@ export interface DropdownMenuProps<T extends ArrayOrNested<DropdownMenuItem> = A
|
||||
* Render the menu in a portal.
|
||||
* @defaultValue true
|
||||
*/
|
||||
portal?: boolean
|
||||
portal?: boolean | string | HTMLElement
|
||||
/**
|
||||
* The key used to get the label from the item.
|
||||
* @defaultValue 'label'
|
||||
|
||||
@@ -10,7 +10,7 @@ type DropdownMenu = ComponentConfig<typeof theme, AppConfig, 'dropdownMenu'>
|
||||
|
||||
interface DropdownMenuContentProps<T extends ArrayOrNested<DropdownMenuItem>> extends Omit<RekaDropdownMenuContentProps, 'as' | 'asChild' | 'forceMount'> {
|
||||
items?: T
|
||||
portal?: boolean
|
||||
portal?: boolean | string | HTMLElement
|
||||
sub?: boolean
|
||||
labelKey: keyof NestedItem<T>
|
||||
/**
|
||||
@@ -39,12 +39,13 @@ type DropdownMenuContentSlots<T extends ArrayOrNested<DropdownMenuItem>> = Omit<
|
||||
</script>
|
||||
|
||||
<script setup lang="ts" generic="T extends ArrayOrNested<DropdownMenuItem>">
|
||||
import { computed } from 'vue'
|
||||
import { computed, toRef } from 'vue'
|
||||
import { DropdownMenu } from 'reka-ui/namespaced'
|
||||
import { useForwardPropsEmits } from 'reka-ui'
|
||||
import { reactiveOmit, createReusableTemplate } from '@vueuse/core'
|
||||
import { useAppConfig } from '#imports'
|
||||
import { useLocale } from '../composables/useLocale'
|
||||
import { usePortal } from '../composables/usePortal'
|
||||
import { omit, get, isArrayOfArray } from '../utils'
|
||||
import { pickLinkProps } from '../utils/link'
|
||||
import ULinkBase from './LinkBase.vue'
|
||||
@@ -62,6 +63,7 @@ const slots = defineSlots<DropdownMenuContentSlots<T>>()
|
||||
const { dir } = useLocale()
|
||||
const appConfig = useAppConfig()
|
||||
|
||||
const portalProps = usePortal(toRef(() => props.portal))
|
||||
const contentProps = useForwardPropsEmits(reactiveOmit(props, 'sub', 'items', 'portal', 'labelKey', 'checkedIcon', 'loadingIcon', 'externalIcon', 'class', 'ui', 'uiOverride'), emits)
|
||||
const proxySlots = omit(slots, ['default'])
|
||||
|
||||
@@ -109,7 +111,7 @@ const groups = computed<DropdownMenuItem[][]>(() =>
|
||||
</slot>
|
||||
</DefineItemTemplate>
|
||||
|
||||
<DropdownMenu.Portal :disabled="!portal">
|
||||
<DropdownMenu.Portal v-bind="portalProps">
|
||||
<component :is="sub ? DropdownMenu.SubContent : DropdownMenu.Content" :class="props.class" v-bind="contentProps">
|
||||
<DropdownMenu.Group v-for="(group, groupIndex) in groups" :key="`group-${groupIndex}`" :class="ui.group({ class: uiOverride?.group })">
|
||||
<template v-for="(item, index) in group" :key="`group-${groupIndex}-${index}`">
|
||||
|
||||
@@ -86,7 +86,7 @@ export interface InputMenuProps<T extends ArrayOrNested<InputMenuItem> = ArrayOr
|
||||
* Render the menu in a portal.
|
||||
* @defaultValue true
|
||||
*/
|
||||
portal?: boolean
|
||||
portal?: boolean | string | HTMLElement
|
||||
/**
|
||||
* When `items` is an array of objects, select the field to use as the value instead of the object itself.
|
||||
* @defaultValue undefined
|
||||
@@ -177,6 +177,7 @@ import { useButtonGroup } from '../composables/useButtonGroup'
|
||||
import { useComponentIcons } from '../composables/useComponentIcons'
|
||||
import { useFormField } from '../composables/useFormField'
|
||||
import { useLocale } from '../composables/useLocale'
|
||||
import { usePortal } from '../composables/usePortal'
|
||||
import { compare, get, isArrayOfArray } from '../utils'
|
||||
import { tv } from '../utils/tv'
|
||||
import UIcon from './Icon.vue'
|
||||
@@ -203,6 +204,7 @@ const appConfig = useAppConfig() as InputMenu['AppConfig']
|
||||
const { contains } = useFilter({ sensitivity: 'base' })
|
||||
|
||||
const rootProps = useForwardPropsEmits(reactivePick(props, 'as', 'modelValue', 'defaultValue', 'open', 'defaultOpen', 'required', 'multiple', 'resetSearchTermOnBlur', 'resetSearchTermOnSelect', 'highlightOnHover', 'ignoreFilter'), emits)
|
||||
const portalProps = usePortal(toRef(() => props.portal))
|
||||
const contentProps = toRef(() => defu(props.content, { side: 'bottom', sideOffset: 8, collisionPadding: 8, position: 'popper' }) as ComboboxContentProps)
|
||||
const arrowProps = toRef(() => props.arrow as ComboboxArrowProps)
|
||||
|
||||
@@ -474,7 +476,7 @@ defineExpose({
|
||||
</ComboboxTrigger>
|
||||
</ComboboxAnchor>
|
||||
|
||||
<ComboboxPortal :disabled="!portal">
|
||||
<ComboboxPortal v-bind="portalProps">
|
||||
<ComboboxContent :class="ui.content({ class: props.ui?.content })" v-bind="contentProps">
|
||||
<ComboboxEmpty :class="ui.empty({ class: props.ui?.empty })">
|
||||
<slot name="empty" :search-term="searchTerm">
|
||||
|
||||
@@ -31,7 +31,7 @@ export interface ModalProps extends DialogRootProps {
|
||||
* Render the modal in a portal.
|
||||
* @defaultValue true
|
||||
*/
|
||||
portal?: boolean
|
||||
portal?: boolean | string | HTMLElement
|
||||
/**
|
||||
* Display a close button to dismiss the modal.
|
||||
* `{ size: 'md', color: 'neutral', variant: 'ghost' }`{lang="ts-type"}
|
||||
@@ -75,6 +75,7 @@ import { DialogRoot, DialogTrigger, DialogPortal, DialogOverlay, DialogContent,
|
||||
import { reactivePick } from '@vueuse/core'
|
||||
import { useAppConfig } from '#imports'
|
||||
import { useLocale } from '../composables/useLocale'
|
||||
import { usePortal } from '../composables/usePortal'
|
||||
import { tv } from '../utils/tv'
|
||||
import UButton from './Button.vue'
|
||||
|
||||
@@ -93,6 +94,7 @@ const { t } = useLocale()
|
||||
const appConfig = useAppConfig() as Modal['AppConfig']
|
||||
|
||||
const rootProps = useForwardPropsEmits(reactivePick(props, 'open', 'defaultOpen', 'modal'), emits)
|
||||
const portalProps = usePortal(toRef(() => props.portal))
|
||||
const contentProps = toRef(() => props.content)
|
||||
const contentEvents = computed(() => {
|
||||
const events = {
|
||||
@@ -123,7 +125,7 @@ const ui = computed(() => tv({ extend: tv(theme), ...(appConfig.ui?.modal || {})
|
||||
<slot :open="open" />
|
||||
</DialogTrigger>
|
||||
|
||||
<DialogPortal :disabled="!portal">
|
||||
<DialogPortal v-bind="portalProps">
|
||||
<DialogOverlay v-if="overlay" :class="ui.overlay({ class: props.ui?.overlay })" />
|
||||
|
||||
<DialogContent :class="ui.content({ class: [!slots.default && props.class, props.ui?.content] })" v-bind="contentProps" @after-leave="emits('after:leave')" v-on="contentEvents">
|
||||
|
||||
@@ -26,7 +26,7 @@ export interface PopoverProps extends PopoverRootProps, Pick<HoverCardRootProps,
|
||||
* Render the popover in a portal.
|
||||
* @defaultValue true
|
||||
*/
|
||||
portal?: boolean
|
||||
portal?: boolean | string | HTMLElement
|
||||
/**
|
||||
* When `false`, the popover will not close when clicking outside or pressing escape.
|
||||
* @defaultValue true
|
||||
@@ -51,6 +51,7 @@ import { useForwardPropsEmits } from 'reka-ui'
|
||||
import { Popover, HoverCard } from 'reka-ui/namespaced'
|
||||
import { reactivePick } from '@vueuse/core'
|
||||
import { useAppConfig } from '#imports'
|
||||
import { usePortal } from '../composables/usePortal'
|
||||
import { tv } from '../utils/tv'
|
||||
|
||||
const props = withDefaults(defineProps<PopoverProps>(), {
|
||||
@@ -67,6 +68,7 @@ const appConfig = useAppConfig() as Popover['AppConfig']
|
||||
|
||||
const pick = props.mode === 'hover' ? reactivePick(props, 'defaultOpen', 'open', 'openDelay', 'closeDelay') : reactivePick(props, 'defaultOpen', 'open', 'modal')
|
||||
const rootProps = useForwardPropsEmits(pick, emits)
|
||||
const portalProps = usePortal(toRef(() => props.portal))
|
||||
const contentProps = toRef(() => defu(props.content, { side: 'bottom', sideOffset: 8, collisionPadding: 8 }) as PopoverContentProps)
|
||||
const contentEvents = computed(() => {
|
||||
if (!props.dismissible) {
|
||||
@@ -95,7 +97,7 @@ const Component = computed(() => props.mode === 'hover' ? HoverCard : Popover)
|
||||
<slot :open="open" />
|
||||
</Component.Trigger>
|
||||
|
||||
<Component.Portal :disabled="!portal">
|
||||
<Component.Portal v-bind="portalProps">
|
||||
<Component.Content v-bind="contentProps" :class="ui.content({ class: [!slots.default && props.class, props.ui?.content] })" v-on="contentEvents">
|
||||
<slot name="content" />
|
||||
|
||||
|
||||
@@ -70,7 +70,7 @@ export interface SelectProps<T extends ArrayOrNested<SelectItem> = ArrayOrNested
|
||||
* Render the menu in a portal.
|
||||
* @defaultValue true
|
||||
*/
|
||||
portal?: boolean
|
||||
portal?: boolean | string | HTMLElement
|
||||
/**
|
||||
* When `items` is an array of objects, select the field to use as the value.
|
||||
* @defaultValue 'value'
|
||||
@@ -138,6 +138,7 @@ import { useAppConfig } from '#imports'
|
||||
import { useButtonGroup } from '../composables/useButtonGroup'
|
||||
import { useComponentIcons } from '../composables/useComponentIcons'
|
||||
import { useFormField } from '../composables/useFormField'
|
||||
import { usePortal } from '../composables/usePortal'
|
||||
import { compare, get, isArrayOfArray } from '../utils'
|
||||
import { tv } from '../utils/tv'
|
||||
import UIcon from './Icon.vue'
|
||||
@@ -157,6 +158,7 @@ const slots = defineSlots<SelectSlots<T, VK, M>>()
|
||||
const appConfig = useAppConfig() as Select['AppConfig']
|
||||
|
||||
const rootProps = useForwardPropsEmits(reactivePick(props, 'open', 'defaultOpen', 'disabled', 'autocomplete', 'required', 'multiple'), emits)
|
||||
const portalProps = usePortal(toRef(() => props.portal))
|
||||
const contentProps = toRef(() => defu(props.content, { side: 'bottom', sideOffset: 8, collisionPadding: 8, position: 'popper' }) as SelectContentProps)
|
||||
const arrowProps = toRef(() => props.arrow as SelectArrowProps)
|
||||
|
||||
@@ -260,7 +262,7 @@ function isSelectItem(item: SelectItem): item is SelectItemBase {
|
||||
</span>
|
||||
</SelectTrigger>
|
||||
|
||||
<SelectPortal :disabled="!portal">
|
||||
<SelectPortal v-bind="portalProps">
|
||||
<SelectContent :class="ui.content({ class: props.ui?.content })" v-bind="contentProps">
|
||||
<SelectViewport :class="ui.viewport({ class: props.ui?.viewport })">
|
||||
<SelectGroup v-for="(group, groupIndex) in groups" :key="`group-${groupIndex}`" :class="ui.group({ class: props.ui?.group })">
|
||||
|
||||
@@ -78,7 +78,7 @@ export interface SelectMenuProps<T extends ArrayOrNested<SelectMenuItem> = Array
|
||||
* Render the menu in a portal.
|
||||
* @defaultValue true
|
||||
*/
|
||||
portal?: boolean
|
||||
portal?: boolean | string | HTMLElement
|
||||
/**
|
||||
* When `items` is an array of objects, select the field to use as the value instead of the object itself.
|
||||
* @defaultValue undefined
|
||||
@@ -170,6 +170,7 @@ import { useButtonGroup } from '../composables/useButtonGroup'
|
||||
import { useComponentIcons } from '../composables/useComponentIcons'
|
||||
import { useFormField } from '../composables/useFormField'
|
||||
import { useLocale } from '../composables/useLocale'
|
||||
import { usePortal } from '../composables/usePortal'
|
||||
import { compare, get, isArrayOfArray } from '../utils'
|
||||
import { tv } from '../utils/tv'
|
||||
import UIcon from './Icon.vue'
|
||||
@@ -196,6 +197,7 @@ const appConfig = useAppConfig() as SelectMenu['AppConfig']
|
||||
const { contains } = useFilter({ sensitivity: 'base' })
|
||||
|
||||
const rootProps = useForwardPropsEmits(reactivePick(props, 'modelValue', 'defaultValue', 'open', 'defaultOpen', 'required', 'multiple', 'resetSearchTermOnBlur', 'resetSearchTermOnSelect', 'highlightOnHover'), emits)
|
||||
const portalProps = usePortal(toRef(() => props.portal))
|
||||
const contentProps = toRef(() => defu(props.content, { side: 'bottom', sideOffset: 8, collisionPadding: 8, position: 'popper' }) as ComboboxContentProps)
|
||||
const arrowProps = toRef(() => props.arrow as ComboboxArrowProps)
|
||||
const searchInputProps = toRef(() => defu(props.searchInput, { placeholder: t('selectMenu.search'), variant: 'none' }) as InputProps)
|
||||
@@ -396,7 +398,7 @@ function isSelectItem(item: SelectMenuItem): item is _SelectMenuItem {
|
||||
</ComboboxTrigger>
|
||||
</ComboboxAnchor>
|
||||
|
||||
<ComboboxPortal :disabled="!portal">
|
||||
<ComboboxPortal v-bind="portalProps">
|
||||
<ComboboxContent :class="ui.content({ class: props.ui?.content })" v-bind="contentProps">
|
||||
<FocusScope trapped :class="ui.focusScope({ class: props.ui?.focusScope })">
|
||||
<ComboboxInput v-if="!!searchInput" v-model="searchTerm" :display-value="() => searchTerm" as-child>
|
||||
|
||||
@@ -31,7 +31,7 @@ export interface SlideoverProps extends DialogRootProps {
|
||||
* Render the slideover in a portal.
|
||||
* @defaultValue true
|
||||
*/
|
||||
portal?: boolean
|
||||
portal?: boolean | string | HTMLElement
|
||||
/**
|
||||
* Display a close button to dismiss the slideover.
|
||||
* `{ size: 'md', color: 'neutral', variant: 'ghost' }`{lang="ts-type"}
|
||||
@@ -75,6 +75,7 @@ import { DialogRoot, DialogTrigger, DialogPortal, DialogOverlay, DialogContent,
|
||||
import { reactivePick } from '@vueuse/core'
|
||||
import { useAppConfig } from '#imports'
|
||||
import { useLocale } from '../composables/useLocale'
|
||||
import { usePortal } from '../composables/usePortal'
|
||||
import { tv } from '../utils/tv'
|
||||
import UButton from './Button.vue'
|
||||
|
||||
@@ -94,6 +95,7 @@ const { t } = useLocale()
|
||||
const appConfig = useAppConfig() as Slideover['AppConfig']
|
||||
|
||||
const rootProps = useForwardPropsEmits(reactivePick(props, 'open', 'defaultOpen', 'modal'), emits)
|
||||
const portalProps = usePortal(toRef(() => props.portal))
|
||||
const contentProps = toRef(() => props.content)
|
||||
const contentEvents = computed(() => {
|
||||
const events = {
|
||||
@@ -124,7 +126,7 @@ const ui = computed(() => tv({ extend: tv(theme), ...(appConfig.ui?.slideover ||
|
||||
<slot :open="open" />
|
||||
</DialogTrigger>
|
||||
|
||||
<DialogPortal :disabled="!portal">
|
||||
<DialogPortal v-bind="portalProps">
|
||||
<DialogOverlay v-if="overlay" :class="ui.overlay({ class: props.ui?.overlay })" />
|
||||
|
||||
<DialogContent :data-side="side" :class="ui.content({ class: [!slots.default && props.class, props.ui?.content] })" v-bind="contentProps" @after-leave="emits('after:leave')" v-on="contentEvents">
|
||||
|
||||
@@ -21,7 +21,7 @@ export interface ToasterProps extends Omit<ToastProviderProps, 'swipeDirection'>
|
||||
* Render the toaster in a portal.
|
||||
* @defaultValue true
|
||||
*/
|
||||
portal?: boolean
|
||||
portal?: boolean | string | HTMLElement
|
||||
class?: any
|
||||
ui?: Toaster['slots']
|
||||
}
|
||||
@@ -36,11 +36,12 @@ export default {
|
||||
</script>
|
||||
|
||||
<script setup lang="ts">
|
||||
import { ref, computed } from 'vue'
|
||||
import { ref, computed, toRef } from 'vue'
|
||||
import { ToastProvider, ToastViewport, ToastPortal, useForwardProps } from 'reka-ui'
|
||||
import { reactivePick } from '@vueuse/core'
|
||||
import { useAppConfig } from '#imports'
|
||||
import { useToast } from '../composables/useToast'
|
||||
import { usePortal } from '../composables/usePortal'
|
||||
import { omit } from '../utils'
|
||||
import { tv } from '../utils/tv'
|
||||
import UToast from './Toast.vue'
|
||||
@@ -56,6 +57,7 @@ const { toasts, remove } = useToast()
|
||||
const appConfig = useAppConfig() as Toaster['AppConfig']
|
||||
|
||||
const providerProps = useForwardProps(reactivePick(props, 'duration', 'label', 'swipeThreshold'))
|
||||
const portalProps = usePortal(toRef(() => props.portal))
|
||||
|
||||
const swipeDirection = computed(() => {
|
||||
switch (props.position) {
|
||||
@@ -126,7 +128,7 @@ function getOffset(index: number) {
|
||||
@click="toast.onClick && toast.onClick(toast)"
|
||||
/>
|
||||
|
||||
<ToastPortal :disabled="!portal">
|
||||
<ToastPortal v-bind="portalProps">
|
||||
<ToastViewport
|
||||
:data-expanded="expanded"
|
||||
:class="ui.viewport({ class: [props.class, props.ui?.viewport] })"
|
||||
|
||||
@@ -26,7 +26,7 @@ export interface TooltipProps extends TooltipRootProps {
|
||||
* Render the tooltip in a portal.
|
||||
* @defaultValue true
|
||||
*/
|
||||
portal?: boolean
|
||||
portal?: boolean | string | HTMLElement
|
||||
class?: any
|
||||
ui?: Tooltip['slots']
|
||||
}
|
||||
@@ -45,6 +45,7 @@ import { defu } from 'defu'
|
||||
import { TooltipRoot, TooltipTrigger, TooltipPortal, TooltipContent, TooltipArrow, useForwardPropsEmits } from 'reka-ui'
|
||||
import { reactivePick } from '@vueuse/core'
|
||||
import { useAppConfig } from '#imports'
|
||||
import { usePortal } from '../composables/usePortal'
|
||||
import { tv } from '../utils/tv'
|
||||
import UKbd from './Kbd.vue'
|
||||
|
||||
@@ -57,6 +58,7 @@ const slots = defineSlots<TooltipSlots>()
|
||||
const appConfig = useAppConfig() as Tooltip['AppConfig']
|
||||
|
||||
const rootProps = useForwardPropsEmits(reactivePick(props, 'defaultOpen', 'open', 'delayDuration', 'disableHoverableContent', 'disableClosingTrigger', 'disabled', 'ignoreNonKeyboardFocus'), emits)
|
||||
const portalProps = usePortal(toRef(() => props.portal))
|
||||
const contentProps = toRef(() => defu(props.content, { side: 'bottom', sideOffset: 8, collisionPadding: 8 }) as TooltipContentProps)
|
||||
const arrowProps = toRef(() => props.arrow as TooltipArrowProps)
|
||||
|
||||
@@ -72,7 +74,7 @@ const ui = computed(() => tv({ extend: tv(theme), ...(appConfig.ui?.tooltip || {
|
||||
<slot :open="open" />
|
||||
</TooltipTrigger>
|
||||
|
||||
<TooltipPortal :disabled="!portal">
|
||||
<TooltipPortal v-bind="portalProps">
|
||||
<TooltipContent v-bind="contentProps" :class="ui.content({ class: [!slots.default && props.class, props.ui?.content] })">
|
||||
<slot name="content">
|
||||
<span v-if="text" :class="ui.text({ class: props.ui?.text })">{{ text }}</span>
|
||||
|
||||
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