UNPKG

@carbon/ibm-products

Version:
178 lines (176 loc) 6.85 kB
/** * 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_settings = require("../../settings.js"); const require_useIsomorphicEffect = require("../../global/js/hooks/useIsomorphicEffect.js"); const require_usePrefersReducedMotion = require("../../global/js/hooks/usePrefersReducedMotion.js"); const require_devtools = require("../../global/js/utils/devtools.js"); require("../Coachmark/utils/enums.js"); const require_context = require("../Coachmark/utils/context.js"); const require_CoachmarkOverlay = require("../Coachmark/CoachmarkOverlay.js"); const require_CoachmarkTagline = require("../Coachmark/CoachmarkTagline.js"); let react = require("react"); react = require_runtime.__toESM(react); let prop_types = require("prop-types"); prop_types = require_runtime.__toESM(prop_types); let react_dom = require("react-dom"); //#region src/components/CoachmarkFixed/CoachmarkFixed.tsx /** * Copyright IBM Corp. 2023, 2024 * * 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 coachmarkClass = `${require_settings.pkg.prefix}--coachmark`; const blockClass = `${coachmarkClass}-fixed`; const overlayBlockClass = `${coachmarkClass}-overlay`; const componentName = "CoachmarkFixed"; const defaults = { onClose: () => {}, theme: "light", tagline: "" }; /** * Fixed coachmarks are used to call out specific functionality or concepts * within the UI that may not be intuitive but are important for the * user to gain understanding of the product's main value and discover new use cases. * This variant allows the a coachmark overlay to be displayed by interacting with the tagline. * @deprecated This component is deprecated. */ const CoachmarkFixed = react.default.forwardRef(({ children, className, onClose = defaults.onClose, portalTarget, tagline = defaults.tagline, theme = defaults.theme, closeIconDescription, ...rest }, ref) => { const overlayRef = (0, react.useRef)(null); const portalNode = (0, react.useRef)(null); const [isOpen, setIsOpen] = (0, react.useState)(false); const [shouldResetPosition, setShouldResetPosition] = (0, react.useState)(false); const [targetRect, setTargetRect] = (0, react.useState)(); const [targetOffset, setTargetOffset] = (0, react.useState)({ x: 0, y: 0 }); const [fixedIsVisible, setFixedIsVisible] = (0, react.useState)(false); const shouldReduceMotion = require_usePrefersReducedMotion.usePrefersReducedMotion(); require_useIsomorphicEffect.useIsomorphicEffect(() => { portalNode.current = portalTarget ? document?.querySelector(portalTarget) ?? document?.querySelector("body") : document?.querySelector("body"); }, [portalTarget]); const handleClose = (0, react.useCallback)(() => { if (shouldReduceMotion) setIsOpen(false); else setFixedIsVisible(false); }, [shouldReduceMotion]); const handleTransitionEnd = (e) => { if (e.propertyName === "transform" && !fixedIsVisible) { setIsOpen(false); onClose(); } }; const handleTargetClick = (e) => { setTargetRect(e.target.getBoundingClientRect()); setTargetOffset({ x: e.target.offsetLeft, y: e.target.offsetTop }); setIsOpen(false); setShouldResetPosition(true); }; const escFunction = (0, react.useCallback)((event) => { if (event.key === "Escape") handleClose(); }, [handleClose]); (0, react.useEffect)(() => { document.addEventListener("keydown", escFunction, false); return () => { document.removeEventListener("keydown", escFunction, false); }; }, [escFunction]); const contextValue = { buttonProps: { "aria-expanded": isOpen, tabIndex: 0, onClick: handleTargetClick, onDoubleClick: handleTargetClick }, closeButtonProps: { onClick: handleClose }, targetRect, targetOffset, isOpen, closeIconDescription }; (0, react.useEffect)(() => { if (shouldResetPosition) { setShouldResetPosition(false); setIsOpen(true); } }, [shouldResetPosition]); (0, react.useEffect)(() => { setFixedIsVisible(isOpen); }, [isOpen]); (0, react.useEffect)(() => { return () => setIsOpen(false); }, []); return /* @__PURE__ */ react.default.createElement(require_context.CoachmarkContext.Provider, { value: contextValue }, /* @__PURE__ */ react.default.createElement("div", { ...rest, className: (0, import_classnames.default)(blockClass, `${blockClass}__${theme}`, className), ref, ...require_devtools.getDevtoolsProps(componentName) }, /* @__PURE__ */ react.default.createElement(require_CoachmarkTagline.CoachmarkTagline, { title: tagline, onClose }), isOpen && portalNode?.current && (0, react_dom.createPortal)(/* @__PURE__ */ react.default.createElement(require_CoachmarkOverlay.CoachmarkOverlay, { ref: overlayRef, fixedIsVisible, kind: "fixed", onClose: handleClose, onTransitionEnd: handleTransitionEnd, theme, className: (0, import_classnames.default)(fixedIsVisible && `${overlayBlockClass}--is-visible`, overlayBlockClass) }, children), portalNode?.current))); }); /**@ts-ignore*/ CoachmarkFixed.deprecated = { level: "warn", details: `${componentName} is deprecated.` }; CoachmarkFixed.displayName = componentName; CoachmarkFixed.propTypes = { /** * CoachmarkFixed should use a single CoachmarkOverlayElements component as a child. */ children: prop_types.default.node.isRequired, /** * Optional class name for this component. */ className: prop_types.default.string, /** * Tooltip text and aria label for the Close button icon. */ closeIconDescription: prop_types.default.string, /** * Function to call when the Coachmark closes. */ onClose: prop_types.default.func, /** * By default, the Coachmark will be appended to the end of `document.body`. * The Coachmark will remain persistent as the user navigates the app until * the user closes the Coachmark. * * Alternatively, the app developer can tightly couple the Coachmark to a DOM * element or other component by specifying a CSS selector. The Coachmark will * remain visible as long as that element remains visible or mounted. When the * element is hidden or component is unmounted, the Coachmark will disappear. */ portalTarget: prop_types.default.string, /** * The tagline title which will be fixed to the bottom right of the window and will serve as the display trigger. */ tagline: prop_types.default.string.isRequired, /** * Determines the theme of the component. */ theme: prop_types.default.oneOf(["light", "dark"]) }; //#endregion exports.CoachmarkFixed = CoachmarkFixed;