Skip to content

Commit

Permalink
overlay: stop propagation on escape keydown event
Browse files Browse the repository at this point in the history
This adds a call to e.stopPropagation() to prevent nested overlays
from closing on a single key press.

Fixes: #5099
  • Loading branch information
dlech committed Jun 3, 2022
1 parent 268ab68 commit 83778a0
Show file tree
Hide file tree
Showing 9 changed files with 15 additions and 17 deletions.
2 changes: 2 additions & 0 deletions packages/core/src/components/overlay/overlay.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -659,6 +659,8 @@ export class Overlay extends AbstractPureComponent2<OverlayProps, IOverlayState>
const { canEscapeKeyClose, onClose } = this.props;
if (e.key === "Escape" && canEscapeKeyClose) {
onClose?.(e);
// prevent other overlays from closing
e.stopPropagation();
// prevent browser-specific escape key behavior (Safari exits fullscreen)
e.preventDefault();
}
Expand Down
6 changes: 3 additions & 3 deletions packages/core/test/alert/alertTests.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { mount, shallow, ShallowWrapper } from "enzyme";
import * as React from "react";
import { SinonStub, spy, stub } from "sinon";

import { Alert, Button, Classes, IAlertProps, IButtonProps, Icon, Intent, Keys } from "../../src";
import { Alert, Button, Classes, IAlertProps, IButtonProps, Icon, Intent } from "../../src";
import * as Errors from "../../src/common/errors";
import { findInPortal } from "../utils";

