Skip to content

Portal: Add PortalProvider for default container context#3812

Open
rxliuli wants to merge 2 commits intoradix-ui:mainfrom
rxliuli:feat/portal-provider
Open

Portal: Add PortalProvider for default container context#3812
rxliuli wants to merge 2 commits intoradix-ui:mainfrom
rxliuli:feat/portal-provider

Conversation

@rxliuli
Copy link

@rxliuli rxliuli commented Feb 12, 2026

Summary

Add PortalProvider component to @radix-ui/react-portal, allowing users to set a default portal container via React context. This is particularly useful for browser extensions and Shadow DOM scenarios, where portaling to document.body breaks style isolation.

Without PortalProvider, every portal-based component (Dialog, Popover, Tooltip, Select, Menu, etc.) needs an explicit container prop — tedious and error-prone. Now users can set it once at the top level:

function App() {
  const [container, setContainer] = React.useState<HTMLElement | null>(null);
  return (
    <Portal.PortalProvider container={container!}>
      <div ref={setContainer} />
      <Dialog.Root>
        <Dialog.Trigger>Open</Dialog.Trigger>
        <Dialog.Portal>  {/* no container prop needed */}
          <Dialog.Content>...</Dialog.Content>
        </Dialog.Portal>
      </Dialog.Root>
    </Portal.PortalProvider>
  );
}

Container priority: explicit container prop > PortalProvider context > document.body

No changes needed to downstream components — they all pass container through to Portal, so undefined now falls back to context automatically.

Related issues

Test plan

  • pnpm typecheck passes
  • pnpm build passes
  • pnpm lint passes
  • Verify Portal still renders to document.body by default (no PortalProvider)
  • Verify PortalProvider sets the default container for nested Portals
  • Verify explicit container prop on Portal overrides PortalProvider

🤖 Generated with Claude Code

When using Radix components inside a Shadow DOM, every portal-based
component needs a custom `container` prop. This adds a `PortalProvider`
context so users can set the default portal container once at the top
level, and all nested Portal components automatically use it.

Priority chain: explicit `container` prop > PortalProvider context > document.body

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@changeset-bot
Copy link

changeset-bot bot commented Feb 12, 2026

⚠️ No Changeset found

Latest commit: a66b0e5

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feat:Portal] Override default portal'd location

1 participant