UNPKG

@carbon/ibm-products

Version:

Carbon for IBM Products

137 lines (128 loc) 4.5 kB
/** * 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, useEffect } from 'react'; import PropTypes from '../../_virtual/index.js'; import cx from 'classnames'; import { Draggable, Close } from '@carbon/react/icons'; import { Button } from '@carbon/react'; import { getDevtoolsProps } from '../../global/js/utils/devtools.js'; import { pkg } from '../../settings.js'; var _Draggable; // Carbon and package components we use. /* TODO: @import(s) of carbon components and other package components. */ // The block part of our conventional BEM class names (blockClass__E--M). const blockClass = `${pkg.prefix}--coachmark-dragbar`; const overlayBlockClass = `${pkg.prefix}--coachmark-overlay`; const componentName = 'CoachmarkDragbar'; const defaults = { closeIconDescription: 'Close', onDrag: () => {}, onClose: () => {}, showCloseButton: true, theme: 'light' }; /** * DO NOT USE. This component is for the exclusive use * of other Onboarding components. */ let CoachmarkDragbar = /*#__PURE__*/React__default.forwardRef((_ref, ref) => { let { a11yKeyboardHandler, closeIconDescription = defaults.closeIconDescription, onClose = defaults.onClose, onDrag = defaults.onDrag, showCloseButton = defaults.showCloseButton, theme = defaults.theme, ...rest } = _ref; const [isDragging, setIsDragging] = useState(false); useEffect(() => { const handleDragStop = () => setIsDragging(false); window.addEventListener('mouseup', handleDragStop); return () => { window.removeEventListener('mouseup', handleDragStop); }; }, []); useEffect(() => { const handleDrag = event => { onDrag(event.movementX, event.movementY); }; if (isDragging) { window.addEventListener('mousemove', handleDrag); } return () => { window.removeEventListener('mousemove', handleDrag); }; }, [isDragging, onDrag]); const handleDragStart = () => setIsDragging(true); return /*#__PURE__*/React__default.createElement("header", _extends({}, rest, { className: cx(blockClass, // Apply the block class to the main HTML element `${blockClass}__${theme}`, // example: `${blockClass}__template-string-class-${kind}-n-${size}`, { // switched classes dependant on props or state // example: [`${blockClass}__here-if-small`]: size === 'sm', }), ref: ref }, getDevtoolsProps(componentName)), /*#__PURE__*/React__default.createElement("button", { type: "button", className: `${overlayBlockClass}__handle`, onMouseDown: handleDragStart, onKeyDown: a11yKeyboardHandler // TODO: i18n , title: "Drag Handle" }, _Draggable || (_Draggable = /*#__PURE__*/React__default.createElement(Draggable, { size: "16" }))), showCloseButton && /*#__PURE__*/React__default.createElement(Button, { kind: "ghost", size: "sm", renderIcon: Close, iconDescription: closeIconDescription, hasIconOnly: true, className: `${overlayBlockClass}--close-btn`, onClick: onClose })); }); // Return a placeholder if not released and not enabled by feature flag CoachmarkDragbar = pkg.checkComponentEnabled(CoachmarkDragbar, componentName); // The display name of the component, used by React. Note that displayName // is used in preference to relying on function.name. CoachmarkDragbar.displayName = componentName; // The types and DocGen commentary for the component props, // in alphabetical order (for consistency). // See https://www.npmjs.com/package/prop-types#usage. CoachmarkDragbar.propTypes = { /** * Handler to manage keyboard interactions with the dragbar. */ a11yKeyboardHandler: PropTypes.func.isRequired, /** * Tooltip text and aria label for the Close button icon. */ closeIconDescription: PropTypes.string, /** * Function to call when the close button is clicked. */ onClose: PropTypes.func, /** * Function to call when the user clicks and drags the Coachmark. * For internal use only by the parent CoachmarkOverlay. */ onDrag: PropTypes.func, /** * Show/hide the "X" close button. */ showCloseButton: PropTypes.bool, /** * Determines the theme of the component. */ theme: PropTypes.oneOf(['light', 'dark']) }; export { CoachmarkDragbar };