import { useRef, useState } from 'react'; import { createPortal } from 'react-dom'; import clsx from 'clsx'; import { v4 as uuidV4 } from 'uuid'; import { AppTooltip } from './AppTooltip'; import styles from './OverflowingTextWithTooltip.module.css'; export const OverflowingTextWithTooltip = ({ size = 'small', text, mutliline, }: { size?: 'large' | 'small'; text: string | null | undefined; mutliline?: boolean; }) => { const textElementId = `title-id-${uuidV4()}`; const textRef = useRef(null); const [isTitleOverflowing, setIsTitleOverflowing] = useState(false); const handleMouseEnter = () => { const isOverflowing = (text?.length ?? 0) > 0 && textRef.current ? textRef.current?.scrollHeight > textRef.current?.clientHeight || textRef.current.scrollWidth > textRef.current.clientWidth : false; setIsTitleOverflowing(isOverflowing); }; const handleMouseLeave = () => { setIsTitleOverflowing(false); }; const handleTooltipClick = (event: React.MouseEvent) => { event.stopPropagation(); event.preventDefault(); }; return ( <>
{text}
{isTitleOverflowing && createPortal(
{mutliline ?
{text}
: ''}
, document.body, )} ); };