UNPKG

@carbon/ibm-products

Version:

Carbon for IBM Products

105 lines (99 loc) 3.7 kB
/** * Copyright IBM Corp. 2020, 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. */ import { extends as _extends } from '../../_virtual/_rollupPluginBabelHelpers.js'; import React__default, { useState, useContext, useEffect, isValidElement } from 'react'; import PropTypes from '../../_virtual/index.js'; import { InterstitialScreenContext } from './InterstitialScreen.js'; import { ModalBody } from '@carbon/react'; import { pkg } from '../../settings.js'; import { Carousel } from '../Carousel/Carousel.js'; import '../Carousel/CarouselItem.js'; const InterstitialScreenBody = /*#__PURE__*/React__default.forwardRef(props => { const { className = '', contentRenderer, ...rest } = props; const blockClass = `${pkg.prefix}--interstitial-screen`; const bodyBlockClass = `${blockClass}--internal-body`; const [stepType, setStepType] = useState(); const { setBodyChildrenData, bodyChildrenData, isFullScreen, setProgStep, bodyScrollRef, scrollRef, handleGotoStep, progStep, setStepCount, disableButtonConfig, setDisableButtonConfig } = useContext(InterstitialScreenContext); const [scrollPercent, setScrollPercent] = useState(-1); useEffect(() => { const _bodyContent = contentRenderer({ handleGotoStep, progStep, disableActionButton }); const isElement = /*#__PURE__*/isValidElement(_bodyContent); const children = isElement ? _bodyContent.props.children : _bodyContent; // Set body children data setBodyChildrenData?.(children); // If the children is an array, treat it as a multiStep if (isElement && Array.isArray(children)) { setStepType('multi'); setStepCount?.(children.length); } else { setStepType('single'); } // eslint-disable-next-line react-hooks/exhaustive-deps }, [progStep]); useEffect(() => { if (scrollPercent !== -1) { const stepSize = bodyChildrenData && bodyChildrenData.length > 1 ? parseFloat((1 / (bodyChildrenData.length - 1)).toFixed(2)) : 0; const currentStep = scrollPercent / stepSize; setProgStep?.(Math.ceil(currentStep)); } // eslint-disable-next-line react-hooks/exhaustive-deps }, [scrollPercent]); const disableActionButton = config => { setDisableButtonConfig?.({ ...disableButtonConfig, ...config }); }; const onScrollHandler = scrollPercent => setScrollPercent(scrollPercent); const renderBody = () => /*#__PURE__*/React__default.createElement("div", _extends({ className: `${blockClass}--body ${className}`, ref: bodyScrollRef }, rest), /*#__PURE__*/React__default.createElement("div", { className: `${blockClass}--content` }, stepType === 'multi' ? /*#__PURE__*/React__default.createElement("div", { className: `${blockClass}__carousel` }, /*#__PURE__*/React__default.createElement(Carousel, { disableArrowScroll: true, ref: scrollRef, onScroll: onScrollHandler }, bodyChildrenData)) : stepType === 'single' ? bodyChildrenData : '')); return isFullScreen ? renderBody() : /*#__PURE__*/React__default.createElement(ModalBody, { className: bodyBlockClass }, renderBody()); }); InterstitialScreenBody.propTypes = { /** * Provide an optional class to be applied to the containing node. */ className: PropTypes.string, /** * This is a required callback that has to return the content to render in the body section. * It can be a single child or an array of children depending on your need */ contentRenderer: PropTypes.func.isRequired }; export { InterstitialScreenBody as default };