Skip to content

Commit

Permalink
[fixed] Prevent unintended register and unregister
Browse files Browse the repository at this point in the history
- Fixed [#808](#808)
- Checking `this.props.isOpen` instead of `this.state.isOpen` in `componentWillUnmount`.
- Moved the call to `this.beforeOpen()` from the beginning of the `open` method to an else block.
- Set `this.closeTimer` without waiting until the end of the setState.
- Call `this.afterClose()` directly after setting the state in `closeWithoutTimeout`.
  • Loading branch information
honeymaro committed Feb 13, 2024
1 parent a275399 commit ea5aaf0
Showing 1 changed file with 14 additions and 17 deletions.
31 changes: 14 additions & 17 deletions src/components/ModalPortal.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ export default class ModalPortal extends Component {
}

componentWillUnmount() {
if (this.state.isOpen) {
if (this.props.isOpen) {
this.afterClose();
}
clearTimeout(this.closeTimer);
Expand Down Expand Up @@ -227,11 +227,11 @@ export default class ModalPortal extends Component {
};

open = () => {
this.beforeOpen();
if (this.state.afterOpen && this.state.beforeClose) {
clearTimeout(this.closeTimer);
this.setState({ beforeClose: false });
} else {
this.beforeOpen();
if (this.props.shouldFocusAfterRender) {
focusManager.setupScopedFocus(this.node);
focusManager.markForFocusLater();
Expand Down Expand Up @@ -268,24 +268,21 @@ export default class ModalPortal extends Component {

closeWithTimeout = () => {
const closesAt = Date.now() + this.props.closeTimeoutMS;
this.setState({ beforeClose: true, closesAt }, () => {
this.closeTimer = setTimeout(
this.closeWithoutTimeout,
this.state.closesAt - Date.now()
);
});
this.setState({ beforeClose: true, closesAt });
this.closeTimer = setTimeout(
this.closeWithoutTimeout,
this.props.closeTimeoutMS
);
};

closeWithoutTimeout = () => {
this.setState(
{
beforeClose: false,
isOpen: false,
afterOpen: false,
closesAt: null
},
this.afterClose
);
this.setState({
beforeClose: false,
isOpen: false,
afterOpen: false,
closesAt: null
});
this.afterClose();
};

handleKeyDown = event => {
Expand Down

0 comments on commit ea5aaf0

Please sign in to comment.