Installations
npm install react-detect-click-outside
Developer Guide
Typescript
No
Module System
CommonJS
Score
93.5
Supply Chain
92.1
Quality
75.8
Maintenance
100
Vulnerability
100
License
Releases
Unable to fetch releases
Contributors
Unable to fetch Contributors
Languages
TypeScript (97.45%)
JavaScript (2.22%)
Shell (0.33%)
Love this project? Help keep it running — sponsor us today! 🚀
Developer
zhaluza
Download Statistics
Total Downloads
2,838,488
Last Day
749
Last Week
29,093
Last Month
111,114
Last Year
1,279,413
GitHub Statistics
66 Stars
78 Commits
11 Forks
2 Watchers
1 Branches
5 Contributors
Updated on Sep 23, 2024
Bundle Size
921.00 B
Minified
503.00 B
Minified + Gzipped
Package Meta Information
Latest Version
1.1.7
Package Id
react-detect-click-outside@1.1.7
Unpacked Size
15.24 kB
Size
4.97 kB
File Count
9
Total Downloads
Cumulative downloads
Total Downloads
2,838,488
Last Day
-13.1%
749
Compared to previous day
Last Week
5.7%
29,093
Compared to previous week
Last Month
42.1%
111,114
Compared to previous month
Last Year
38.4%
1,279,413
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
react-detect-click-outside
A lightweight React hook that detects clicks outside elements and triggers a callback. Can also detect keypresses.
📈 Over 7,600 weekly users (as of June 2022).
👍 Great for toggling dropdowns!
Notice: This package is looking for maintainers! Due to my professional and personal commitments, I don't have a whole lot of time to devote to maintaining this library. Some users have pointed out a few issues in the Github repo - feel free to take a stab at them!
Table of Contents
Features
- 🖱 💻 Detects clicks outside an element and/or keypresses.
- 🔨 Customizable: disable clicks, disable keypresses, or specify keys that will trigger the callback
- 🎣 Built with React hooks
- 🔥 Written in TypeScript
Installation
Install with Yarn:
1yarn add react-detect-click-outside
Or with NPM:
1npm i react-detect-click-outside
Import into your component like so:
1import { useDetectClickOutside } from 'react-detect-click-outside';
Usage
Here's an example of how to use the hook.
This library was built with UI features like dropdowns in mind. Below is a quick and functioning example of how to include it in a dropdown component:
1const Dropdown = ({ closeDropdown }) => { 2 const ref = useDetectClickOutside({ onTriggered: closeDropdown }); 3 return ( 4 <div className="dropdown" ref={ref}> 5 <p>This is a dropdown!</p> 6 </div> 7 ); 8};
How to implement the hook inside a function component:
- Assign the hook to a variable before the component's return statement (above we use
ref
). Pass an empty object into the hook as an argument.
1const ref = useDetectClickOutside({});
-
The object you just passed into the
useDetectClickOutside
hook requires a property calledonTriggered
. The value ofonTriggered
must be a function — by default, it'll be called anytime a user clicks outside the component or hits theEscape
key.In the example above, we used a function called
closeToggle
. This function is passed down from a parent component (let's call itContainer
) and controls a piece of state that determines whether theDropdown
component is visible.Here's a quick, trimmed-down example:
1const Container = () => { 2 const [displayDropdown, setDisplayDropdown] = useState(false); 3 4 const closeDropdown = () => { 5 setDisplayDropdown(false); 6 } 7 return ( 8 { displayDropdown && <Dropdown/> } 9 ) 10}
Now, go ahead and pass your callback into the
useDetectClickOutside
hook!1const ref = useDetectClickOutside({ onTriggered: closeDropdown });
- Assign your
ref
constant (or whatever constant you assigned theuseDetectClickOutside
hook to) as a ref to the outermost element returned by your target component.
1return ( 2 <div className="dropdown" ref={ref}> 3 <p>This is a dropdown!</p> 4 </div> 5);
Here's what the whole component should look like now:
1const Dropdown = ({ closeDropdown }) => { 2 const ref = useDetectClickOutside({ onTriggered: closeDropdown }); 3 return ( 4 <div className="dropdown" ref={ref}> 5 <p>This is a dropdown!</p> 6 </div> 7 ); 8};
Congrats! Your
useDetectClickOutside
should now trigger anytime a user clicks outside your component or presses theEscape
key.Want to customize your hook? Check out some of the additional options below.
- Assign your
Options
The object passed into the useDetectClickOutside
hook accepts the following properties. Note that only onTriggered
is required.
onTriggered
(required)
Type: () => void
A callback function, e.g. one that toggles the visibility of the component. By default, this function is triggered by a click outside the component, and by an Escape
keyup event.
Example:
1const ref = useDetectClickOutside({ onTriggered: closeDropdown });
disableClick
(optional)
Type: boolean
When passed to the hook, this option will prevent clicks from triggering the onTriggered
callback when the component is in the DOM. This option is disabled by default.
Example:
1const ref = useDetectClickOutside({ 2 onTriggered: closeDropdown, 3 disableClick: true, 4});
disableTouch
(optional)
Type: boolean
When passed to the hook, this option will prevent touch events from triggering the onTriggered
callback when the component is in the DOM. This option is disabled by default.
Example:
1const ref = useDetectClickOutside({ 2 onTriggered: closeDropdown, 3 disableTouch: true, 4});
disableKeys
(optional)
Type: boolean
This option will prevent keypresses from triggering the onTriggered
callback when the component is in the DOM. This option is disabled by default.
Example:
1const ref = useDetectClickOutside({ 2 onTriggered: closeDropdown, 3 disableKeys: true, 4});
allowAnyKey
(optional)
Type: boolean
This option will let any keypress trigger the onTriggered
callback when the component is in the DOM - not just the Escape
key. This option is disabled by default.
Example:
1const ref = useDetectClickOutside({ 2 onTriggered: closeDropdown, 3 allowAnyKey: true, 4});
triggerKeys
(optional)
Type: string[]
An array of key values that will trigger the onTriggered
callback.
Example:
1const ref = useDetectClickOutside({ 2 onTriggered: closeDropdown, 3 triggerKeys: ['Enter', 'Tab', 'x'], 4});
Note: This option overrides the default hook behavior of triggering the onTriggered
callback with the Escape
key. If you still wish to trigger the onTriggered
function with Escape
, you need to add it to the array (e.g. triggerKeys=['Escape', 'Enter']
).
Requirements
You must be using React 16.8.0 or later. In other words, your version of React must support hooks.

