-
Notifications
You must be signed in to change notification settings - Fork 424
feat(clerk-js): Introduce UNSAFE_PortalProvider
#7310
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
Open
alexcarpenter
wants to merge
27
commits into
main
Choose a base branch
from
alexcarpenter/portal-provider-3
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 18 commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
71ff27a
init
alexcarpenter 922010b
Create wet-phones-camp.md
alexcarpenter 3d5f3bd
Apply suggestion from @alexcarpenter
alexcarpenter e9b66d5
wip
alexcarpenter af3e86b
Merge branch 'alexcarpenter/portal-provider-3' of github.com:clerk/ja…
alexcarpenter 2423766
wip
alexcarpenter 66ae887
Merge branch 'main' into alexcarpenter/portal-provider-3
alexcarpenter 44854e1
expand portalprovider usage
alexcarpenter fe0c34a
fix export
alexcarpenter f3c69a3
Merge branch 'main' into alexcarpenter/portal-provider-3
alexcarpenter c90b168
rename to UNSAFE_PortalProvider
alexcarpenter 998c30a
fix menu item wrapping
alexcarpenter 7d4e63c
remove PortalRootManager usage
alexcarpenter fa6ef65
feat(vue): UNSAFE_PortalProvider implementation (#7491)
wobsoriano fd628a2
add changeset
alexcarpenter 5532a47
Delete .changeset/wet-phones-camp.md
alexcarpenter 1b9d5a0
Merge branch 'main' into alexcarpenter/portal-provider-3
alexcarpenter 13f622f
Merge branch 'alexcarpenter/portal-provider-3' of github.com:clerk/ja…
alexcarpenter 7bbc154
Merge branch 'main' into alexcarpenter/portal-provider-3
alexcarpenter d1c12d1
remove duplicates
alexcarpenter ee2bc55
Merge branch 'alexcarpenter/portal-provider-3' of github.com:clerk/ja…
alexcarpenter 632874a
chore(ui): Modify bundle limits
dstaley f3045f7
Update exports.test.ts.snap
alexcarpenter f80e145
Update exports.test.ts.snap
alexcarpenter a3a64e9
sort
alexcarpenter 9814d4b
update expo export
alexcarpenter 01126b7
Merge branch 'main' into alexcarpenter/portal-provider-3
alexcarpenter 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 |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| --- | ||
| '@clerk/tanstack-react-start': minor | ||
| '@clerk/react-router': minor | ||
| '@clerk/nextjs': minor | ||
| '@clerk/shared': minor | ||
| '@clerk/astro': minor | ||
| '@clerk/react': minor | ||
| '@clerk/expo': minor | ||
| '@clerk/nuxt': minor | ||
| '@clerk/vue': minor | ||
| '@clerk/ui': minor | ||
| --- | ||
|
|
||
| Introduce `<UNSAFE_PortalProvider>` component which allows you to specify a custom container for Clerk floating UI elements (popovers, modals, tooltips, etc.) that use portals. Only Clerk components within the 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 [Radix Dialog](https://www.radix-ui.com/primitives/docs/components/dialog) or [React Aria Components](https://react-spectrum.adobe.com/react-aria/components.html), where portaled elements need to render within the dialog's container to remain interact-able. | ||
|
|
||
| ```tsx | ||
| 'use client'; | ||
|
|
||
| import { useRef } from 'react'; | ||
| import * as Dialog from '@radix-ui/react-dialog'; | ||
| import { UNSAFE_PortalProvider, UserButton } from '@clerk/nextjs'; | ||
|
|
||
| export function UserDialog() { | ||
| const containerRef = useRef<HTMLDivElement>(null); | ||
|
|
||
| return ( | ||
| <Dialog.Root> | ||
| <Dialog.Trigger>Open Dialog</Dialog.Trigger> | ||
| <Dialog.Portal> | ||
| <Dialog.Overlay /> | ||
| <Dialog.Content ref={containerRef}> | ||
| <UNSAFE_PortalProvider getContainer={() => containerRef.current}> | ||
| <UserButton /> | ||
| </UNSAFE_PortalProvider> | ||
| </Dialog.Content> | ||
| </Dialog.Portal> | ||
| </Dialog.Root> | ||
| ); | ||
| } | ||
| ``` |
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
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
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 |
|---|---|---|
|
|
@@ -23,4 +23,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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| export * from './ReactRouterClerkProvider'; | ||
| export type { WithClerkState } from './types'; | ||
| export { SignIn, SignUp, OrganizationProfile, UserProfile } from './uiComponents'; | ||
| export { UNSAFE_PortalProvider } from '@clerk/react'; |
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 |
|---|---|---|
| @@ -1 +1,2 @@ | ||
| export { ClerkProvider } from './ClerkProvider'; | ||
| export { UNSAFE_PortalProvider } from '@clerk/shared/react'; |
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,65 @@ | ||
| 'use client'; | ||
|
|
||
| import React from 'react'; | ||
|
|
||
| import { createContextAndHook } from './hooks/createContextAndHook'; | ||
|
|
||
| type PortalProviderProps = React.PropsWithChildren<{ | ||
| /** | ||
| * Function that returns the container element where portals should be rendered. | ||
| * This allows Clerk components to render inside external dialogs/popovers | ||
| * (e.g., Radix Dialog, React Aria Components) instead of document.body. | ||
| */ | ||
| getContainer: () => HTMLElement | null; | ||
| }>; | ||
|
|
||
| const [PortalContext, , usePortalContextWithoutGuarantee] = createContextAndHook<{ | ||
| getContainer: () => HTMLElement | null; | ||
| }>('PortalProvider'); | ||
|
|
||
| /** | ||
| * 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 Radix Dialog or React Aria Components, where portaled elements need to render | ||
| * within the dialog's container to remain interactable. | ||
| * | ||
| * @example | ||
| * ```tsx | ||
| * function Example() { | ||
| * const containerRef = useRef(null); | ||
| * return ( | ||
| * <RadixDialog ref={containerRef}> | ||
| * <UNSAFE_PortalProvider getContainer={() => containerRef.current}> | ||
| * <UserButton /> | ||
| * </UNSAFE_PortalProvider> | ||
| * </RadixDialog> | ||
| * ); | ||
| * } | ||
| * ``` | ||
| */ | ||
| export const UNSAFE_PortalProvider = ({ children, getContainer }: PortalProviderProps) => { | ||
| const contextValue = React.useMemo(() => ({ value: { getContainer } }), [getContainer]); | ||
|
|
||
| return <PortalContext.Provider value={contextValue}>{children}</PortalContext.Provider>; | ||
| }; | ||
|
|
||
| /** | ||
| * Hook 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 contextValue = usePortalContextWithoutGuarantee(); | ||
|
|
||
| if (contextValue && 'getContainer' in contextValue && contextValue.getContainer) { | ||
| return contextValue.getContainer; | ||
| } | ||
|
|
||
| // Return a function that returns null when not inside a PortalProvider | ||
| return () => null; | ||
| }; |
103 changes: 103 additions & 0 deletions
103
packages/shared/src/react/__tests__/PortalProvider.test.tsx
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,103 @@ | ||
| import { describe, expect, it } from 'vitest'; | ||
| import React from 'react'; | ||
| import { render, screen } from '@testing-library/react'; | ||
|
|
||
| import { UNSAFE_PortalProvider, usePortalRoot } from '../PortalProvider'; | ||
|
|
||
| describe('UNSAFE_PortalProvider', () => { | ||
| it('provides getContainer to children via context', () => { | ||
| const container = document.createElement('div'); | ||
| const getContainer = () => container; | ||
|
|
||
| const TestComponent = () => { | ||
| const portalRoot = usePortalRoot(); | ||
| return <div data-testid='test'>{portalRoot === getContainer ? 'found' : 'not-found'}</div>; | ||
| }; | ||
|
|
||
| render( | ||
| <UNSAFE_PortalProvider getContainer={getContainer}> | ||
| <TestComponent /> | ||
| </UNSAFE_PortalProvider>, | ||
| ); | ||
|
|
||
| expect(screen.getByTestId('test').textContent).toBe('found'); | ||
| }); | ||
|
|
||
| it('only affects components within the provider', () => { | ||
| const container = document.createElement('div'); | ||
| const getContainer = () => container; | ||
|
|
||
| const InsideComponent = () => { | ||
| const portalRoot = usePortalRoot(); | ||
| return <div data-testid='inside'>{portalRoot() === container ? 'container' : 'null'}</div>; | ||
| }; | ||
|
|
||
| const OutsideComponent = () => { | ||
| const portalRoot = usePortalRoot(); | ||
| return <div data-testid='outside'>{portalRoot() === null ? 'null' : 'container'}</div>; | ||
| }; | ||
|
|
||
| render( | ||
| <> | ||
| <OutsideComponent /> | ||
| <UNSAFE_PortalProvider getContainer={getContainer}> | ||
| <InsideComponent /> | ||
| </UNSAFE_PortalProvider> | ||
| </>, | ||
| ); | ||
|
|
||
| expect(screen.getByTestId('inside').textContent).toBe('container'); | ||
| expect(screen.getByTestId('outside').textContent).toBe('null'); | ||
| }); | ||
| }); | ||
|
|
||
| describe('usePortalRoot', () => { | ||
| it('returns getContainer from context when inside PortalProvider', () => { | ||
| const container = document.createElement('div'); | ||
| const getContainer = () => container; | ||
|
|
||
| const TestComponent = () => { | ||
| const portalRoot = usePortalRoot(); | ||
| return <div data-testid='test'>{portalRoot() === container ? 'found' : 'not-found'}</div>; | ||
| }; | ||
|
|
||
| render( | ||
| <UNSAFE_PortalProvider getContainer={getContainer}> | ||
| <TestComponent /> | ||
| </UNSAFE_PortalProvider>, | ||
| ); | ||
|
|
||
| expect(screen.getByTestId('test').textContent).toBe('found'); | ||
| }); | ||
|
|
||
| it('returns a function that returns null when outside PortalProvider', () => { | ||
| const TestComponent = () => { | ||
| const portalRoot = usePortalRoot(); | ||
| return <div data-testid='test'>{portalRoot() === null ? 'null' : 'not-null'}</div>; | ||
| }; | ||
|
|
||
| render(<TestComponent />); | ||
|
|
||
| expect(screen.getByTestId('test').textContent).toBe('null'); | ||
| }); | ||
|
|
||
| it('supports nested providers with innermost taking precedence', () => { | ||
| const outerContainer = document.createElement('div'); | ||
| const innerContainer = document.createElement('div'); | ||
|
|
||
| const TestComponent = () => { | ||
| const portalRoot = usePortalRoot(); | ||
| return <div data-testid='test'>{portalRoot() === innerContainer ? 'inner' : 'outer'}</div>; | ||
| }; | ||
|
|
||
| render( | ||
| <UNSAFE_PortalProvider getContainer={() => outerContainer}> | ||
| <UNSAFE_PortalProvider getContainer={() => innerContainer}> | ||
| <TestComponent /> | ||
| </UNSAFE_PortalProvider> | ||
| </UNSAFE_PortalProvider>, | ||
| ); | ||
|
|
||
| expect(screen.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
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 |
|---|---|---|
| @@ -1,2 +1,3 @@ | ||
| export * from './ClerkProvider'; | ||
| export { SignIn, SignUp, OrganizationProfile, OrganizationList, UserProfile } from './uiComponents'; | ||
| export { UNSAFE_PortalProvider } from '@clerk/react'; |
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.