@carbon/ibm-products
Version:
Carbon for IBM Products
111 lines (105 loc) • 4.13 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.
*/
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, blockClass } from './context.js';
import { ModalBody } from '@carbon/react';
import { Carousel } from '../Carousel/Carousel.js';
import '../Carousel/CarouselItem.js';
import { getDevtoolsProps } from '../../global/js/utils/devtools.js';
const InterstitialScreenBody = /*#__PURE__*/React__default.forwardRef((props, ref) => {
const {
className = '',
contentRenderer,
...rest
} = props;
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
}) ?? props.children;
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 ?? ref
}, getDevtoolsProps('InterstitialScreenBody'), isFullScreen ? 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, _extends({
ref: ref,
className: bodyBlockClass
}, rest), renderBody());
});
InterstitialScreenBody.propTypes = {
/**
* Optional static content for body
*/
children: PropTypes.node,
/**
* Provide an optional class to be applied to the containing node.
*/
className: PropTypes.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: PropTypes.func
};
export { InterstitialScreenBody as default };