@carbon/ibm-products
Version:
Carbon for IBM Products
129 lines (127 loc) • 5.4 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.
*/
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_useIsomorphicEffect = require("../../global/js/hooks/useIsomorphicEffect.js");
const require_devtools = require("../../global/js/utils/devtools.js");
const require_props_helper = require("../../global/js/utils/props-helper.js");
const require_enums = require("./utils/enums.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");
//#region src/components/StringFormatter/StringFormatter.jsx
/**
* Copyright IBM Corp. 2024, 2024
*
* 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}--string-formatter`;
const componentName = "StringFormatter";
const defaults = {
autoAlign: false,
lines: 1,
tooltipDirection: require_enums.StringFormatterAlignment.BOTTOM_START,
truncate: false,
width: null
};
/**
* StringFormatter allows for truncating text while displaying a tooltip
* overlay on hover or focus with the entirety of the provided copy.
* @deprecated This component is deprecated and will be removed in the next major version. Please use TruncatedText instead.
*/
let StringFormatter = react.default.forwardRef(({ className, autoAlign = defaults.autoAlign, lines = defaults.lines, tooltipDirection = defaults.tooltipDirection, truncate = defaults.truncate, width = defaults.width, value, ...rest }, ref) => {
const outerRef = (0, react.useRef)(null);
const contentRef = (0, react.useRef)(null);
const [isTextTruncated, setIsTextTruncated] = (0, react.useState)(false);
const mergedRefs = (node) => {
outerRef.current = node;
if (typeof ref === "function") ref(node);
else if (ref) ref.current = node;
};
require_useIsomorphicEffect.useIsomorphicEffect(() => {
const checkTruncation = () => {
const element = contentRef.current;
if (element) {
element.style.webkitLineClamp = truncate ? lines : void 0;
element.style.maxWidth = width;
const buffer = element.clientHeight / (2 * lines);
setIsTextTruncated(element.scrollHeight > element.clientHeight + buffer);
}
};
const resizeObserver = new ResizeObserver(checkTruncation);
if (outerRef.current) {
resizeObserver.observe(outerRef.current);
checkTruncation();
}
return () => {
resizeObserver.disconnect();
};
}, [
lines,
value,
width,
truncate
]);
const stringFormatterContent = /* @__PURE__ */ react.default.createElement("span", {
ref: contentRef,
className: (0, import_classnames.default)(`${blockClass}--content`, { [`${blockClass}--truncate`]: truncate })
}, value);
return /* @__PURE__ */ react.default.createElement("span", {
...rest,
className: (0, import_classnames.default)(blockClass, className),
ref: mergedRefs,
...require_devtools.getDevtoolsProps(componentName)
}, truncate && isTextTruncated ? /* @__PURE__ */ react.default.createElement(_carbon_react.DefinitionTooltip, {
className: `${blockClass}__tooltip`,
align: tooltipDirection,
autoAlign,
definition: value,
openOnHover: true
}, stringFormatterContent) : stringFormatterContent);
});
/**@ts-ignore*/
StringFormatter.deprecated = {
level: "warn",
details: `Please replace ${componentName} with TruncatedText`
};
StringFormatter = require_settings.pkg.checkComponentEnabled(StringFormatter, componentName);
StringFormatter.displayName = componentName;
StringFormatter.validateAlignment = () => (props, propName, componentName) => {
const prop = props[propName];
if (Object.values(require_enums.deprecated_StringFormatterAlignment).includes(prop)) {
const mappedNewProp = require_enums.propMappingFunction(prop);
console.warn(`"${prop}" is a deprecated value for the "${propName}" prop on the "${componentName}" component. Use "${mappedNewProp}" instead. Allowable values are: ${Object.values(require_enums.StringFormatterAlignment).join(", ")}.`);
}
};
StringFormatter.propTypes = {
/**
* Provide an optional class to be applied to the containing node.
*/
className: prop_types.default.string,
/** Number of lines to clamp value. */
lines: prop_types.default.number,
/** Specify the direction of the tooltip. Can be either top or bottom. */
tooltipDirection: require_props_helper.allPropTypes([StringFormatter.validateAlignment(), prop_types.default.oneOf(Object.values(require_enums.deprecated_StringFormatterAlignment), Object.values(require_enums.StringFormatterAlignment))]),
/** Whether or not the value should be truncated. if it exceeds lines. */
truncate: prop_types.default.bool,
/** Value to format. */
value: prop_types.default.string.isRequired,
/** Maximum width of value which should include */
width: prop_types.default.string
};
//#endregion
Object.defineProperty(exports, "StringFormatter", {
enumerable: true,
get: function() {
return StringFormatter;
}
});