@carbon/react
Version:
React components for the Carbon Design System
46 lines (44 loc) • 1.54 kB
JavaScript
/**
* 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_usePresence = require("./usePresence.js");
let react = require("react");
//#region src/internal/usePresenceContext.ts
/**
* Copyright IBM Corp. 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.
*/
/**
* Returns if the presence node is present and the context value to be used by a presence context, e.g. ModalPresence.
*/
const usePresenceContext = (open, initialPresenceId) => {
const presenceIdRef = (0, react.useRef)(initialPresenceId);
const presenceRef = (0, react.useRef)(null);
const prevPresenceRef = (0, react.useRef)(null);
if (!initialPresenceId && prevPresenceRef.current && !presenceRef.current) presenceIdRef.current = null;
prevPresenceRef.current = presenceRef.current;
const { isPresent, isExiting } = require_usePresence.usePresence(presenceRef, open);
const isPresenceExclusive = (0, react.useCallback)((id) => {
if (!id) return false;
if (presenceIdRef.current && presenceIdRef.current !== id) return false;
presenceIdRef.current = id;
return true;
}, []);
return [isPresent, (0, react.useMemo)(() => ({
presenceRef,
isPresenceExclusive,
isExiting
}), [
presenceRef,
isPresenceExclusive,
isExiting
])];
};
//#endregion
exports.usePresenceContext = usePresenceContext;