Debian Codegraph @

node/@patternfly/react-core/dist/esm/components/Truncate/Truncate.js

raw
1 import { __rest } from "tslib";
2 import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
3 import { Fragment, useEffect, useRef, useState, forwardRef, useImperativeHandle } from 'react';
4 import styles from '@patternfly/react-styles/css/components/Truncate/truncate.mjs';
5 import { css } from '@patternfly/react-styles';
6 import { Tooltip } from '../Tooltip';
7 import { getReferenceElement } from '../../helpers';
8 import { getResizeObserver } from '../../helpers/resizeObserver';
9 import { debounce } from '../../helpers/util';
10 export var TruncatePosition;
11 (function (TruncatePosition) {
12 TruncatePosition["start"] = "start";
13 TruncatePosition["end"] = "end";
14 TruncatePosition["middle"] = "middle";
15 })(TruncatePosition || (TruncatePosition = {}));
16 const truncateStyles = {
17 start: styles.truncateEnd,
18 end: styles.truncateStart
19 };
20 const minWidthCharacters = 12;
21 const sliceTrailingContent = (str, slice) => [str.slice(0, str.length - slice), str.slice(-slice)];
22 const TruncateBase = (_a) => {
23 var { className, href, position = 'end', tooltipPosition = 'top', tooltipProps, trailingNumChars = 7, maxCharsDisplayed, omissionContent = '\u2026', content, innerRef } = _a, props = __rest(_a, ["className", "href", "position", "tooltipPosition", "tooltipProps", "trailingNumChars", "maxCharsDisplayed", "omissionContent", "content", "innerRef"]);
24 const [isTruncated, setIsTruncated] = useState(true);
25 const [parentElement, setParentElement] = useState(null);
26 const [textElement, setTextElement] = useState(null);
27 const [shouldRenderByMaxChars, setShouldRenderByMaxChars] = useState(maxCharsDisplayed > 0);
28 const textRef = useRef(null);
29 useImperativeHandle(innerRef, () => textRef.current);
30 const defaultSubParentRef = useRef(null);
31 const subParentRef = (tooltipProps === null || tooltipProps === void 0 ? void 0 : tooltipProps.triggerRef) || defaultSubParentRef;
32 const observer = useRef(null);
33 if (maxCharsDisplayed <= 0) {
34 // eslint-disable-next-line no-console
35 console.warn('Truncate: the maxCharsDisplayed must be greater than 0, otherwise no content will be visible.');
36 }
37 const getActualWidth = (element) => {
38 const computedStyle = getComputedStyle(element);
39 return (parseFloat(computedStyle.width) -
40 parseFloat(computedStyle.paddingLeft) -
41 parseFloat(computedStyle.paddingRight) -
42 parseFloat(computedStyle.borderRight) -
43 parseFloat(computedStyle.borderLeft));
44 };
45 const calculateTotalTextWidth = (element, trailingNumChars, content) => {
46 const firstTextWidth = element.scrollWidth;
47 const firstTextLength = content.length;
48 return (firstTextWidth / firstTextLength) * trailingNumChars + firstTextWidth;
49 };
50 useEffect(() => {
51 if (textRef && textRef.current && !textElement) {
52 setTextElement(textRef.current);
53 }
54 }, [textRef, textElement]);
55 useEffect(() => {
56 const refElement = getReferenceElement(subParentRef);
57 if ((refElement === null || refElement === void 0 ? void 0 : refElement.parentElement) && !parentElement) {
58 setParentElement(refElement.parentElement);
59 }
60 }, [subParentRef, parentElement]);
61 useEffect(() => {
62 if (textElement && parentElement && !observer.current && !shouldRenderByMaxChars) {
63 const totalTextWidth = calculateTotalTextWidth(textElement, trailingNumChars, content);
64 const textWidth = position === 'middle' ? totalTextWidth : textElement.scrollWidth;
65 const debouncedHandleResize = debounce(() => {
66 const parentWidth = getActualWidth(parentElement);
67 setIsTruncated(textWidth >= parentWidth);
68 }, 500);
69 const observer = getResizeObserver(parentElement, debouncedHandleResize);
70 return () => {
71 observer();
72 };
73 }
74 }, [textElement, parentElement, trailingNumChars, content, position, shouldRenderByMaxChars]);
75 useEffect(() => {
76 if (shouldRenderByMaxChars) {
77 setIsTruncated(content.length > maxCharsDisplayed);
78 }
79 }, [shouldRenderByMaxChars, content.length, maxCharsDisplayed]);
80 useEffect(() => {
81 setShouldRenderByMaxChars(maxCharsDisplayed > 0);
82 }, [maxCharsDisplayed]);
83 const lrmEntity = _jsx(Fragment, { children: "\u200E" });
84 const isStartPosition = position === TruncatePosition.start;
85 const isEndPosition = position === TruncatePosition.end;
86 const renderResizeObserverContent = () => {
87 if (isEndPosition || isStartPosition) {
88 return (_jsx(_Fragment, { children: _jsxs("span", { ref: textRef, className: truncateStyles[position], children: [isStartPosition && lrmEntity, content, isStartPosition && lrmEntity] }) }));
89 }
90 const shouldSliceContent = content.length - trailingNumChars > minWidthCharacters;
91 return (_jsx(_Fragment, { children: _jsxs(Fragment, { children: [_jsx("span", { ref: textRef, className: styles.truncateStart, children: shouldSliceContent ? sliceTrailingContent(content, trailingNumChars)[0] : content }), shouldSliceContent && (_jsx("span", { className: styles.truncateEnd, children: sliceTrailingContent(content, trailingNumChars)[1] }))] }) }));
92 };
93 const renderMaxDisplayContent = () => {
94 const renderVisibleContent = (contentToRender) => (_jsx("span", { className: `${styles.truncate}__text`, children: contentToRender }));
95 if (!isTruncated) {
96 return renderVisibleContent(content);
97 }
98 const omissionElement = (_jsx("span", { className: `${styles.truncate}__omission`, "aria-hidden": "true", children: omissionContent }));
99 const renderVisuallyHiddenContent = (contentToHide) => (_jsx("span", { className: "pf-v6-screen-reader", children: contentToHide }));
100 if (position === TruncatePosition.start) {
101 return (_jsxs(_Fragment, { children: [renderVisuallyHiddenContent(content.slice(0, maxCharsDisplayed * -1)), omissionElement, renderVisibleContent(content.slice(maxCharsDisplayed * -1))] }));
102 }
103 if (isEndPosition) {
104 return (_jsxs(_Fragment, { children: [renderVisibleContent(content.slice(0, maxCharsDisplayed)), omissionElement, renderVisuallyHiddenContent(content.slice(maxCharsDisplayed))] }));
105 }
106 const trueMiddleStart = Math.floor(maxCharsDisplayed / 2);
107 const trueMiddleEnd = Math.ceil(maxCharsDisplayed / 2) * -1;
108 return (_jsxs(_Fragment, { children: [renderVisibleContent(content.slice(0, trueMiddleStart)), omissionElement, renderVisuallyHiddenContent(content.slice(trueMiddleStart, trueMiddleEnd)), renderVisibleContent(content.slice(trueMiddleEnd))] }));
109 };
110 const TruncateWrapper = href ? 'a' : 'span';
111 const truncateBody = (_jsx(TruncateWrapper, Object.assign({ ref: !(tooltipProps === null || tooltipProps === void 0 ? void 0 : tooltipProps.triggerRef) ? subParentRef : null, href: href, className: css(styles.truncate, shouldRenderByMaxChars && styles.modifiers.fixed, className) }, (isTruncated && !href && !(tooltipProps === null || tooltipProps === void 0 ? void 0 : tooltipProps.triggerRef) && { tabIndex: 0 }), props, { children: !shouldRenderByMaxChars ? renderResizeObserverContent() : renderMaxDisplayContent() })));
112 return (_jsxs(_Fragment, { children: [isTruncated && (_jsx(Tooltip, Object.assign({ hidden: !isTruncated, position: tooltipPosition, content: content, triggerRef: subParentRef }, tooltipProps))), truncateBody] }));
113 };
114 export const Truncate = forwardRef((props, ref) => (_jsx(TruncateBase, Object.assign({ innerRef: ref }, props))));
115 Truncate.displayName = 'Truncate';
116 //# sourceMappingURL=Truncate.js.map