Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
51 changes: 51 additions & 0 deletions docs/src/pages/components/click-away-listener/LeadingClickAway.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import React from 'react';
import { makeStyles } from '@material-ui/core/styles';
import ClickAwayListener from '@material-ui/core/ClickAwayListener';

const useStyles = makeStyles((theme) => ({
root: {
position: 'relative',
},
dropdown: {
position: 'absolute',
top: 28,
right: 0,
left: 0,
zIndex: 1,
border: '1px solid',
padding: theme.spacing(1),
backgroundColor: theme.palette.background.paper,
},
}));

export default function LeadingClickAway() {
const classes = useStyles();
const [open, setOpen] = React.useState(false);

const handleClick = () => {
setOpen((prev) => !prev);
};

const handleClickAway = () => {
setOpen(false);
};

return (
<ClickAwayListener
mouseEvent="onMouseDown"
touchEvent="onTouchStart"
onClickAway={handleClickAway}
>
<div className={classes.root}>
<button type="button" onClick={handleClick}>
Open menu dropdown
</button>
{open ? (
<div className={classes.dropdown}>
Click me, I will stay visible until you click outside.
</div>
) : null}
</div>
</ClickAwayListener>
);
}
53 changes: 53 additions & 0 deletions docs/src/pages/components/click-away-listener/LeadingClickAway.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import React from 'react';
import { makeStyles, createStyles, Theme } from '@material-ui/core/styles';
import ClickAwayListener from '@material-ui/core/ClickAwayListener';

const useStyles = makeStyles((theme: Theme) =>
createStyles({
root: {
position: 'relative',
},
dropdown: {
position: 'absolute',
top: 28,
right: 0,
left: 0,
zIndex: 1,
border: '1px solid',
padding: theme.spacing(1),
backgroundColor: theme.palette.background.paper,
},
}),
);

export default function LeadingClickAway() {
const classes = useStyles();
const [open, setOpen] = React.useState(false);

const handleClick = () => {
setOpen((prev) => !prev);
};

const handleClickAway = () => {
setOpen(false);
};

return (
<ClickAwayListener
mouseEvent="onMouseDown"
touchEvent="onTouchStart"
onClickAway={handleClickAway}
>
<div className={classes.root}>
<button type="button" onClick={handleClick}>
Open menu dropdown
</button>
{open ? (
<div className={classes.dropdown}>
Click me, I will stay visible until you click outside.
</div>
) : null}
</div>
</ClickAwayListener>
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,10 @@ You can find a more advanced demo on the [Menu documentation section](/component
The following demo uses [`Portal`](/components/portal/) to render the dropdown into a new "subtree" outside of current DOM hierarchy.

{{"demo": "pages/components/click-away-listener/PortalClickAway.js"}}

## Leading edge

By default, the component responds to the trailing events (click + touch end).
However, you can configure it to respond to the leading events (mouse down + touch start).

{{"demo": "pages/components/click-away-listener/LeadingClickAway.js"}}
10 changes: 9 additions & 1 deletion packages/material-ui/src/ClickAwayListener/ClickAwayListener.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@ function mapEventPropToEvent(eventProp) {
return eventProp.substring(2).toLowerCase();
}

function hasClickedScrollbar(event) {
return (
document.documentElement.clientWidth < event.clientX ||
document.documentElement.clientHeight < event.clientY
);
}

/**
* Listen for click events that occur somewhere in the document, outside of the element itself.
* For instance, if you need to hide a menu when people click anywhere else on your page.
Expand Down Expand Up @@ -55,7 +62,8 @@ function ClickAwayListener(props) {

// 1. IE 11 support, which trigger the handleClickAway even after the unbind
// 2. The child might render null.
if (!mountedRef.current || !nodeRef.current) {
// 3. Behave like a blur listener.
if (!mountedRef.current || !nodeRef.current || hasClickedScrollbar(event)) {
return;
}

Expand Down