1.0.2 • Published 4 years ago

use-is-clicked v1.0.2

Weekly downloads
2
License
MIT
Repository
github
Last release
4 years ago

use-is-clicked

🎯 a simple React hook that tracks whether an element or its descendant is clicked. This is useful for tracking a modal/dropdown state and enabling it to close if clicked away.

To install:

  • npm i use-is-clicked

or

  • yarn add use-is-clicked

To use:

  • create a ref using the useRef() hook
  • add the ref to the target element using the ref attribute.
  • pass the ref to the useIsClicked hook.

import React, { useRef } from "react";
import { useIsClicked } from "use-is-clicked";
import { Navbar } from "react-bootstrap";


const Example = () => {
  const navRef = useRef();
  const isClicked = useIsClicked(navRef);

  return (
 <Navbar ref={navRef} expanded={isClicked} expand="lg" bg="dark" variant="dark">
...
 </Navbar>
  )
}

export default Example

The hook returns a boolean which tracks whether the element(or one of its descendants) has been clicked on or away from.

1.0.2

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago