Skip to content

Custom overlays

Custom overlays

This page documents the hook useModalLayer.

Everything a modal surface (Dialog, Drawer) needs, composed in the order that works:

1. useDismiss({ kind: 'modal' }) — Escape and outside presses (the backdrop is outside the content surface, so a click on it dismisses; a drag from the content does not).
2. useFocusTrap — initial focus, Tab cycle incl. toasts and descendant layers.
3. useModalIsolation — the rest of the page is inert (instead of aria-modal).
4. useScrollLock — ref-counted page scroll lock.
5. useRestoreFocus — the opener (or triggerRef/finalFocusRef) gets focus back.

On close the trap is released before the isolation is lifted, and the isolation is lifted before focus is restored (an inert opener cannot take focus). Wrap the surface in Portal layerId={layerId} so layers opened inside the modal become its descendants.

modalType: 'alert' composes the same with outsidePress: false. modalType: 'non-modal' registers a dialog layer (Escape only from inside its tree, no outside press, no focus-outside dismissal unless lightDismiss asks for them), keeps the trap, the isolation and the scroll lock off, moves focus in on open with the trap's initial-focus rule (useInitialFocus) and restores focus only when it is inside the surface (or in a layer opened from it) at close, or was lost to <body> from there. Every hook is always called and keys on open: a modalType that changes while open applies at once (the trap, isolation and lock follow). It leaves focus inside the surface where it is; focus outside it moves in, as on open: a switch to modal or alert starts the trap (the page turns inert), and a switch to non-modal runs the initial focus (focus in the toast region, which a trap lets in, moves into the surface). The Tab order of a non-modal surface (its place after the opener) is the component's.

A surface that mounts through the presence core (Dialog, Drawer) passes its open state as open, never its mount (isMounted), and its element as container also while it is mounted closed: the layer, the trap, the isolation and the scroll lock end and focus is restored in the commit that closes it, while the element still runs its exit motion (inert) or stays mounted (hidden, with unmountOnClose={false}); the next open starts them again with the same element.

Examples

Popup on the public hooks

This example opens a dialog or panel. Use the button to show it.

export const PopupOnPublicHooks: Story = {
  name: 'Popup on the public hooks',
  render: () => (
    <Toaster>
      <div className="p-8">
        <NotifyButton />{' '}
        <CustomPopup label="Details">
          <Popover>
            <Popover.Trigger>
              <Button>More</Button>
            </Popover.Trigger>
            <Popover.Content aria-label="More details">Escape closes this first.</Popover.Content>
          </Popover>
        </CustomPopup>
      </div>
    </Toaster>
  ),
};

Fallback placements and hide when detached

This example opens a dialog or panel. Use the button to show it.

export const FlipAndHideWhenDetached: Story = {
  name: 'Fallback placements and hide when detached',
  render: () => (
    <div
      className="h-64 overflow-auto rounded-md border border-wave-border p-4"
      role="region"
      aria-label="Scrolling page"
      tabIndex={0}
    >
      <div className="h-24" />
      <CustomPopup
        label="Anchored"
        positioning={{ fallbackPlacements: [{ side: 'end' }, { side: 'top' }] }}
        hideWhenDetached
      >
        Scroll the page: this popup hides while its button is out of view.
      </CustomPopup>
      <div className="h-[60rem]" />
    </div>
  ),
};

Popover fitted to the viewport

This example opens a dialog or panel. Use the button to show it.

export const FitViewportPopover: Story = {
  name: 'Popover fitted to the viewport',
  render: () => (
    <div className="flex h-72 items-end p-4">
      <Popover defaultOpen positioning={{ fitViewport: true }}>
        <Popover.Trigger>
          <Button>Terms</Button>
        </Popover.Trigger>
        <Popover.Content title="Terms">
          {Array.from({ length: 24 }, (_, index) => (
            <p key={index}>Clause {index + 1}: the long text scrolls inside the popover.</p>
          ))}
        </Popover.Content>
      </Popover>
    </div>
  ),
};

Command palette over a Dialog

This example opens a dialog or panel. Use the button to show it.

export const PaletteOverDialog: Story = {
  name: 'Command palette over a Dialog',
  render: () => {
    function Example() {
      const [paletteOpen, setPaletteOpen] = React.useState(false);
      const openerRef = React.useRef<HTMLButtonElement>(null);
      return (
        <Toaster>
          <div className="p-8">
            <Dialog defaultOpen>
              <Dialog.Content title="Project">
                <Button ref={openerRef} onClick={() => setPaletteOpen(true)}>
                  Open palette
                </Button>{' '}
                <NotifyButton />
              </Dialog.Content>
            </Dialog>
            <CommandPalette
              open={paletteOpen}
              onOpenChange={setPaletteOpen}
              triggerRef={openerRef}
            />
            <AppStatusRegion />
          </div>
        </Toaster>
      );
    }
    return <Example />;
  },
};

Grid roving with columns

This example opens a dialog or panel. Use the button to show it.

export const GridRoving: Story = {
  name: 'Grid roving with columns',
  render: () => {
    function Grid() {
      const { containerProps, getTabIndex } = useRovingTabIndex({
        orientation: 'grid',
        columns: 4,
        loop: false,
      });
      return (
        <div
          role="group"
          aria-label="Cells in a grid of four columns"
          className="grid w-80 grid-cols-4 gap-2 p-4"
          {...containerProps}
        >
          {CELLS.map((name) => (
            <Button key={name} size="small" data-roving-value={name} tabIndex={getTabIndex(name)}>
              {name.replace('Cell ', '')}
            </Button>
          ))}
        </div>
      );
    }
    return <Grid />;
  },
};

Focusable group rows

This example opens a dialog or panel. Use the button to show it.

export const FocusableGroupRows: Story = {
  name: 'Focusable group rows',
  render: () => {
    function Row({ title }: { title: string }) {
      const { groupProps } = useFocusableGroup({ tabBehavior: 'limited' });
      return (
        <div
          role="group"
          aria-label={title}
          className="flex items-center gap-2 rounded-md border border-wave-border p-2"
          {...groupProps}
        >
          <span className="grow">{title}</span>
          <Button size="small">Open</Button>
          <Button size="small">Share</Button>
        </div>
      );
    }
    return (
      <div className="p-4">
        <Button>Before the list</Button>
        <div role="region" aria-label="Documents" className="my-2 flex flex-col gap-2">
          {ROWS.map((title) => (
            <Row key={title} title={title} />
          ))}
        </div>
        <Button>After the list</Button>
      </div>
    );
  },
};

Keyboard mode

This example opens a dialog or panel. Use the button to show it.

export const KeyboardMode: Story = {
  name: 'Keyboard mode',
  render: () => {
    function Indicator() {
      const keyboard = useFocusVisible();
      return (
        <p role="status" className="p-4">
          Input mode: {keyboard ? 'keyboard' : 'pointer'}
        </p>
      );
    }
    return <Indicator />;
  },
};