Gathering detailed insights and metrics for react-cool-onclickoutside
Gathering detailed insights and metrics for react-cool-onclickoutside
😎 🖱 React hook to listen for clicks outside of the component(s).
npm install react-cool-onclickoutside
Typescript
Module System
Node Version
NPM Version
95.7
Supply Chain
99.5
Quality
76.1
Maintenance
100
Vulnerability
100
License
TypeScript (67.59%)
SCSS (11.84%)
HTML (11.39%)
JavaScript (8.68%)
Shell (0.49%)
Love this project? Help keep it running — sponsor us today! 🚀
Total Downloads
10,159,194
Last Day
9,862
Last Week
49,674
Last Month
222,603
Last Year
2,747,951
MIT License
547 Stars
1,512 Commits
13 Forks
5 Watchers
30 Branches
4 Contributors
Updated on Feb 11, 2025
Minified
Minified + Gzipped
Latest Version
1.7.0
Package Id
react-cool-onclickoutside@1.7.0
Size
8.07 kB
NPM Version
6.14.12
Node Version
14.16.1
Published on
Oct 06, 2021
Cumulative downloads
Total Downloads
Last Day
0.6%
9,862
Compared to previous day
Last Week
-2.2%
49,674
Compared to previous week
Last Month
20.5%
222,603
Compared to previous month
Last Year
-3.2%
2,747,951
Compared to previous year
1
41
This is a React hook to trigger callback when user clicks outside of the target component(s) area. It's a useful logic for UI interaction design (IxD) like dismiss a dropdown menu, modal or tooltip etc. You can check the features section to learn more.
⚡️ Live demo: https://react-cool-onclickoutside.netlify.app
❤️ it? ⭐️ it on GitHub or Tweet about it.
refs
for some reasons.react
.To use react-cool-onclickoutside
, you must use react@16.8.0
or greater which includes hooks.
This package is distributed via npm.
1$ yarn add react-cool-onclickoutside 2# or 3$ npm install --save react-cool-onclickoutside
Common use case.
1import { useState } from "react"; 2import useOnclickOutside from "react-cool-onclickoutside"; 3 4const Dropdown = () => { 5 const [openMenu, setOpenMenu] = useState(false); 6 const ref = useOnclickOutside(() => { 7 setOpenMenu(false); 8 }); 9 10 const handleClickBtn = () => { 11 setOpenMenu(!openMenu); 12 }; 13 14 return ( 15 <div> 16 <button onClick={handleClickBtn}>Button</button> 17 {openMenu && <div ref={ref}>Menu</div>} 18 </div> 19 ); 20};
Support multiple refs. Callback only be triggered when user clicks outside of the registered components.
1import { useState } from "react"; 2import useOnclickOutside from "react-cool-onclickoutside"; 3 4const App = () => { 5 const [showTips, setShowTips] = useState(true); 6 const ref = useOnclickOutside(() => { 7 setShowTips(false); 8 }); 9 10 return ( 11 <div> 12 {showTips && ( 13 <> 14 <div ref={ref}>Tooltip 1</div> 15 <div ref={ref}>Tooltip 2</div> 16 </> 17 )} 18 </div> 19 ); 20};
You can tell react-cool-onclickoutside
to ignore certain elements during the event loop by the ignore-onclickoutside
CSS class name. If you want explicit control over the class name, use the ignoreClass
option.
1import { useState } from "react"; 2import useOnclickOutside from "react-cool-onclickoutside"; 3 4// Use the default CSS class name 5const App = () => { 6 const ref = useOnclickOutside(() => { 7 // Do something... 8 }); 9 10 return ( 11 <div> 12 <div ref={ref}>I'm a ????</div> 13 <div>Click me will trigger the event's callback</div> 14 <div className="ignore-onclickoutside"> 15 Click me won't trigger the event's callback 16 </div> 17 </div> 18 ); 19}; 20 21// Use your own CSS class name 22const App = () => { 23 const ref = useOnclickOutside( 24 () => { 25 // Do something... 26 }, 27 { 28 ignoreClass: "my-ignore-class", // Or ["class-1", "class-2"] 29 } 30 ); 31 32 return ( 33 <div> 34 <div ref={ref}>I'm a ????</div> 35 <div>Click me will trigger the event's callback</div> 36 <div className="my-ignore-class"> 37 Click me won't trigger the event's callback 38 </div> 39 </div> 40 ); 41};
In case you want to disable the event listener for performance reasons or fulfill some use cases. We provide the disabled
option for you. Once you set it to true
, the callback won’t be triggered.
1import { useState } from "react"; 2import useOnclickOutside from "react-cool-onclickoutside"; 3 4const App = () => { 5 const [disabled, setDisabled] = useState(false); 6 const ref = useOnclickOutside( 7 () => { 8 // Do something... 9 }, 10 { disabled } 11 ); 12 13 const handleBtnClick = () => { 14 setDisabled(true); 15 }; 16 17 return ( 18 <div> 19 <button onClick={handleBtnClick}> 20 Stop listening for outside clicks 21 </button> 22 <div ref={ref}>I'm a ????</div> 23 </div> 24 ); 25};
ref
In case of you had a ref already or you want to share a ref for other purposes. You can pass in the ref instead of using the one provided by this hook.
1const ref = useRef(); 2 3useOnclickOutside( 4 () => { 5 // Do something... 6 }, 7 { refs: [ref] } 8);
Clicks on an <iframe>
element won't trigger document.documentElement
listeners, because it's literally different page with different security domain. However, when clicking on an iframe moves focus
to its content's window that triggers the main window.blur event. react-cool-onclickoutside
in conjunction the blur
event with document.activeElement to detect if an iframe is clicked, and execute the provided callback.
The above-mentioned workaround has its caveats:
For our convenience, this feature is enabled by default. You can optionally disable it by setting the detectIFrame
to false
if you find it conflicting with your use-case.
1const ref = useOnclickOutside(callback: (event: Event) => void, options?: object);
You must register the ref
and pass the callback
to use this hook. Moreover you can access the event
object via the callback's parameter, default will be MouseEvent or TouchEvent.
1const callback = (event) => { 2 console.log("Event: ", event); 3};
The options
object contains the following keys.
Key | Type | Default | Description |
---|---|---|---|
refs | Array | For some reasons, you can pass in your own ref(s) instead of using the built-in. | |
disabled | boolean | false | Enable/disable the event listener. |
eventTypes | Array | ['mousedown', 'touchstart'] | Which events to listen for. |
excludeScrollbar | boolean | false | Whether or not to listen (ignore) to browser scrollbar clicks. |
ignoreClass | string | string[] | ignore-onclickoutside | To ignore certain elements during the event loop by the CSS class name that you defined. |
detectIFrame | boolean | true | To disable the feature of detecting iframe clicks. |
???? If you have written any blog post or article about
react-cool-onclickoutside
, please open a PR to add it here.
Thanks goes to these wonderful people (emoji key):
Welly ???? ???? ???? | DmitryScaletta ???? | vardani ???? | Alexey Cherepanov ???? |
This project follows the all-contributors specification. Contributions of any kind welcome!
No vulnerabilities found.
Reason
no dangerous workflow patterns detected
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
SAST tool detected but not run on all commits
Details
Reason
security policy file detected
Details
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
Found 1/24 approved changesets -- score normalized to 0
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
52 existing vulnerabilities detected
Details
Score
Last Scanned on 2025-02-03
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