A custom React hook to fire an event when user clicks outside the component.
npm i @bscop/use-click-out
import useClickOut from "@bscop/use-click-out";
const App = () => {
const ref = useClickOut(
() => {
console.log("Click out of #demo");
}
);
return (
<div id="#demo" ref={ref}>
App!
</div>
);
};
useClickOut
accepts a second optional parameter; it can be:
-
a boolean - set to false if the listener should not be active (default is
true
). -
an object, with shape
{ active ?: boolean; ref: React.RefObject<HTMLElement> }
- this is useful in case you already haveref
.
Read the guidelines.
npm test
Coverage reports are hosted on codecov.
npm run badge:coverage -- --token=<guid>
Bruno Scopelliti
www.brunoscopelliti.com