Skip to content

Commit 2ebb4d4

Browse files
[charts] Fix tooltip position (#17440)
1 parent 124bfdd commit 2ebb4d4

File tree

2 files changed

+16
-12
lines changed

2 files changed

+16
-12
lines changed

packages/x-charts/src/ChartsTooltip/ChartsTooltipContainer.tsx

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import PropTypes from 'prop-types';
44
import HTMLElementType from '@mui/utils/HTMLElementType';
55
import useLazyRef from '@mui/utils/useLazyRef';
66
import { styled, useThemeProps } from '@mui/material/styles';
7-
import Popper, { PopperPlacementType, PopperProps } from '@mui/material/Popper';
7+
import Popper, { PopperProps } from '@mui/material/Popper';
88
import NoSsr from '@mui/material/NoSsr';
99
import { useSvgRef } from '../hooks/useSvgRef';
1010
import { TriggerOptions, usePointerType } from './utils';
@@ -122,17 +122,24 @@ function ChartsTooltipContainer(inProps: ChartsTooltipContainerProps) {
122122
{
123123
name: 'offset',
124124
options: {
125-
offset: ({ placement }: { placement: PopperPlacementType }) => {
125+
offset: () => {
126126
if (pointerType?.pointerType !== 'touch') {
127-
return [0, 0];
127+
return [0, 8];
128128
}
129-
130-
const isBottom = placement.startsWith('bottom');
131-
const placementOffset = isBottom ? 32 : 8;
132-
return [0, pointerType.height + placementOffset];
129+
return [0, 64];
133130
},
134131
},
135132
},
133+
...(pointerType?.pointerType === 'mouse'
134+
? [] // Keep default behavior
135+
: [
136+
{
137+
name: 'flip',
138+
options: {
139+
fallbackPlacements: ['top-end', 'top-start', 'bottom-end', 'bottom'],
140+
},
141+
},
142+
]),
136143
],
137144
[pointerType],
138145
);
@@ -147,9 +154,7 @@ function ChartsTooltipContainer(inProps: ChartsTooltipContainerProps) {
147154
<ChartsTooltipRoot
148155
className={classes?.root}
149156
open={popperOpen}
150-
placement={
151-
pointerType?.pointerType === 'mouse' ? ('right-start' as const) : ('top' as const)
152-
}
157+
placement={pointerType?.pointerType === 'mouse' ? 'right-start' : 'top'}
153158
popperRef={popperRef}
154159
anchorEl={anchorEl}
155160
modifiers={modifiers}

packages/x-charts/src/ChartsTooltip/utils.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ export function useMouseTracker(): UseMouseTrackerReturnValue {
5757
return mousePosition;
5858
}
5959

60-
type PointerType = Pick<MousePosition, 'height' | 'pointerType'>;
60+
type PointerType = Pick<MousePosition, 'pointerType'>;
6161

6262
export function usePointerType(): null | PointerType {
6363
const svgRef = useSvgRef();
@@ -79,7 +79,6 @@ export function usePointerType(): null | PointerType {
7979

8080
const handleEnter = (event: PointerEvent) => {
8181
setPointerType({
82-
height: event.height,
8382
pointerType: event.pointerType as PointerType['pointerType'],
8483
});
8584
};

0 commit comments

Comments
 (0)