UNPKG

@carbon/ibm-products

Version:
35 lines (33 loc) 1.05 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 { useCallback, useEffect, useState } from "react"; //#region src/global/js/hooks/useOverflowString.ts /** * Copyright IBM Corp. 2025, 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 useOverflowStringWidth(elementRef) { const innerText = elementRef?.current?.innerText; const [isOverflowing, setIsOverflowing] = useState(); const checkWidthOverflow = useCallback(() => { const offsetWidth = elementRef?.current?.offsetWidth; const scrollWidth = elementRef?.current?.scrollWidth; if (offsetWidth && scrollWidth) setIsOverflowing(offsetWidth < scrollWidth); }, [elementRef]); useEffect(() => { checkWidthOverflow(); }, [ checkWidthOverflow, elementRef, innerText ]); return isOverflowing; } //#endregion export { useOverflowStringWidth };