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

v0.2.2

Published

A decorator for listening onClickOutside event for React components

Downloads

624

Readme

This is a React decorator(high-order component) that you can add to your React components if you want to have them listen for clicks 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).

Note that this decorator relies on the .classList property, which is supported by all modern browsers, but not by no longer supported browsers like IE8 or even older. For setups that need to support deprecated browsers, using something like the MDN classlist-polyfill will be necessary.

Forked from react-onclickoutside

Installation

npm install react-onclickoutside-decorator

Usage

import React, { Component } from 'react';
import onClickOutside from 'react-onclickoutside-decorator';

class App extends Component {
  render() {
    return <Child onClickOutside={this.handleClickOutside} />;
  }
  handleClickOutside() {
    // ...
  }
}

@onClickOutside
class Child extends Component {
  render() {
    return <div />;
  }
}

Regulate whether or not to listen for outside clicks

When using this decorator, a component has two functions that can be used to explicitly listen for, or do nothing with, outside clicks

  • enableOnClickOutside() - Enables outside click listening by setting up the event listening bindings.
  • disableOnClickOutside() - Disables outside click listening by explicitly removing the event listening bindings.

In addition, you can create a component that uses this decorator such that it has the code set up and ready to go, but not listening for outside click events until you explicitly issue its enableOnClickOutside(), by passing in a properly called disableOnClickOutside:

// modal.js
import React from 'react';
import PropTypes from 'prop-types';
import classNames from 'classnames';
import ModalContent from './modal-content';

function Modal({ show, onClickOutside }) {
  return (
    <div
      className={classNames(
        'modal',
        { 'is-active': show },
      )}
    >
      <div className="modal-background" />
      <div className="modal-card">
        <ModalContent
          onClickOutside={onClickOutside}
          disableOnClickOutside={!show}
        />
      </div>
    </div>
  );
}

Modal.propTypes = {
  show: PropTypes.bool,
  onClickOutside: PropTypes.func.isRequired,
};

Modal.defaultProps = {
  show: false,
};

export default Modal;
// modal-content.js
import React, { Component } from 'react';
import onClickOutside from 'react-onclickoutside-decorator';

@onClickOutside
class ModalContent extends Component {
  render() {
    return (
      <div>
        <header className="modal-card-head">
          <p className="modal-card-title">Modal</p>
        </header>
        <section className="modal-card-body">
          Try clicking outside the modal.
        </section>
        <footer className="modal-card-foot" />
      </div>
    );
  }
}

export default ModalContent;

Marking elements as "skip over this one" during the event loop

If you want the decorator to ignore certain elements, then add the class ignore-react-onclickoutside to that element and the callback won't be invoked when the click happens inside elements with that class.