UNPKG

@carbon/ibm-products

Version:
77 lines (75 loc) 2.97 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. */ 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 useTruncatedText from "./useTruncatedText.js"; import React, { forwardRef, useState } from "react"; import { Tooltip } from "@carbon/react"; //#region src/components/TruncatedText/TruncatedText.tsx /** * Copyright IBM Corp. 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 import_classnames = /* @__PURE__ */ __toESM(require_classnames()); const blockClass = `${pkg.prefix}--truncated-text`; const componentName = "TruncatedText"; const TruncatedText = forwardRef((props, ref) => { const { align = "top", autoAlign = false, className, collapseLabel, expandLabel, id = `${blockClass}-default-id`, lines = 2, value, type = "tooltip", ...rest } = props; const [expanded, setExpanded] = useState(false); const { ref: contentRef, truncated } = useTruncatedText({ lines, value, expanded }); const textContentStyles = { WebkitLineClamp: type === "expand" && !expanded || type === "tooltip" ? lines : "none" }; const textContentClasses = (0, import_classnames.default)(`${blockClass}__text-content`, { [`${blockClass}__text-content--expanded`]: expanded }); const handleExpand = () => { setExpanded(!expanded); }; const handleExpandKey = (evt) => { const { key } = evt; if (key === "Enter" || key === " ") handleExpand(); }; const valueBody = /* @__PURE__ */ React.createElement("span", { ref: contentRef, className: textContentClasses, id, style: textContentStyles }, value); const tooltipVariant = truncated ? /* @__PURE__ */ React.createElement(Tooltip, { align, autoAlign, label: value }, /* @__PURE__ */ React.createElement("button", { type: "button", className: `${blockClass}__tooltip-trigger`, "aria-label": value }, valueBody)) : valueBody; const expandVariant = /* @__PURE__ */ React.createElement(React.Fragment, null, valueBody, /* @__PURE__ */ React.createElement("span", { "aria-controls": id, "aria-expanded": expanded, className: `${blockClass}__expand-toggle`, onClick: handleExpand, onKeyDown: handleExpandKey, role: "button", tabIndex: 0 }, expanded ? collapseLabel : expandLabel)); const truncatedBody = type === "expand" ? expandVariant : tooltipVariant; return /* @__PURE__ */ React.createElement("div", { ...rest, className: (0, import_classnames.default)(blockClass, className), ref, ...getDevtoolsProps(componentName) }, truncated ? truncatedBody : valueBody); }); TruncatedText.displayName = componentName; //#endregion export { TruncatedText };