From 7f2eac15372e77ba04f1adcd0989eab540b6017b Mon Sep 17 00:00:00 2001 From: Peyton Lee Date: Thu, 5 Dec 2024 19:32:58 -0800 Subject: [PATCH] refactor: Simplified HoverTooltip, moved tooltip styling into CanvasHoverTooltip, renamed state values for clarity --- src/Viewer.tsx | 15 +++++----- .../Tooltips/CanvasHoverTooltip.tsx | 30 ++++++++++++++----- src/components/Tooltips/HoverTooltip.tsx | 8 ----- 3 files changed, 29 insertions(+), 24 deletions(-) diff --git a/src/Viewer.tsx b/src/Viewer.tsx index c02ac416f..b436d56d4 100644 --- a/src/Viewer.tsx +++ b/src/Viewer.tsx @@ -200,9 +200,8 @@ function Viewer(): ReactElement { */ const [frameInput, setFrameInput] = useState(0); const [findTrackInput, setFindTrackInput] = useState(""); - // Prevent jarring jumps in the hover tooltip by using the last non-null value - const [lastHoveredId, setLastHoveredId] = useState(null); - const [showHoveredId, setShowHoveredId] = useState(false); + const [lastValidHoveredId, setLastValidHoveredId] = useState(-1); + const [showObjectHoverInfo, setShowObjectHoverInfo] = useState(false); // UTILITY METHODS ///////////////////////////////////////////////////////////// @@ -996,8 +995,8 @@ function Viewer(): ReactElement { @@ -1026,12 +1025,12 @@ function Viewer(): ReactElement { inRangeLUT={inRangeLUT} onMouseHover={(id: number): void => { const isObject = id !== BACKGROUND_ID; - setShowHoveredId(isObject); + setShowObjectHoverInfo(isObject); if (isObject) { - setLastHoveredId(id); + setLastValidHoveredId(id); } }} - onMouseLeave={() => setShowHoveredId(false)} + onMouseLeave={() => setShowObjectHoverInfo(false)} showAlert={isInitialDatasetLoaded ? showAlert : undefined} /> diff --git a/src/components/Tooltips/CanvasHoverTooltip.tsx b/src/components/Tooltips/CanvasHoverTooltip.tsx index 5646ba11f..bcace4eef 100644 --- a/src/components/Tooltips/CanvasHoverTooltip.tsx +++ b/src/components/Tooltips/CanvasHoverTooltip.tsx @@ -1,4 +1,5 @@ import React, { PropsWithChildren, ReactElement, useCallback } from "react"; +import styled from "styled-components"; import { Dataset, VECTOR_KEY_MOTION_DELTA, VectorTooltipMode, ViewerConfig } from "../../colorizer"; import { numberToStringDecimal } from "../../colorizer/utils/math_utils"; @@ -8,12 +9,25 @@ import HoverTooltip from "./HoverTooltip"; type CanvasHoverTooltipProps = { dataset: Dataset | null; featureKey: string; - lastHoveredId: number | null; - showHoveredId: boolean; + lastValidHoveredId: number; + showObjectHoverInfo: boolean; motionDeltas: Float32Array | null; config: ViewerConfig; }; +// Styling for the tooltip +const TooltipCard = styled.div` + font-family: var(--default-font); + + border-radius: var(--radius-control-small); + border: 1px solid var(--color-dividers); + background-color: var(--color-background); + padding: 6px 8px; + overflow-wrap: break-word; + + transition: opacity 300ms; +`; + /** * Sets up and configures the hover tooltip for the main viewport canvas. * By default, displays the track ID and the value of the feature at the hovered point. @@ -22,7 +36,7 @@ type CanvasHoverTooltipProps = { * - If vectors are enabled, the vector value (either magnitude or components) will be displayed. */ export default function CanvasHoverTooltip(props: PropsWithChildren): ReactElement { - const { dataset, featureKey, lastHoveredId, motionDeltas, config } = props; + const { dataset, featureKey, lastValidHoveredId: lastHoveredId, motionDeltas, config } = props; const getFeatureValue = useCallback( (id: number): string => { @@ -79,8 +93,8 @@ export default function CanvasHoverTooltip(props: PropsWithChildrenTrack ID: {lastHoveredId && dataset?.getTrackId(lastHoveredId)}

,

@@ -90,9 +104,9 @@ export default function CanvasHoverTooltip(props: PropsWithChildren{vectorTooltipText}

: null, ]; - return ( - - {props.children} - + const tooltipContent = ( + {hoverTooltipContent} ); + + return {props.children}; } diff --git a/src/components/Tooltips/HoverTooltip.tsx b/src/components/Tooltips/HoverTooltip.tsx index 4279c7c88..fd9702be2 100644 --- a/src/components/Tooltips/HoverTooltip.tsx +++ b/src/components/Tooltips/HoverTooltip.tsx @@ -18,16 +18,8 @@ const defaultProps: Partial = { maxWidthPx: 250, }; -// Styling for the tooltip const TooltipDiv = styled.div` position: fixed; - font-family: var(--default-font); - - border-radius: var(--radius-control-small); - border: 1px solid var(--color-dividers); - background-color: var(--color-background); - padding: 6px 8px; - overflow-wrap: break-word; transition: opacity 300ms; z-index: 1;