Research
Security News
Malicious npm Package Targets Solana Developers and Hijacks Funds
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
react-cool-onclickoutside
Advanced tools
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.
react
.To use react-cool-onclickoutside
, you must use react@16.8.0
or greater which includes hooks.
This package is distributed via npm.
$ yarn add react-cool-onclickoutside
# or
$ npm install --save react-cool-onclickoutside
Common use case.
import React, { useState, useRef } from 'react';
import useOnclickOutside from 'react-cool-onclickoutside';
const Dropdown = () => {
const [openMenu, setOpenMenu] = useState(false);
const ref = useRef();
useOnclickOutside(ref, () => {
setOpenMenu(false);
});
const handleClickBtn = () => {
setOpenMenu(!openMenu);
};
return (
<div>
<button onClick={handleClickBtn}>Button</button>
{openMenu && <div ref={ref}>Menu</div>}
</div>
);
};
Support multiple refs. Callback only be triggered when user clicks outside of the registered components.
import React, { useState, useRef } from 'react';
import useOnclickOutside from 'react-cool-onclickoutside';
const App = () => {
const [showTips, setShowTips] = useState(true);
const t1Ref = useRef();
const t2Ref = useRef();
useOnclickOutside([t1Ref, t2Ref], () => {
setShowTips(false);
});
return (
<div>
{showTips && (
<>
<div ref={t1Ref}>Tooltip 1</div>
<div ref={t2Ref}>Tooltip 2</div>
</>
)}
</div>
);
};
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.
import React, { useState, useRef } from 'react';
import useOnclickOutside from 'react-cool-onclickoutside';
// Use default CSS class name
const App = () => {
const ref = useRef();
useOnclickOutside(ref, () => {
// Do something...
});
return (
<div>
<div ref={ref}>I'm a 🍕</div>
<div>Click me will trigger the event's callback</div>
<div className="ignore-onclickoutside">
Click me won't trigger the event's callback
</div>
</div>
);
};
// Use the CSS class name that you defined
const App = () => {
const ref = useRef();
useOnclickOutside(
ref,
() => {
// Do something...
},
{ ignoreClass: 'my-ignore-class' }
);
return (
<div>
<div ref={ref}>I'm a 🍕</div>
<div>Click me will trigger the event's callback</div>
<div className="my-ignore-class">
Click me won't trigger the event's callback
</div>
</div>
);
};
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 as true
, the callback won’t be triggered.
import React, { useState, useRef } from 'react';
import useOnclickOutside from 'react-cool-onclickoutside';
const App = () => {
const [disabled, setDisabled] = useState(false);
const ref = useRef();
useOnclickOutside(
ref,
() => {
// Do something...
},
{ disabled }
);
const handleBtnClick = () => {
setDisabled(true);
};
return (
<div>
<button onClick={handleBtnClick}>
Stop listening for outside clicks
</button>
<div ref={ref}>I'm a 🍎</div>
</div>
);
};
type Ref = RefObject<HTMLElement>;
type Callback = (event?: Event) => void;
useOnclickOutside(ref: Ref | Ref[], callback: Callback, options?: object);
You must pass the ref
and callback
to use this hook and you can access the event
object via the callback parameter, default will be MouseEvent or TouchEvent.
const callback = (event) => {
console.log('Event: ', event);
};
The options
object may contain the following keys.
Key | Type | Default | Description |
---|---|---|---|
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 | ignore-onclickoutside | To ignore certain elements during the event loop by the CSS class name that you defined. |
Thanks goes to these wonderful people (emoji key):
Welly 💻 📖 🚧 |
This project follows the all-contributors specification. Contributions of any kind welcome!
FAQs
React hook to listen for clicks outside of the component(s).
The npm package react-cool-onclickoutside receives a total of 45,600 weekly downloads. As such, react-cool-onclickoutside popularity was classified as popular.
We found that react-cool-onclickoutside demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
Security News
Research
Socket researchers have discovered malicious npm packages targeting crypto developers, stealing credentials and wallet data using spyware delivered through typosquats of popular cryptographic libraries.
Security News
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.