From 47e33bb23571bc2b7d0484833abe762b5b186f9e Mon Sep 17 00:00:00 2001 From: Marko Stijak Date: Tue, 24 Nov 2026 20:21:59 +0100 Subject: [PATCH] feat(Overlay): call onBeforeDismiss before an overlay is dismissed Overlays such as Window and Dropdown call onBeforeDismiss(instance, component) before they are dismissed, e.g. with Escape, the close button or the function returned by open, and stay open if it returns false. It can also name a controller method. The option was declared in the types, but it was never called. Setting visible to false closes the overlay without calling it. --- homedocs/src/pages/docs/layout/overlay.mdx | 1 + homedocs/src/pages/docs/layout/window.mdx | 1 + packages/cx/src/widgets/overlay/Overlay.tsx | 9 +- .../widgets/overlay/Window.browser.spec.tsx | 94 ++++++++++++++++++- 4 files changed, 102 insertions(+), 3 deletions(-) diff --git a/homedocs/src/pages/docs/layout/overlay.mdx b/homedocs/src/pages/docs/layout/overlay.mdx index 31494ecc4..d894c4107 100644 --- a/homedocs/src/pages/docs/layout/overlay.mdx +++ b/homedocs/src/pages/docs/layout/overlay.mdx @@ -56,6 +56,7 @@ The `open` method returns a `close` function that can be called to dismiss the o | `closeOnEscape` | `boolean` | Closes the overlay when Escape key is pressed. | | `dismissOnFocusOut` | `boolean` | Dismisses the overlay when it loses focus. | | `dismissOnPopState` | `boolean` | Dismisses the overlay if the user presses the browser back button. | +| `onBeforeDismiss` | `function` | Called with `(instance, component)` before the overlay is dismissed. Return `false` to keep it open. Setting `visible` to `false` closes the overlay without calling it. | | `animate` | `boolean` | Appends the `animate` state after initial render for CSS animations. | | `destroyDelay` | `number` | Milliseconds to wait before removing from DOM (use with `animate`). | | `autoFocus` | `boolean` | Automatically focuses the overlay when shown. | diff --git a/homedocs/src/pages/docs/layout/window.mdx b/homedocs/src/pages/docs/layout/window.mdx index 9747908b2..197dacf8a 100644 --- a/homedocs/src/pages/docs/layout/window.mdx +++ b/homedocs/src/pages/docs/layout/window.mdx @@ -82,5 +82,6 @@ The factory returns a Promise that resolves when the window is closed, making it | `resizable` | `boolean` | Enables window resizing. | | `closable` | `boolean` | Controls close button visibility. Defaults to `true`. | | `closeOnEscape` | `boolean` | Closes the window when Escape key is pressed. | +| `onBeforeDismiss` | `function` | Called with `(instance, component)` before the window is dismissed, e.g. with Escape or the close button. Return `false` to keep it open, e.g. to ask whether unsaved changes should be discarded. | | `fixed` | `boolean` | Disables moving the window by dragging the header. | | `pad` | `boolean` | Adds default padding to the window body. | diff --git a/packages/cx/src/widgets/overlay/Overlay.tsx b/packages/cx/src/widgets/overlay/Overlay.tsx index 256569295..ef03aa3bc 100644 --- a/packages/cx/src/widgets/overlay/Overlay.tsx +++ b/packages/cx/src/widgets/overlay/Overlay.tsx @@ -118,8 +118,11 @@ export interface OverlayConfig extends StyledContainerConfig { /** Callback for key down event. */ onKeyDown?: string; - /** Callback fired before dismiss. */ - onBeforeDismiss?: string | (() => boolean); + /** + * Callback invoked before the overlay is dismissed, e.g. with Escape, the close button or the function returned + * by `open`. Return `false` to keep the overlay open. Setting `visible` to `false` closes the overlay without it. + */ + onBeforeDismiss?: string | ((instance: Instance, component: any) => boolean | void); /** Callback fired when overlay will dismiss. */ overlayWillDismiss?: (instance: Instance, component: any) => boolean; @@ -186,6 +189,7 @@ export class OverlayBase< declare onClick?: string; declare onMouseDown?: string; declare onBackdropClick?: string; + declare onBeforeDismiss?: string | ((instance: Instance, component: any) => boolean | void); declare overlayWillDismiss?: (instance: Instance, component: any) => boolean; declare style?: any; declare pad?: boolean; @@ -818,6 +822,7 @@ export class OverlayComponent< let { instance } = this.props; let { widget } = instance; + if (widget.onBeforeDismiss && instance.invoke("onBeforeDismiss", instance, this) === false) return false; if (widget.overlayWillDismiss && widget.overlayWillDismiss(instance, this) === false) return false; this.dismissed = true; diff --git a/packages/cx/src/widgets/overlay/Window.browser.spec.tsx b/packages/cx/src/widgets/overlay/Window.browser.spec.tsx index f1bc5f2b3..85111fb3a 100644 --- a/packages/cx/src/widgets/overlay/Window.browser.spec.tsx +++ b/packages/cx/src/widgets/overlay/Window.browser.spec.tsx @@ -1,6 +1,8 @@ -import { describe, it } from "vitest"; +import { describe, expect, it } from "vitest"; +import { userEvent } from "vitest/browser"; import { createModel } from "../../data/createAccessorModelProxy"; import { Store } from "../../data/Store"; +import { Instance } from "../../ui/Instance"; import { Controller } from "../../ui/Controller"; import { render, settle } from "../../util/test/render"; import { Window } from "./Window"; @@ -58,3 +60,93 @@ describe("Window", () => { assert.equal(store.get(m.visible), false); }); }); + +describe("Window onBeforeDismiss", () => { + const m = createModel(); + + function closeButton() { + return document.querySelector(".cxb-window button") as HTMLElement; + } + + it("keeps the window open while it returns false", async () => { + let allow = false; + let calls: Instance[] = []; + let store = new Store({ data: { visible: true } }); + await render( + + { + calls.push(instance); + return allow; + }} + > + + + , + { store }, + ); + + document.querySelector(".cxb-window input")!.focus(); + await settle(() => userEvent.keyboard("{Escape}")); + assert.equal(store.get(m.visible), true); + + await settle(() => userEvent.click(closeButton())); + assert.equal(store.get(m.visible), true); + + allow = true; + await settle(() => userEvent.click(closeButton())); + assert.equal(store.get(m.visible), false); + + assert.equal(calls.length, 3); + assert.ok(calls[0].widget instanceof Window); + }); + + it("calls a controller method given by name", async () => { + let calls = 0; + + class PageController extends Controller { + onBeforeDismiss() { + calls++; + return false; + } + } + + let store = new Store({ data: { visible: true } }); + await render( + +
+ + Content + +
+
, + { store }, + ); + + await settle(() => userEvent.click(closeButton())); + assert.equal(calls, 1); + assert.equal(store.get(m.visible), true); + }); + + it("is invoked by the dismiss function of a window opened with open", async () => { + let allow = false; + let dismiss = Window.create({ + title: "Opened", + onBeforeDismiss: () => allow, + items: Content, + }).open(new Store()); + await expect.poll(() => document.querySelector(".cxb-window")).toBeTruthy(); + + dismiss(); + await settle(); + assert.ok(document.querySelector(".cxb-window"), "the window was closed"); + + allow = true; + dismiss(); + await expect.poll(() => document.querySelector(".cxb-window")).toBeNull(); + }); +});