2024-09-11 19:02:17 +04:30
|
|
|
import './LineClampedText.styles.scss';
|
|
|
|
|
|
2024-09-30 11:48:48 +05:30
|
|
|
import { Tooltip, TooltipProps } from 'antd';
|
2024-09-11 19:02:17 +04:30
|
|
|
import { useEffect, useRef, useState } from 'react';
|
|
|
|
|
|
|
|
|
|
function LineClampedText({
|
|
|
|
|
text,
|
|
|
|
|
lines,
|
2024-09-30 11:48:48 +05:30
|
|
|
tooltipProps,
|
2024-09-11 19:02:17 +04:30
|
|
|
}: {
|
|
|
|
|
text: string;
|
|
|
|
|
lines?: number;
|
2024-09-30 11:48:48 +05:30
|
|
|
tooltipProps?: TooltipProps;
|
2024-09-11 19:02:17 +04:30
|
|
|
}): JSX.Element {
|
|
|
|
|
const [isOverflowing, setIsOverflowing] = useState(false);
|
|
|
|
|
const textRef = useRef<HTMLDivElement>(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 = (
|
|
|
|
|
<div
|
|
|
|
|
ref={textRef}
|
2025-02-04 17:59:23 +04:30
|
|
|
className="line-clamped-wrapper__text"
|
2024-09-11 19:02:17 +04:30
|
|
|
style={{
|
|
|
|
|
WebkitLineClamp: lines,
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
{text}
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
|
2024-09-30 11:48:48 +05:30
|
|
|
return isOverflowing ? (
|
|
|
|
|
<Tooltip
|
2025-02-04 17:59:23 +04:30
|
|
|
// eslint-disable-next-line jsx-a11y/no-static-element-interactions, jsx-a11y/click-events-have-key-events
|
|
|
|
|
title={<div onClick={(e): void => e.stopPropagation()}>{text}</div>}
|
|
|
|
|
overlayClassName="line-clamped-wrapper"
|
2024-09-30 11:48:48 +05:30
|
|
|
// eslint-disable-next-line react/jsx-props-no-spreading
|
|
|
|
|
{...tooltipProps}
|
|
|
|
|
>
|
|
|
|
|
{content}
|
|
|
|
|
</Tooltip>
|
|
|
|
|
) : (
|
|
|
|
|
content
|
|
|
|
|
);
|
2024-09-11 19:02:17 +04:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
LineClampedText.defaultProps = {
|
|
|
|
|
lines: 1,
|
2024-09-30 11:48:48 +05:30
|
|
|
tooltipProps: {},
|
2024-09-11 19:02:17 +04:30
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default LineClampedText;
|