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

@souvik666/react-outside-click-hook

v1.0.1

Published

A React hook for capturing outside click events. The useOutsideClick hook allows you to detect when a user clicks outside of a specific element on your website. It returns a reference to the element, which you can attach to a DOM node using the ref attrib

Downloads

43

Readme

useOutsideClick

A React hook for capturing outside click events. The useOutsideClick hook allows you to detect when a user clicks outside of a specific element on your website. It returns a reference to the element, which you can attach to a DOM node using the ref attribute, and a boolean indicating whether the user has clicked outside of the element.

Installation

npm i @souvik666/react-outside-click-hook

Usage

import { useOutsideClick } from "@souvik666/react-outside-click-hook";

const MyComponent = () => {

  const [isClickedOutside, ref] = useOutsideClick<HTMLDivElement>({
    callback: () => console.log("clicked outside"),
  });

  return (
    <>
      <div ref={ref}>Click outside of me</div>
      {isClickedOutside && <div>clicked outside</div>}
    </>
  );
};
export default MyComponent;

Parameters

| Parameter | Type | Description | | --------- | -------- | ------------------------------------------------------------------------------------------ | | callback | function | A function that will be called when a user clicks outside of the element. | | isOpen | boolean | (optional) A boolean variable that indicate whether the element should be open or closed. | | return | ref | A ref that should be attached to the DOM element that you want to detect clicks outside of |

Note that, the isOpen parameter is optional and you can use it to conditionally render the elements based on whether it's open or closed. If you don't pass this parameter, the hook will assume that the element is always open and will call the callback function whenever a click outside of the element occurs.

Also, the return is the ref variable that should be attached to the DOM element that you want to detect clicks outside of, you can use this variable to attach to the DOM element using ref attribute.

Example

import { useOutsideClick } from "@souvik666/react-outside-click-hook";

const MyComponent = () => {

  const [isClickedOutside, ref] = useOutsideClick<HTMLDivElement>({
    callback: () => console.log("clicked outside"),
  });

  return (
    <>
      <div ref={ref}>Click outside of me</div>
      {isClickedOutside && <div>clicked outside</div>}
    </>
  );
};

export default MyComponent;

FAQ

Q: How do I attach the returned ref to a DOM element?

A: You can attach the ref to a DOM element using the ref attribute. For example, if your component has a div that you want to detect clicks outside of, you can do something like this:

const ref = useOutsideClick<HTMLDivElement>(myCallback);
return <div ref={ref}>Content goes here</div>;

Q: What is the second parameter for?

A: The second parameter is an optional boolean that indicates whether the element is open or closed. If you pass true, the hook will call the callback function when a click outside of the element occurs. If you pass false, the hook will do nothing. If you don't pass this parameter, the hook will assume that the element is always open.

Q: Can I use this hook with a class component?

A: No, this hook can only be used in functional components.

Please note that, you should use this hook in a functional component.