@carbon/ibm-products
Version:
Carbon for IBM Products
27 lines (22 loc) • 649 B
JavaScript
/**
* Copyright IBM Corp. 2020, 2025
*
* This source code is licensed under the Apache-2.0 license found in the
* LICENSE file in the root directory of this source tree.
*/
;
var React = require('react');
const useClickOutside = (ref, callback) => {
React.useEffect(() => {
const handleClick = event => {
if (ref.current && !ref.current.contains(event.target)) {
callback(event.target);
}
};
document.addEventListener('click', handleClick);
return () => {
document.removeEventListener('click', handleClick);
};
}, [callback, ref]);
};
exports.useClickOutside = useClickOutside;