
Company News
/Security News
Socket Selected for OpenAI's Cybersecurity Grant Program
Socket is an initial recipient of OpenAI's Cybersecurity Grant Program, which commits $10M in API credits to defenders securing open source software.
react-advanced-click-away
Advanced tools
A worry-free click away listener for advanced use cases.
npm install --save react-advanced-click-away
# or
yarn add react-advanced-click-away
import React from 'react';
import ClickAwayListener from 'react-advanced-click-away';
const MyComponent = () => (
<ClickAwayListener onClickAway={() => console.log('Clicked away!')}>
<div>
Inside
</div>
</ClickAwayListener>
);
export default MyComponent;
The original version of this component is adapted from Material UI's ClickAwayListener and inherits basic features like support for touch events and React portals, as well as many fixes for non-obvious issues, like with handling iframes or for this bug with useEffect timing in React v16. It passes all of Material UI's original unit tests1.
This library however aims to support some advanced cases when nesting multiple <ClickAwayListener> components, which is useful when building nested popovers, menus and modals. Notably, we listen to events during the capture phase, which often lets us stop mouse event propagation elsewhere our React tree without affecting the click away behaviour of unrelated components.
Check out the docs and demos to see everything in action.
| Name | Description | Default | |
|---|---|---|---|
| onClickAway* | Handler called on click away | (event: MouseEvent | TouchEvent) => void | |
| children | A ref-accepting child | ReactElement | |
| mouseEvent | Mouse click away event to listen to | "click" | "mousedown" | "mouseup" | "mousedown" |
| touchEvent | Touch click away event to listen to | "touchstart" | "touchend" | "touchstart" |
| disableReactTree | If true, elements inside portals will be considered to be outside of the click away listener. | boolean | false |
| ignoreScrollbars | If true, clicking the window scrollbars will not trigger the onClickAway() handler. | boolean | false |
| layer | Root element, document by default. | boolean |
Wrap modal contents in a <ClickAwayLayer> to freeze any click away listeners underneath the modal.
If building modals you may also consider locking scroll, locking focus and in some cases stopping event propagation.
// Click away listeners underneath the modal will be disabled until the modal is unmounted.
const BaseModal = ({ open, children }) => (
<>
{open && (
<ScrollLock>
<FocusLock>
<StopPropagation all>
<ClickAwayLayer>
<div role="dialog">{children}</div>
</ClickAwayLayer>
</StopPropagation>
</FocusLock>
</ScrollLock>
)}
</>
);
// Don't forget to establish a root layer in the app.
// This is not needed if you don't use click away layers.
const App = ({ children }) => <ClickAwayLayer root><Component /></Layer>
See docs for examples of usage.
The original version of this component was adapted from Material UI.
This project is licensed under the terms of the MIT license.
1. With one minor prop change in mouseEvent / touchEvent. Compatibility with MUI is not guaranteed.
FAQs
A worry-free click away listener for advanced use cases.
We found that react-advanced-click-away demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Company News
/Security News
Socket is an initial recipient of OpenAI's Cybersecurity Grant Program, which commits $10M in API credits to defenders securing open source software.

Security News
Socket CEO Feross Aboukhadijeh joins 10 Minutes or Less, a podcast by Ali Rohde, to discuss the recent surge in open source supply chain attacks.

Research
/Security News
Campaign of 108 extensions harvests identities, steals sessions, and adds backdoors to browsers, all tied to the same C2 infrastructure.