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

Portal reopens if parent component sets its state immediately after .closePortal() #91

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 9 additions & 10 deletions lib/portal.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ export default class Portal extends React.Component {

constructor() {
super();
this.state = { active: false };
this.handleWrapperClick = this.handleWrapperClick.bind(this);
this.closePortal = this.closePortal.bind(this);
this.handleOutsideMouseClick = this.handleOutsideMouseClick.bind(this);
Expand Down Expand Up @@ -37,19 +36,19 @@ export default class Portal extends React.Component {
// portal's 'is open' state is handled through the prop isOpen
if (typeof newProps.isOpen !== 'undefined') {
if (newProps.isOpen) {
if (this.state.active) {
if (this.active) {
this.renderPortal(newProps);
} else {
this.openPortal(newProps);
}
}
if (!newProps.isOpen && this.state.active) {
if (!newProps.isOpen && this.active) {
this.closePortal();
}
}

// portal handles its own 'is open' state
if (typeof newProps.isOpen === 'undefined' && this.state.active) {
if (typeof newProps.isOpen === 'undefined' && this.active) {
this.renderPortal(newProps);
}
}
Expand All @@ -70,12 +69,12 @@ export default class Portal extends React.Component {
handleWrapperClick(e) {
e.preventDefault();
e.stopPropagation();
if (this.state.active) { return; }
if (this.active) { return; }
this.openPortal();
}

openPortal(props = this.props) {
this.setState({ active: true });
this.active = true;
this.renderPortal(props, true);
}

Expand All @@ -88,11 +87,11 @@ export default class Portal extends React.Component {
this.portal = null;
this.node = null;
if (isUnmounted !== true) {
this.setState({ active: false });
this.active = false;
}
};

if (this.state.active) {
if (this.active) {
if (this.props.beforeClose) {
this.props.beforeClose(this.node, resetPortalState);
} else {
Expand All @@ -104,7 +103,7 @@ export default class Portal extends React.Component {
}

handleOutsideMouseClick(e) {
if (!this.state.active) { return; }
if (!this.active) { return; }

const root = findDOMNode(this.portal);
if (root.contains(e.target) || (e.button && e.button !== 0)) { return; }
Expand All @@ -114,7 +113,7 @@ export default class Portal extends React.Component {
}

handleKeydown(e) {
if (e.keyCode === KEYCODES.ESCAPE && this.state.active) {
if (e.keyCode === KEYCODES.ESCAPE && this.active) {
this.closePortal();
}
}
Expand Down