
Product
Introducing Socket Firewall Enterprise: Flexible, Configurable Protection for Modern Package Ecosystems
Socket Firewall Enterprise is now available with flexible deployment, configurable policies, and expanded language support.
@fluentui/react-portal
Advanced tools
React Portal components for Fluent UI React
This package contains the Portal component, which allow consumers to render React portals with Fluent styling and RTL awareness.
Portal can be used as standalone with any part of a Fluent app. The component should be under a FluentProvider in the tree to make sure that proper theming and RTL handling is available.
By default Portal will render content to document body
<FluentProvider>
<Portal>Content rendered by default to Fluent's document.body</Portal>
</FluentProvider>
The mount location of the portal can be customized
const node = document.getElementById('customNode');
<Portal mountNode={node}>Render to a custom node in DOM</Portal>;
Portal renders React children directly to the default/configured DOM node. Therefore styling should be applied to the children by users directly.
Out of order DOM elements can be problematic when using 'click outside' event listeners since you cannot rely on element.contains(event.target) because the Portal elements are out of DOM order.
const outerButtonRef = React.useRef();
const innerButtonRef = React.useRef();
<Portal>
<div>
<button ref={outerButtonRef}> Outer button </button>
<Portal>
<div>
<button ref={innerButtonRef}> Inner button </button>
</div>
</Portal>
</div>
</Portal>
// DOM output
<div>
<button>Outer button</button>
</div>
<div>
<button>Inner button</button>
</div>
// Let's add an event listener to 'dismss' the outer portal when clicked outside
// ⚠⚠⚠ This will always be called when clicking on the inner button
document.addEventListener((event) => {
if (outerButtonRef.current.contains(event.target)) {
dismissOuterPortal();
}
})
When the above case is not required, using element.contains is perfectly fine. But nested cases should still be handled appropriately. We do this using the concept of virtual parents
Portal will make public 2 utilities that will only be used in cases where the user needs to know if an out of order DOM element will need to be used or not.
setVirtualParent - sets virtual parent. Portal uses this already internally.elementContains - similar to element.contains but uses the virtual hierarchy as referenceBelow shows what a virtual parent is
// Setting a virtual parent
const parent = document.getElementById('parent');
const child = document.getElementById('child');
child._virtual.parent = parent;
Portals will render a hidden span that will be the virtual parent, by nesting portals virtual parens will also be nested so that elementContains will work predictably.
<FluentProvider>
<Portal id="portal-1" />
<Portal id="portal-2" />
</FluentProvider>
DOM output:
<body>
<div>
{/* Virtual parent for portal*/}
<span aria-hidden />
{/* Virtual parent for portal*/}
<span aria-hidden />
</div>
<div id="portal-1" class="theme-provider-0">
{children}
</div>
<div id="portal-2" class="theme-provider-0">
{children}
</div>
</body>
<FluentProvider>
<Portal id="portal-1">
<Portal id="portal-2" />
</Portal>
</FluentProvider>
DOM output:
<body>
<div>
{/* Virtual parent for outer portal*/}
<span aria-hidden></span>
</div>
<div id="portal-1" class="theme-provider-0">
{/* Virtual parent for inner portal*/}
<span aria-hidden />
{children}
</div>
<div id="portal-2" class="theme-provider-0">
{children}
</div>
</body>
react-dom is a core package of React that provides the ReactDOM.createPortal method, which is used to render children into a DOM node outside of the parent component's hierarchy. It is a more low-level API compared to @fluentui/react-portal, which provides a higher-level component abstraction.
react-portal is a popular package that provides a simple API for creating portals in React applications. It offers more features and flexibility compared to @fluentui/react-portal, such as event handling and more control over the portal's lifecycle.
react-overlays is a library that provides a set of components for creating overlays, including modals, tooltips, and popovers. It uses portals to render these components outside the normal DOM hierarchy, similar to @fluentui/react-portal, but with additional features tailored for overlay components.
FAQs
A utility component that creates portals compatible with Fluent UI
The npm package @fluentui/react-portal receives a total of 212,946 weekly downloads. As such, @fluentui/react-portal popularity was classified as popular.
We found that @fluentui/react-portal demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 10 open source maintainers collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Product
Socket Firewall Enterprise is now available with flexible deployment, configurable policies, and expanded language support.

Security News
Open source dashboard CNAPulse tracks CVE Numbering Authorities’ publishing activity, highlighting trends and transparency across the CVE ecosystem.

Product
Detect malware, unsafe data flows, and license issues in GitHub Actions with Socket’s new workflow scanning support.