Skip to content

Commit 707cb88

Browse files
committed
fix(tooltip): set initial position for follow mouse mode
fix(tooltip): listen to mousemove event also for closed tooltip test(tooltip): add visibility tests for followMouse case test(tooltip): remove unnecessary followMouse flag from test fix(tooltip): pr changes
1 parent 4e3f89d commit 707cb88

2 files changed

Lines changed: 29 additions & 11 deletions

File tree

src/components/Tooltip/Tooltip.test.tsx

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ describe("Tooltip", () => {
165165
expect(screen.queryByTestId("tooltip-portal")).not.toBeInTheDocument();
166166
});
167167

168-
it("renders tooltip message on focus", async () => {
168+
it("shows tooltip message on focus", async () => {
169169
render(
170170
<Tooltip message="tooltip text">
171171
<button>open the tooltip</button>
@@ -177,8 +177,24 @@ describe("Tooltip", () => {
177177
await act(async () => {
178178
await userEventWithTimers.tab();
179179
});
180-
expect(screen.getByTestId("tooltip-portal")).toBeInTheDocument();
181-
expect(screen.getByText("tooltip text")).toBeInTheDocument();
180+
expect(screen.getByTestId("tooltip-portal")).toBeVisible();
181+
expect(screen.getByText("tooltip text")).toBeVisible();
182+
});
183+
184+
it("shows tooltip message on focus, with followMouse active", async () => {
185+
render(
186+
<Tooltip message="tooltip text" followMouse>
187+
<button>open the tooltip</button>
188+
</Tooltip>,
189+
);
190+
191+
expect(screen.queryByTestId("tooltip-portal")).not.toBeInTheDocument();
192+
expect(screen.queryByText("tooltip text")).not.toBeInTheDocument();
193+
await act(async () => {
194+
await userEventWithTimers.tab();
195+
});
196+
expect(screen.getByTestId("tooltip-portal")).toBeVisible();
197+
expect(screen.getByText("tooltip text")).toBeVisible();
182198
});
183199

184200
it("updates the tooltip to fit on the screen", async () => {

src/components/Tooltip/Tooltip.tsx

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -248,13 +248,7 @@ const Tooltip = ({
248248
);
249249

250250
// Handle mouse events.
251-
useListener(
252-
wrapperRef.current,
253-
mouseHandler,
254-
"mousemove",
255-
true,
256-
followMouse && isOpen,
257-
);
251+
useListener(wrapperRef.current, mouseHandler, "mousemove", true, followMouse);
258252

259253
// Handle adjusting the position of the tooltip so that it remains on screen.
260254
useWindowFitment(
@@ -307,6 +301,14 @@ const Tooltip = ({
307301
openPortal();
308302
};
309303

304+
const handleFocus = () => {
305+
if (followMouse && wrapperRef.current) {
306+
// set initial position for the tooltip
307+
setPositionStyle(getPositionStyle(adjustedPosition, wrapperRef.current));
308+
}
309+
openPortal();
310+
};
311+
310312
const delayedOpenPortal: MouseEventHandler = useCallback(() => {
311313
if (isOpen) {
312314
return;
@@ -325,7 +327,7 @@ const Tooltip = ({
325327
className={className}
326328
onBlur={handleBlur}
327329
onClick={handleClick}
328-
onFocus={openPortal}
330+
onFocus={handleFocus}
329331
onMouseOut={handleBlur}
330332
onMouseOver={delayedOpenPortal}
331333
>

0 commit comments

Comments
 (0)