Skip to content

Commit

Permalink
Portal should close with only a left button click
Browse files Browse the repository at this point in the history
When `closeOnOutsideClick` is set, an outside click will close only when
the button clicked was the main button (typically a left-button click)
rather than any mouse event button.
  • Loading branch information
Peter Jodogne committed Mar 7, 2016
1 parent b83034a commit f023691
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
2 changes: 1 addition & 1 deletion lib/portal.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ export default class Portal extends React.Component {
if (!this.state.active) { return; }

const root = findDOMNode(this.portal);
if (root.contains(e.target) || e.target.tagName === 'HTML') { return; }
if (root.contains(e.target) || e.target.tagName === 'HTML' || e.button !== 0) { return; }

e.stopPropagation();
this.closePortal();
Expand Down
11 changes: 9 additions & 2 deletions test/portal_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -199,8 +199,15 @@ describe('react-portal', () => {
it('closeOnOutsideClick', () => {
mount(<Portal closeOnOutsideClick isOpened><p>Hi</p></Portal>);
assert.equal(document.body.childElementCount, 1);
const mouseEvent = new window.MouseEvent('mousedown', {view: window});
document.dispatchEvent(mouseEvent);

// Should not close when outside click isn't a main click
const rightClickMouseEvent = new window.MouseEvent('mousedown', {view: window, button: 2});
document.dispatchEvent(rightClickMouseEvent);
assert.equal(document.body.childElementCount, 1);

// Should close when outside click is a main click (typically left button click)
const leftClickMouseEvent = new window.MouseEvent('mousedown', {view: window, button: 0});
document.dispatchEvent(leftClickMouseEvent);
assert.equal(document.body.childElementCount, 0);
});
});
Expand Down

0 comments on commit f023691

Please sign in to comment.