A React hook to detect clicks outside of an element. Returns a ref and a boolean indicating if clicked outside. Useful for modals, dropdown menus, and other UI components that need to be hidden when clicked outside.
Installations
npm install @souvik666/react-outside-click-hook
Developer Guide
Typescript
Yes
Module System
CommonJS
Node Version
16.17.1
NPM Version
8.15.0
Score
63
Supply Chain
89.3
Quality
75.4
Maintenance
100
Vulnerability
100
License
Releases
Contributors
Unable to fetch Contributors
Languages
TypeScript (70.94%)
JavaScript (29.06%)
Love this project? Help keep it running — sponsor us today! 🚀
Developer
Download Statistics
Total Downloads
910
Last Day
1
Last Week
2
Last Month
33
Last Year
305
GitHub Statistics
2 Stars
135 Commits
1 Watchers
6 Branches
1 Contributors
Updated on Oct 14, 2024
Bundle Size
619.00 B
Minified
367.00 B
Minified + Gzipped
Package Meta Information
Latest Version
1.0.1
Package Id
@souvik666/react-outside-click-hook@1.0.1
Unpacked Size
14.98 kB
Size
3.48 kB
File Count
17
NPM Version
8.15.0
Node Version
16.17.1
Published on
Jan 26, 2023
Total Downloads
Cumulative downloads
Total Downloads
910
Last Day
0%
1
Compared to previous day
Last Week
-50%
2
Compared to previous week
Last Month
175%
33
Compared to previous month
Last Year
2.3%
305
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
Dev Dependencies
33
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
1npm i @souvik666/react-outside-click-hook 2
Usage
1import { useOutsideClick } from "@souvik666/react-outside-click-hook"; 2 3const MyComponent = () => { 4 5 const [isClickedOutside, ref] = useOutsideClick<HTMLDivElement>({ 6 callback: () => console.log("clicked outside"), 7 }); 8 9 return ( 10 <> 11 <div ref={ref}>Click outside of me</div> 12 {isClickedOutside && <div>clicked outside</div>} 13 </> 14 ); 15}; 16export default MyComponent; 17
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
1import { useOutsideClick } from "@souvik666/react-outside-click-hook"; 2 3const MyComponent = () => { 4 5 const [isClickedOutside, ref] = useOutsideClick<HTMLDivElement>({ 6 callback: () => console.log("clicked outside"), 7 }); 8 9 return ( 10 <> 11 <div ref={ref}>Click outside of me</div> 12 {isClickedOutside && <div>clicked outside</div>} 13 </> 14 ); 15}; 16 17export default MyComponent; 18
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:
1const ref = useOutsideClick<HTMLDivElement>(myCallback); 2return <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.

No vulnerabilities found.

No security vulnerabilities found.