UNPKG

@carbon/ibm-products

Version:
44 lines (42 loc) 1.13 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 { checkHeightOverflow } from "../../global/js/utils/checkForOverflow.js"; import { useEffect, useRef, useState } from "react"; //#region src/components/TruncatedText/useTruncatedText.ts /** * 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. */ function useTruncatedText({ lines, value, expanded }) { 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 }; } //#endregion export { useTruncatedText as default };