import React from "react"; import { Tooltip } from "@sparkle/components/Tooltip"; import { cn } from "@sparkle/lib/utils"; interface TruncatedTextProps extends React.HTMLAttributes { children: string | React.ReactNode; lineClamp?: number; mountPortal?: boolean; mountPortalContainer?: HTMLElement; } export const TruncatedText: React.FC = ({ children, className, lineClamp = 2, mountPortal, mountPortalContainer, ...props }) => { const [isTruncated, setIsTruncated] = React.useState(false); const textRef = React.useRef(null); // Check if content is actually truncated by comparing scroll height to // client height // This ensures we only show the tooltip when text is cut off by line-clamp- React.useLayoutEffect(() => { const element = textRef.current; if (element) { const isOverflowing = element.scrollHeight > element.clientHeight; setIsTruncated(isOverflowing); } }); const textElement = (
{children}
); if (isTruncated) { return ( ); } return textElement; };