diff --git a/src/hooks/useCheckTextLines.tsx b/src/hooks/useCheckTextLines.tsx new file mode 100644 index 00000000..0c3b3b95 --- /dev/null +++ b/src/hooks/useCheckTextLines.tsx @@ -0,0 +1,31 @@ +import { useState, useEffect, useCallback, RefObject } from 'react'; + +const useCheckTextLines = ( + textContainerRef: RefObject, + text: string, +): boolean => { + const [showButton, setShowButton] = useState(false); + + const checkTextLines = useCallback(() => { + if (textContainerRef.current) { + const element = textContainerRef.current; + const lineHeight = parseFloat( + window.getComputedStyle(element).lineHeight, + ); + const lines = element.scrollHeight / lineHeight; + setShowButton(lines > 3.5); + } + }, [textContainerRef]); + + useEffect(() => { + if (textContainerRef.current) { + checkTextLines(); + window.addEventListener('resize', checkTextLines); + return () => window.removeEventListener('resize', checkTextLines); + } + }, [text, textContainerRef, checkTextLines]); + + return showButton; +}; + +export default useCheckTextLines;