UNPKG

@carbon/react

Version:

React components for the Carbon Design System

38 lines (33 loc) 1.18 kB
/** * Copyright IBM Corp. 2016, 2023 * * 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 React, { createContext, useContext, useMemo } from 'react'; import { usePresenceContext } from '../../internal/usePresenceContext.js'; 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__*/React.createElement(ModalPresenceContext.Provider, { value: contextValue }, children); }; const ModalPresenceContext = /*#__PURE__*/createContext(undefined); /** * Handles occurrences where only a single modal must consume a context. */ const useExclusiveModalPresenceContext = id => { const ctx = useContext(ModalPresenceContext); return ctx?.isPresenceExclusive(id) ? ctx : undefined; }; export { ModalPresence, ModalPresenceContext, useExclusiveModalPresenceContext };