UNPKG

@carbon/ibm-products

Version:
163 lines (161 loc) 7.48 kB
/** * 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. */ const require_runtime = require("../../_virtual/_rolldown/runtime.js"); const require_index = require("../../node_modules/classnames/index.js"); const require_settings = require("../../settings.js"); const require_devtools = require("../../global/js/utils/devtools.js"); const require_uuidv4 = require("../../global/js/utils/uuidv4.js"); const require_utils = require("./utils.js"); let react = require("react"); react = require_runtime.__toESM(react); let prop_types = require("prop-types"); prop_types = require_runtime.__toESM(prop_types); let _carbon_react = require("@carbon/react"); let _carbon_react_icons = require("@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__ */ require_runtime.__toESM(require_index.default); const blockClass = `${require_settings.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.default.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] = (0, react.useState)(collapsible); const labelId = (0, react.useRef)(require_uuidv4.default()).current; const previewText = (0, react.useMemo)(() => require_utils.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); (0, react.useEffect)(() => { setIsCollapsed(collapsible); }, [collapsible]); return /* @__PURE__ */ react.default.createElement(_carbon_react.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", ...require_devtools.getDevtoolsProps(componentName) }, /* @__PURE__ */ react.default.createElement("div", { className: `${blockClass}__close-icon-wrapper` }, /* @__PURE__ */ react.default.createElement(_carbon_react.IconButton, { className: `${blockClass}__close-icon`, kind: "ghost", label: closeIconDescription, onClick: onClose, size: "lg" }, /* @__PURE__ */ react.default.createElement(_carbon_react_icons.Close, { size: 16 }))), (!renderMedia && narrow || !narrow) && /* @__PURE__ */ react.default.createElement("div", { className: `${blockClass}__icon-idea`, tabIndex: -1 }, /* @__PURE__ */ react.default.createElement(_carbon_react_icons.Idea, { size: 16 })), /* @__PURE__ */ react.default.createElement("div", { className: `${blockClass}__content` }, /* @__PURE__ */ react.default.createElement(_carbon_react.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(_carbon_react.Button, { className: `${blockClass}__toggle-btn`, kind: "ghost", size: "md", onClick: () => { setIsCollapsed((prevState) => !prevState); } }, isCollapsed ? expandButtonLabel : collapseButtonLabel), tertiaryButtonLabel && /* @__PURE__ */ react.default.createElement(_carbon_react.Button, { className: `${blockClass}__close-btn`, size: "md", onClick, kind: "tertiary", renderIcon: () => /* @__PURE__ */ react.default.createElement(_carbon_react_icons.Crossroads, { size: 16 }) }, tertiaryButtonLabel))), renderMedia && /* @__PURE__ */ react.default.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: prop_types.default.node, /** * Provide the contents of the InlineTip. */ children: prop_types.default.node.isRequired, /** * Provide an optional class to be applied to the containing node. */ className: prop_types.default.string, /** * Tooltip text and aria label for the Close button icon. */ closeIconDescription: prop_types.default.string, /** * The label for the collapse button. * This button is not visible if `media` is specified. */ collapseButtonLabel: prop_types.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: prop_types.default.bool, /** * The label for the expand button. * This button is not visible if `media` is specified. */ expandButtonLabel: prop_types.default.string, /** * Set to `true` to arrange the information in a format * that is easier to read in a limited space. */ narrow: prop_types.default.bool, /** * Function to call when the tertiary button is clicked. */ onClick: prop_types.default.func, /** * Function to call when the InlineTip is closed via the "X" button. */ onClose: prop_types.default.func, /** * Optional prop to render any media like images or animated media. */ renderMedia: prop_types.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: prop_types.default.string, /** * The title of the InlineTip. */ title: prop_types.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: prop_types.default.bool }; //#endregion exports.InlineTip = InlineTip;