UNPKG

@carbon/react

Version:

React components for the Carbon Design System

51 lines (49 loc) 1.84 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. */ import { usePresenceContext } from "../../internal/usePresenceContext.js"; import { createContext, useContext, useMemo } from "react"; import { jsx } from "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] = usePresenceContext(open, presenceId); const contextValue = useMemo(() => ({ autoEnablePresence, ...context }), [autoEnablePresence, context]); if (!isPresent) return null; return /* @__PURE__ */ jsx(ModalPresenceContext.Provider, { value: contextValue, children }); }; const ModalPresenceContext = createContext(void 0); /** * Handles occurrences where only a single modal must consume a context. */ const useExclusiveModalPresenceContext = (id) => { const ctx = 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__ */ jsx(ModalPresence, { open, children: /* @__PURE__ */ jsx(Component, { ...componentProps }) }); WithModalPresence.displayName = `withModalPresence(${Component.displayName || Component.name || "Component"})`; return WithModalPresence; }; //#endregion export { ModalPresence, ModalPresenceContext, useExclusiveModalPresenceContext, withModalPresence };