Latest Threat Research:SANDWORM_MODE: Shai-Hulud-Style npm Worm Hijacks CI Workflows and Poisons AI Toolchains.Details
Socket
Book a DemoInstallSign in
Socket

outsideclick-react

Package Overview
Dependencies
Maintainers
1
Versions
14
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

outsideclick-react

This is a lightweight React package that provides a hook and component for detecting clicks outside of a specified element.

Source
npmnpm
Version
0.1.0
Version published
Weekly downloads
181
-43.08%
Maintainers
1
Weekly downloads
 
Created
Source

OutSideClick React

outsideclick-react is a lightweight React package that provides a hook and component for detecting clicks outside of a specified element. This can be useful for creating dropdown menus, modals, and other UI components that need to close when the user clicks outside of them.

Installation

You can install outsideclick-react using NPM or Yarn:

npm install outsideclick-react

or

yarn add outsideclick-react

How to use

Hook Usage

A hook that returns a ref object that can be attached to a target element to detect clicks outside of it.

import { useOutsideClick } from "outsideclick-react";

function MyComponent() {
    const handleOutsideClick = (e){
        // Handle outside click
    }
    const ref = useOutsideClick(handleOutsideClick)
    return(
        <div ref={ref}>
            {/* Your component */}
        </div>
    )
}

In this example, we're using the useOutsideClick hook returns a ref object that can be used to detect clicks outside of an element. Pass in a handleOutsideClick function as a parameter to the hook, and when a click outside of the target element is detected, the function is called with the event object as the argument. This allows you to perform any necessary actions in response to the click.

Component Usage

Alternatively, you can use the OutsideClick component provided by the package to wrap your component:

import { OutsideClick } from 'outsideclick-react'

function MyComponent() {
    return (
        <OutsideClick
            onOutsideClick={() => {
                // Handle outside click
            }}
        >
            <div>{/* Your component */}</div>
        </OutsideClick>
    )
}

In the example above, we use the OutsideClick component to wrap our component and pass a callback function to the onOutsideClick prop that will be called when a click occurs outside of the wrapped element.

Note: The OutsideClick component returns a div element and supports all of the properties of a standard div element.


If you'd like to contribute, please do submit a pull request.

In case you want support my work

"Buy Me A Coffee"

Keywords

outsideclick

FAQs

Package last updated on 26 Feb 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts