Layout & primitives
Portal
Free9.7 KB gzippedRenders children at the end of document.body (or container) so overlays escape parent overflow and stacking contexts.
Accessibility: WCAG 2.2 AA · No A or AA violations in light or dark mode · No keyboard interaction of its own · Full report
Import
import { Portal } from "@vjdesigns/vjdesigns-ui";
Importing only Portal adds about 9.7 KB of minified, gzipped JavaScript to your app (React not included). The stylesheet is shared by every component and loaded once: 75.1 KB gzipped.
Examples
Into acontainer
Renders its children somewhere else in the DOM: document.body by default, or a container. Overlays use it to escape clipping and stacking contexts.
The source: the portal is declared in here.
import { useState } from "react";
import { Box, Button, Portal, Stack, Text } from "@vjdesigns/vjdesigns-ui";
export function IntoAContainer() {
const [target, setTarget] = useState<HTMLDivElement | null>(null);
const [show, setShow] = useState(true);
return (
<Stack gap={4} style={{ width: "100%", maxWidth: "26rem" }}>
<Box border radius="md" p={4} overflow="hidden">
<Text size="sm" tone="muted">
The source: the portal is declared in here.
</Text>
{show && (
<Portal container={target}>
<Box tone="primary" radius="md" p={3}>
<Text size="sm" weight="semibold" tone="inherit">
I'm rendered in the other box.
</Text>
</Box>
</Portal>
)}
</Box>
<Box ref={setTarget} border="strong" radius="md" p={4} />
<Button variant="outline" tone="neutral" onClick={() => setShow((s) => !s)}>
{show ? "Unmount portal" : "Mount portal"}
</Button>
</Stack>
);
}
Accessibility
Portaled content is moved in the DOM, so it's also moved in the Tab order and the reading order. Use it for content that floats over the page; the library's overlays (Modal, Popover, Menu, Tooltip…) portal for you and manage focus, so prefer them for anything interactive.
Declared here, inside a clipped box.
import { useState } from "react";
import { Box, Portal, Stack, Text } from "@vjdesigns/vjdesigns-ui";
export function Accessibility() {
const [target, setTarget] = useState<HTMLDivElement | null>(null);
return (
<Stack gap={4} style={{ width: "100%", maxWidth: "26rem" }}>
<Box border radius="md" p={4} overflow="hidden">
<Text size="sm" tone="muted">
Declared here, inside a clipped box.
</Text>
<Portal container={target}>
<Text size="sm" weight="semibold">
Read and focused here, where it's rendered.
</Text>
</Portal>
</Box>
<Box ref={setTarget} border="strong" radius="md" p={4} />
</Stack>
);
}
Disabled
disabled renders the children in place instead, which is handy when the same component sometimes needs to float and sometimes not.
Rendered in place.
import { useState } from "react";
import { Box, Button, Portal, Stack, Text } from "@vjdesigns/vjdesigns-ui";
export function Disabled() {
const [inline, setInline] = useState(true);
const [target, setTarget] = useState<HTMLDivElement | null>(null);
return (
<Stack gap={4} style={{ width: "100%", maxWidth: "26rem" }}>
<Box border radius="md" p={4}>
<Portal container={target} disabled={inline}>
<Text size="sm" weight="semibold">
{inline ? "Rendered in place." : "Moved to the box below."}
</Text>
</Portal>
</Box>
<Box ref={setTarget} border="strong" radius="md" p={4} />
<Button variant="outline" tone="neutral" onClick={() => setInline((i) => !i)}>
{inline ? "Portal it" : "Render in place"}
</Button>
</Stack>
);
}
Translated
Content portaled out of a LocaleProvider keeps that provider's language, so screen readers keep pronouncing it in French even though it renders elsewhere.
Section en français.
import { useState } from "react";
import {
Box,
Portal,
Stack,
Text,
LocaleProvider,
} from "@vjdesigns/vjdesigns-ui";
import fr from "@vjdesigns/vjdesigns-ui/locales/fr";
export function Translated() {
const [target, setTarget] = useState<HTMLDivElement | null>(null);
return (
<Stack gap={4} style={{ width: "100%", maxWidth: "26rem" }}>
<LocaleProvider locale="fr" messages={fr}>
<Box border radius="md" p={4}>
<Text size="sm" tone="muted">
Section en français.
</Text>
<Portal container={target}>
<Text size="sm" weight="semibold">
Affiché ailleurs, toujours en français.
</Text>
</Portal>
</Box>
</LocaleProvider>
<Box ref={setTarget} border="strong" radius="md" p={4} />
</Stack>
);
}
Right to left
The first example again, inside a right-to-left LocaleProvider (Arabic). Layout, spacing and arrow icons mirror, and the components' built-in text is translated. The example's own text stays in English.
Into acontainer
The source: the portal is declared in here.
import { LocaleProvider } from "@vjdesigns/vjdesigns-ui";
import ar from "@vjdesigns/vjdesigns-ui/locales/ar";
// IntoAContainer is the "Into acontainer" example above.
<LocaleProvider locale="ar" messages={ar}>
<IntoAContainer />
</LocaleProvider>
Usage notes
On the client, children mount in the same commit as the Portal, so a parent's layout effects already see their refs. During server rendering (and the matching hydration pass) it renders nothing, then mounts.
Inside a Theme wrapper, the portaled content is wrapped in an element (display: contents, so layout is unaffected) carrying the same data-brand / data-accent / data-theme (and a custom brand's generated colors), so overlays keep their area's theme. Inside a LocaleProvider, it also carries lang and dir, so overlays keep their area's language.
API reference
Portal
| Prop | Type | Default |
|---|---|---|
childrenRequired | ReactNode | — |
containerElement to render into. Defaults to document.body. | HTMLElement | null | — |
disabledRender inline instead of portaling. | boolean | — |
See what changed in each version in the Changelog.
Accessibility report
Portal targets WCAG 2.2 AA. Below: a live axe-core audit of this page's examples, Lighthouse scores for the page in both modes, the accessibility behavior built into the component, and the checks only a person can do.
axe-core audit (live)
Checking the examples above…
Lighthouse audit
Light mode
Score 10035 passed · 0 failed · 30 not applicable · 10 to check by hand
Dark mode
Score 10035 passed · 0 failed · 30 not applicable · 10 to check by hand
Lighthouse 13.5.0 audits the whole docs page (header and sidebar included), not just the examples, in English. Last run 11/27/2026. Some failed audits carry no weight in the score, so a page can score 100 and still list one.
Built-in behavior
Keyboard
Not focusable, so there's no keyboard interaction of its own.
Roles and states
| Element | Role | States and properties |
|---|---|---|
| None of its own | none | Renders its children into document.body. |
Manual checks
Automated tools find roughly a third of accessibility issues. Test these in your app, with your content.
| Check | What to test | WCAG 2.2 |
|---|---|---|
| Keyboard | Every action works with Tab, Shift+Tab, Enter, Space and the arrow keys, and focus never gets stuck. | 2.1.1 (A), 2.1.2 (A) |
| Focus | Focus moves in a logical order, is always visible, and isn't hidden behind sticky headers. | 2.4.3 (A), 2.4.7 (AA), 2.4.11 (AA), 2.4.13 (AAA) |
| Screen readers | Names, roles and states are announced correctly, and status changes are announced without moving focus. | 1.3.1 (A), 4.1.2 (A), 4.1.3 (AA) |
| Color | Meaning never depends on color alone; your own colors meet contrast in both modes. | 1.4.1 (A), 1.4.3 (AA), 1.4.11 (AA), 1.4.6 (AAA) |
| Zoom and reflow | At 200% text size and at 320px wide (400% zoom), nothing is cut off and no sideways scrolling is needed. | 1.4.4 (AA), 1.4.10 (AA), 1.4.12 (AA) |
| Pointer targets | Click and tap targets are at least 24×24px (44×44px for AAA), or spaced so they don't overlap. | 2.5.8 (AA), 2.5.5 (AAA) |
| Motion | Animations respect the reduced-motion setting. | 2.3.3 (AAA) |