@carbon/ibm-products
Version:
Carbon for IBM Products
115 lines (107 loc) • 4.22 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 context = require('./context.js');
var react = require('@carbon/react');
var Carousel = require('../Carousel/Carousel.js');
require('../Carousel/CarouselItem.js');
var devtools = require('../../global/js/utils/devtools.js');
const InterstitialScreenBody = /*#__PURE__*/React.forwardRef((props, ref) => {
const {
className = '',
contentRenderer,
...rest
} = props;
const bodyBlockClass = `${context.blockClass}--internal-body`;
const [stepType, setStepType] = React.useState();
const {
setBodyChildrenData,
bodyChildrenData,
isFullScreen,
setProgStep,
bodyScrollRef,
scrollRef,
handleGotoStep,
progStep,
setStepCount,
disableButtonConfig,
setDisableButtonConfig
} = React.useContext(context.InterstitialScreenContext);
const [scrollPercent, setScrollPercent] = React.useState(-1);
React.useEffect(() => {
const _bodyContent = contentRenderer?.({
handleGotoStep,
progStep,
disableActionButton
}) ?? props.children;
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: `${context.blockClass}--body ${className}`,
ref: bodyScrollRef ?? ref
}, devtools.getDevtoolsProps('InterstitialScreenBody'), isFullScreen ? rest : {}), /*#__PURE__*/React.createElement("div", {
className: `${context.blockClass}--content`
}, stepType === 'multi' ? /*#__PURE__*/React.createElement("div", {
className: `${context.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, _rollupPluginBabelHelpers.extends({
ref: ref,
className: bodyBlockClass
}, rest), renderBody());
});
InterstitialScreenBody.propTypes = {
/**
* Optional static content for body
*/
children: index.default.node,
/**
* Provide an optional class to be applied to the containing node.
*/
className: index.default.string,
/**
*You can provide content either through a callback (contentRenderer) or as static children—but not both.
* If both are provided, contentRenderer always takes precedence. This optional callback should return the content
* to be rendered in the body section, which can be either a single element or an array of elements based on your needs.
* If internal state access isn’t required, you can simply use static children instead
*/
contentRenderer: index.default.func
};
exports.default = InterstitialScreenBody;