Overlay Prompt
Keeps a banner, a bar, or another floating element usable above an open dialog or sheet. Wrap one element that positions itself, such as a fixed banner.
When to use
- Keep a banner, a bar, or another floating element usable while a
Dialog,AlertDialog, orSheetis open, such as a cookie banner or a support widget. - Prefer
Toasterfor short notifications andSandbarfor a save bar. Both already useOverlayPrompt. - Put a control that belongs to the dialog's task inside the dialog content instead.
How it works
An open modal dialog makes the rest of the page inert: no click, focus, or screen reader reaches it. While a modal dialog is open, OverlayPrompt moves its content into the top one, at each depth of a stack. There the content paints above the dialog, takes clicks and focus, and screen readers reach it. A press in the content does not close the dialog. When the last modal closes, the content moves back to where you render it, so a form owns its buttons again and Tab reaches them in page order.
- No cost without a dialog. The component renders a wrapper with no box (
display: contents) and adds no listener. The move happens only when the top modal dialog changes. - Server rendering. The server renders the content in place, so the first paint and crawlers have it. After hydration the content renders through a portal: it mounts again one time when the page hydrates, and never when a dialog opens or closes.
- Frames and focus. In Chrome and Firefox the move keeps the state of the content, so a frame stays loaded and focus stays. Safari has no move that keeps state, so a frame in the content reloads when a dialog opens or closes.
- Forms. While a dialog is open, a submit button in the content is outside its form. Give the button a
formattribute with the form'sid, or submit from its click handler.Sandbar.SaveButtondoes this for you. - One element. The content moves as a whole and lays out as a child of the
<dialog>, so give it one element that positions itself withfixed.
Why a component and not a class: React removes and inserts the elements it renders in the parent where it put them, and it throws when one has moved. So mantle cannot move an element that your component renders. OverlayPrompt moves an element that React does not own, a portal's container, so the move is safe. The overlay-prompt class still marks an element whose presses must not close an open dialog, but it does not move the element.