@carbon/react
Version:
React components for the Carbon Design System
59 lines (57 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.
*/
import { usePresenceContext } from "../../internal/usePresenceContext.js";
import { useComposedModalState } from "./useComposedModalState.js";
import { createContext, useContext, useMemo } from "react";
import { jsx } from "react/jsx-runtime";
//#region src/components/ComposedModal/ComposedModalPresence.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 ComposedModalPresence = ({ open, _presenceId: presenceId, _autoEnablePresence: autoEnablePresence = true, children }) => {
const modalState = useComposedModalState(open);
const [isOpen] = modalState;
const [isPresent, context] = usePresenceContext(isOpen, presenceId);
const presenceContextValue = useMemo(() => ({
modalState,
autoEnablePresence,
...context
}), [
modalState,
autoEnablePresence,
context
]);
if (!isPresent) return null;
return /* @__PURE__ */ jsx(ComposedModalPresenceContext, {
value: presenceContextValue,
children
});
};
const ComposedModalPresenceContext = createContext(void 0);
/**
* Handles occurrences where only a single composed modal must consume a context.
*/
const useExclusiveComposedModalPresenceContext = (id) => {
const ctx = useContext(ComposedModalPresenceContext);
return ctx?.isPresenceExclusive(id) ? ctx : void 0;
};
/**
* Higher-order function that wraps a component with ComposedModalPresence
*/
const withComposedModalPresence = (Component) => {
const WithComposedModalPresence = ({ open, ...componentProps }) => /* @__PURE__ */ jsx(ComposedModalPresence, {
open,
children: /* @__PURE__ */ jsx(Component, { ...componentProps })
});
WithComposedModalPresence.displayName = `withComposedModalPresence(${Component.displayName || Component.name || "Component"})`;
return WithComposedModalPresence;
};
//#endregion
export { ComposedModalPresence, ComposedModalPresenceContext, useExclusiveComposedModalPresenceContext, withComposedModalPresence };