@carbon/ibm-products
Version:
Carbon for IBM Products
137 lines (128 loc) • 4.56 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.
*/
;
var _rollupPluginBabelHelpers = require('../../_virtual/_rollupPluginBabelHelpers.js');
var React = require('react');
var index = require('../../_virtual/index.js');
var cx = require('classnames');
var icons = require('@carbon/react/icons');
var react = require('@carbon/react');
var devtools = require('../../global/js/utils/devtools.js');
var settings = require('../../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 = `${settings.pkg.prefix}--coachmark-dragbar`;
const overlayBlockClass = `${settings.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.
*/
exports.CoachmarkDragbar = /*#__PURE__*/React.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] = React.useState(false);
React.useEffect(() => {
const handleDragStop = () => setIsDragging(false);
window.addEventListener('mouseup', handleDragStop);
return () => {
window.removeEventListener('mouseup', handleDragStop);
};
}, []);
React.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.createElement("header", _rollupPluginBabelHelpers.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
}, devtools.getDevtoolsProps(componentName)), /*#__PURE__*/React.createElement("button", {
type: "button",
className: `${overlayBlockClass}__handle`,
onMouseDown: handleDragStart,
onKeyDown: a11yKeyboardHandler
// TODO: i18n
,
title: "Drag Handle"
}, _Draggable || (_Draggable = /*#__PURE__*/React.createElement(icons.Draggable, {
size: "16"
}))), showCloseButton && /*#__PURE__*/React.createElement(react.Button, {
kind: "ghost",
size: "sm",
renderIcon: icons.Close,
iconDescription: closeIconDescription,
hasIconOnly: true,
className: `${overlayBlockClass}--close-btn`,
onClick: onClose
}));
});
// Return a placeholder if not released and not enabled by feature flag
exports.CoachmarkDragbar = settings.pkg.checkComponentEnabled(exports.CoachmarkDragbar, componentName);
// The display name of the component, used by React. Note that displayName
// is used in preference to relying on function.name.
exports.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.
exports.CoachmarkDragbar.propTypes = {
/**
* Handler to manage keyboard interactions with the dragbar.
*/
a11yKeyboardHandler: index.default.func.isRequired,
/**
* Tooltip text and aria label for the Close button icon.
*/
closeIconDescription: index.default.string,
/**
* Function to call when the close button is clicked.
*/
onClose: index.default.func,
/**
* Function to call when the user clicks and drags the Coachmark.
* For internal use only by the parent CoachmarkOverlay.
*/
onDrag: index.default.func,
/**
* Show/hide the "X" close button.
*/
showCloseButton: index.default.bool,
/**
* Determines the theme of the component.
*/
theme: index.default.oneOf(['light', 'dark'])
};