@carbon/ibm-products
Version:
Carbon for IBM Products
30 lines (28 loc) • 889 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.
*/
require("../../../_virtual/_rolldown/runtime.js");
let react = require("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) => {
(0, 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]);
};
//#endregion
exports.useClickOutside = useClickOutside;