@spaced-out/ui-design-system
Version:
Sense UI components library
106 lines (100 loc) • 4.22 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.TruncatedTextWithTooltip = void 0;
var React = _interopRequireWildcard(require("react"));
var _debounce = _interopRequireDefault(require("lodash/debounce"));
var _useWindowSize = require("../../hooks/useWindowSize");
var _classify = _interopRequireDefault(require("../../utils/classify"));
var _qa = require("../../utils/qa");
var _ConditionalWrapper = require("../ConditionalWrapper");
var _Tooltip = require("../Tooltip");
var _TruncatedTextWithTooltipModule = _interopRequireDefault(require("./TruncatedTextWithTooltip.module.css"));
var _jsxRuntime = require("react/jsx-runtime");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); }
// Define the expected props for the child element that can be cloned
// Type for a child element that can be cloned
const TruncatedTextWithTooltip = _ref => {
let {
classNames,
children,
tooltip,
wrapText = false,
line = 1,
wordBreak = 'break-all',
testId
} = _ref;
const {
width
} = (0, _useWindowSize.useWindowSize)();
const [showMouseTip, setShowMouseTip] = React.useState(false);
const truncatedRef = React.useRef(null);
const checkIfTextOverflowing = React.useMemo(() => (0, _debounce.default)(() => {
if (!wrapText && truncatedRef && truncatedRef.current && truncatedRef.current.scrollHeight > truncatedRef.current.clientHeight) {
setShowMouseTip(true);
} else {
setShowMouseTip(false);
}
}, 200, {
leading: true
}), []);
React.useEffect(() => {
checkIfTextOverflowing();
}, [width, line, children, wrapText]);
const styles = {
'--line-clamp': wrapText ? 'unset' : line,
'--word-break': wordBreak
};
return /*#__PURE__*/(0, _jsxRuntime.jsx)(_ConditionalWrapper.ConditionalWrapper, {
condition: Boolean(showMouseTip),
wrapper: children => {
const child = React.Children.only(children);
// Type guard to ensure child is a ReactElement
if (! /*#__PURE__*/React.isValidElement(child)) {
return child;
}
// Cast to our typed child element
const childElement = child;
const childProps = childElement.props;
//Vivek: This children in tooltip body is modified to remove line clamp style
const modifiedChild = /*#__PURE__*/React.cloneElement(childElement, {
...childProps,
style: {
...childProps.style,
'--line-clamp': tooltip?.bodyMaxLines || line,
'--word-break': wordBreak
}
});
return /*#__PURE__*/(0, _jsxRuntime.jsx)(_Tooltip.Tooltip, {
...tooltip,
body: tooltip?.body || modifiedChild,
elevation: tooltip?.elevation ?? 'toast',
testId: (0, _qa.generateTestId)({
base: testId,
slot: 'tooltip'
}),
children: /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
className: _TruncatedTextWithTooltipModule.default.childrenWrapper,
"data-testid": (0, _qa.generateTestId)({
base: testId,
slot: 'wrapper'
}),
children: children
})
});
},
children: /*#__PURE__*/(0, _jsxRuntime.jsx)("span", {
className: (0, _classify.default)(_TruncatedTextWithTooltipModule.default.truncateLineClamp, classNames?.wrapper),
style: styles,
ref: truncatedRef,
"data-testid": (0, _qa.generateTestId)({
base: testId,
slot: 'text'
}),
children: children
})
});
};
exports.TruncatedTextWithTooltip = TruncatedTextWithTooltip;