Skip to content

Commit

Permalink
BUGFIX: Limit click-outside handling for dialogs to their semi-transp…
Browse files Browse the repository at this point in the history
…arent overlay
  • Loading branch information
grebaldi committed May 12, 2023
1 parent ef72c79 commit bfd2024
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 11 deletions.
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;

0 comments on commit bfd2024

Please sign in to comment.