Skip to content

feat(Overlay): allow onBeforeDismiss to return a promise - #1372

Merged
mstijak merged 1 commit into
masterfrom
feat/overlay-async-before-dismiss
Nov 24, 2026
Merged

mstijak merged 1 commit into
masterfrom
feat/overlay-async-before-dismiss

Conversation

@mstijak

@mstijak mstijak commented Nov 24, 2026 •

Copy link
Copy Markdown
Member

onBeforeDismiss may return a promise, e.g. from MsgBox.yesNo, to ask before an overlay is dismissed. The overlay closes when the promise resolves to anything other than false. While the promise is pending, other attempts to dismiss the overlay, e.g. pressing Escape again, are ignored. If the overlay is closed in another way in the meantime, the result is ignored. onBeforeDismiss is also no longer called again for an overlay that is already closing, e.g. if dismiss is called twice in a row.

<Window
   visible={m.editor.visible}
   onBeforeDismiss={async (instance) =>
      !instance.store.get(m.editor.dirty) || (await MsgBox.yesNo("Discard unsaved changes?")) == "yes"
   }
>

Follows #1371. Synchronous results and overlayWillDismiss work as before.

Implementation

  • OverlayComponent.onBeforeDismiss returns a promise if onBeforeDismiss does. overlayWillDismiss and the rest of the preparation for dismissing (now in beginDismiss) run after the promise resolves, unless the overlay is unmounting. While a promise is pending, or once dismissing is allowed and the overlay is about to close (dismissed), the method returns false, so other dismiss attempts are ignored; with dismissOnFocusOut, opening a MsgBox from the window would otherwise dismiss it again, and a window with a bound visible stays mounted until the next render after visible is set to false.
  • Both places that dismiss an overlay, the dismiss of overlays with a bound visible and the function returned by open, use dismissIfAllowed, which waits for a promise and closes the overlay if it allows it.
  • A rejected promise keeps the overlay open; the error is not caught, so it is reported as an unhandled rejection.

Docs

The onBeforeDismiss rows of Window and Overlay mention promises.

Tests

New in Window.browser.spec.tsx:

  • closes the window when the promise resolves to true;
  • keeps the window open when it resolves to false, and can be dismissed again afterwards;
  • ignores other attempts to dismiss the window while the promise is pending (close button, Escape);
  • does nothing when the promise resolves after the window was closed otherwise;
  • is awaited by the dismiss function of a window opened with open.

In the synchronous tests: "is called once if the window is dismissed twice before it closes".

All but "does nothing when the promise resolves after the window was closed otherwise" fail without the change.

onBeforeDismiss may return a promise, e.g. from MsgBox.yesNo, to ask before an overlay is dismissed. The overlay closes when the promise resolves to anything other than false. While the promise is pending, other attempts to dismiss the overlay, e.g. pressing Escape again, are ignored. If the overlay is closed in another way in the meantime, the result is ignored. onBeforeDismiss is also no longer called again for an overlay that is already closing, e.g. if dismiss is called twice in a row.
@mstijak
mstijak force-pushed the feat/overlay-async-before-dismiss branch from 4f831a0 to cc1f364 Compare November 24, 2026 19:36
@mstijak
mstijak merged commit 519b3cf into master Nov 24, 2026
2 checks passed
@mstijak
mstijak deleted the feat/overlay-async-before-dismiss branch November 24, 2026 19:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant