Skip to content

Commit

Permalink
[Dialog] fix eager closing (#2756)
Browse files Browse the repository at this point in the history
* replace Dialog's broken canOutsideClickClose with pointer-events

* add type="button" to close button

* remove obsolete test
  • Loading branch information
giladgray authored Aug 3, 2018
1 parent f3a45fb commit 1c1675b
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 29 deletions.
9 changes: 2 additions & 7 deletions packages/core/src/components/dialog/_dialog.scss
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,8 @@ $dialog-padding: $pt-grid-size * 2 !default;
justify-content: center;
width: 100%;
min-height: 100%;
pointer-events: none;
user-select: none;

// LEGACY: override old (<= 1.24.0) dialog styles when inside a container to respect flex layout
.#{$ns}-dialog {
// stylelint-disable-next-line declaration-no-important
position: static !important;
transform: none;
}
}

.#{$ns}-dialog {
Expand All @@ -76,6 +70,7 @@ $dialog-padding: $pt-grid-size * 2 !default;
background: $light-gray4;
width: $pt-grid-size * 50;
padding-bottom: $pt-grid-size * 2;
pointer-events: all;
user-select: text;

&:focus {
Expand Down
18 changes: 7 additions & 11 deletions packages/core/src/components/dialog/dialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import { AbstractPureComponent } from "../../common/abstractPureComponent";
import * as Classes from "../../common/classes";
import * as Errors from "../../common/errors";
import { DISPLAYNAME_PREFIX, IProps } from "../../common/props";
import { safeInvoke } from "../../common/utils";
import { H4 } from "../html/html";
import { Icon, IconName } from "../icon/icon";
import { IBackdropProps, IOverlayableProps, Overlay } from "../overlay/overlay";
Expand Down Expand Up @@ -73,7 +72,7 @@ export class Dialog extends AbstractPureComponent<IDialogProps, {}> {
public render() {
return (
<Overlay {...this.props} className={Classes.OVERLAY_SCROLL_CONTAINER} hasBackdrop={true}>
<div className={Classes.DIALOG_CONTAINER} onMouseDown={this.handleContainerMouseDown}>
<div className={Classes.DIALOG_CONTAINER}>
<div className={classNames(Classes.DIALOG, this.props.className)} style={this.props.style}>
{this.maybeRenderHeader()}
{this.props.children}
Expand All @@ -99,7 +98,12 @@ export class Dialog extends AbstractPureComponent<IDialogProps, {}> {
// this gives us a behavior as if the default value were `true`
if (this.props.isCloseButtonShown !== false) {
return (
<button aria-label="Close" className={Classes.DIALOG_CLOSE_BUTTON} onClick={this.props.onClose}>
<button
aria-label="Close"
className={Classes.DIALOG_CLOSE_BUTTON}
onClick={this.props.onClose}
type="button"
>
<Icon icon="small-cross" iconSize={Icon.SIZE_LARGE} />
</button>
);
Expand All @@ -121,12 +125,4 @@ export class Dialog extends AbstractPureComponent<IDialogProps, {}> {
</div>
);
}

private handleContainerMouseDown = (evt: React.MouseEvent<HTMLDivElement>) => {
// quick re-implementation of canOutsideClickClose because DIALOG_CONTAINER covers the backdrop
const isClickOutsideDialog = (evt.target as HTMLElement).closest(`.${Classes.DIALOG}`) == null;
if (isClickOutsideDialog && this.props.canOutsideClickClose) {
safeInvoke(this.props.onClose, evt);
}
};
}
11 changes: 0 additions & 11 deletions packages/core/test/dialog/dialogTests.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,6 @@ describe("<Dialog>", () => {
assert.isTrue(onClose.calledOnce);
});

it("attempts to close when dialog container element is moused down", () => {
const onClose = spy();
const dialog = mount(
<Dialog isOpen={true} onClose={onClose} usePortal={false}>
{createDialogContents()}
</Dialog>,
);
dialog.find(`.${Classes.DIALOG_CONTAINER}`).simulate("mousedown");
assert.isTrue(onClose.calledOnce);
});

it("doesn't close when canOutsideClickClose=false and overlay backdrop element is moused down", () => {
const onClose = spy();
const dialog = mount(
Expand Down

1 comment on commit 1c1675b

@blueprint-bot
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Dialog] fix eager closing (#2756)

Preview: documentation | landing | table

Please sign in to comment.