@carbon/ibm-products
Version:
Carbon for IBM Products
109 lines (101 loc) • 3.77 kB
JavaScript
/**
* 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.
*/
;
Object.defineProperty(exports, '__esModule', { value: true });
var _rollupPluginBabelHelpers = require('../../_virtual/_rollupPluginBabelHelpers.js');
var React = require('react');
var index = require('../../_virtual/index.js');
var InterstitialScreen = require('./InterstitialScreen.js');
var react = require('@carbon/react');
var settings = require('../../settings.js');
var Carousel = require('../Carousel/Carousel.js');
require('../Carousel/CarouselItem.js');
const InterstitialScreenBody = /*#__PURE__*/React.forwardRef(props => {
const {
className = '',
contentRenderer,
...rest
} = props;
const blockClass = `${settings.pkg.prefix}--interstitial-screen`;
const bodyBlockClass = `${blockClass}--internal-body`;
const [stepType, setStepType] = React.useState();
const {
setBodyChildrenData,
bodyChildrenData,
isFullScreen,
setProgStep,
bodyScrollRef,
scrollRef,
handleGotoStep,
progStep,
setStepCount,
disableButtonConfig,
setDisableButtonConfig
} = React.useContext(InterstitialScreen.InterstitialScreenContext);
const [scrollPercent, setScrollPercent] = React.useState(-1);
React.useEffect(() => {
const _bodyContent = contentRenderer({
handleGotoStep,
progStep,
disableActionButton
});
const isElement = /*#__PURE__*/React.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]);
React.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.createElement("div", _rollupPluginBabelHelpers.extends({
className: `${blockClass}--body ${className}`,
ref: bodyScrollRef
}, rest), /*#__PURE__*/React.createElement("div", {
className: `${blockClass}--content`
}, stepType === 'multi' ? /*#__PURE__*/React.createElement("div", {
className: `${blockClass}__carousel`
}, /*#__PURE__*/React.createElement(Carousel.Carousel, {
disableArrowScroll: true,
ref: scrollRef,
onScroll: onScrollHandler
}, bodyChildrenData)) : stepType === 'single' ? bodyChildrenData : ''));
return isFullScreen ? renderBody() : /*#__PURE__*/React.createElement(react.ModalBody, {
className: bodyBlockClass
}, renderBody());
});
InterstitialScreenBody.propTypes = {
/**
* Provide an optional class to be applied to the containing node.
*/
className: index.default.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: index.default.func.isRequired
};
exports.default = InterstitialScreenBody;