outsideclick-react
v0.3.0
Published
This is a lightweight React package that provides a hook and component for detecting clicks outside of a specified element.
Downloads
1,746
Maintainers
Readme
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)
/* const ref = useOutsideClick(handleOutsideClick,".hello-ignore") */
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 */
}}
ignoreElement=".ignore"
>
<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.
In case you want support my work