Gathering detailed insights and metrics for react-detect-click-outside
Gathering detailed insights and metrics for react-detect-click-outside
Gathering detailed insights and metrics for react-detect-click-outside
Gathering detailed insights and metrics for 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
npm install react-detect-click-outside
Typescript
Module System
93.5
Supply Chain
92.1
Quality
75.8
Maintenance
100
Vulnerability
100
License
TypeScript (97.45%)
JavaScript (2.22%)
Shell (0.33%)
Love this project? Help keep it running — sponsor us today! 🚀
Total Downloads
2,838,488
Last Day
749
Last Week
29,093
Last Month
111,114
Last Year
1,279,413
66 Stars
78 Commits
11 Forks
2 Watchers
1 Branches
5 Contributors
Updated on Sep 23, 2024
Minified
Minified + Gzipped
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
Cumulative downloads
Total Downloads
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
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!
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';
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:
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 called onTriggered
. The value of onTriggered
must be a function — by default, it'll be called anytime a user clicks outside the component or hits the Escape
key.
In the example above, we used a function called closeToggle
. This function is passed down from a parent component (let's call it Container
) and controls a piece of state that determines whether the Dropdown
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 });
ref
constant (or whatever constant you assigned the useDetectClickOutside
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 the Escape
key.
Want to customize your hook? Check out some of the additional options below.
The object passed into the useDetectClickOutside
hook accepts the following properties. Note that only onTriggered
is required.
onTriggered
(required)() => 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)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)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)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)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)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']
).
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
Reason
security policy file not detected
Details
Reason
license file not detected
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Reason
16 existing vulnerabilities detected
Details
Score
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 More