-
Notifications
You must be signed in to change notification settings - Fork 410
feat(clerk-js): PortalProvider POC #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
Draft
alexcarpenter
wants to merge
5
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.
Draft
Changes from 2 commits
Commits
Show all changes
5 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 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,2 @@ | ||
| --- | ||
| --- | ||
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 |
|---|---|---|
| @@ -1 +1,2 @@ | ||
| export { ClerkProvider } from './ClerkProvider'; | ||
| export { 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,76 @@ | ||
| 'use client'; | ||
|
|
||
| import React, { useEffect, useRef } from 'react'; | ||
|
|
||
| import { createContextAndHook } from './hooks/createContextAndHook'; | ||
| import { portalRootManager } from './portal-root-manager'; | ||
|
|
||
| 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'); | ||
|
|
||
| /** | ||
| * PortalProvider allows you to specify a custom container for all Clerk floating UI elements | ||
| * (popovers, modals, tooltips, etc.) that use 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}> | ||
| * <PortalProvider getContainer={() => containerRef.current}> | ||
| * <UserButton /> | ||
| * </PortalProvider> | ||
| * </RadixDialog> | ||
| * ); | ||
| * } | ||
| * ``` | ||
| */ | ||
| export const PortalProvider = ({ children, getContainer }: PortalProviderProps) => { | ||
| const getContainerRef = useRef(getContainer); | ||
| getContainerRef.current = getContainer; | ||
|
|
||
| // Register with the manager for cross-tree access (e.g., modals in Components.tsx) | ||
| useEffect(() => { | ||
| const getContainerWrapper = () => getContainerRef.current(); | ||
| portalRootManager.push(getContainerWrapper); | ||
| return () => { | ||
| portalRootManager.pop(); | ||
| }; | ||
| }, []); | ||
|
|
||
| // Provide context for same-tree access (e.g., UserButton popover) | ||
| const contextValue = React.useMemo(() => ({ value: { getContainer } }), [getContainer]); | ||
|
|
||
| return <PortalContext.Provider value={contextValue}>{children}</PortalContext.Provider>; | ||
| }; | ||
|
|
||
| /** | ||
| * Hook to get the current portal root container. | ||
| * First checks React context (for same-tree components), | ||
| * then falls back to PortalRootManager (for cross-tree like modals). | ||
| */ | ||
| export const usePortalRoot = (): HTMLElement | null => { | ||
| // Try to get from context first (for components in the same React tree) | ||
| const contextValue = usePortalContextWithoutGuarantee(); | ||
| if (contextValue && 'getContainer' in contextValue) { | ||
| return contextValue.getContainer(); | ||
| } | ||
|
|
||
| // Fall back to manager (for components in different React trees, like modals) | ||
| return portalRootManager.getCurrent(); | ||
| }; |
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,37 @@ | ||
| /** | ||
| * PortalRootManager manages a stack of portal root containers. | ||
| * This allows PortalProvider to work across separate React trees | ||
| * (e.g., when Clerk modals are rendered in a different tree via Components.tsx). | ||
| */ | ||
| class PortalRootManager { | ||
| private stack: Array<() => HTMLElement | null> = []; | ||
|
|
||
| /** | ||
| * Push a new portal root getter onto the stack. | ||
| * @param getContainer Function that returns the container element | ||
| */ | ||
| push(getContainer: () => HTMLElement | null): void { | ||
| this.stack.push(getContainer); | ||
| } | ||
|
|
||
| /** | ||
| * Pop the most recent portal root from the stack. | ||
| */ | ||
| pop(): void { | ||
| this.stack.pop(); | ||
| } | ||
|
|
||
| /** | ||
| * Get the current (topmost) portal root container. | ||
| * @returns The container element or null if no provider is active | ||
| */ | ||
| getCurrent(): HTMLElement | null { | ||
| if (this.stack.length === 0) { | ||
| return null; | ||
| } | ||
| const getContainer = this.stack[this.stack.length - 1]; | ||
| return getContainer(); | ||
| } | ||
| } | ||
|
|
||
| export const portalRootManager = new PortalRootManager(); |
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.