UNPKG

@carbon/ibm-products

Version:

Carbon for IBM Products

45 lines (40 loc) 1.51 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. */ 'use strict'; var React = require('react'); function useOverflowStringWidth(elementRef) { const innerText = elementRef?.current?.innerText; const [isOverflowing, setIsOverflowing] = React.useState(); const checkWidthOverflow = React.useCallback(() => { const offsetWidth = elementRef?.current?.offsetWidth; const scrollWidth = elementRef?.current?.scrollWidth; if (offsetWidth && scrollWidth) { setIsOverflowing(offsetWidth < scrollWidth); } }, [elementRef]); React.useEffect(() => { checkWidthOverflow(); }, [checkWidthOverflow, elementRef, innerText]); return isOverflowing; } const useOverflowStringHeight = elementRef => { const innerText = elementRef?.current?.innerText; const [isOverflowing, setIsOverflowing] = React.useState(); const checkHeightOverflow = React.useCallback(() => { const offsetHeight = elementRef?.current?.offsetHeight; const scrollHeight = elementRef?.current?.scrollHeight; if (offsetHeight && scrollHeight) { setIsOverflowing(offsetHeight < scrollHeight); } }, [elementRef]); React.useEffect(() => { checkHeightOverflow(); }, [checkHeightOverflow, elementRef, innerText]); return isOverflowing; }; exports.useOverflowStringHeight = useOverflowStringHeight; exports.useOverflowStringWidth = useOverflowStringWidth;