@carbon/ibm-products
Version:
Carbon for IBM Products
199 lines (191 loc) • 7.41 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.
*/
import { extends as _extends } from '../../_virtual/_rollupPluginBabelHelpers.js';
import { Section, IconButton, Heading, Button } from '@carbon/react';
import { Close, Idea, Crossroads } from '@carbon/react/icons';
import React__default, { useState, useRef, useMemo, useEffect } from 'react';
import PropTypes from '../../_virtual/index.js';
import cx from 'classnames';
import { getComponentText } from './utils.js';
import { getDevtoolsProps } from '../../global/js/utils/devtools.js';
import { pkg } from '../../settings.js';
import uuidv4 from '../../global/js/utils/uuidv4.js';
var _Close, _Idea, _Crossroads;
// The block part of our conventional BEM class names (blockClass__E--M).
const blockClass = `${pkg.prefix}--inline-tip`;
const componentName = 'InlineTip';
// NOTE: the component SCSS is not imported here: it is rolled up separately.
// Default values for props
const defaults = {
closeIconDescription: 'Close',
collapsible: false,
collapseButtonLabel: 'Read less',
expandButtonLabel: 'Read more',
narrow: false,
withLeftGutter: false,
title: 'Use case-specific heading'
};
/**
* Inline tips are messages embedded within other components that
* provide an ambient way to deliver learning content without
* distracting the user from their flow.
*/
let InlineTip = /*#__PURE__*/React__default.forwardRef((_ref, ref) => {
let {
action,
children,
className,
closeIconDescription = defaults.closeIconDescription,
collapsible = defaults.collapsible,
collapseButtonLabel = defaults.collapseButtonLabel,
expandButtonLabel = defaults.expandButtonLabel,
renderMedia,
narrow = defaults.narrow,
onClick,
onClose,
tertiaryButtonLabel,
title = defaults.title,
withLeftGutter = defaults.withLeftGutter,
...rest
} = _ref;
const [isCollapsed, setIsCollapsed] = useState(collapsible);
const labelId = useRef(uuidv4()).current;
const previewText = useMemo(() => getComponentText(React__default.Children.toArray(children)), [children]);
let childrenToRender = children;
if (!renderMedia && collapsible && isCollapsed) {
childrenToRender = /*#__PURE__*/React__default.createElement("p", {
className: `${blockClass}__preview-text`
}, previewText);
}
// If `collapsible` is changed after initial render...
useEffect(() => {
setIsCollapsed(collapsible);
}, [collapsible]);
return /*#__PURE__*/React__default.createElement(Section, _extends({}, rest, {
"aria-labelledby": labelId,
className: cx(blockClass, className, collapsible && `${blockClass}__collapsible`, isCollapsed && `${blockClass}__collapsible-collapsed`, renderMedia && `${blockClass}__has-media`, [narrow ? `${blockClass}__narrow` : `${blockClass}__wide`], withLeftGutter && !narrow && `${blockClass}__with-left-gutter`),
ref: ref,
role: "complementary"
}, getDevtoolsProps(componentName)), /*#__PURE__*/React__default.createElement("div", {
className: `${blockClass}__close-icon-wrapper`
}, /*#__PURE__*/React__default.createElement(IconButton, {
className: `${blockClass}__close-icon`,
kind: "ghost",
label: closeIconDescription,
onClick: onClose,
size: "lg"
}, _Close || (_Close = /*#__PURE__*/React__default.createElement(Close, {
size: 16
})))), (!renderMedia && narrow || !narrow) && /*#__PURE__*/React__default.createElement("div", {
className: `${blockClass}__icon-idea`,
tabIndex: -1
}, _Idea || (_Idea = /*#__PURE__*/React__default.createElement(Idea, {
size: 16
}))), /*#__PURE__*/React__default.createElement("div", {
className: `${blockClass}__content`
}, /*#__PURE__*/React__default.createElement(Heading, {
id: labelId,
className: `${blockClass}__title`
}, title), /*#__PURE__*/React__default.createElement("section", {
className: `${blockClass}__body`
}, childrenToRender, action && (!collapsible || collapsible && !isCollapsed) && /*#__PURE__*/React__default.createElement("div", {
className: `${blockClass}__secondary-btn`
}, action)), (collapsible || tertiaryButtonLabel) && /*#__PURE__*/React__default.createElement("footer", {
className: `${blockClass}__footer`
}, collapsible && !renderMedia && /*#__PURE__*/React__default.createElement(Button, {
className: `${blockClass}__toggle-btn`,
kind: "ghost",
size: "md",
onClick: () => {
setIsCollapsed(prevState => !prevState);
}
}, isCollapsed ? expandButtonLabel : collapseButtonLabel), tertiaryButtonLabel && /*#__PURE__*/React__default.createElement(Button, {
className: `${blockClass}__close-btn`,
size: "md",
onClick: onClick,
kind: "tertiary",
renderIcon: () => _Crossroads || (_Crossroads = /*#__PURE__*/React__default.createElement(Crossroads, {
size: 16
}))
}, tertiaryButtonLabel))), renderMedia && /*#__PURE__*/React__default.createElement("div", {
className: `${blockClass}__media`
}, renderMedia()));
});
InlineTip = pkg.checkComponentEnabled(InlineTip, componentName);
InlineTip.displayName = componentName;
InlineTip.propTypes = {
/**
* Optional "call to action" ghost button or link that can appear
* directly below the content. This component comes with pre-styled
* elements available to use: `InlineTipLink` and `InlineTipButton`.
*/
action: PropTypes.node,
/**
* Provide the contents of the InlineTip.
*/
children: PropTypes.node.isRequired,
/**
* Provide an optional class to be applied to the containing node.
*/
className: PropTypes.string,
/**
* Tooltip text and aria label for the Close button icon.
*/
closeIconDescription: PropTypes.string,
/**
* The label for the collapse button.
* This button is not visible if `media` is specified.
*/
collapseButtonLabel: PropTypes.string,
/**
* If set to `true`, it will truncate the body text to
* one line and expose an expand/collapse button toggle.
*
* This feature is disabled if `media` is specified.
*/
collapsible: PropTypes.bool,
/**
* The label for the expand button.
* This button is not visible if `media` is specified.
*/
expandButtonLabel: PropTypes.string,
/**
* Set to `true` to arrange the information in a format
* that is easier to read in a limited space.
*/
narrow: PropTypes.bool,
/**
* Function to call when the tertiary button is clicked.
*/
onClick: PropTypes.func,
/**
* Function to call when the InlineTip is closed via the "X" button.
*/
onClose: PropTypes.func,
/**
* Optional prop to render any media like images or animated media.
*/
renderMedia: PropTypes.func,
/**
* Defining the label will show a the tertiary button with the crossroads icon.
* You will still need to define the `onClose` method to trigger a callback.
*/
tertiaryButtonLabel: PropTypes.string,
/**
* The title of the InlineTip.
*/
title: PropTypes.string.isRequired,
/**
* If true, insert 1 rem of "space" on the left of the component.
* This will allow the component's content to line up with other
* content on the page under special circumstances.
*
* This will only be applied when `narrow` is false.
*/
withLeftGutter: PropTypes.bool
};
export { InlineTip };