@carbon/ibm-products
Version:
Carbon for IBM Products
199 lines (191 loc) • 7.36 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.
*/
'use strict';
var _rollupPluginBabelHelpers = require('../../_virtual/_rollupPluginBabelHelpers.js');
var react = require('@carbon/react');
var icons = require('@carbon/react/icons');
var React = require('react');
var index = require('../../_virtual/index.js');
var cx = require('classnames');
var utils = require('./utils.js');
var devtools = require('../../global/js/utils/devtools.js');
var settings = require('../../settings.js');
var uuidv4 = require('../../global/js/utils/uuidv4.js');
var _Close, _Idea, _Crossroads;
// The block part of our conventional BEM class names (blockClass__E--M).
const blockClass = `${settings.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.
*/
exports.InlineTip = /*#__PURE__*/React.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] = React.useState(collapsible);
const labelId = React.useRef(uuidv4.default()).current;
const previewText = React.useMemo(() => utils.getComponentText(React.Children.toArray(children)), [children]);
let childrenToRender = children;
if (!renderMedia && collapsible && isCollapsed) {
childrenToRender = /*#__PURE__*/React.createElement("p", {
className: `${blockClass}__preview-text`
}, previewText);
}
// If `collapsible` is changed after initial render...
React.useEffect(() => {
setIsCollapsed(collapsible);
}, [collapsible]);
return /*#__PURE__*/React.createElement(react.Section, _rollupPluginBabelHelpers.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"
}, devtools.getDevtoolsProps(componentName)), /*#__PURE__*/React.createElement("div", {
className: `${blockClass}__close-icon-wrapper`
}, /*#__PURE__*/React.createElement(react.IconButton, {
className: `${blockClass}__close-icon`,
kind: "ghost",
label: closeIconDescription,
onClick: onClose,
size: "lg"
}, _Close || (_Close = /*#__PURE__*/React.createElement(icons.Close, {
size: 16
})))), (!renderMedia && narrow || !narrow) && /*#__PURE__*/React.createElement("div", {
className: `${blockClass}__icon-idea`,
tabIndex: -1
}, _Idea || (_Idea = /*#__PURE__*/React.createElement(icons.Idea, {
size: 16
}))), /*#__PURE__*/React.createElement("div", {
className: `${blockClass}__content`
}, /*#__PURE__*/React.createElement(react.Heading, {
id: labelId,
className: `${blockClass}__title`
}, title), /*#__PURE__*/React.createElement("section", {
className: `${blockClass}__body`
}, childrenToRender, action && (!collapsible || collapsible && !isCollapsed) && /*#__PURE__*/React.createElement("div", {
className: `${blockClass}__secondary-btn`
}, action)), (collapsible || tertiaryButtonLabel) && /*#__PURE__*/React.createElement("footer", {
className: `${blockClass}__footer`
}, collapsible && !renderMedia && /*#__PURE__*/React.createElement(react.Button, {
className: `${blockClass}__toggle-btn`,
kind: "ghost",
size: "md",
onClick: () => {
setIsCollapsed(prevState => !prevState);
}
}, isCollapsed ? expandButtonLabel : collapseButtonLabel), tertiaryButtonLabel && /*#__PURE__*/React.createElement(react.Button, {
className: `${blockClass}__close-btn`,
size: "md",
onClick: onClick,
kind: "tertiary",
renderIcon: () => _Crossroads || (_Crossroads = /*#__PURE__*/React.createElement(icons.Crossroads, {
size: 16
}))
}, tertiaryButtonLabel))), renderMedia && /*#__PURE__*/React.createElement("div", {
className: `${blockClass}__media`
}, renderMedia()));
});
exports.InlineTip = settings.pkg.checkComponentEnabled(exports.InlineTip, componentName);
exports.InlineTip.displayName = componentName;
exports.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: index.default.node,
/**
* Provide the contents of the InlineTip.
*/
children: index.default.node.isRequired,
/**
* Provide an optional class to be applied to the containing node.
*/
className: index.default.string,
/**
* Tooltip text and aria label for the Close button icon.
*/
closeIconDescription: index.default.string,
/**
* The label for the collapse button.
* This button is not visible if `media` is specified.
*/
collapseButtonLabel: index.default.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: index.default.bool,
/**
* The label for the expand button.
* This button is not visible if `media` is specified.
*/
expandButtonLabel: index.default.string,
/**
* Set to `true` to arrange the information in a format
* that is easier to read in a limited space.
*/
narrow: index.default.bool,
/**
* Function to call when the tertiary button is clicked.
*/
onClick: index.default.func,
/**
* Function to call when the InlineTip is closed via the "X" button.
*/
onClose: index.default.func,
/**
* Optional prop to render any media like images or animated media.
*/
renderMedia: index.default.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: index.default.string,
/**
* The title of the InlineTip.
*/
title: index.default.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: index.default.bool
};