Skip to content

Commit b50a243

Browse files
authored
Merge pull request #2157 from Parvinmh/fix/tooltip-position
Apply user-requested tooltip position
2 parents 7c3d739 + 4c42433 commit b50a243

File tree

2 files changed

+30
-17
lines changed

2 files changed

+30
-17
lines changed

example/html-tooltip/index.html

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,22 +79,23 @@ <h4>Section Six</h4>
7979
{
8080
title: '<p>Welcome</p>',
8181
element: '#step1',
82-
intro: "This is a <b>bold</b> tooltip."
82+
intro: "This is a <b>bold</b> tooltip.",
83+
position: 'right'
8384
},
8485
{
8586
element: '#step2',
8687
intro: "Ok, <i>wasn't</i> that fun?",
87-
position: 'right'
88+
position: 'bottom-right-aligned'
8889
},
8990
{
9091
element: '#step3',
9192
intro: 'More features, more <span style="color: red;">f</span><span style="color: green;">u</span><span style="color: blue;">n</span>.',
92-
position: 'left'
93+
position: 'top-middle-aligned'
9394
},
9495
{
9596
element: '#step4',
9697
intro: "<span style='font-family: Tahoma'>Another step with new font!</span>",
97-
position: 'bottom'
98+
position: 'bottom-middle-aligned'
9899
},
99100
{
100101
element: '#step5',

src/packages/tooltip/tooltipPosition.ts

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -104,22 +104,25 @@ export function determineAutoPosition(
104104
removeEntry<TooltipPosition>(possiblePositions, "left");
105105
}
106106

107-
// strip alignment from position
107+
// Check if user requested a specific alignment
108+
const userRequestedAlignment =
109+
desiredTooltipPosition && desiredTooltipPosition.includes("-aligned");
110+
111+
// strip alignment from position for base position checking
112+
let basePosition = desiredTooltipPosition;
108113
if (desiredTooltipPosition) {
109114
// ex: "bottom-right-aligned"
110115
// should return 'bottom'
111-
desiredTooltipPosition = desiredTooltipPosition.split(
112-
"-"
113-
)[0] as TooltipPosition;
116+
basePosition = desiredTooltipPosition.split("-")[0] as TooltipPosition;
114117
}
115118

116119
if (possiblePositions.length) {
117120
// Pick the first valid position, in order
118121
calculatedPosition = possiblePositions[0];
119122

120-
if (possiblePositions.includes(desiredTooltipPosition)) {
123+
if (possiblePositions.includes(basePosition)) {
121124
// If the requested position is in the list, choose that
122-
calculatedPosition = desiredTooltipPosition;
125+
calculatedPosition = basePosition;
123126
}
124127
}
125128

@@ -149,13 +152,22 @@ export function determineAutoPosition(
149152
];
150153
}
151154

152-
calculatedPosition =
153-
determineAutoAlignment(
154-
targetOffset.absoluteLeft,
155-
tooltipWidth,
156-
windowSize.width,
157-
desiredAlignment
158-
) || defaultAlignment;
155+
// If user requested a specific alignment, use it directly
156+
if (
157+
userRequestedAlignment &&
158+
desiredAlignment.includes(desiredTooltipPosition)
159+
) {
160+
calculatedPosition = desiredTooltipPosition;
161+
} else {
162+
// No specific alignment requested, use auto-alignment
163+
calculatedPosition =
164+
determineAutoAlignment(
165+
targetOffset.absoluteLeft,
166+
tooltipWidth,
167+
windowSize.width,
168+
desiredAlignment
169+
) || defaultAlignment;
170+
}
159171
}
160172

161173
return calculatedPosition;

0 commit comments

Comments
 (0)