use-click-away

0.0.1 • Public • Published

React hook to detect click or touch events

useClickAway() reacts to clicks outside the bound element, and calls the expression that is passed in when this event is detected.

Suppose you're working on a Modal component that renders a dialog box, and you wish to close the modal if the user clicks away this is the ideal scenario for useClickAway() custom hook.

Installation

Using npm:

npm i use-click-away --save

Usage

Import the hook:

import { useClickAway } from "use-click-away";

Full example

export default () => {
  const [modal, setModal] = React.useState(false);
  const clickRef = React.useRef("");

  useClickAway(clickRef, () => {
    setModal(false);
  });

  return (
    <div className="container">
      <button onClick={() => setModal(true)}>Show Modal</button>
      {modal && <div ref={clickRef} className="modal">Modal Content</div>}
    </div>
  );
}

Specification

useClickAway() input

  • clickRef: element - The dom element to bind our hook.
  • callback: function - The callback that runs after user click

Built With

React

License

MIT

Dependencies (0)

    Dev Dependencies (5)

    Package Sidebar

    Install

    npm i use-click-away

    Weekly Downloads

    395

    Version

    0.0.1

    License

    MIT

    Unpacked Size

    4.44 kB

    Total Files

    6

    Last publish

    Collaborators

    • geobde