@carbon/ibm-products
Version:
Carbon for IBM Products
161 lines (159 loc) • 6.89 kB
JavaScript
/**
* 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 { getDevtoolsProps } from "../../global/js/utils/devtools.js";
import uuidv4 from "../../global/js/utils/uuidv4.js";
import { getComponentText } from "./utils.js";
import React, { useEffect, useMemo, useRef, useState } from "react";
import PropTypes from "prop-types";
import { Button, Heading, IconButton, Section } from "@carbon/react";
import { Close, Crossroads, Idea } from "@carbon/react/icons";
//#region src/components/InlineTip/InlineTip.tsx
/**
* Copyright IBM Corp. 2023, 2023
*
* 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 blockClass = `${pkg.prefix}--inline-tip`;
const componentName = "InlineTip";
const defaults = {
closeIconDescription: "Close",
collapsible: false,
collapseButtonLabel: "Read less",
expandButtonLabel: "Read more",
narrow: false,
withLeftGutter: false,
onClick: () => {},
onClose: () => {},
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.
*/
const InlineTip = React.forwardRef(({ 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.Children.toArray(children)), [children]);
let childrenToRender = children;
if (!renderMedia && collapsible && isCollapsed) childrenToRender = /* @__PURE__ */ React.createElement("p", { className: `${blockClass}__preview-text` }, previewText);
useEffect(() => {
setIsCollapsed(collapsible);
}, [collapsible]);
return /* @__PURE__ */ React.createElement(Section, {
...rest,
"aria-labelledby": labelId,
className: (0, import_classnames.default)(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,
role: "complementary",
...getDevtoolsProps(componentName)
}, /* @__PURE__ */ React.createElement("div", { className: `${blockClass}__close-icon-wrapper` }, /* @__PURE__ */ React.createElement(IconButton, {
className: `${blockClass}__close-icon`,
kind: "ghost",
label: closeIconDescription,
onClick: onClose,
size: "lg"
}, /* @__PURE__ */ React.createElement(Close, { size: 16 }))), (!renderMedia && narrow || !narrow) && /* @__PURE__ */ React.createElement("div", {
className: `${blockClass}__icon-idea`,
tabIndex: -1
}, /* @__PURE__ */ React.createElement(Idea, { size: 16 })), /* @__PURE__ */ React.createElement("div", { className: `${blockClass}__content` }, /* @__PURE__ */ React.createElement(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(Button, {
className: `${blockClass}__toggle-btn`,
kind: "ghost",
size: "md",
onClick: () => {
setIsCollapsed((prevState) => !prevState);
}
}, isCollapsed ? expandButtonLabel : collapseButtonLabel), tertiaryButtonLabel && /* @__PURE__ */ React.createElement(Button, {
className: `${blockClass}__close-btn`,
size: "md",
onClick,
kind: "tertiary",
renderIcon: () => /* @__PURE__ */ React.createElement(Crossroads, { size: 16 })
}, tertiaryButtonLabel))), renderMedia && /* @__PURE__ */ React.createElement("div", { className: `${blockClass}__media` }, renderMedia()));
});
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
};
//#endregion
export { InlineTip };