Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BUGFIX: Limit click-outside handling for dialogs to their semi-transparent overlay #3492

Merged
merged 6 commits into from
Jul 4, 2023
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,11 @@ exports[`<Dialog/> Portal should render correctly. 1`] = `
<section
autoFocus={true}
className="dialogClassName wideClassName errorClassName"
onClick={[Function]}
role="dialog"
tabIndex={0}
>
<clickOutside(DialogWithoutEscape)
<DialogWithoutEscape
actions={
Array [
"Foo 1",
Expand Down Expand Up @@ -79,7 +80,7 @@ exports[`<Dialog/> Portal should render correctly. 1`] = `
type="error"
>
Foo children
</clickOutside(DialogWithoutEscape)>
</DialogWithoutEscape>
</section>
</Portal>
</CloseOnEscape>
Expand Down
17 changes: 8 additions & 9 deletions packages/react-ui-components/src/Dialog/dialog.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import mergeClassNames from 'classnames';
import React, {PureComponent, ReactNode} from 'react';
import enhanceWithClickOutside from '../enhanceWithClickOutside/index';
import CloseOnEscape from 'react-close-on-escape';
import {Portal} from 'react-portal';

Expand Down Expand Up @@ -129,10 +128,6 @@ export class DialogWithoutEscape extends PureComponent<DialogProps> {
this.ref = ref;
}

public readonly handleClickOutside = () => {
this.props.onRequestClose();
}

public readonly componentDidMount = (): void => {
document.addEventListener('keydown', (event : KeyboardEvent) => this.handleKeyPress(event));
const {autoFocus} = this.props;
Expand All @@ -158,8 +153,6 @@ export class DialogWithoutEscape extends PureComponent<DialogProps> {
}
}

const EnhancedDialogWithoutEscapeWithClickOutside = enhanceWithClickOutside(DialogWithoutEscape);

// tslint:disable-next-line:max-classes-per-file
class DialogWithEscape extends PureComponent<DialogProps> {
public render(): JSX.Element | null {
Expand Down Expand Up @@ -198,8 +191,8 @@ class DialogWithEscape extends PureComponent<DialogProps> {
return (
<CloseOnEscape onEscape={this.onEscape}>
<Portal>
<section {...rest} className={sectionClassName} role="dialog" tabIndex={0}>
<EnhancedDialogWithoutEscapeWithClickOutside {...this.props}/>
<section {...rest} className={sectionClassName} role="dialog" tabIndex={0} onClick={this.handleOverlayClick}>
<DialogWithoutEscape {...this.props}/>
</section>
</Portal>
</CloseOnEscape>
Expand All @@ -209,6 +202,12 @@ class DialogWithEscape extends PureComponent<DialogProps> {
private readonly onEscape = () => {
this.props.onRequestClose();
}

private readonly handleOverlayClick = (ev: React.MouseEvent) => {
if (ev.target === ev.currentTarget) {
this.props.onRequestClose();
}
}
}

export default DialogWithEscape;