1
- import type { BoundingRect , Dimensions } from '@shopify/polaris-viz-core' ;
2
- import { HORIZONTAL_GROUP_LABEL_HEIGHT } from '@shopify/polaris-viz-core' ;
1
+ import type { Dimensions } from '@shopify/polaris-viz-core' ;
2
+ import { clamp , HORIZONTAL_GROUP_LABEL_HEIGHT } from '@shopify/polaris-viz-core' ;
3
3
4
- import { TOOLTIP_MARGIN } from '../constants' ;
4
+ import { SCROLLBAR_WIDTH , TOOLTIP_MARGIN } from '../constants' ;
5
5
import type { AlteredPositionProps , AlteredPositionReturn } from '../types' ;
6
6
7
7
export function getAlteredHorizontalBarPosition (
@@ -20,100 +20,59 @@ function getNegativeOffset(props: AlteredPositionProps): AlteredPositionReturn {
20
20
const yOffset = ( bandwidth - tooltipDimensions . height ) / 2 ;
21
21
22
22
const y = currentY - tooltipDimensions . height ;
23
+
23
24
if ( flippedX - tooltipDimensions . width < 0 ) {
24
- return { x : flippedX , y : y < 0 ? 0 : y } ;
25
+ return clampPosition ( {
26
+ x : flippedX ,
27
+ y : y < 0 ? 0 : y ,
28
+ tooltipDimensions,
29
+ } ) ;
25
30
}
26
31
27
- return {
32
+ return clampPosition ( {
28
33
x : flippedX - tooltipDimensions . width - TOOLTIP_MARGIN ,
29
34
y : currentY + HORIZONTAL_GROUP_LABEL_HEIGHT + yOffset ,
30
- } ;
35
+ tooltipDimensions,
36
+ } ) ;
31
37
}
32
38
33
39
function getPositiveOffset ( props : AlteredPositionProps ) : AlteredPositionReturn {
34
- const { bandwidth , currentX, currentY, tooltipDimensions , chartBounds } = props ;
40
+ const { currentX, currentY} = props ;
35
41
36
- const isOutside = isOutsideBounds ( {
42
+ return clampPosition ( {
37
43
x : currentX ,
38
44
y : currentY ,
39
- tooltipDimensions,
40
- chartBounds,
45
+ tooltipDimensions : props . tooltipDimensions ,
41
46
} ) ;
42
-
43
- if ( isOutside . top && isOutside . right ) {
44
- return {
45
- x : chartBounds . width - tooltipDimensions . width ,
46
- y : 0 ,
47
- } ;
48
- }
49
-
50
- if ( isOutside . top && ! isOutside . right ) {
51
- return {
52
- x : currentX + TOOLTIP_MARGIN ,
53
- y : 0 ,
54
- } ;
55
- }
56
-
57
- if ( ! isOutside . right && ! isOutside . bottom ) {
58
- const yOffset = ( bandwidth - tooltipDimensions . height ) / 2 ;
59
- return {
60
- x : currentX + TOOLTIP_MARGIN ,
61
- y : currentY + HORIZONTAL_GROUP_LABEL_HEIGHT + yOffset ,
62
- } ;
63
- }
64
-
65
- if ( isOutside . right ) {
66
- const x = currentX - tooltipDimensions . width ;
67
- const y =
68
- currentY -
69
- tooltipDimensions . height +
70
- HORIZONTAL_GROUP_LABEL_HEIGHT -
71
- TOOLTIP_MARGIN ;
72
-
73
- if ( y < 0 ) {
74
- return {
75
- x,
76
- y : bandwidth + HORIZONTAL_GROUP_LABEL_HEIGHT + TOOLTIP_MARGIN ,
77
- } ;
78
- }
79
-
80
- return {
81
- x,
82
- y,
83
- } ;
84
- }
85
-
86
- if ( isOutside . bottom ) {
87
- return {
88
- x : currentX + TOOLTIP_MARGIN ,
89
- y :
90
- chartBounds . height -
91
- tooltipDimensions . height -
92
- HORIZONTAL_GROUP_LABEL_HEIGHT ,
93
- } ;
94
- }
95
-
96
- return { x : currentX , y : currentY } ;
97
47
}
98
48
99
- function isOutsideBounds ( {
49
+ function clampPosition ( {
100
50
x,
101
51
y,
102
52
tooltipDimensions,
103
- chartBounds,
104
53
} : {
105
54
x : number ;
106
55
y : number ;
107
56
tooltipDimensions : Dimensions ;
108
- chartBounds : BoundingRect ;
109
57
} ) {
110
- const right = x + TOOLTIP_MARGIN + tooltipDimensions . width ;
111
- const bottom = y + tooltipDimensions . height ;
112
-
113
58
return {
114
- left : x <= 0 ,
115
- right : right > chartBounds . width ,
116
- bottom : bottom > chartBounds . height ,
117
- top : y <= 0 ,
59
+ x : clamp ( {
60
+ amount : x ,
61
+ min : TOOLTIP_MARGIN ,
62
+ max :
63
+ window . innerWidth -
64
+ tooltipDimensions . width -
65
+ TOOLTIP_MARGIN -
66
+ SCROLLBAR_WIDTH ,
67
+ } ) ,
68
+ y : clamp ( {
69
+ amount : y ,
70
+ min : window . scrollY + TOOLTIP_MARGIN ,
71
+ max :
72
+ window . scrollY +
73
+ window . innerHeight -
74
+ tooltipDimensions . height -
75
+ TOOLTIP_MARGIN ,
76
+ } ) ,
118
77
} ;
119
78
}
0 commit comments