Skip to content

Commit

Permalink
[604] Use the ClickAwayListener to close the ContextMenu
Browse files Browse the repository at this point in the history
Bug: #604
Signed-off-by: Stéphane Bégaudeau <stephane.begaudeau@obeo.fr>
  • Loading branch information
sbegaudeau committed Jul 2, 2021
1 parent d4cf261 commit 256553f
Showing 1 changed file with 11 additions and 22 deletions.
33 changes: 11 additions & 22 deletions frontend/src/core/contextmenu/ContextMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
* Contributors:
* Obeo - initial API and implementation
*******************************************************************************/
import ClickAwayListener from '@material-ui/core/ClickAwayListener';
import { Text } from 'core/text/Text';
import PropTypes from 'prop-types';
import React, { useCallback, useEffect, useRef } from 'react';
Expand Down Expand Up @@ -62,21 +63,7 @@ const contextMenuPropTypes = {
export const ContextMenu = ({ x, y, caretPosition, children, onClose, 'data-testid': dataTestId }) => {
const domNode = useRef(null);

const onClickOutsideElement = useCallback(
(event) => {
if (domNode.current && !domNode.current.contains(event.target)) {
onClose();
}
},
[onClose]
);

useEffect(() => {
document.addEventListener('click', onClickOutsideElement, false);
return () => {
document.removeEventListener('click', onClickOutsideElement, false);
};
}, [onClickOutsideElement]);
const handleClickAway = () => onClose();

const onKeyDown = useCallback(
(event) => {
Expand Down Expand Up @@ -119,13 +106,15 @@ export const ContextMenu = ({ x, y, caretPosition, children, onClose, 'data-test
);
}
return (
<div className={styles.contextmenu} style={style}>
{caret}
<ul className={styles.menucontents} ref={domNode} data-testid={dataTestId}>
{children}
</ul>
<div />
</div>
<ClickAwayListener onClickAway={handleClickAway}>
<div className={styles.contextmenu} style={style}>
{caret}
<ul className={styles.menucontents} ref={domNode} data-testid={dataTestId}>
{children}
</ul>
<div />
</div>
</ClickAwayListener>
);
};
ContextMenu.propTypes = contextMenuPropTypes;

0 comments on commit 256553f

Please sign in to comment.