@carbon/ibm-products
Version:
Carbon for IBM Products
168 lines (166 loc) • 7.27 kB
JavaScript
/**
* Copyright IBM Corp. 2020, 2026
*
* This source code is licensed under the Apache-2.0 license found in the
* LICENSE file in the root directory of this source tree.
*/
const require_runtime = require("../../_virtual/_rolldown/runtime.js");
const require_index = require("../../node_modules/classnames/index.js");
const require_pconsole = require("../../global/js/utils/pconsole.js");
const require_settings = require("../../settings.js");
const require_devtools = require("../../global/js/utils/devtools.js");
const require_clamp = require("../../global/js/utils/clamp.js");
const require_context = require("../Coachmark/utils/context.js");
const require_Carousel = require("../Carousel/Carousel.js");
let react = require("react");
react = require_runtime.__toESM(react);
let prop_types = require("prop-types");
prop_types = require_runtime.__toESM(prop_types);
let _carbon_react = require("@carbon/react");
//#region src/components/CoachmarkOverlayElements/CoachmarkOverlayElements.tsx
/**
* Copyright IBM Corp. 2023, 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.
*/
var import_classnames = /* @__PURE__ */ require_runtime.__toESM(require_index.default);
const blockClass = `${require_settings.pkg.prefix}--coachmark-overlay-elements`;
const componentName = "CoachmarkOverlayElements";
const defaults = {
isVisible: false,
nextButtonText: "Next",
previousButtonLabel: "Back",
closeButtonLabel: "Got it",
onNext: void 0,
onBack: void 0,
currentStep: 0
};
/**
* Composable container to allow for the displaying of CoachmarkOverlayElement
* components in a carousel fashion.
* @deprecated This component is deprecated.
*/
const CoachmarkOverlayElements = react.default.forwardRef(({ className, children, isVisible = defaults.isVisible, renderMedia, currentStep = defaults.currentStep, nextButtonText = defaults.nextButtonText, previousButtonLabel = defaults.previousButtonLabel, closeButtonLabel = defaults.closeButtonLabel, onNext = defaults.onNext, onBack = defaults.onBack, ...rest }, ref) => {
const buttonFocusRef = (0, react.useRef)(void 0);
const scrollRef = (0, react.useRef)(void 0);
const [scrollPosition, setScrollPosition] = (0, react.useState)(0);
const [currentProgStep, _setCurrentProgStep] = (0, react.useState)(currentStep);
const coachmark = require_context.useCoachmark();
const setCurrentProgStep = (value) => {
if (currentProgStep > 0 && value === 0 && buttonFocusRef.current) setTimeout(() => {
buttonFocusRef.current?.focus();
}, 1e3);
_setCurrentProgStep(value);
};
const numProgSteps = react.Children.count(children);
const progStepFloor = 0;
const progStepCeil = numProgSteps - 1;
const renderMediaContent = (0, react.useMemo)(() => renderMedia?.({ playStep: currentProgStep }), [currentProgStep, renderMedia]);
(0, react.useEffect)(() => {
const targetStep = require_clamp.clamp(currentStep, progStepFloor, progStepCeil);
scrollRef?.current?.scrollToView?.(targetStep);
}, [currentStep]);
(0, react.useEffect)(() => {
if (buttonFocusRef.current) buttonFocusRef.current.focus();
}, []);
(0, react.useEffect)(() => {
setTimeout(() => {
if (buttonFocusRef.current && isVisible) buttonFocusRef.current.focus();
}, 100);
}, [isVisible]);
if (!coachmark) return require_pconsole.default.warn(`The ${componentName} is a composable container element which should be used only within the scope of a Coachmark or a CoachmarkFixed component.`);
return /* @__PURE__ */ react.default.createElement("section", {
...rest,
className: (0, import_classnames.default)(blockClass, className, {}),
ref,
...require_devtools.getDevtoolsProps(componentName)
}, renderMedia && /* @__PURE__ */ react.default.createElement("div", { className: `${blockClass}__element-stepped-media` }, renderMediaContent), numProgSteps === 1 ? /* @__PURE__ */ react.default.createElement(react.default.Fragment, null, children, closeButtonLabel && /* @__PURE__ */ react.default.createElement("div", { className: (0, import_classnames.default)(`${blockClass}__footer`, "coachmark-carousel-controls") }, /* @__PURE__ */ react.default.createElement(_carbon_react.Button, {
size: "sm",
...coachmark.closeButtonProps,
ref: buttonFocusRef
}, closeButtonLabel))) : /* @__PURE__ */ react.default.createElement(react.default.Fragment, null, /* @__PURE__ */ react.default.createElement(require_Carousel.Carousel, {
ref: scrollRef,
onScroll: (scrollPercent) => {
setScrollPosition(scrollPercent);
}
}, children), /* @__PURE__ */ react.default.createElement("div", { className: (0, import_classnames.default)(`${blockClass}__footer`) }, /* @__PURE__ */ react.default.createElement("div", { className: `${blockClass}--controls-progress` }, `${currentProgStep + 1} / ${numProgSteps}`), scrollPosition > 0 && /* @__PURE__ */ react.default.createElement(_carbon_react.Button, {
size: "sm",
kind: "ghost",
title: previousButtonLabel,
disabled: scrollPosition === 0,
onClick: () => {
const targetStep = require_clamp.clamp(currentProgStep - 1, progStepFloor, progStepCeil);
scrollRef?.current?.scrollToView?.(targetStep);
setCurrentProgStep(targetStep);
onBack?.();
}
}, previousButtonLabel), currentProgStep < progStepCeil ? /* @__PURE__ */ react.default.createElement(_carbon_react.Button, {
size: "sm",
ref: buttonFocusRef,
title: nextButtonText,
disabled: scrollPosition === 1,
onClick: () => {
const targetStep = require_clamp.clamp(currentProgStep + 1, progStepFloor, progStepCeil);
scrollRef?.current?.scrollToView?.(targetStep);
setCurrentProgStep(targetStep);
onNext?.();
}
}, nextButtonText) : closeButtonLabel && /* @__PURE__ */ react.default.createElement(_carbon_react.Button, {
size: "sm",
ref: buttonFocusRef,
...coachmark.closeButtonProps
}, closeButtonLabel))));
});
/**@ts-ignore*/
CoachmarkOverlayElements.deprecated = {
level: "warn",
details: `${componentName} is deprecated.`
};
CoachmarkOverlayElements.displayName = componentName;
CoachmarkOverlayElements.propTypes = {
/**
* CoachmarkOverlayElements should be used with one or many CoachmarkOverlayElement components as children.
* @see CoachmarkOverlayElement
*/
children: prop_types.default.node.isRequired,
/**
* Optional class name for this component.
*/
className: prop_types.default.string,
/**
* The label for the Close button.
*/
closeButtonLabel: prop_types.default.string,
/**
* Current step of the coachmarks
*/
currentStep: prop_types.default.number,
/**
* The visibility of CoachmarkOverlayElements is
* managed in the parent component.
*/
isVisible: prop_types.default.bool,
/**
* The label for the Next button.
*/
nextButtonText: prop_types.default.string,
/**
* Optional callback called when clicking on the Previous button.
*/
onBack: prop_types.default.func,
/**
* Optional callback called when clicking on the Next button.
*/
onNext: prop_types.default.func,
/**
* The label for the Previous button.
*/
previousButtonLabel: prop_types.default.string,
/**
* Optional prop to render any media like images or animated media.
*/
renderMedia: prop_types.default.func
};
//#endregion
exports.CoachmarkOverlayElements = CoachmarkOverlayElements;