import './LineClampedText.styles.scss'; import { Tooltip, TooltipProps } from 'antd'; import { useEffect, useRef, useState } from 'react'; function LineClampedText({ text, lines, tooltipProps, }: { text: string; lines?: number; tooltipProps?: TooltipProps; }): JSX.Element { const [isOverflowing, setIsOverflowing] = useState(false); const textRef = useRef(null); useEffect(() => { const checkOverflow = (): void => { if (textRef.current) { setIsOverflowing( textRef.current.scrollHeight > textRef.current.clientHeight, ); } }; checkOverflow(); window.addEventListener('resize', checkOverflow); return (): void => { window.removeEventListener('resize', checkOverflow); }; }, [text, lines]); const content = (
{text}
); return isOverflowing ? ( e.stopPropagation()}>{text}} overlayClassName="line-clamped-wrapper" // eslint-disable-next-line react/jsx-props-no-spreading {...tooltipProps} > {content} ) : ( content ); } LineClampedText.defaultProps = { lines: 1, tooltipProps: {}, }; export default LineClampedText;