ModalProvider

ModalProvider renders one modal system into a portal.

<ModalProvider system={modals} />

Props

system

The instance returned by createModalSystem().

defaultOptions

Shared render and interaction defaults. Definition defaults and per-open options override these values.

<ModalProvider
  system={modals}
  defaultOptions={{
    closeOnPressEsc: true,
    closeOnClickOutside: true,
    backdrop: {
      className: 'app-modal-backdrop',
    },
  }}
/>

portalRoot

An optional portal target. It defaults to document.body.

<ModalProvider system={modals} portalRoot={overlayRoot} />

Lifecycle behavior

  • A modal opened before mount renders when the provider becomes available.
  • Escape and backdrop cancellation use distinct outcome reasons.
  • Only the top modal is interactive and exposed as aria-modal; covered and exiting layers are inert, so they cannot receive focus or clicks while an exit animation finishes.
  • Focus moves into the top dialog on open, and closing a layer restores the exact element that was focused before it opened (for example the trigger of the last modal) whenever that element is still in the document.
  • Body scroll is locked while the stack is non-empty. The inline overflow value seen on the first lock is restored once the last lock releases.
  • Browser Back and Forward navigation cancels the full stack with programmatic.
  • Unmounting the last provider cancels pending operations with provider-unmounted.
  • React Strict Mode remounts do not cancel a live operation.

Multiple providers on one document

Several providers (for example micro-frontends or embedded tools) can share one document. The library coordinates their side effects per document:

  • Body scroll stays locked until every provider with open modals closes them, regardless of the closing or unmounting order. While locked, the body's overscroll-behavior is set to none, and the owning provider cancels touch drags that start outside its stack (mobile Safari keeps scrolling a overflow: hidden body otherwise). Dialog bodies set overscroll-behavior: contain so their internal scrolling never chains to the page.
  • The provider that opened the most recent modal owns Escape and Tab. Other providers keep their stacks untouched, and a top modal with closeOnPressEsc: false blocks Escape for every provider.
  • Focus that escapes the stack programmatically (application code focusing a background element while a modal is open) is pulled back into the top dialog, restoring the element that was focused last inside it.
  • The most recent provider also renders on top: each claim moves the provider's portal container above every other provider's, so Escape, Tab and pointer order match the visual order.
  • When the owning provider unmounts or empties its stack, keyboard ownership returns to the remaining provider.
  • Providers portaled into another document (for example inside an iframe) lock and listen within that document only.