Skip to content
Merged
Show file tree
Hide file tree
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
38 changes: 29 additions & 9 deletions packages/menus/src/containers/MenuContainer.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,15 +151,15 @@ class MenuContainer extends ControlledComponent {
}

componentDidMount() {
const doc = getDocument ? getDocument(this.props) : document;

doc.addEventListener('mousedown', this.handleOutsideMouseDown);
this.getDocuments().forEach(doc => {
doc.addEventListener('mousedown', this.handleOutsideMouseDown);
});
}

componentWillUnmount() {
const doc = getDocument ? getDocument(this.props) : document;

doc.removeEventListener('mousedown', this.handleOutsideMouseDown);
this.getDocuments().forEach(doc => {
doc.removeEventListener('mousedown', this.handleOutsideMouseDown);
});
}

componentDidUpdate(prevProps, prevState) {
Expand Down Expand Up @@ -195,18 +195,38 @@ class MenuContainer extends ControlledComponent {
}
}

getDocuments = () => {
const doc = getDocument ? getDocument(this.props) : document;
const iframes = doc.querySelectorAll('iframe');

return [...iframes].reduce(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be good, but lets check that this is transpiled into IE11 compatible syntax.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah babel handles this fine transforms it to:

return [].concat(_toConsumableArray(iframes)

Tested this already in all browsers in the checkbox marked in the description. So IE11 and edge are all good.

(documents, iframe) => {
try {
if (iframe.contentDocument) {
return [...documents, iframe.contentDocument];
}
} catch (e) {} // eslint-disable-line no-empty

return documents;
},
[doc]
);
};

/**
* Used to know when a Menu is blured by mouse
*/
handleOutsideMouseDown = event => {
const { isOpen } = this.getControlledState();
const { target, which } = event;
const isLeftClick = which === 1;

if (!isOpen) {
if (!isOpen || !isLeftClick) {
return;
}

if (this.menuReference && !this.menuReference.contains(event.target)) {
if (this.triggerReference && !this.triggerReference.contains(event.target)) {
if (this.menuReference && !this.menuReference.contains(target)) {
if (this.triggerReference && !this.triggerReference.contains(target)) {
this.toggleMenuVisibility({ closedByBlur: true });
}
}
Expand Down
4 changes: 2 additions & 2 deletions packages/menus/src/containers/MenuContainer.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ describe('MenuContainer', () => {
});
});

it('closes menu if blured by click', () => {
it('closes menu if blurred by click', () => {
/**
* Enzyme doesn't attach mount/shallow wrapper to the document by default.
* This allows us to test the blur events correctly.
Expand All @@ -247,7 +247,7 @@ describe('MenuContainer', () => {
});
findTrigger(wrapper).simulate('click');
jest.runOnlyPendingTimers();
document.dispatchEvent(new Event('mousedown'));
document.dispatchEvent(new MouseEvent('mousedown', { which: 1 }));
wrapper.update();

expect(findMenu(wrapper)).not.toExist();
Expand Down