JSPM

simple-react-clickaway

1.0.6
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 27
  • Score
    100M100P100Q47377F
  • License MIT

Simple react hook to listen click away

Package Exports

  • simple-react-clickaway

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (simple-react-clickaway) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

Simple React Clickaway Hook

Simple react useClickAway hook, which listens for clicks outside the element.

Installation

npm i simple-react-clickaway

Import hook

import {useClickAway} from 'simple-react-clickaway';

And use it like:

const {disable, enable} = useClickAway(ref, onClickAway);

Example

Here is a simple example of detecting a click outside a modal window:

import "./styles.css";
import { useClickAway } from "simple-react-clickaway";
import { useRef } from "react";

export default function App() {
  const modalRef = useRef();
  const handleClickAway = () => {
    alert("You clicked away!");
  };
  const { disable, enable } = useClickAway(modalRef, handleClickAway);
  return (
    <div className="App">
      <div className="modal" ref={modalRef}>
        <button onClick={disable}>Disable click away</button>
        <button onClick={enable}>Enable click away</button>
      </div>
    </div>
  );
}

View it on a code sandbox