npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-advanced-click-away

v2.1.7

Published

A worry-free click away listener for advanced use cases.

Downloads

757

Readme

react-advanced-click-away

A worry-free click away listener for advanced use cases.

NPM CI Coverage

Installation

npm install --save react-advanced-click-away
# or
yarn add react-advanced-click-away

Simple example

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;

Motivation

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.

Docs and demos

Check out the docs and demos to see everything in action.

Props

| 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 | |

Layers

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.

Attribution

The original version of this component was adapted from Material UI.

https://github.com/mui-org/material-ui/blob/512896/packages/mui-material/src/ClickAwayListener/ClickAwayListener.tsx

License

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.