Skip to content

Commit 7e770da

Browse files
author
Brian Vaughn
authored
Profiler tooltip tweaks (facebook#18082)
* Moved Profiler views into Profiler folder * Tweaked Profiler tooltip CSS styles * Tweaked Tooltip positioning code
1 parent b6c94d6 commit 7e770da

File tree

9 files changed

+35
-44
lines changed

9 files changed

+35
-44
lines changed

packages/react-devtools-shared/src/devtools/views/Profiler/CommitFlamegraph.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import HoveredFiberInfo from './HoveredFiberInfo';
2323
import {scale} from './utils';
2424
import {StoreContext} from '../context';
2525
import {SettingsContext} from '../Settings/SettingsContext';
26-
import Tooltip from '../Components/Tooltip';
26+
import Tooltip from './Tooltip';
2727

2828
import styles from './CommitFlamegraph.css';
2929

packages/react-devtools-shared/src/devtools/views/Profiler/CommitRanked.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import HoveredFiberInfo from './HoveredFiberInfo';
1717
import {scale} from './utils';
1818
import {StoreContext} from '../context';
1919
import {SettingsContext} from '../Settings/SettingsContext';
20-
import Tooltip from '../Components/Tooltip';
20+
import Tooltip from './Tooltip';
2121

2222
import styles from './CommitRanked.css';
2323

packages/react-devtools-shared/src/devtools/views/Profiler/HoveredFiberInfo.css

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
.Toolbar {
2-
height: 2.25rem;
3-
padding: 0 0.5rem;
2+
padding: 0.25rem 0;
3+
margin-bottom: 0.25rem;
44
flex: 0 0 auto;
55
display: flex;
66
align-items: center;
77
border-bottom: 1px solid var(--color-border);
88
}
99

1010
.Content {
11-
padding: 0.5rem;
1211
user-select: none;
1312
overflow-y: auto;
1413
}
@@ -25,14 +24,13 @@
2524

2625
.Label {
2726
font-weight: bold;
28-
margin-bottom: 0.5rem;
2927
}
3028

3129
.CurrentCommit {
30+
margin-top: 0.25rem;
3231
display: block;
3332
width: 100%;
3433
text-align: left;
3534
background: none;
3635
border: none;
37-
padding: 0.25rem 0.5rem;
3836
}

packages/react-devtools-shared/src/devtools/views/Profiler/HoveredFiberInfo.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import React, {Fragment, useContext} from 'react';
1111
import {ProfilerContext} from './ProfilerContext';
1212
import {formatDuration, formatTime} from './utils';
13-
import ProfilerWhatChanged from '../Components/ProfilerWhatChanged';
13+
import WhatChanged from './WhatChanged';
1414
import {StoreContext} from '../context';
1515

1616
import styles from './HoveredFiberInfo.css';
@@ -67,7 +67,7 @@ export default function HoveredFiberInfo({fiberData}: Props) {
6767
<div className={styles.Component}>{name}</div>
6868
</div>
6969
<div className={styles.Content}>
70-
<ProfilerWhatChanged fiberID={((id: any): number)} />
70+
<WhatChanged fiberID={((id: any): number)} />
7171
{renderDurationInfo || (
7272
<div>Did not render during this profiling session.</div>
7373
)}

packages/react-devtools-shared/src/devtools/views/Profiler/SidebarSelectedFiberInfo.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*/
99

1010
import React, {Fragment, useContext} from 'react';
11-
import ProfilerWhatChanged from '../Components/ProfilerWhatChanged';
11+
import WhatChanged from './WhatChanged';
1212
import {ProfilerContext} from './ProfilerContext';
1313
import {formatDuration, formatTime} from './utils';
1414
import {StoreContext} from '../context';
@@ -75,7 +75,7 @@ export default function SidebarSelectedFiberInfo(_: Props) {
7575
</Button>
7676
</div>
7777
<div className={styles.Content}>
78-
<ProfilerWhatChanged fiberID={((selectedFiberID: any): number)} />
78+
<WhatChanged fiberID={((selectedFiberID: any): number)} />
7979
{listItems.length > 0 && (
8080
<Fragment>
8181
<label className={styles.Label}>Rendered at</label>: {listItems}

packages/react-devtools-shared/src/devtools/views/Components/Tooltip.js renamed to packages/react-devtools-shared/src/devtools/views/Profiler/Tooltip.js

Lines changed: 22 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -43,46 +43,32 @@ export default function Tooltip({children, label}: any) {
4343
);
4444
}
4545

46+
const TOOLTIP_OFFSET = 5;
47+
4648
// Method used to find the position of the tooltip based on current mouse position
4749
function getTooltipPosition(element, mousePosition) {
4850
const {height, mouseX, mouseY, width} = mousePosition;
49-
const TOOLTIP_OFFSET_X = 5;
50-
const TOOLTIP_OFFSET_Y = 15;
5151
let top = 0;
5252
let left = 0;
5353

54-
// Let's check the vertical position.
55-
if (mouseY + TOOLTIP_OFFSET_Y + element.offsetHeight >= height) {
56-
// The tooltip doesn't fit below the mouse cursor (which is our
57-
// default strategy). Therefore we try to position it either above the
58-
// mouse cursor or finally aligned with the window's top edge.
59-
if (mouseY - TOOLTIP_OFFSET_Y - element.offsetHeight > 0) {
60-
// We position the tooltip above the mouse cursor if it fits there.
61-
top = `${mouseY - element.offsetHeight - TOOLTIP_OFFSET_Y}px`;
54+
if (mouseY + TOOLTIP_OFFSET + element.offsetHeight >= height) {
55+
if (mouseY - TOOLTIP_OFFSET - element.offsetHeight > 0) {
56+
top = `${mouseY - element.offsetHeight - TOOLTIP_OFFSET}px`;
6257
} else {
63-
// Otherwise we align the tooltip with the window's top edge.
6458
top = '0px';
6559
}
6660
} else {
67-
top = `${mouseY + TOOLTIP_OFFSET_Y}px`;
61+
top = `${mouseY + TOOLTIP_OFFSET}px`;
6862
}
6963

70-
// Now let's check the horizontal position.
71-
if (mouseX + TOOLTIP_OFFSET_X + element.offsetWidth >= width) {
72-
// The tooltip doesn't fit at the right of the mouse cursor (which is
73-
// our default strategy). Therefore we try to position it either at the
74-
// left of the mouse cursor or finally aligned with the window's left
75-
// edge.
76-
if (mouseX - TOOLTIP_OFFSET_X - element.offsetWidth > 0) {
77-
// We position the tooltip at the left of the mouse cursor if it fits
78-
// there.
79-
left = `${mouseX - element.offsetWidth - TOOLTIP_OFFSET_X}px`;
64+
if (mouseX + TOOLTIP_OFFSET + element.offsetWidth >= width) {
65+
if (mouseX - TOOLTIP_OFFSET - element.offsetWidth > 0) {
66+
left = `${mouseX - element.offsetWidth - TOOLTIP_OFFSET}px`;
8067
} else {
81-
// Otherwise, align the tooltip with the window's left edge.
8268
left = '0px';
8369
}
8470
} else {
85-
left = `${mouseX + TOOLTIP_OFFSET_X * 2}px`;
71+
left = `${mouseX + TOOLTIP_OFFSET * 2}px`;
8672
}
8773

8874
return {left, top};
@@ -94,9 +80,19 @@ function getMousePosition(
9480
mouseEvent: SyntheticMouseEvent<*>,
9581
) {
9682
if (relativeContainer !== null) {
97-
const {height, top, width} = relativeContainer.getBoundingClientRect();
83+
// Positon within the nearest position:relative container.
84+
let targetContainer = relativeContainer;
85+
while (targetContainer.parentElement != null) {
86+
if (targetContainer.style.position === 'relative') {
87+
break;
88+
} else {
89+
targetContainer = targetContainer.parentElement;
90+
}
91+
}
92+
93+
const {height, left, top, width} = targetContainer.getBoundingClientRect();
9894

99-
const mouseX = mouseEvent.clientX;
95+
const mouseX = mouseEvent.clientX - left;
10096
const mouseY = mouseEvent.clientY - top;
10197

10298
return {height, mouseX, mouseY, width};

packages/react-devtools-shared/src/devtools/views/Components/ProfilerWhatChanged.css renamed to packages/react-devtools-shared/src/devtools/views/Profiler/WhatChanged.css

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
.Component {
2-
margin-bottom: 1rem;
2+
margin-bottom: 0.5rem;
33
}
44

55
.Item {
@@ -26,5 +26,4 @@
2626

2727
.Label {
2828
font-weight: bold;
29-
margin-bottom: 0.5rem;
3029
}

packages/react-devtools-shared/src/devtools/views/Components/ProfilerWhatChanged.js renamed to packages/react-devtools-shared/src/devtools/views/Profiler/WhatChanged.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,13 @@ import React, {useContext} from 'react';
1111
import {ProfilerContext} from '../Profiler/ProfilerContext';
1212
import {StoreContext} from '../context';
1313

14-
import styles from './ProfilerWhatChanged.css';
14+
import styles from './WhatChanged.css';
1515

16-
type ProfilerWhatChangedProps = {|
16+
type Props = {|
1717
fiberID: number,
1818
|};
1919

20-
export default function ProfilerWhatChanged({
21-
fiberID,
22-
}: ProfilerWhatChangedProps) {
20+
export default function WhatChanged({fiberID}: Props) {
2321
const {profilerStore} = useContext(StoreContext);
2422
const {rootID, selectedCommitIndex} = useContext(ProfilerContext);
2523

0 commit comments

Comments
 (0)