UNPKG

@carbon/react

Version:

React components for the Carbon Design System

42 lines (36 loc) 1.28 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. */ 'use strict'; var React = require('react'); var usePresenceContext = require('../../internal/usePresenceContext.js'); const ModalPresence = ({ open, _presenceId: presenceId, _autoEnablePresence: autoEnablePresence = true, children }) => { const [isPresent, context] = usePresenceContext.usePresenceContext(open, presenceId); const contextValue = React.useMemo(() => ({ autoEnablePresence, ...context }), [autoEnablePresence, context]); if (!isPresent) return null; return /*#__PURE__*/React.createElement(ModalPresenceContext.Provider, { value: contextValue }, children); }; const ModalPresenceContext = /*#__PURE__*/React.createContext(undefined); /** * Handles occurrences where only a single modal must consume a context. */ const useExclusiveModalPresenceContext = id => { const ctx = React.useContext(ModalPresenceContext); return ctx?.isPresenceExclusive(id) ? ctx : undefined; }; exports.ModalPresence = ModalPresence; exports.ModalPresenceContext = ModalPresenceContext; exports.useExclusiveModalPresenceContext = useExclusiveModalPresenceContext;