No vulnerabilities found.
Reason
no binaries found in the repo
Reason
Found 3/17 approved changesets -- score normalized to 1
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
security policy file not detected
Details
- Warn: no security policy file detected
- Warn: no security file to analyze
- Warn: no security file to analyze
- Warn: no security file to analyze
Reason
license file not detected
Details
- Warn: project does not have a license file
Reason
branch protection not enabled on development/release branches
Details
- Warn: branch protection not enabled for branch 'master'
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
- Warn: 0 commits out of 16 are checked with a SAST tool
Reason
16 existing vulnerabilities detected
Details
- Warn: Project is vulnerable to: GHSA-67hx-6x53-jw92
- Warn: Project is vulnerable to: GHSA-93q8-gq69-wqmw
- Warn: Project is vulnerable to: GHSA-grv7-fg5c-xmjg
- Warn: Project is vulnerable to: GHSA-3xgq-45jj-v275
- Warn: Project is vulnerable to: GHSA-w573-4hg7-7wgq
- Warn: Project is vulnerable to: GHSA-896r-f27r-55mw
- Warn: Project is vulnerable to: GHSA-9c47-m6qq-7p4h
- Warn: Project is vulnerable to: GHSA-952p-6rrq-rcjv
- Warn: Project is vulnerable to: GHSA-f8q6-p94x-37v3
- Warn: Project is vulnerable to: GHSA-hrpp-h998-j3pp
- Warn: Project is vulnerable to: GHSA-p8p7-x288-28g6
- Warn: Project is vulnerable to: GHSA-gcx4-mw62-g8wm
- Warn: Project is vulnerable to: GHSA-c2qf-rxjj-qqgw
- Warn: Project is vulnerable to: GHSA-72xf-g2v4-qvf3
- Warn: Project is vulnerable to: GHSA-j8xg-fqg3-53r7
- Warn: Project is vulnerable to: GHSA-3h5v-q93c-6h6q
Score
1.4
/10
Last Scanned on 2025-02-10
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn MoreOther packages similar to react-detect-click-outside
click-outside-detect
this package detect the click outside the components
react-outer-click
React component and hook which detect mouse clicks outside of itself.
use-outside-click-hook
React use outside click hook. It's a simple hook that allows you to detect clicks outside of a component.
@souvik666/react-outside-click-hook
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