UNPKG

@carbon/ibm-products

Version:

Carbon for IBM Products

42 lines (38 loc) 1.43 kB
/** * 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, useCallback, useEffect } from 'react'; 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; } const useOverflowStringHeight = elementRef => { const innerText = elementRef?.current?.innerText; const [isOverflowing, setIsOverflowing] = useState(); const checkHeightOverflow = useCallback(() => { const offsetHeight = elementRef?.current?.offsetHeight; const scrollHeight = elementRef?.current?.scrollHeight; if (offsetHeight && scrollHeight) { setIsOverflowing(offsetHeight < scrollHeight); } }, [elementRef]); useEffect(() => { checkHeightOverflow(); }, [checkHeightOverflow, elementRef, innerText]); return isOverflowing; }; export { useOverflowStringHeight, useOverflowStringWidth };