UNPKG

@carbon/ibm-products

Version:
176 lines (174 loc) 6.29 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. */ import { __toESM } from "../../_virtual/_rolldown/runtime.js"; import { require_classnames } from "../../node_modules/classnames/index.js"; import { pkg } from "../../settings.js"; import { useIsomorphicEffect } from "../../global/js/hooks/useIsomorphicEffect.js"; import { usePrefersReducedMotion } from "../../global/js/hooks/usePrefersReducedMotion.js"; import { getDevtoolsProps } from "../../global/js/utils/devtools.js"; import "../Coachmark/utils/enums.js"; import { CoachmarkContext } from "../Coachmark/utils/context.js"; import { CoachmarkOverlay } from "../Coachmark/CoachmarkOverlay.js"; import { CoachmarkTagline } from "../Coachmark/CoachmarkTagline.js"; import React, { useCallback, useEffect, useRef, useState } from "react"; import PropTypes from "prop-types"; import { createPortal } from "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__ */ __toESM(require_classnames()); const coachmarkClass = `${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.forwardRef(({ children, className, onClose = defaults.onClose, portalTarget, tagline = defaults.tagline, theme = defaults.theme, closeIconDescription, ...rest }, ref) => { const overlayRef = useRef(null); const portalNode = useRef(null); const [isOpen, setIsOpen] = useState(false); const [shouldResetPosition, setShouldResetPosition] = useState(false); const [targetRect, setTargetRect] = useState(); const [targetOffset, setTargetOffset] = useState({ x: 0, y: 0 }); const [fixedIsVisible, setFixedIsVisible] = useState(false); const shouldReduceMotion = usePrefersReducedMotion(); useIsomorphicEffect(() => { portalNode.current = portalTarget ? document?.querySelector(portalTarget) ?? document?.querySelector("body") : document?.querySelector("body"); }, [portalTarget]); const handleClose = 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 = useCallback((event) => { if (event.key === "Escape") handleClose(); }, [handleClose]); 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 }; useEffect(() => { if (shouldResetPosition) { setShouldResetPosition(false); setIsOpen(true); } }, [shouldResetPosition]); useEffect(() => { setFixedIsVisible(isOpen); }, [isOpen]); useEffect(() => { return () => setIsOpen(false); }, []); return /* @__PURE__ */ React.createElement(CoachmarkContext.Provider, { value: contextValue }, /* @__PURE__ */ React.createElement("div", { ...rest, className: (0, import_classnames.default)(blockClass, `${blockClass}__${theme}`, className), ref, ...getDevtoolsProps(componentName) }, /* @__PURE__ */ React.createElement(CoachmarkTagline, { title: tagline, onClose }), isOpen && portalNode?.current && createPortal(/* @__PURE__ */ React.createElement(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: PropTypes.node.isRequired, /** * Optional class name for this component. */ className: PropTypes.string, /** * Tooltip text and aria label for the Close button icon. */ closeIconDescription: PropTypes.string, /** * Function to call when the Coachmark closes. */ onClose: PropTypes.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: PropTypes.string, /** * The tagline title which will be fixed to the bottom right of the window and will serve as the display trigger. */ tagline: PropTypes.string.isRequired, /** * Determines the theme of the component. */ theme: PropTypes.oneOf(["light", "dark"]) }; //#endregion export { CoachmarkFixed };