RoyLee1224 commented on code in PR #58359: URL: https://github.com/apache/airflow/pull/58359#discussion_r2583496600
########## airflow-core/src/airflow/ui/src/components/BasicTooltip.tsx: ########## @@ -0,0 +1,127 @@ +/*! + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +import { Box, Portal } from "@chakra-ui/react"; +import type { ReactElement, ReactNode } from "react"; +import { cloneElement, useCallback, useEffect, useLayoutEffect, useRef, useState } from "react"; + +type Props = { + readonly children: ReactElement; + readonly content: ReactNode; +}; + +const offset = 8; + +export const BasicTooltip = ({ children, content }: Props): ReactElement => { + const triggerRef = useRef<HTMLElement>(null); + const tooltipRef = useRef<HTMLDivElement>(null); + const [isOpen, setIsOpen] = useState(false); + const [showOnTop, setShowOnTop] = useState(false); + const timeoutRef = useRef<NodeJS.Timeout>(); + + const handleMouseEnter = useCallback(() => { + if (timeoutRef.current) { + clearTimeout(timeoutRef.current); + } + timeoutRef.current = setTimeout(() => { + setIsOpen(true); + }, 500); + }, []); + + const handleMouseLeave = useCallback(() => { + if (timeoutRef.current) { + clearTimeout(timeoutRef.current); + timeoutRef.current = undefined; + } + setIsOpen(false); + }, []); + + // Calculate position based on actual tooltip height before paint + useLayoutEffect(() => { + if (isOpen && triggerRef.current && tooltipRef.current) { + const triggerRect = triggerRef.current.getBoundingClientRect(); + const tooltipHeight = tooltipRef.current.clientHeight; + const wouldOverflow = triggerRect.bottom + offset + tooltipHeight > globalThis.innerHeight; + + setShowOnTop(wouldOverflow); + } + }, [isOpen]); + + // Cleanup on unmount Review Comment: Strictly speaking, the current code won't crash because React 18 suppresses updates on unmounted components. But I think the cleanup prevents potential crashes if we access refs inside the timeout, and it handles the edge case where the component unmounts without triggering `onMouseLeave` (e.g., switching to Graph view). -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
