Skip to content

Commit 29fa462

Browse files
feat(App): add global portal prop (#3688)
Co-authored-by: Benjamin Canac <[email protected]>
1 parent 7a35bae commit 29fa462

15 files changed

+82
-28
lines changed

src/runtime/components/App.vue

+9-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ export interface AppProps<T extends Messages = Messages> extends Omit<ConfigProv
66
tooltip?: TooltipProviderProps
77
toaster?: ToasterProps | null
88
locale?: Locale<T>
9+
portal?: string | HTMLElement
910
}
1011
1112
export interface AppSlots {
@@ -22,10 +23,14 @@ import { toRef, useId, provide } from 'vue'
2223
import { ConfigProvider, TooltipProvider, useForwardProps } from 'reka-ui'
2324
import { reactivePick } from '@vueuse/core'
2425
import { localeContextInjectionKey } from '../composables/useLocale'
26+
import { portalTargetInjectionKey } from '../composables/usePortal'
2527
import UToaster from './Toaster.vue'
2628
import UOverlayProvider from './OverlayProvider.vue'
2729
28-
const props = defineProps<AppProps<T>>()
30+
const props = withDefaults(defineProps<AppProps<T>>(), {
31+
portal: 'body'
32+
})
33+
2934
defineSlots<AppSlots>()
3035
3136
const configProviderProps = useForwardProps(reactivePick(props, 'scrollBody'))
@@ -34,6 +39,9 @@ const toasterProps = toRef(() => props.toaster)
3439
3540
const locale = toRef(() => props.locale)
3641
provide(localeContextInjectionKey, locale)
42+
43+
const portal = toRef(() => props.portal)
44+
provide(portalTargetInjectionKey, portal)
3745
</script>
3846

3947
<template>

src/runtime/components/ContextMenu.vue

+1-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ export interface ContextMenuProps<T extends ArrayOrNested<ContextMenuItem> = Arr
6666
* Render the menu in a portal.
6767
* @defaultValue true
6868
*/
69-
portal?: boolean
69+
portal?: boolean | string | HTMLElement
7070
/**
7171
* The key used to get the label from the item.
7272
* @defaultValue 'label'

src/runtime/components/ContextMenuContent.vue

+5-3
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ type ContextMenu = ComponentConfig<typeof theme, AppConfig, 'contextMenu'>
99
1010
interface ContextMenuContentProps<T extends ArrayOrNested<ContextMenuItem>> extends Omit<RekaContextMenuContentProps, 'as' | 'asChild' | 'forceMount'> {
1111
items?: T
12-
portal?: boolean
12+
portal?: boolean | string | HTMLElement
1313
sub?: boolean
1414
labelKey: keyof NestedItem<T>
1515
/**
@@ -33,12 +33,13 @@ interface ContextMenuContentEmits extends RekaContextMenuContentEmits {}
3333
</script>
3434

3535
<script setup lang="ts" generic="T extends ArrayOrNested<ContextMenuItem>">
36-
import { computed } from 'vue'
36+
import { computed, toRef } from 'vue'
3737
import { ContextMenu } from 'reka-ui/namespaced'
3838
import { useForwardPropsEmits } from 'reka-ui'
3939
import { reactiveOmit, createReusableTemplate } from '@vueuse/core'
4040
import { useAppConfig } from '#imports'
4141
import { useLocale } from '../composables/useLocale'
42+
import { usePortal } from '../composables/usePortal'
4243
import { omit, get, isArrayOfArray } from '../utils'
4344
import { pickLinkProps } from '../utils/link'
4445
import ULinkBase from './LinkBase.vue'
@@ -56,6 +57,7 @@ const slots = defineSlots<ContextMenuSlots<T>>()
5657
const { dir } = useLocale()
5758
const appConfig = useAppConfig()
5859
60+
const portalProps = usePortal(toRef(() => props.portal))
5961
const contentProps = useForwardPropsEmits(reactiveOmit(props, 'sub', 'items', 'portal', 'labelKey', 'checkedIcon', 'loadingIcon', 'externalIcon', 'class', 'ui', 'uiOverride'), emits)
6062
const proxySlots = omit(slots, ['default'])
6163
@@ -103,7 +105,7 @@ const groups = computed<ContextMenuItem[][]>(() =>
103105
</slot>
104106
</DefineItemTemplate>
105107

106-
<ContextMenu.Portal :disabled="!portal">
108+
<ContextMenu.Portal v-bind="portalProps">
107109
<component :is="sub ? ContextMenu.SubContent : ContextMenu.Content" :class="props.class" v-bind="contentProps">
108110
<ContextMenu.Group v-for="(group, groupIndex) in groups" :key="`group-${groupIndex}`" :class="ui.group({ class: uiOverride?.group })">
109111
<template v-for="(item, index) in group" :key="`group-${groupIndex}-${index}`">

src/runtime/components/Drawer.vue

+4-2
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export interface DrawerProps extends Pick<DrawerRootProps, 'activeSnapPoint' | '
3636
* Render the drawer in a portal.
3737
* @defaultValue true
3838
*/
39-
portal?: boolean
39+
portal?: boolean | string | HTMLElement
4040
class?: any
4141
ui?: Drawer['slots']
4242
}
@@ -60,6 +60,7 @@ import { useForwardPropsEmits } from 'reka-ui'
6060
import { DrawerRoot, DrawerTrigger, DrawerPortal, DrawerOverlay, DrawerContent, DrawerTitle, DrawerDescription, DrawerHandle } from 'vaul-vue'
6161
import { reactivePick } from '@vueuse/core'
6262
import { useAppConfig } from '#imports'
63+
import { usePortal } from '../composables/usePortal'
6364
import { tv } from '../utils/tv'
6465
6566
const props = withDefaults(defineProps<DrawerProps>(), {
@@ -76,6 +77,7 @@ const slots = defineSlots<DrawerSlots>()
7677
const appConfig = useAppConfig() as Drawer['AppConfig']
7778
7879
const rootProps = useForwardPropsEmits(reactivePick(props, 'activeSnapPoint', 'closeThreshold', 'shouldScaleBackground', 'setBackgroundColorOnScale', 'scrollLockTimeout', 'fixed', 'dismissible', 'modal', 'open', 'defaultOpen', 'nested', 'direction', 'noBodyStyles', 'handleOnly', 'preventScrollRestoration', 'snapPoints'), emits)
80+
const portalProps = usePortal(toRef(() => props.portal))
7981
const contentProps = toRef(() => props.content)
8082
const contentEvents = {
8183
closeAutoFocus: (e: Event) => e.preventDefault()
@@ -93,7 +95,7 @@ const ui = computed(() => tv({ extend: tv(theme), ...(appConfig.ui?.drawer || {}
9395
<slot />
9496
</DrawerTrigger>
9597

96-
<DrawerPortal :disabled="!portal">
98+
<DrawerPortal v-bind="portalProps">
9799
<DrawerOverlay v-if="overlay" :class="ui.overlay({ class: props.ui?.overlay })" />
98100

99101
<DrawerContent :class="ui.content({ class: [!slots.default && props.class, props.ui?.content] })" v-bind="contentProps" v-on="contentEvents">

src/runtime/components/DropdownMenu.vue

+1-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ export interface DropdownMenuProps<T extends ArrayOrNested<DropdownMenuItem> = A
7474
* Render the menu in a portal.
7575
* @defaultValue true
7676
*/
77-
portal?: boolean
77+
portal?: boolean | string | HTMLElement
7878
/**
7979
* The key used to get the label from the item.
8080
* @defaultValue 'label'

src/runtime/components/DropdownMenuContent.vue

+5-3
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ type DropdownMenu = ComponentConfig<typeof theme, AppConfig, 'dropdownMenu'>
1010

1111
interface DropdownMenuContentProps<T extends ArrayOrNested<DropdownMenuItem>> extends Omit<RekaDropdownMenuContentProps, 'as' | 'asChild' | 'forceMount'> {
1212
items?: T
13-
portal?: boolean
13+
portal?: boolean | string | HTMLElement
1414
sub?: boolean
1515
labelKey: keyof NestedItem<T>
1616
/**
@@ -39,12 +39,13 @@ type DropdownMenuContentSlots<T extends ArrayOrNested<DropdownMenuItem>> = Omit<
3939
</script>
4040

4141
<script setup lang="ts" generic="T extends ArrayOrNested<DropdownMenuItem>">
42-
import { computed } from 'vue'
42+
import { computed, toRef } from 'vue'
4343
import { DropdownMenu } from 'reka-ui/namespaced'
4444
import { useForwardPropsEmits } from 'reka-ui'
4545
import { reactiveOmit, createReusableTemplate } from '@vueuse/core'
4646
import { useAppConfig } from '#imports'
4747
import { useLocale } from '../composables/useLocale'
48+
import { usePortal } from '../composables/usePortal'
4849
import { omit, get, isArrayOfArray } from '../utils'
4950
import { pickLinkProps } from '../utils/link'
5051
import ULinkBase from './LinkBase.vue'
@@ -62,6 +63,7 @@ const slots = defineSlots<DropdownMenuContentSlots<T>>()
6263
const { dir } = useLocale()
6364
const appConfig = useAppConfig()
6465

66+
const portalProps = usePortal(toRef(() => props.portal))
6567
const contentProps = useForwardPropsEmits(reactiveOmit(props, 'sub', 'items', 'portal', 'labelKey', 'checkedIcon', 'loadingIcon', 'externalIcon', 'class', 'ui', 'uiOverride'), emits)
6668
const proxySlots = omit(slots, ['default'])
6769

@@ -109,7 +111,7 @@ const groups = computed<DropdownMenuItem[][]>(() =>
109111
</slot>
110112
</DefineItemTemplate>
111113

112-
<DropdownMenu.Portal :disabled="!portal">
114+
<DropdownMenu.Portal v-bind="portalProps">
113115
<component :is="sub ? DropdownMenu.SubContent : DropdownMenu.Content" :class="props.class" v-bind="contentProps">
114116
<DropdownMenu.Group v-for="(group, groupIndex) in groups" :key="`group-${groupIndex}`" :class="ui.group({ class: uiOverride?.group })">
115117
<template v-for="(item, index) in group" :key="`group-${groupIndex}-${index}`">

src/runtime/components/InputMenu.vue

+4-2
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ export interface InputMenuProps<T extends ArrayOrNested<InputMenuItem> = ArrayOr
8686
* Render the menu in a portal.
8787
* @defaultValue true
8888
*/
89-
portal?: boolean
89+
portal?: boolean | string | HTMLElement
9090
/**
9191
* When `items` is an array of objects, select the field to use as the value instead of the object itself.
9292
* @defaultValue undefined
@@ -177,6 +177,7 @@ import { useButtonGroup } from '../composables/useButtonGroup'
177177
import { useComponentIcons } from '../composables/useComponentIcons'
178178
import { useFormField } from '../composables/useFormField'
179179
import { useLocale } from '../composables/useLocale'
180+
import { usePortal } from '../composables/usePortal'
180181
import { compare, get, isArrayOfArray } from '../utils'
181182
import { tv } from '../utils/tv'
182183
import UIcon from './Icon.vue'
@@ -203,6 +204,7 @@ const appConfig = useAppConfig() as InputMenu['AppConfig']
203204
const { contains } = useFilter({ sensitivity: 'base' })
204205
205206
const rootProps = useForwardPropsEmits(reactivePick(props, 'as', 'modelValue', 'defaultValue', 'open', 'defaultOpen', 'required', 'multiple', 'resetSearchTermOnBlur', 'resetSearchTermOnSelect', 'highlightOnHover', 'ignoreFilter'), emits)
207+
const portalProps = usePortal(toRef(() => props.portal))
206208
const contentProps = toRef(() => defu(props.content, { side: 'bottom', sideOffset: 8, collisionPadding: 8, position: 'popper' }) as ComboboxContentProps)
207209
const arrowProps = toRef(() => props.arrow as ComboboxArrowProps)
208210
@@ -474,7 +476,7 @@ defineExpose({
474476
</ComboboxTrigger>
475477
</ComboboxAnchor>
476478

477-
<ComboboxPortal :disabled="!portal">
479+
<ComboboxPortal v-bind="portalProps">
478480
<ComboboxContent :class="ui.content({ class: props.ui?.content })" v-bind="contentProps">
479481
<ComboboxEmpty :class="ui.empty({ class: props.ui?.empty })">
480482
<slot name="empty" :search-term="searchTerm">

src/runtime/components/Modal.vue

+4-2
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ export interface ModalProps extends DialogRootProps {
3131
* Render the modal in a portal.
3232
* @defaultValue true
3333
*/
34-
portal?: boolean
34+
portal?: boolean | string | HTMLElement
3535
/**
3636
* Display a close button to dismiss the modal.
3737
* `{ size: 'md', color: 'neutral', variant: 'ghost' }`{lang="ts-type"}
@@ -75,6 +75,7 @@ import { DialogRoot, DialogTrigger, DialogPortal, DialogOverlay, DialogContent,
7575
import { reactivePick } from '@vueuse/core'
7676
import { useAppConfig } from '#imports'
7777
import { useLocale } from '../composables/useLocale'
78+
import { usePortal } from '../composables/usePortal'
7879
import { tv } from '../utils/tv'
7980
import UButton from './Button.vue'
8081
@@ -93,6 +94,7 @@ const { t } = useLocale()
9394
const appConfig = useAppConfig() as Modal['AppConfig']
9495
9596
const rootProps = useForwardPropsEmits(reactivePick(props, 'open', 'defaultOpen', 'modal'), emits)
97+
const portalProps = usePortal(toRef(() => props.portal))
9698
const contentProps = toRef(() => props.content)
9799
const contentEvents = computed(() => {
98100
const events = {
@@ -123,7 +125,7 @@ const ui = computed(() => tv({ extend: tv(theme), ...(appConfig.ui?.modal || {})
123125
<slot :open="open" />
124126
</DialogTrigger>
125127

126-
<DialogPortal :disabled="!portal">
128+
<DialogPortal v-bind="portalProps">
127129
<DialogOverlay v-if="overlay" :class="ui.overlay({ class: props.ui?.overlay })" />
128130

129131
<DialogContent :class="ui.content({ class: [!slots.default && props.class, props.ui?.content] })" v-bind="contentProps" @after-leave="emits('after:leave')" v-on="contentEvents">

src/runtime/components/Popover.vue

+4-2
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export interface PopoverProps extends PopoverRootProps, Pick<HoverCardRootProps,
2626
* Render the popover in a portal.
2727
* @defaultValue true
2828
*/
29-
portal?: boolean
29+
portal?: boolean | string | HTMLElement
3030
/**
3131
* When `false`, the popover will not close when clicking outside or pressing escape.
3232
* @defaultValue true
@@ -51,6 +51,7 @@ import { useForwardPropsEmits } from 'reka-ui'
5151
import { Popover, HoverCard } from 'reka-ui/namespaced'
5252
import { reactivePick } from '@vueuse/core'
5353
import { useAppConfig } from '#imports'
54+
import { usePortal } from '../composables/usePortal'
5455
import { tv } from '../utils/tv'
5556
5657
const props = withDefaults(defineProps<PopoverProps>(), {
@@ -67,6 +68,7 @@ const appConfig = useAppConfig() as Popover['AppConfig']
6768
6869
const pick = props.mode === 'hover' ? reactivePick(props, 'defaultOpen', 'open', 'openDelay', 'closeDelay') : reactivePick(props, 'defaultOpen', 'open', 'modal')
6970
const rootProps = useForwardPropsEmits(pick, emits)
71+
const portalProps = usePortal(toRef(() => props.portal))
7072
const contentProps = toRef(() => defu(props.content, { side: 'bottom', sideOffset: 8, collisionPadding: 8 }) as PopoverContentProps)
7173
const contentEvents = computed(() => {
7274
if (!props.dismissible) {
@@ -95,7 +97,7 @@ const Component = computed(() => props.mode === 'hover' ? HoverCard : Popover)
9597
<slot :open="open" />
9698
</Component.Trigger>
9799

98-
<Component.Portal :disabled="!portal">
100+
<Component.Portal v-bind="portalProps">
99101
<Component.Content v-bind="contentProps" :class="ui.content({ class: [!slots.default && props.class, props.ui?.content] })" v-on="contentEvents">
100102
<slot name="content" />
101103

src/runtime/components/Select.vue

+4-2
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ export interface SelectProps<T extends ArrayOrNested<SelectItem> = ArrayOrNested
7070
* Render the menu in a portal.
7171
* @defaultValue true
7272
*/
73-
portal?: boolean
73+
portal?: boolean | string | HTMLElement
7474
/**
7575
* When `items` is an array of objects, select the field to use as the value.
7676
* @defaultValue 'value'
@@ -138,6 +138,7 @@ import { useAppConfig } from '#imports'
138138
import { useButtonGroup } from '../composables/useButtonGroup'
139139
import { useComponentIcons } from '../composables/useComponentIcons'
140140
import { useFormField } from '../composables/useFormField'
141+
import { usePortal } from '../composables/usePortal'
141142
import { compare, get, isArrayOfArray } from '../utils'
142143
import { tv } from '../utils/tv'
143144
import UIcon from './Icon.vue'
@@ -157,6 +158,7 @@ const slots = defineSlots<SelectSlots<T, VK, M>>()
157158
const appConfig = useAppConfig() as Select['AppConfig']
158159
159160
const rootProps = useForwardPropsEmits(reactivePick(props, 'open', 'defaultOpen', 'disabled', 'autocomplete', 'required', 'multiple'), emits)
161+
const portalProps = usePortal(toRef(() => props.portal))
160162
const contentProps = toRef(() => defu(props.content, { side: 'bottom', sideOffset: 8, collisionPadding: 8, position: 'popper' }) as SelectContentProps)
161163
const arrowProps = toRef(() => props.arrow as SelectArrowProps)
162164
@@ -260,7 +262,7 @@ function isSelectItem(item: SelectItem): item is SelectItemBase {
260262
</span>
261263
</SelectTrigger>
262264

263-
<SelectPortal :disabled="!portal">
265+
<SelectPortal v-bind="portalProps">
264266
<SelectContent :class="ui.content({ class: props.ui?.content })" v-bind="contentProps">
265267
<SelectViewport :class="ui.viewport({ class: props.ui?.viewport })">
266268
<SelectGroup v-for="(group, groupIndex) in groups" :key="`group-${groupIndex}`" :class="ui.group({ class: props.ui?.group })">

src/runtime/components/SelectMenu.vue

+4-2
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ export interface SelectMenuProps<T extends ArrayOrNested<SelectMenuItem> = Array
7878
* Render the menu in a portal.
7979
* @defaultValue true
8080
*/
81-
portal?: boolean
81+
portal?: boolean | string | HTMLElement
8282
/**
8383
* When `items` is an array of objects, select the field to use as the value instead of the object itself.
8484
* @defaultValue undefined
@@ -170,6 +170,7 @@ import { useButtonGroup } from '../composables/useButtonGroup'
170170
import { useComponentIcons } from '../composables/useComponentIcons'
171171
import { useFormField } from '../composables/useFormField'
172172
import { useLocale } from '../composables/useLocale'
173+
import { usePortal } from '../composables/usePortal'
173174
import { compare, get, isArrayOfArray } from '../utils'
174175
import { tv } from '../utils/tv'
175176
import UIcon from './Icon.vue'
@@ -196,6 +197,7 @@ const appConfig = useAppConfig() as SelectMenu['AppConfig']
196197
const { contains } = useFilter({ sensitivity: 'base' })
197198
198199
const rootProps = useForwardPropsEmits(reactivePick(props, 'modelValue', 'defaultValue', 'open', 'defaultOpen', 'required', 'multiple', 'resetSearchTermOnBlur', 'resetSearchTermOnSelect', 'highlightOnHover'), emits)
200+
const portalProps = usePortal(toRef(() => props.portal))
199201
const contentProps = toRef(() => defu(props.content, { side: 'bottom', sideOffset: 8, collisionPadding: 8, position: 'popper' }) as ComboboxContentProps)
200202
const arrowProps = toRef(() => props.arrow as ComboboxArrowProps)
201203
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 {
396398
</ComboboxTrigger>
397399
</ComboboxAnchor>
398400

399-
<ComboboxPortal :disabled="!portal">
401+
<ComboboxPortal v-bind="portalProps">
400402
<ComboboxContent :class="ui.content({ class: props.ui?.content })" v-bind="contentProps">
401403
<FocusScope trapped :class="ui.focusScope({ class: props.ui?.focusScope })">
402404
<ComboboxInput v-if="!!searchInput" v-model="searchTerm" :display-value="() => searchTerm" as-child>

src/runtime/components/Slideover.vue

+4-2
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ export interface SlideoverProps extends DialogRootProps {
3131
* Render the slideover in a portal.
3232
* @defaultValue true
3333
*/
34-
portal?: boolean
34+
portal?: boolean | string | HTMLElement
3535
/**
3636
* Display a close button to dismiss the slideover.
3737
* `{ size: 'md', color: 'neutral', variant: 'ghost' }`{lang="ts-type"}
@@ -75,6 +75,7 @@ import { DialogRoot, DialogTrigger, DialogPortal, DialogOverlay, DialogContent,
7575
import { reactivePick } from '@vueuse/core'
7676
import { useAppConfig } from '#imports'
7777
import { useLocale } from '../composables/useLocale'
78+
import { usePortal } from '../composables/usePortal'
7879
import { tv } from '../utils/tv'
7980
import UButton from './Button.vue'
8081
@@ -94,6 +95,7 @@ const { t } = useLocale()
9495
const appConfig = useAppConfig() as Slideover['AppConfig']
9596
9697
const rootProps = useForwardPropsEmits(reactivePick(props, 'open', 'defaultOpen', 'modal'), emits)
98+
const portalProps = usePortal(toRef(() => props.portal))
9799
const contentProps = toRef(() => props.content)
98100
const contentEvents = computed(() => {
99101
const events = {
@@ -124,7 +126,7 @@ const ui = computed(() => tv({ extend: tv(theme), ...(appConfig.ui?.slideover ||
124126
<slot :open="open" />
125127
</DialogTrigger>
126128

127-
<DialogPortal :disabled="!portal">
129+
<DialogPortal v-bind="portalProps">
128130
<DialogOverlay v-if="overlay" :class="ui.overlay({ class: props.ui?.overlay })" />
129131

130132
<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">

0 commit comments

Comments
 (0)