-
Notifications
You must be signed in to change notification settings - Fork 419
feat(vue): UNSAFE_PortalProvider implementation #7491
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
Merged
alexcarpenter
merged 2 commits into
alexcarpenter/portal-provider-3
from
rob/vue-unsafe-portalprovider
Dec 17, 2025
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -25,4 +25,5 @@ export { | |
| SignOutButton, | ||
| SignInWithMetamaskButton, | ||
| PricingTable, | ||
| UNSAFE_PortalProvider, | ||
| } from '@clerk/vue'; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| import { defineComponent, type PropType, provide } from 'vue'; | ||
|
|
||
| import { PortalInjectionKey } from '../keys'; | ||
|
|
||
| /** | ||
| * UNSAFE_PortalProvider allows you to specify a custom container for Clerk floating UI elements | ||
| * (popovers, modals, tooltips, etc.) that use portals. | ||
| * | ||
| * Only components within this provider will be affected. Components outside the provider | ||
| * will continue to use the default document.body for portals. | ||
| * | ||
| * This is particularly useful when using Clerk components inside external UI libraries | ||
| * like Reka UI Dialog, where portaled elements need to render within the dialog's | ||
| * container to remain interactable. | ||
| * | ||
| * @example | ||
| * ```vue | ||
| * <script setup> | ||
| * import { useTemplateRef } from 'vue'; | ||
| * import { DialogContent } from 'reka-ui'; | ||
| * import { UNSAFE_PortalProvider, UserButton } from '@clerk/vue'; | ||
| * | ||
| * const dialogContentRef = useTemplateRef('dialogContentRef'); | ||
| * </script> | ||
| * | ||
| * <template> | ||
| * <DialogContent ref="dialogContentRef"> | ||
| * <UNSAFE_PortalProvider :getContainer="() => dialogContentRef?.$el"> | ||
| * <UserButton /> | ||
| * </UNSAFE_PortalProvider> | ||
| * </DialogContent> | ||
| * </template> | ||
| * ``` | ||
| */ | ||
| export const UNSAFE_PortalProvider = defineComponent({ | ||
| name: 'UNSAFE_PortalProvider', | ||
| props: { | ||
| /** | ||
| * Function that returns the container element where portals should be rendered. | ||
| * This allows Clerk components to render inside external dialogs/popovers | ||
| * (e.g., Reka UI Dialog) instead of document.body. | ||
| */ | ||
| getContainer: { | ||
| type: Function as PropType<() => HTMLElement | null>, | ||
| required: true, | ||
| }, | ||
| }, | ||
| setup(props, { slots }) { | ||
| provide(PortalInjectionKey, { getContainer: props.getContainer }); | ||
| return () => slots.default?.(); | ||
| }, | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
100 changes: 100 additions & 0 deletions
100
packages/vue/src/composables/__tests__/usePortalRoot.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| import { render } from '@testing-library/vue'; | ||
| import { describe, expect, it } from 'vitest'; | ||
| import { defineComponent, h } from 'vue'; | ||
|
|
||
| import { UNSAFE_PortalProvider } from '../../components/PortalProvider'; | ||
| import { usePortalRoot } from '../usePortalRoot'; | ||
|
|
||
| describe('usePortalRoot', () => { | ||
| it('returns getContainer from context when inside PortalProvider', () => { | ||
| const container = document.createElement('div'); | ||
| const getContainer = () => container; | ||
|
|
||
| const TestComponent = defineComponent({ | ||
| setup() { | ||
| const portalRoot = usePortalRoot(); | ||
| return () => h('div', { 'data-testid': 'test' }, portalRoot() === container ? 'found' : 'not-found'); | ||
| }, | ||
| }); | ||
|
|
||
| const { getByTestId } = render(h(UNSAFE_PortalProvider, { getContainer }, () => h(TestComponent))); | ||
|
|
||
| expect(getByTestId('test').textContent).toBe('found'); | ||
| }); | ||
|
|
||
| it('returns a function that returns null when outside PortalProvider', () => { | ||
| const TestComponent = defineComponent({ | ||
| setup() { | ||
| const portalRoot = usePortalRoot(); | ||
| return () => h('div', { 'data-testid': 'test' }, portalRoot() === null ? 'null' : 'not-null'); | ||
| }, | ||
| }); | ||
|
|
||
| const { getByTestId } = render(TestComponent); | ||
|
|
||
| expect(getByTestId('test').textContent).toBe('null'); | ||
| }); | ||
|
|
||
| it('only affects components within the provider', () => { | ||
| const container = document.createElement('div'); | ||
| const getContainer = () => container; | ||
|
|
||
| const InsideComponent = defineComponent({ | ||
| setup() { | ||
| const portalRoot = usePortalRoot(); | ||
| return () => h('div', { 'data-testid': 'inside' }, portalRoot() === container ? 'container' : 'null'); | ||
| }, | ||
| }); | ||
|
|
||
| const OutsideComponent = defineComponent({ | ||
| setup() { | ||
| const portalRoot = usePortalRoot(); | ||
| return () => h('div', { 'data-testid': 'outside' }, portalRoot() === null ? 'null' : 'container'); | ||
| }, | ||
| }); | ||
|
|
||
| const { getByTestId } = render({ | ||
| components: { InsideComponent, OutsideComponent, UNSAFE_PortalProvider }, | ||
| template: ` | ||
| <OutsideComponent /> | ||
| <UNSAFE_PortalProvider :getContainer="getContainer"> | ||
| <InsideComponent /> | ||
| </UNSAFE_PortalProvider> | ||
| `, | ||
| setup() { | ||
| return { getContainer }; | ||
| }, | ||
| }); | ||
|
|
||
| expect(getByTestId('inside').textContent).toBe('container'); | ||
| expect(getByTestId('outside').textContent).toBe('null'); | ||
| }); | ||
|
|
||
| it('supports nested providers with innermost taking precedence', () => { | ||
| const outerContainer = document.createElement('div'); | ||
| const innerContainer = document.createElement('div'); | ||
|
|
||
| const TestComponent = defineComponent({ | ||
| setup() { | ||
| const portalRoot = usePortalRoot(); | ||
| return () => h('div', { 'data-testid': 'test' }, portalRoot() === innerContainer ? 'inner' : 'outer'); | ||
| }, | ||
| }); | ||
|
|
||
| const { getByTestId } = render({ | ||
| components: { TestComponent, UNSAFE_PortalProvider }, | ||
| template: ` | ||
| <UNSAFE_PortalProvider :getContainer="() => outerContainer"> | ||
| <UNSAFE_PortalProvider :getContainer="() => innerContainer"> | ||
| <TestComponent /> | ||
| </UNSAFE_PortalProvider> | ||
| </UNSAFE_PortalProvider> | ||
| `, | ||
| setup() { | ||
| return { outerContainer, innerContainer }; | ||
| }, | ||
| }); | ||
|
|
||
| expect(getByTestId('test').textContent).toBe('inner'); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| import { inject } from 'vue'; | ||
|
|
||
| import { PortalInjectionKey } from '../keys'; | ||
|
|
||
| /** | ||
| * Composable to get the current portal root container. | ||
| * Returns the getContainer function from context if inside a PortalProvider, | ||
| * otherwise returns a function that returns null (default behavior). | ||
| */ | ||
| export const usePortalRoot = (): (() => HTMLElement | null) => { | ||
| const context = inject(PortalInjectionKey, null); | ||
|
|
||
| if (context && context.getContainer) { | ||
| return context.getContainer; | ||
| } | ||
|
|
||
| // Return a function that returns null when not inside a PortalProvider | ||
| return () => null; | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am using Reka UI here as example (Radix in Vue ecosystem).