Skip to content
Open
7 changes: 4 additions & 3 deletions apps/web/src/components/pullRequest/PullRequestSummaryTab.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ import {
MenuTrigger,
} from "../ui/menu";
import { toastManager } from "../ui/toast";
import { Tooltip, TooltipPopup, TooltipTrigger } from "../ui/tooltip";
import { Tooltip, TooltipPopup, TooltipTrigger, TooltipScrollDismissArea } from "../ui/tooltip";
import {
PullRequestActorLabel,
PullRequestCheckStatusIcon,
Expand Down Expand Up @@ -866,7 +866,8 @@ export function PullRequestSummaryTab({
};

return (
<div
<TooltipScrollDismissArea
dismissFocused
ref={(node) => {
scrollRef.current = node;
scrollerRef(node);
Expand Down Expand Up @@ -1214,6 +1215,6 @@ export function PullRequestSummaryTab({
</>
)}
</PullRequestSection>
</div>
</TooltipScrollDismissArea>
);
}
50 changes: 26 additions & 24 deletions apps/web/src/components/pullRequest/PullRequestTimelineTab.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ import { Button } from "../ui/button";
import { PullRequestEditButton } from "./PullRequestEditButton";
import { Collapsible, CollapsiblePanel, CollapsibleTrigger } from "../ui/collapsible";
import { toastManager } from "../ui/toast";
import { Tooltip, TooltipPopup, TooltipTrigger } from "../ui/tooltip";
import { Tooltip, TooltipPopup, TooltipTrigger, TooltipScrollDismissArea } from "../ui/tooltip";
import {
buildPullRequestTimeline,
groupPullRequestTimelineConversations,
Expand Down Expand Up @@ -985,28 +985,30 @@ export function PullRequestTimelineTab({
// Virtualized: a large pull request has thousands of events, and mounting every one stalls
// the page. The connector line is drawn by the content container, behind the rows.
return (
<LegendList<PullRequestTimelineRow>
ref={(list: LegendListRef | null) => {
listRef.current = list;
scrollerRef(list?.getScrollableNode() ?? null);
}}
data={rows}
extraData={{ editable, reactions, newestCommitAt }}
keyExtractor={rowKey}
renderItem={renderRow}
estimatedItemSize={140}
className="scrollbar-gutter-both h-full px-4"
contentContainerClassName="relative mx-auto w-full max-w-3xl before:absolute before:bottom-10 before:left-[15px] before:top-6 before:w-px before:bg-border/45"
ListHeaderComponent={
anchor !== null && targetIndex < 0 && revealUrl === undefined ? (
<p role="status" className="py-4 text-xs text-muted-foreground">
<T>The linked item is not in the loaded activity.</T>
</p>
) : (
<div className="h-5" />
)
}
ListFooterComponent={<div className="h-5" />}
/>
<TooltipScrollDismissArea dismissFocused className="h-full">
<LegendList<PullRequestTimelineRow>
ref={(list: LegendListRef | null) => {
listRef.current = list;
scrollerRef(list?.getScrollableNode() ?? null);
}}
data={rows}
extraData={{ editable, reactions, newestCommitAt }}
keyExtractor={rowKey}
renderItem={renderRow}
estimatedItemSize={140}
className="scrollbar-gutter-both h-full px-4"
contentContainerClassName="relative mx-auto w-full max-w-3xl before:absolute before:bottom-10 before:left-[15px] before:top-6 before:w-px before:bg-border/45"
ListHeaderComponent={
anchor !== null && targetIndex < 0 && revealUrl === undefined ? (
<p role="status" className="py-4 text-xs text-muted-foreground">
<T>The linked item is not in the loaded activity.</T>
</p>
) : (
<div className="h-5" />
)
}
ListFooterComponent={<div className="h-5" />}
/>
</TooltipScrollDismissArea>
);
}
102 changes: 71 additions & 31 deletions apps/web/src/components/ui/tooltip.browser.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,38 +6,78 @@ import { render } from "../../../test/render";
import { Tooltip, TooltipPopup, TooltipScrollDismissArea, TooltipTrigger } from "./tooltip";

it.each([
{ focus: "mouse", keptOpen: false },
{ focus: "keyboard", keptOpen: true },
])("scrolling with a $focus-focused hovered trigger keeps it open: $keptOpen", async (c) => {
{ focus: "mouse", hover: true, dismissFocused: false, keptOpen: false },
{ focus: "keyboard", hover: true, dismissFocused: false, keptOpen: true },
{ focus: "keyboard", hover: false, dismissFocused: true, keptOpen: false },
{ focus: "keyboard", hover: true, dismissFocused: true, keptOpen: false },
])(
"scrolling a $focus-focused trigger (hover: $hover, dismissFocused: $dismissFocused) keeps it open: $keptOpen",
async (c) => {
const onScroll = vi.fn();
await using screen = await render(
<TooltipScrollDismissArea
dismissFocused={c.dismissFocused}
data-testid="timeline"
onScrollCapture={onScroll}
className="h-32 overflow-y-auto"
>
<Tooltip>
<TooltipTrigger>Timeline action</TooltipTrigger>
<TooltipPopup>Description</TooltipPopup>
</Tooltip>
<div className="h-96" />
</TooltipScrollDismissArea>,
);
const trigger = screen.getByRole("button", { name: "Timeline action" });
if (c.focus === "mouse") {
// A press closes the tooltip; hovering again reopens it with mouse focus kept.
await userEvent.click(trigger);
await userEvent.unhover(trigger);
} else {
await userEvent.keyboard("{Tab}");
}
if (c.hover) await userEvent.hover(trigger);
await expect.element(trigger).toHaveFocus();
await expect.element(page.getByText("Description")).toBeVisible();

screen.getByTestId("timeline").element().scrollTop = 20;
await vi.waitFor(() => expect(onScroll).toHaveBeenCalledOnce());

if (c.keptOpen) await expect.element(page.getByText("Description")).toBeVisible();
else await expect.element(page.getByText("Description")).not.toBeInTheDocument();
await expect.element(trigger).toHaveFocus();
},
);

it("keeps a tooltip through focus scrolling and dismisses it on the next scroll", async () => {
const onScroll = vi.fn();
await using screen = await render(
<TooltipScrollDismissArea
data-testid="timeline"
onScrollCapture={onScroll}
className="h-32 overflow-y-auto"
>
<Tooltip>
<TooltipTrigger>Timeline action</TooltipTrigger>
<TooltipPopup>Description</TooltipPopup>
</Tooltip>
<div className="h-96" />
</TooltipScrollDismissArea>,
<>
<button>Start navigation</button>
<TooltipScrollDismissArea
dismissFocused
data-testid="scroller"
onScrollCapture={onScroll}
className="h-32 overflow-y-auto"
>
<div className="h-96" />
<Tooltip>
<TooltipTrigger>Offscreen action</TooltipTrigger>
<TooltipPopup>Offscreen description</TooltipPopup>
</Tooltip>
<div className="h-96" />
</TooltipScrollDismissArea>
</>,
);
const trigger = screen.getByRole("button", { name: "Timeline action" });
if (c.focus === "mouse") {
// A press closes the tooltip; hovering again reopens it with mouse focus kept.
await userEvent.click(trigger);
await userEvent.unhover(trigger);
} else {
await userEvent.keyboard("{Tab}");
}
await userEvent.hover(trigger);
await expect.element(trigger).toHaveFocus();
await expect.element(page.getByText("Description")).toBeVisible();

screen.getByTestId("timeline").element().scrollTop = 20;
await vi.waitFor(() => expect(onScroll).toHaveBeenCalledOnce());

if (c.keptOpen) await expect.element(page.getByText("Description")).toBeVisible();
else await expect.element(page.getByText("Description")).not.toBeInTheDocument();
const scroller = screen.getByTestId("scroller").element();
await userEvent.click(screen.getByRole("button", { name: "Start navigation" }));
await userEvent.keyboard("{Tab}");
await vi.waitFor(() => {
expect(scroller.scrollTop).toBeGreaterThan(0);
expect(onScroll).toHaveBeenCalled();
});
await expect.element(screen.getByRole("button", { name: "Offscreen action" })).toHaveFocus();
await expect.element(page.getByText("Offscreen description")).toBeVisible();
scroller.scrollTop += 20;
await expect.element(page.getByText("Offscreen description")).not.toBeInTheDocument();
});
94 changes: 79 additions & 15 deletions apps/web/src/components/ui/tooltip.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,26 +19,60 @@ const TooltipProvider = TooltipPrimitive.Provider;

type TooltipActionsRef = RefObject<TooltipPrimitive.Root.Actions | null>;

// The compiler freezes context values; update hovered tooltips through calls instead of shared ref writes.
type TrackedTooltip = { trigger: HTMLElement; actionsRef: TooltipActionsRef };

// The compiler freezes context values; update tracked tooltips through calls instead of shared ref writes.
function createTooltipHoverTracker() {
let hovered: { trigger: HTMLElement; actionsRef: TooltipActionsRef } | null = null;
let hovered: TrackedTooltip | null = null;
let focused: TrackedTooltip | null = null;
let focusScrollFrame: number | null = null;
const settleFocus = () => {
if (focusScrollFrame !== null) cancelAnimationFrame(focusScrollFrame);
focusScrollFrame = null;
};
const blur = (actionsRef: TooltipActionsRef) => {
if (focused?.actionsRef !== actionsRef) return;
settleFocus();
focused = null;
};
const release = (actionsRef: TooltipActionsRef) => {
if (hovered?.actionsRef === actionsRef) hovered = null;
blur(actionsRef);
};
return {
hover(trigger: HTMLElement, actionsRef: TooltipActionsRef) {
hovered = { trigger, actionsRef };
},
release(actionsRef: TooltipActionsRef) {
if (hovered?.actionsRef === actionsRef) hovered = null;
focus(trigger: HTMLElement, actionsRef: TooltipActionsRef) {
settleFocus();
focused = { trigger, actionsRef };
// Browser focus scrolling is dispatched before the next animation frame.
focusScrollFrame = requestAnimationFrame(() => {
focusScrollFrame = null;
});
},
dismissUnfocused() {
const tooltip = hovered;
if (!tooltip) return;
const focused = tooltip.trigger.ownerDocument.activeElement;
if (focused && tooltip.trigger.contains(focused) && focused.matches(":focus-visible")) {
return;
blur,
release,
settleFocus,
dismiss(dismissFocused: boolean) {
const focusScrollPending = focusScrollFrame !== null;
settleFocus();
const tooltips =
hovered?.actionsRef === focused?.actionsRef ? [focused ?? hovered] : [hovered, focused];
for (const tooltip of tooltips) {
if (!tooltip) continue;
const active = tooltip.trigger.ownerDocument.activeElement;
if (
active &&
tooltip.trigger.contains(active) &&
active.matches(":focus-visible") &&
(!dismissFocused || focusScrollPending)
)
continue;
release(tooltip.actionsRef);
// Base UI also cancels delayed hover opens through this action.
tooltip.actionsRef.current?.close();
}
hovered = null;
// Base UI also cancels delayed hover opens through this action.
tooltip.actionsRef.current?.close();
},
};
}
Expand All @@ -47,15 +81,37 @@ type TooltipHoverTracker = ReturnType<typeof createTooltipHoverTracker>;
const TooltipHoverContext = createContext<TooltipActionsRef | null>(null);
const TooltipScrollContext = createContext<TooltipHoverTracker | null>(null);

function TooltipScrollDismissArea({ onScrollCapture, ...props }: ComponentProps<"div">) {
function TooltipScrollDismissArea({
onScrollCapture,
onWheelCapture,
onKeyDownCapture,
onPointerDownCapture,
dismissFocused = false,
...props
}: ComponentProps<"div"> & {
/** Also dismiss keyboard-focused tooltips when the user scrolls. */
dismissFocused?: boolean;
}) {
const [tracker] = useState(createTooltipHoverTracker);
return (
<TooltipScrollContext value={tracker}>
<div
{...props}
onWheelCapture={(event) => {
tracker.settleFocus();
onWheelCapture?.(event);
}}
onKeyDownCapture={(event) => {
tracker.settleFocus();
onKeyDownCapture?.(event);
}}
onPointerDownCapture={(event) => {
tracker.settleFocus();
onPointerDownCapture?.(event);
}}
onScrollCapture={(event) => {
onScrollCapture?.(event);
tracker.dismissUnfocused();
tracker.dismiss(dismissFocused);
}}
/>
</TooltipScrollContext>
Expand Down Expand Up @@ -98,6 +154,14 @@ function TooltipTrigger(props: TooltipPrimitive.Trigger.Props) {
props.onMouseEnter?.(event);
tracker.hover(event.currentTarget, actionsRef);
}}
onFocus={(event) => {
props.onFocus?.(event);
tracker.focus(event.currentTarget, actionsRef);
}}
onBlur={(event) => {
props.onBlur?.(event);
tracker.blur(actionsRef);
}}
/>
);
}
Expand Down
Loading