Skip to content

Commit 75adfa0

Browse files
committed
feat(plugin): add onPositionCalculated callback option to tooltip
1 parent e8755b8 commit 75adfa0

File tree

1 file changed

+39
-2
lines changed

1 file changed

+39
-2
lines changed

src/plugins/tooltip.tsx

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,30 @@ type TooltipConfigOptions = {
8686
* @default false
8787
*/
8888
readonly fixed?: boolean;
89+
/**
90+
* Optional callback to process or modify the calculated tooltip position.
91+
* Receives the calculated position and placement preference, and should return a position object with the same structure.
92+
* Use this to implement custom positioning logic or constraints.
93+
*
94+
* @param position - The calculated position with left and top coordinates
95+
* @param placement - The placement preference that was used for calculation
96+
* @returns Modified position object with left and top coordinates
97+
*
98+
* @example
99+
* ```ts
100+
* onPositionCalculated: (position, placement) => ({
101+
* left: Math.max(0, position.left), // Prevent negative positioning
102+
* top: placement.includes('top') ? position.top - 5 : position.top + 10
103+
* })
104+
* ```
105+
*/
106+
readonly onPositionCalculated?: (
107+
position: { left: number; top: number },
108+
placement: TooltipCursorPlacement,
109+
) => {
110+
left: number;
111+
top: number;
112+
};
89113
};
90114

91115
const TOOLTIP_OFFSET_X = 8;
@@ -161,6 +185,7 @@ export type TooltipPluginOptions = TooltipRootProps & TooltipConfigOptions;
161185
* - Automatic positioning with edge detection and flipping
162186
* - Scroll-aware positioning that works with page scrolling
163187
* - Configurable placement preferences
188+
* - Position callback for custom positioning logic or overrides
164189
* - Accessible tooltip with proper ARIA attributes
165190
* - Automatic cleanup and memory management
166191
*
@@ -261,7 +286,11 @@ export const tooltip = (
261286

262287
const chartCursorData = () => bus.data.cursor?.state[u.root.id];
263288

264-
const [tooltipOptions, containerProps] = splitProps(_options, ["placement", "fixed"]);
289+
const [tooltipOptions, containerProps] = splitProps(_options, [
290+
"placement",
291+
"fixed",
292+
"onPositionCalculated",
293+
]);
265294

266295
return (
267296
<Show when={chartCursorData()}>
@@ -284,14 +313,22 @@ export const tooltip = (
284313
? cursorTop
285314
: cursorTop + window.scrollY;
286315

287-
return getTooltipPosition(
316+
const calculatedPosition = getTooltipPosition(
288317
tooltipOptions.placement,
289318
absoluteLeft,
290319
absoluteTop,
291320
tooltipWidth,
292321
tooltipHeight,
293322
tooltipOptions.fixed,
294323
);
324+
325+
// Allow user to override or modify the calculated position
326+
return tooltipOptions.onPositionCalculated
327+
? tooltipOptions.onPositionCalculated(
328+
calculatedPosition,
329+
tooltipOptions.placement,
330+
)
331+
: calculatedPosition;
295332
};
296333

297334
return (

0 commit comments

Comments
 (0)