Skip to content

Commit

Permalink
Afterall, we can solve it :)
Browse files Browse the repository at this point in the history
  • Loading branch information
oliviertassinari committed Apr 5, 2020
1 parent a004b70 commit 435b0f7
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,3 @@ For instance, if you need to hide a menu dropdown when people click anywhere els

Notice that the component only accepts one child element.
You can find a more advanced demo on the [Menu documentation section](/components/menus/#menulist-composition).

## Limitations

If a page contains an element/component that gets removed from DOM when clicked, the `ClickAwayListener` will not be able to raise the `onClickAway` event due to a check trying to prevent raising the event for already removed `ClickAwayListener` children.

The workaround for this is to either use css/style to hide the clicked element or delay the removal of the element.
Check the example below

```jsx
// Instead of removing from DOM
{showButton && (<Button onClick={handleClick}>
Click to hide
</Button>)}

// Use styles prop
<Button style={showButton ? { display: 'block' } : { display: 'none' }} onClick={handleClick}>
Click to hide
</Button>)
```
16 changes: 13 additions & 3 deletions packages/material-ui/src/ClickAwayListener/ClickAwayListener.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,20 @@ const ClickAwayListener = React.forwardRef(function ClickAwayListener(props, ref
return;
}

// Multi window support
const doc = ownerDocument(nodeRef.current);
let insideDOM;

if (doc.documentElement.contains(event.target) && !nodeRef.current.contains(event.target)) {
// If not enough, can use https://github.com/DieterHolvoet/event-propagation-path/blob/master/propagationPath.js
if (event.composedPath) {
insideDOM = event.composedPath().indexOf(nodeRef.current) > -1;
} else {
const doc = ownerDocument(nodeRef.current);
// TODO v6 remove dead logic https://caniuse.com/#search=composedPath.
insideDOM =
!(doc.documentElement && doc.documentElement.contains(event.target)) ||
nodeRef.current.contains(event.target);
}

if (!insideDOM) {
onClickAway(event);
}
});
Expand Down
3 changes: 2 additions & 1 deletion packages/material-ui/src/Tooltip/Tooltip.js
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,7 @@ const Tooltip = React.forwardRef(function Tooltip(props, ref) {
...other,
...children.props,
className: clsx(other.className, children.props.className),
ref: handleRef,
};

const interactiveWrapperListeners = {};
Expand Down Expand Up @@ -502,7 +503,7 @@ const Tooltip = React.forwardRef(function Tooltip(props, ref) {

return (
<React.Fragment>
{React.cloneElement(children, { ref: handleRef, ...childrenProps })}
{React.cloneElement(children, childrenProps)}
<Popper
className={clsx(classes.popper, {
[classes.popperInteractive]: interactive,
Expand Down

0 comments on commit 435b0f7

Please sign in to comment.