Skip to content

Commit

Permalink
๐Ÿ”จ refactor: useCheckTextLines ํ›… ๋ถ„๋ฆฌ
Browse files Browse the repository at this point in the history
  • Loading branch information
easyhyun00 committed Jan 27, 2024
1 parent 3051bf2 commit 1fe5682
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions src/hooks/useCheckTextLines.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { useState, useEffect, useCallback, RefObject } from 'react';

const useCheckTextLines = (
textContainerRef: RefObject<HTMLDivElement>,
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;

0 comments on commit 1fe5682

Please sign in to comment.