Skip to main content

Portal

Portal renders content into a shared overlay layer that sits above the rest of the app. It is the primitive behind dialogs, snackbars, menus, tooltips, and bottom sheets: anything that must escape the layout and the z-order of its parent.

Setup​

Wrap your app root in PortalHost once. Every <Portal> anywhere in the tree below mounts into the overlay layer of this host.

On iOS and Android, the root PortalHost also adds a SafeAreaProvider when the app has none, so the components that apply insets read them from its context. With a provider of your own, as below or from Expo Router, it adds nothing. On the web it adds none.

Put the host outside Layout, not inside it. Each overlay pads its own safe-area insets, so a host inside the Layout padding gets them twice: the snackbar sits one inset too high, and a modal drawer one inset too far in.

import { PortalHost, Typography } from '@rootnative/components'
import { mdiResolver } from '@rootnative/components/mdi'
import { ThemeProvider } from '@rootnative/core'
import { SafeAreaProvider } from 'react-native-safe-area-context'
import { View } from 'react-native'

export default function App() {
return (
<SafeAreaProvider>
<ThemeProvider iconResolver={mdiResolver}>
<PortalHost>
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
<Typography variant="bodyLarge">App content lives here.</Typography>
</View>
</PortalHost>
</ThemeProvider>
</SafeAreaProvider>
)
}

Usage​

import { Button, Portal, PortalHost, Typography } from '@rootnative/components'
import { mdiResolver } from '@rootnative/components/mdi'
import { ThemeProvider } from '@rootnative/core'
import { SafeAreaProvider } from 'react-native-safe-area-context'
import { useState } from 'react'
import { Pressable, View } from 'react-native'

function Screen() {
const [open, setOpen] = useState(false)
return (
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
<Button variant="filled" onPress={() => setOpen(true)}>Open overlay</Button>
{open ? (
<Portal>
<Pressable
onPress={() => setOpen(false)}
style={{ position: 'absolute', inset: 0, backgroundColor: 'rgba(0,0,0,0.4)', alignItems: 'center', justifyContent: 'center' }}
>
<View style={{ backgroundColor: 'white', padding: 24, borderRadius: 12 }}>
<Typography variant="titleMedium">Hello from a Portal</Typography>
<Typography variant="bodyMedium">Tap outside to close.</Typography>
</View>
</Pressable>
</Portal>
) : null}
</View>
)
}

export default function App() {
return (
<SafeAreaProvider>
<ThemeProvider iconResolver={mdiResolver}>
<PortalHost>
<Screen />
</PortalHost>
</ThemeProvider>
</SafeAreaProvider>
)
}

The Portal component itself renders nothing inline. Its children appear inside the overlay layer of the host instead. Mount and unmount it like any other React component.

Theme​

The content keeps the theme of the place where you write the Portal, not the theme at the host. Portal reads the theme, the mode, the icon resolver and the named transitions at its call site with useThemeScope() from @rootnative/core, and provides them again at the host with ThemeScopeProvider. So a Portal under a nested ThemeProvider renders its content with the nested theme, and the library overlays do the same. See Nested providers.

How it works​

PortalHost wraps its children in a root View and adds an absolutely positioned overlay layer on top. Each <Portal> registers its children with the host through context. The host renders them into that layer, sorted by priority.

The overlay layer uses pointerEvents="box-none", so an empty region of the overlay passes a touch through to the content below. Each Portal entry positions itself and draws its own scrim, usually with position: 'absolute' and a full-bleed View that captures touches.

The host holds the Portal entries outside React state, and each host subscribes only to its own entries. So an overlay that opens or closes re-renders the overlay layer, not the app tree under the host.

Stacking order​

Portals in the same host stack by ascending priority. A tie falls back to the mount order: open Dialog A, then Dialog B at the same priority, and B sits above A.

PORTAL_LAYERS is the z-order contract that the overlay components of Root Native use:

LayerValueUsed by
sheet100Bottom sheets and side sheets, plus their scrim
dialog200Dialogs and their scrim
snackbar300Snackbars. They stay visible over an open dialog
menu400Menus and dropdowns, which can open from any surface below
tooltip500Tooltips. They are topmost, so they can describe a control in any layer

The gaps of 100 points are intentional. Put a custom overlay between two layers with PORTAL_LAYERS.dialog + 1, with no change to the other numbers.

import { Portal, PORTAL_LAYERS } from '@rootnative/components'

<Portal priority={PORTAL_LAYERS.dialog}>
<MyDialog />
</Portal>

A <Portal> with no priority defaults to 0, below every named layer.

Named hosts​

<Portal hostName="…"> targets a <PortalHost name="…"> elsewhere in the tree instead of the root overlay. Use it to scope overlays to a screen or a nested navigator. The named host renders its portals where it sits, so that subtree clips and positions them, not the app root.

<PortalHost>
<App />
<View style={styles.sheetArea}>
<PortalHost name="sheet-area" />
</View>
</PortalHost>

Two rules follow from that:

  • priority orders portals inside one host only. The stack between hosts follows the tree position. Anything that must take part in the app-wide z-order belongs in the default host with a PORTAL_LAYERS priority. That is what the Dialog, Snackbar, Menu, Tooltip, and BottomSheet of Root Native do.
  • An unmatched hostName falls back to the default host, and doesn't drop the content. If the named host mounts later, the portal moves to it. If it unmounts, the portal moves back.

The name of the root host is exported as DEFAULT_PORTAL_HOST (the string 'default'). Code that targets it explicitly, or compares against it, can import the constant instead of the literal:

import { DEFAULT_PORTAL_HOST } from '@rootnative/components/portal'

<Portal hostName={DEFAULT_PORTAL_HOST}>{overlay}</Portal>

Without a host​

A <Portal> outside a <PortalHost> logs a development error and falls back to inline rendering, so your screen still works. Add a PortalHost at the app root to silence the warning and get the overlay behavior.

Portal Props​

PropTypeDefaultRequiredDescription
childrenReactNode-YesContent teleported into the host's overlay layer.
hostNamestringdefaultNoName of the host to render into. Falls back to the default host when no host with this name is mounted.
prioritynumber0NoStack order within the host — higher renders above lower, ties broken by mount order. Use the `PORTAL_LAYERS` constants for the layers Root Native itself defines.

PortalHost Props​

PropTypeDefaultRequiredDescription
childrenReactNode-NoContent rendered inside the host. Optional on a named (slot) host.
namestring-NoRegisters this host under a name that `<Portal hostName="…">` can target. A named host must sit inside a root `<PortalHost>` — it renders its portals where it appears in the tree instead of in the root overlay, which is the point of naming one (scoping overlays to a screen or a nested navigator). Cross-host stacking therefore follows tree position, not `priority`; `priority` only orders portals inside the same host. Anything that has to participate in the app-wide z-order belongs in the default host with a `priority` from `PORTAL_LAYERS`.
styleStyleProp<ViewStyle>-NoStyle applied to the root container on a root host, or to the overlay container on a named host (which absolute-fills its parent by default).