Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions homedocs/src/pages/docs/layout/overlay.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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. |
Expand Down
1 change: 1 addition & 0 deletions homedocs/src/pages/docs/layout/window.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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. |
9 changes: 7 additions & 2 deletions packages/cx/src/widgets/overlay/Overlay.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
94 changes: 93 additions & 1 deletion packages/cx/src/widgets/overlay/Window.browser.spec.tsx
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -58,3 +60,93 @@ describe("Window", () => {
assert.equal(store.get(m.visible), false);
});
});

describe("Window onBeforeDismiss", () => {
const m = createModel<Model>();

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(
<cx>
<Window
visible={m.visible}
title="Edit"
closable
closeOnEscape
onBeforeDismiss={(instance) => {
calls.push(instance);
return allow;
}}
>
<input />
</Window>
</cx>,
{ store },
);

document.querySelector<HTMLInputElement>(".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(
<cx>
<div controller={PageController}>
<Window visible={m.visible} title="Edit" closable onBeforeDismiss="onBeforeDismiss">
Content
</Window>
</div>
</cx>,
{ 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: <cx>Content</cx>,
}).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();
});
});
Loading