@carbon/ibm-products
Version:
Carbon for IBM Products
42 lines (38 loc) • 990 B
JavaScript
/**
* Copyright IBM Corp. 2020, 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.
*/
import { useState, useRef, useEffect } from 'react';
import { checkHeightOverflow } from '../../global/js/utils/checkForOverflow.js';
function useTruncatedText(_ref) {
let {
lines,
value,
expanded
} = _ref;
const [truncated, setTruncated] = useState(false);
const ref = useRef(null);
useEffect(() => {
const resizeObserver = new ResizeObserver(() => {
if (!expanded) {
const result = checkHeightOverflow(ref.current);
if (result !== truncated) {
setTruncated(result);
}
}
});
if (ref.current) {
resizeObserver.observe(ref.current);
}
return () => {
resizeObserver.disconnect();
};
}, [expanded, lines, value, truncated]);
return {
ref,
truncated
};
}
export { useTruncatedText as default };