@carbon/ibm-products
Version:
Carbon for IBM Products
25 lines (21 loc) • 618 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.
*/
import { useEffect } from 'react';
const useClickOutside = (ref, callback) => {
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]);
};
export { useClickOutside };