@carbon/ibm-products
Version:
Carbon for IBM Products
29 lines (27 loc) • 817 B
JavaScript
/**
* Copyright IBM Corp. 2020, 2026
*
* 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";
//#region src/global/js/hooks/useClickOutside.js
/**
* Copyright IBM Corp. 2020, 2021
*
* This source code is licensed under the Apache-2.0 license found in the
* LICENSE file in the root directory of this source tree.
*/
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]);
};
//#endregion
export { useClickOutside };