@carbon/react
Version:
React components for the Carbon Design System
56 lines (54 loc) • 2.19 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.
*/
const require_runtime = require("../../_virtual/_rolldown/runtime.js");
const require_usePresenceContext = require("../../internal/usePresenceContext.js");
let react = require("react");
react = require_runtime.__toESM(react);
let react_jsx_runtime = require("react/jsx-runtime");
//#region src/components/Modal/ModalPresence.tsx
/**
* Copyright IBM Corp. 2016, 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.
*/
const ModalPresence = ({ open, _presenceId: presenceId, _autoEnablePresence: autoEnablePresence = true, children }) => {
const [isPresent, context] = require_usePresenceContext.usePresenceContext(open, presenceId);
const contextValue = (0, react.useMemo)(() => ({
autoEnablePresence,
...context
}), [autoEnablePresence, context]);
if (!isPresent) return null;
return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(ModalPresenceContext.Provider, {
value: contextValue,
children
});
};
const ModalPresenceContext = (0, react.createContext)(void 0);
/**
* Handles occurrences where only a single modal must consume a context.
*/
const useExclusiveModalPresenceContext = (id) => {
const ctx = (0, react.useContext)(ModalPresenceContext);
return ctx?.isPresenceExclusive(id) ? ctx : void 0;
};
/**
* Higher-order function that wraps a component with ModalPresence
*/
const withModalPresence = (Component) => {
const WithModalPresence = ({ open, ...componentProps }) => /* @__PURE__ */ (0, react_jsx_runtime.jsx)(ModalPresence, {
open,
children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(Component, { ...componentProps })
});
WithModalPresence.displayName = `withModalPresence(${Component.displayName || Component.name || "Component"})`;
return WithModalPresence;
};
//#endregion
exports.ModalPresence = ModalPresence;
exports.ModalPresenceContext = ModalPresenceContext;
exports.useExclusiveModalPresenceContext = useExclusiveModalPresenceContext;
exports.withModalPresence = withModalPresence;