Skip to content

Click-away listener

The ClickAwayListener component detects when a click event happens outside of its child element.

Introduction

ClickAwayListener is a utility component that listens for click events outside of its child. (Note that it only accepts one child element.)

This is useful for components like PopperUnstyled which should close when the user clicks anywhere else in the document.

ClickAwayListener also supports the Portal component.

Component

Usage

After installation, you can start building with this component using the following basic elements:

import ClickAwayListener from '@mui/base/ClickAwayListener';

export default function MyApp() {
  return (
    <ClickAwayListener>
      {/* the child listening for a click outside of its container */}
    </ClickAwayListener>
  );
}

Basics

The following demo shows how to hide a menu dropdown when users click anywhere else on the page:

<ClickAwayListener onClickAway={handleClickAway}>
  <Box sx={{ position: 'relative' }}>
    <button type="button" onClick={handleClick}>
      Open menu dropdown
    </button>
    {open ? (
      <Box sx={styles}>
        Click me, I will stay visible until you click outside.
      </Box>
    ) : null}
  </Box>
</ClickAwayListener>

Usage with Portal

The following demo uses the Portal component to render the dropdown into a new subtree outside of the current DOM hierarchy:

<ClickAwayListener onClickAway={handleClickAway}>
  <div>
    <button type="button" onClick={handleClick}>
      Open menu dropdown
    </button>
    {open ? (
      <Portal>
        <Box sx={styles}>
          Click me, I will stay visible until you click outside.
        </Box>
      </Portal>
    ) : null}
  </div>
</ClickAwayListener>

Customization

Listening for leading events

By default, the ClickAwayListener component responds to trailing events—the end of a click or touch.

You can set the component to listen for leading events (the start of a click or touch) using the mouseEvent and touchEvent props, as shown in the following demo:

<ClickAwayListener
  mouseEvent="onMouseDown"
  touchEvent="onTouchStart"
  onClickAway={handleClickAway}
>
  <Box sx={{ position: 'relative' }}>
    <button type="button" onClick={handleClick}>
      Open menu dropdown
    </button>
    {open ? (
      <Box sx={styles}>
        Click me, I will stay visible until you click outside.
      </Box>
    ) : null}
  </Box>
</ClickAwayListener>

Accessibility

By default, ClickAwayListener will add an onClick handler to its child. This can result in screen readers announcing that the child is clickable, even though this onClick handler has no effect on the child itself.

To prevent this behavior, add role="presentation" to the child element:

<ClickAwayListener>
  <div role="presentation">
    <h1>non-interactive heading</h1>
  </div>
</ClickAwayListener>

This is also required to fix a known issue in NVDA when using Firefox that prevents the announcement of alert messages—see mui/material-ui#29080 for details.