React hook to detect clicks outside of the components. With shadow DOM support.
MIT License
React hook to execute callback when a click is happened outside of component.
yarn add use-click-outside
import React, { useRef } from 'react';
import useClickOutside from 'use-click-outside';
const Modal = ({ onClose }) => {
const ref = useRef();
useClickOutside(ref, onClose);
return <dialog ref={ref}>Hello, World!</dialog>;
};
Or if you need a custom event type instead of default 'click', third parameter can be passed:
useClickOutside(ref, onClose, 'mousedown');
Give a ⭐️ if this project helped you!
This README was generated with ❤️ by readme-md-generator