import { useEffect, useRef, useState } from 'react'; import { createPortal } from 'react-dom'; import styled from '@emotion/styled'; import { v4 as uuidV4 } from 'uuid'; import { AppTooltip } from './AppTooltip'; const StyledOverflowingText = styled.div<{ cursorPointer: boolean }>` cursor: ${({ cursorPointer }) => (cursorPointer ? 'pointer' : 'inherit')}; font-family: inherit; font-size: inherit; font-weight: inherit; max-width: 100%; overflow: hidden; text-decoration: inherit; text-overflow: ellipsis; white-space: nowrap; `; export const OverflowingTextWithTooltip = ({ text, }: { text: string | null | undefined; }) => { const textElementId = `title-id-${uuidV4()}`; const textRef = useRef(null); const [isTitleOverflowing, setIsTitleOverflowing] = useState(false); useEffect(() => { const isOverflowing = (text?.length ?? 0) > 0 && textRef.current ? textRef.current?.scrollHeight > textRef.current?.clientHeight || textRef.current.scrollWidth > textRef.current.clientWidth : false; if (isTitleOverflowing !== isOverflowing) { setIsTitleOverflowing(isOverflowing); } }, [isTitleOverflowing, text]); const handleTooltipClick = (event: React.MouseEvent) => { event.stopPropagation(); event.preventDefault(); }; return ( <> {text} {isTitleOverflowing && createPortal(
, document.body, )} ); };