UNPKG

@carbon/react

Version:

React components for the Carbon Design System

32 lines (30 loc) 1.06 kB
/** * Copyright IBM Corp. 2016, 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"); const require_useEvent = require("./useEvent.js"); const require_environment = require("./environment.js"); let react = require("react"); //#region src/internal/useOutsideClick.ts /** * Copyright IBM Corp. 2016, 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. */ const useOutsideClick = (ref, callback) => { const savedCallback = (0, react.useRef)(callback); (0, react.useEffect)(() => { savedCallback.current = callback; }, [callback]); require_useEvent.useWindowEvent("click", (event) => { if (!require_environment.canUseDOM) return; const { target } = event; if (target instanceof Node && ref.current && !ref.current.contains(target)) savedCallback.current(event); }); }; //#endregion exports.useOutsideClick = useOutsideClick;