Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 19 additions & 3 deletions src/components/Tooltip/Tooltip.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ describe("Tooltip", () => {
expect(screen.queryByTestId("tooltip-portal")).not.toBeInTheDocument();
});

it("renders tooltip message on focus", async () => {
it("shows tooltip message on focus", async () => {
render(
<Tooltip message="tooltip text">
<button>open the tooltip</button>
Expand All @@ -177,8 +177,24 @@ describe("Tooltip", () => {
await act(async () => {
await userEventWithTimers.tab();
});
expect(screen.getByTestId("tooltip-portal")).toBeInTheDocument();
expect(screen.getByText("tooltip text")).toBeInTheDocument();
expect(screen.getByTestId("tooltip-portal")).toBeVisible();
expect(screen.getByText("tooltip text")).toBeVisible();
});

it("shows tooltip message on focus, with followMouse active", async () => {
render(
<Tooltip message="tooltip text" followMouse>
<button>open the tooltip</button>
</Tooltip>,
);

expect(screen.queryByTestId("tooltip-portal")).not.toBeInTheDocument();
expect(screen.queryByText("tooltip text")).not.toBeInTheDocument();
await act(async () => {
await userEventWithTimers.tab();
});
expect(screen.getByTestId("tooltip-portal")).toBeVisible();
expect(screen.getByText("tooltip text")).toBeVisible();
});

it("updates the tooltip to fit on the screen", async () => {
Expand Down
18 changes: 10 additions & 8 deletions src/components/Tooltip/Tooltip.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -275,13 +275,7 @@ const Tooltip = ({
);

// Handle mouse events.
useListener(
wrapperRef.current,
mouseHandler,
"mousemove",
true,
followMouse && isOpen,
);
useListener(wrapperRef.current, mouseHandler, "mousemove", true, followMouse);

// Handle adjusting the position of the tooltip so that it remains on screen.
useWindowFitment(
Expand Down Expand Up @@ -334,6 +328,14 @@ const Tooltip = ({
openPortal();
};

const handleFocus = () => {
if (followMouse && wrapperRef.current) {
// set initial position for the tooltip
setPositionStyle(getPositionStyle(adjustedPosition, wrapperRef.current));
}
openPortal();
};

const delayedOpenPortal: MouseEventHandler = useCallback(() => {
if (isOpen) {
return;
Expand All @@ -352,7 +354,7 @@ const Tooltip = ({
className={className}
onBlur={handleBlur}
onClick={handleClick}
onFocus={openPortal}
onFocus={handleFocus}
onMouseOut={handleBlur}
onMouseOver={delayedOpenPortal}
>
Expand Down