Skip to content

Commit e138a0d

Browse files
author
Brian Vaughn
committed
Fixed Scheduler Profiler tooltip positions in extension builds
1 parent fbfee8d commit e138a0d

File tree

3 files changed

+24
-6
lines changed

3 files changed

+24
-6
lines changed

packages/react-devtools-scheduling-profiler/src/CanvasPage.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -509,6 +509,7 @@ function AutoSizedCanvas({data, height, width}: AutoSizedCanvasProps) {
509509
</ContextMenu>
510510
{!isContextMenuShown && !surfaceRef.current.hasActiveView() && (
511511
<EventTooltip
512+
canvasRef={canvasRef}
512513
data={data}
513514
hoveredEvent={hoveredEvent}
514515
origin={mouseLocation}

packages/react-devtools-scheduling-profiler/src/EventTooltip.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import useSmartTooltip from './utils/useSmartTooltip';
2828
import styles from './EventTooltip.css';
2929

3030
type Props = {|
31+
canvasRef: {|current: HTMLCanvasElement | null|},
3132
data: ReactProfilerData,
3233
hoveredEvent: ReactHoverContextInfo | null,
3334
origin: Point,
@@ -102,8 +103,14 @@ function getReactMeasureLabel(type): string | null {
102103
}
103104
}
104105

105-
export default function EventTooltip({data, hoveredEvent, origin}: Props) {
106+
export default function EventTooltip({
107+
canvasRef,
108+
data,
109+
hoveredEvent,
110+
origin,
111+
}: Props) {
106112
const tooltipRef = useSmartTooltip({
113+
canvasRef,
107114
mouseX: origin.x,
108115
mouseY: origin.y,
109116
});

packages/react-devtools-scheduling-profiler/src/utils/useSmartTooltip.js

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,32 @@ import {useLayoutEffect, useRef} from 'react';
1212
const TOOLTIP_OFFSET = 4;
1313

1414
export default function useSmartTooltip({
15+
canvasRef,
1516
mouseX,
1617
mouseY,
1718
}: {
19+
canvasRef: {|current: HTMLCanvasElement | null|},
1820
mouseX: number,
1921
mouseY: number,
2022
}) {
2123
const ref = useRef<HTMLElement | null>(null);
2224

25+
// HACK: Browser extension reports window.innerHeight of 0,
26+
// so we fallback to using the tooltip target element.
27+
let height = window.innerHeight;
28+
let width = window.innerWidth;
29+
const target = canvasRef.current;
30+
if (target !== null) {
31+
const rect = target.getBoundingClientRect();
32+
height = rect.top + rect.height;
33+
width = rect.left + rect.width;
34+
}
35+
2336
useLayoutEffect(() => {
2437
const element = ref.current;
2538
if (element !== null) {
2639
// Let's check the vertical position.
27-
if (
28-
mouseY + TOOLTIP_OFFSET + element.offsetHeight >=
29-
window.innerHeight
30-
) {
40+
if (mouseY + TOOLTIP_OFFSET + element.offsetHeight >= height) {
3141
// The tooltip doesn't fit below the mouse cursor (which is our
3242
// default strategy). Therefore we try to position it either above the
3343
// mouse cursor or finally aligned with the window's top edge.
@@ -45,7 +55,7 @@ export default function useSmartTooltip({
4555
}
4656

4757
// Now let's check the horizontal position.
48-
if (mouseX + TOOLTIP_OFFSET + element.offsetWidth >= window.innerWidth) {
58+
if (mouseX + TOOLTIP_OFFSET + element.offsetWidth >= width) {
4959
// The tooltip doesn't fit at the right of the mouse cursor (which is
5060
// our default strategy). Therefore we try to position it either at the
5161
// left of the mouse cursor or finally aligned with the window's left

0 commit comments

Comments
 (0)