-
-
Save Sensiblemnd/ab4a6b5ac6ce1d2fb4a9a532e3de3302 to your computer and use it in GitHub Desktop.
Revisions
-
nkint renamed this gist
May 4, 2021 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
nkint created this gist
May 4, 2021 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,26 @@ import { useLayoutEffect, useState } from 'react'; /** * Determine if the input DOM element is truncated by CSS (using ellipse for example) * @param domElement * @returns boolean */ export function isTruncated(domElement: Element): boolean { // https://developer.mozilla.org/en-US/docs/Web/API/Element/scrollWidth return domElement.scrollWidth > domElement.clientWidth; } export function useIsTruncated<T extends Element>(ref: React.MutableRefObject<null | T>): boolean { const [isTruncatedState, setIsTruncatedState] = useState(false); useLayoutEffect(() => { if (ref.current !== null) { const isTextTruncated = isTruncated(ref.current); if (isTruncatedState !== isTextTruncated) { setIsTruncatedState(isTextTruncated); } } }, [isTruncatedState, ref]); return isTruncatedState; }