Skip to Content
Neshel-UI
Guides

Overlays and portals

How dialogs, sheets, popovers, menus and selection popups layer, keep themes across portals, and when you need a container.

Overlay contents — DialogContent, AlertDialogContent, SheetContent, PopoverContent, DropdownMenuContent, TooltipContent and the popups of Select, MultiSelect and Combobox — render through a portal into document.body by default, so they escape overflow: hidden and stacking contexts.

Themes survive portals

A portal leaves the DOM subtree of its trigger, and with it any data-neshel-theme scope. Neshel fixes this: the content is wrapped in <div data-neshel-portal data-neshel-theme="…"> (with display: contents) carrying the theme of the trigger's nearest data-neshel-theme ancestor. A dialog opened from a dark sidebar inside a light page is dark.

Inline custom properties need a portal container

The theme attribute is copied — inline custom properties are not. If you override tokens or component properties inline on an ancestor, a portalled overlay does not see them:

<section style={{ "--neshel-primary": "#b42318" } as React.CSSProperties}>
  {/* The Button sees the override; a portalled dialog would not. */}
  <Button>Red primary</Button>
</section>

Pass container so the portal renders inside the element that carries the properties:

branded-section.tsx
"use client";
import {
  Button,
  Dialog,
  DialogContent,
  DialogTitle,
  DialogTrigger,
} from "@neshel-ui/components";
import { type CSSProperties, useState } from "react";

export function BrandedSection() {
  const [container, setContainer] = useState<HTMLElement | null>(null);
  return (
    <section ref={setContainer} style={{ "--neshel-primary": "#b42318" } as CSSProperties}>
      <Dialog>
        <DialogTrigger asChild>
          <Button>Open</Button>
        </DialogTrigger>
        <DialogContent container={container} aria-describedby={undefined}>
          <DialogTitle>Uses the section's red primary</DialogTitle>
        </DialogContent>
      </Dialog>
    </section>
  );
}

Every overlay content and every selection control accepts container. Prefer named themes in neshel.config.ts (copied automatically) over inline overrides when you can.

Layering and Escape

  • z-index: dialogs, sheets and backdrops use var(--neshel-z-overlay, 50); popovers, menus, tooltips and selection popups use var(--neshel-z-popover, 60).
  • Escape closes the innermost layer only. A selection popup inside a dialog closes first; the next Escape closes the dialog. The same applies to a Combobox inside a Popover.
  • Selection popups opened inside a dialog, sheet or popover render inside that overlay (so focus trapping and outside-press detection treat them as part of it).
Preview: popover-combobox
Preview theme
popover-combobox.tsx
"use client";
import {
  Button,
  Combobox,
  Label,
  Popover,
  PopoverContent,
  PopoverTrigger,
} from "@neshel-ui/components";
import { Stack } from "@neshel-ui/layout";
import { useState } from "react";

const assignees = [
  { value: "ana", label: "Ana Rojas" },
  { value: "luis", label: "Luis Mora" },
  { value: "mei", label: "Mei Tanaka" },
  { value: "sam", label: "Sam Ortiz" },
];

/** A Combobox inside a Popover: Escape closes the listbox first, then the popover. */
export default function PopoverCombobox() {
  const [assignee, setAssignee] = useState<string | null>(null);
  const name = assignees.find((item) => item.value === assignee)?.label;
  return (
    <Popover>
      <PopoverTrigger asChild>
        <Button variant="outline">{name ? `Assigned to ${name}` : "Assign"}</Button>
      </PopoverTrigger>
      <PopoverContent align="start">
        <Stack space="2" className="w-64">
          <Label htmlFor="popover-assignee">Assignee</Label>
          <Combobox
            id="popover-assignee"
            items={assignees}
            value={assignee}
            onValueChange={setAssignee}
            placeholder="Search people"
          />
        </Stack>
      </PopoverContent>
    </Popover>
  );
}

Nested layers

For Dialog, Sheet, MobileMenu, AlertDialog and Popover:

  • One Escape closes one layer: the topmost open one, following the React tree through portals.
  • Only that layer's onEscapeKeyDown runs, once. event.preventDefault() in it keeps every layer open.
  • Focus returns to the trigger of the layer that closed.
  • A controlled layer that ignores onOpenChange(false) stays open, and so do the layers below it.

DropdownMenu, Tooltip and NavigationMenu keep Radix's own handling, as do Radix layers you add yourself.

In 0.1.0

With very fast input, one Escape could close two nested dialogs in the published 0.1.0. The rules above are on the main branch and will be in the next release.

Toasts and overlays

A toast never moves focus, so it does not close or interrupt an open dialog. Escape closes a toast only while focus is inside it. Toast is not in 0.1.0 yet.

Backdrop

The modal backdrop colour is var(--neshel-overlay-backdrop) when you set it, otherwise a mix of the theme's colours: the foreground at 45 % in light contexts, the background at 75 % in dark contexts (via CSS light-dark()). Browsers without light-dark() get an equivalent fallback for the built-in light, dark and system themes; for a named dark theme in those browsers set --neshel-overlay-backdrop explicitly.

Focus

Focus moves into an opened dialog or sheet, is trapped while it is open, and returns to the trigger (or to the element that opened a controlled dialog) when it closes. Popovers and menus return focus to their trigger. See each component's Keyboard section.

Edit on GitHub

On this page