/******************************************************************************** * Copyright (c) 2026 Contributors to the Eclipse Foundation * * See the NOTICE file(s) distributed with this work for additional * information regarding copyright ownership. * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License 2.0 which is available at * https://www.eclipse.org/legal/epl-2.0 * * SPDX-License-Identifier: EPL-2.0 ********************************************************************************/ import type { ReactElement } from 'react'; import { FunctionComponent, useRef, useState, useEffect, cloneElement } from 'react'; import { Tooltip, TooltipProps } from '@mui/material'; interface ConditionalTooltipProps extends Omit { title: string; } /** * A Tooltip component that only shows when the child content is truncated with ellipsis. * Uses ref to check if the content overflows its container. */ export const ConditionalTooltip: FunctionComponent = ({ title, children, ...tooltipProps }) => { const textRef = useRef(null); const [isTruncated, setIsTruncated] = useState(false); useEffect(() => { const checkTruncation = () => { if (textRef.current) { setIsTruncated(textRef.current.scrollWidth > textRef.current.clientWidth); } }; checkTruncation(); // Recheck on window resize window.addEventListener('resize', checkTruncation); return () => window.removeEventListener('resize', checkTruncation); }, [title, children]); return ( {cloneElement(children as ReactElement, { ref: textRef, })} ); };