Expand Down Expand Up @@ -175,11 +175,11 @@ describe("<Alert>", () => {
);
const overlay = findInPortal(alert, "." + Classes.OVERLAY).first();

overlay.simulate("keydown", { which: Keys.ESCAPE });
overlay.simulate("keydown", { key: "Escape" });
assert.isTrue(onCancel.notCalled);

alert.setProps({ canEscapeKeyCancel: true });
overlay.simulate("keydown", { which: Keys.ESCAPE });
overlay.simulate("keydown", { key: "Escape" });
assert.isTrue(onCancel.calledOnce);

alert.unmount();
Expand Down
3 changes: 1 addition & 2 deletions packages/core/test/dialog/dialogTests.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import * as React from "react";
import { spy } from "sinon";

import { Button, Classes, Dialog, DialogProps, H4, Icon, IconSize } from "../../src";
import * as Keys from "../../src/common/keys";

describe("<Dialog>", () => {
it("renders its content correctly", () => {
Expand Down Expand Up @@ -93,7 +92,7 @@ describe("<Dialog>", () => {
{createDialogContents()}
</Dialog>,
);
dialog.simulate("keydown", { which: Keys.ESCAPE });
dialog.simulate("keydown", { key: "Escape" });
assert.isTrue(onClose.notCalled);
});

Expand Down
3 changes: 1 addition & 2 deletions packages/core/test/drawer/drawerTests.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import * as React from "react";
import { spy } from "sinon";

import { Button, Classes, Drawer, DrawerProps, Position } from "../../src";
import * as Keys from "../../src/common/keys";

describe("<Drawer>", () => {
let drawer: ReactWrapper<DrawerProps, any>;
Expand Down Expand Up @@ -197,7 +196,7 @@ describe("<Drawer>", () => {
{createDrawerContents()}
</Drawer>,
);
drawer.simulate("keydown", { which: Keys.ESCAPE });
drawer.simulate("keydown", { key: "Escape" });
assert.isTrue(onClose.notCalled);
});

Expand Down
5 changes: 2 additions & 3 deletions packages/core/test/overlay/overlayTests.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import { spy } from "sinon";
import { dispatchMouseEvent } from "@blueprintjs/test-commons";

import { Classes, Overlay, OverlayProps, Portal, Utils } from "../../src";
import * as Keys from "../../src/common/keys";
import { findInPortal } from "../utils";

const BACKDROP_SELECTOR = `.${Classes.OVERLAY_BACKDROP}`;
Expand Down Expand Up @@ -224,7 +223,7 @@ describe("<Overlay>", () => {
{createOverlayContents()}
</Overlay>,
);
wrapper.simulate("keydown", { which: Keys.ESCAPE });
wrapper.simulate("keydown", { key: "Escape" });
assert.isTrue(onClose.calledOnce);
});

Expand All @@ -235,7 +234,7 @@ describe("<Overlay>", () => {
{createOverlayContents()}
</Overlay>,
);
overlay.simulate("keydown", { which: Keys.ESCAPE });
overlay.simulate("keydown", { key: "Escape" });
assert.isTrue(onClose.notCalled);
overlay.unmount();
});
Expand Down
3 changes: 1 addition & 2 deletions packages/core/test/popover/popoverTests.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import { dispatchMouseEvent } from "@blueprintjs/test-commons";
import { Portal } from "../../src";
import * as Classes from "../../src/common/classes";
import * as Errors from "../../src/common/errors";
import * as Keys from "../../src/common/keys";
import { Overlay } from "../../src/components/overlay/overlay";
import { IPopoverProps, IPopoverState, Popover, PopoverInteractionKind } from "../../src/components/popover/popover";
import { PopoverArrow } from "../../src/components/popover/popoverArrow";
Expand Down Expand Up @@ -833,8 +832,8 @@ describe("<Popover>", () => {
};
wrapper.sendEscapeKey = () => {
wrapper!.findClass(Classes.OVERLAY_OPEN).simulate("keydown", {
key: "Escape",
nativeEvent: new KeyboardEvent("keydown"),
which: Keys.ESCAPE,
});
return wrapper!;
};
Expand Down
2 changes: 1 addition & 1 deletion packages/datetime/test/dateInputTests.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ describe("<DateInput>", () => {
wrapper.setState({ isOpen: true });
/* eslint-disable-next-line deprecation/deprecation */
assert.isTrue(wrapper.find(Popover).prop("isOpen"));
wrapper.find("input").simulate("keydown", { which: Keys.ESCAPE });
wrapper.find("input").simulate("keydown", { key: "Escape" });
/* eslint-disable-next-line deprecation/deprecation */
assert.isFalse(wrapper.find(Popover).prop("isOpen"));
});
Expand Down
4 changes: 2 additions & 2 deletions packages/popover2/test/contextMenu2Tests.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import { mount, ReactWrapper } from "enzyme";
import * as React from "react";
import { spy } from "sinon";

import { Classes as CoreClasses, Keys, Menu, MenuItem } from "@blueprintjs/core";
import { Classes as CoreClasses, Menu, MenuItem } from "@blueprintjs/core";

import {
Classes,
Expand Down Expand Up @@ -80,8 +80,8 @@ describe("ContextMenu2", () => {
.find(`.${CoreClasses.OVERLAY_OPEN}`)
.hostNodes()
.simulate("keydown", {
key: "Escape",
nativeEvent: new KeyboardEvent("keydown"),
which: Keys.ESCAPE,
});
assert.isFalse(ctxMenu.find(Popover2).prop("isOpen"));
});
Expand Down
4 changes: 2 additions & 2 deletions packages/popover2/test/popover2Tests.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { mount, ReactWrapper, shallow } from "enzyme";
import * as React from "react";
import * as sinon from "sinon";

import { Classes as CoreClasses, Keys, Menu, MenuItem, Overlay, Portal } from "@blueprintjs/core";
import { Classes as CoreClasses, Menu, MenuItem, Overlay, Portal } from "@blueprintjs/core";
import { dispatchMouseEvent } from "@blueprintjs/test-commons";

import { Classes, Errors } from "../src";
Expand Down Expand Up @@ -832,8 +832,8 @@ describe("<Popover2>", () => {
};
wrapper.sendEscapeKey = () => {
wrapper!.findClass(CoreClasses.OVERLAY_OPEN).simulate("keydown", {
key: "Escape",
nativeEvent: new KeyboardEvent("keydown"),
which: Keys.ESCAPE,
});
return wrapper!;
};
Expand Down

0 comments on commit 83778a0

Please sign in to comment.