fix(ui): show cut-off text whole in a tooltip (#3800)

Text shortened with an ellipsis gave no way to read it whole. The tooltip host now shows such text in full after the usual delay, only while it is actually cut off and when it has no native title. Inside an element with a tooltip of its own (an editor tab, a commit-panel file with its change kind) the whole text comes first and that tooltip beside it. The commit panel's change kind moves from title to the tooltip host.
Цей коміт міститься в:
l0ng-ai authored and GitHub committed 2026-11-17 00:54:31 +08:00
1 джерело 6cdb2f1f1e
коміт 122d7f3d0f
3 files changed
+133 -9

No files matched your search

+2 -1
Переглянути файл
@@ -38,6 +38,7 @@ import {
registerDropTarget,
showContextMenu,
startPointerDrag,
tooltipAttrs,
type MenuItem,
type TabItem,
type TreeHandle,
@@ -884,7 +885,7 @@ export function ChangesTree({ mode }: { mode: "commit" | "local" | "dialog" }) {
n.type === "group" && n.changelist && !n.active && styles.inactiveList,
)}
style={n.type === "file" && n.kind ? { color: KIND_COLOR[n.kind] } : undefined}
title={n.type === "file" && n.kind ? tx(KIND_LABEL[n.kind]) : undefined}
{...(n.type === "file" && n.kind && tooltipAttrs(KIND_LABEL[n.kind]))}
>
{nodeText(n)}
</span>
+88
Переглянути файл
@@ -92,3 +92,91 @@ describe("TooltipHost", () => {
}
});
});
describe("TooltipHost on cut-off text", () => {
beforeEach(() => {
vi.useFakeTimers();
});
afterEach(() => {
cleanup();
vi.useRealTimers();
});
/** jsdom lays nothing out: give `el` the widths a browser would. */
const widths = (el: HTMLElement, scroll: number, client: number) => {
Object.defineProperty(el, "scrollWidth", { configurable: true, value: scroll });
Object.defineProperty(el, "clientWidth", { configurable: true, value: client });
};
const hover = (el: Element) => {
fireEvent.pointerOver(el);
act(() => {
vi.advanceTimersByTime(TOOLTIP_DELAY);
});
return screen.queryByRole("tooltip")?.textContent ?? null;
};
it("shows the whole text only while it is cut off", () => {
render(
<>
<TooltipHost />
<div style={{ textOverflow: "ellipsis", overflow: "hidden" }} data-testid="row">
<span>origin/feature/REQ-126-明细汇总多选</span>
</div>
</>,
);
const row = screen.getByTestId("row");
widths(row, 300, 120);
expect(hover(row.firstElementChild!)).toBe("origin/feature/REQ-126-明细汇总多选");
fireEvent.pointerOut(row);
widths(row, 120, 120);
expect(hover(row.firstElementChild!)).toBeNull();
});
it("leaves text with a title alone, and text without an ellipsis; adds its own tooltip beside it", () => {
render(
<>
<TooltipHost />
<div style={{ textOverflow: "ellipsis" }} title="native" data-testid="titled">
long name
</div>
<div style={{ textOverflow: "clip" }} data-testid="clipped">
long name
</div>
<div style={{ textOverflow: "ellipsis" }} data-testid="own" {...tooltipAttrs("Own tooltip", undefined, true)}>
long name
</div>
</>,
);
for (const id of ["titled", "clipped", "own"]) widths(screen.getByTestId(id), 300, 120);
expect(hover(screen.getByTestId("titled"))).toBeNull();
expect(hover(screen.getByTestId("clipped"))).toBeNull();
expect(hover(screen.getByTestId("own"))).toBe("long nameOwn tooltip");
});
});
it("shows a tab's cut-off title whole, with the tab's own tooltip beside it", () => {
vi.useFakeTimers();
render(
<>
<TooltipHost />
<div role="tab" {...tooltipAttrs("/repo/src/history/PscPrinted.js", undefined, true)}>
<span style={{ textOverflow: "ellipsis" }} data-testid="label">
历史记录: PscPrinted.js
</span>
</div>
</>,
);
const label = screen.getByTestId("label");
Object.defineProperty(label, "scrollWidth", { configurable: true, value: 300 });
Object.defineProperty(label, "clientWidth", { configurable: true, value: 100 });
fireEvent.pointerOver(label);
act(() => {
vi.advanceTimersByTime(TOOLTIP_DELAY);
});
expect(screen.getByRole("tooltip").textContent).toBe("历史记录: PscPrinted.js/repo/src/history/PscPrinted.js");
cleanup();
vi.useRealTimers();
});
+43 -8
Переглянути файл
@@ -2,6 +2,9 @@
* Tooltips are attribute-driven: any element with `data-tooltip` (and optional
* `data-tooltip-shortcut`) gets one, shown by the single `<TooltipHost/>` after
* 600ms of hover. Hidden on scroll, key press, pointer down or leave.
*
* Text cut off with an ellipsis gets one too, showing it whole, while it is cut off and
* has no tooltip or `title` of its own.
*/
import { useEffect, useRef, useState } from "react";
import clsx from "clsx";
@@ -22,6 +25,34 @@ export function tooltipAttrs(text: string | undefined, shortcut?: string, verbat
return attrs;
}
/** How far up from the hovered node an ellipsised text is looked for. */
const TRUNCATED_DEPTH = 4;
/** The element whose text is cut off with an ellipsis around `node`, up to `stop`, if any. */
function truncatedText(node: Element | null, stop: Element | null): HTMLElement | null {
for (let el = node, depth = 0; el instanceof HTMLElement && depth < TRUNCATED_DEPTH; el = el.parentElement, depth++) {
// A native tooltip shows instead.
if (el.hasAttribute("title")) return null;
if (isTruncated(el)) return el;
if (el === stop) break;
}
return null;
}
const isTruncated = (el: HTMLElement) =>
el.scrollWidth > el.clientWidth && getComputedStyle(el).textOverflow === "ellipsis" && !!el.textContent?.trim();
/**
* What the tooltip of `el` shows: its own text, and the whole of `cut`'s text while that is
* cut off (with its own text beside it, when that says something else).
*/
function tooltipText(el: HTMLElement, cut: HTMLElement | null): { text: string; note?: string } | undefined {
const own = el.dataset.tooltipVerbatim === undefined ? tx(el.dataset.tooltip) : el.dataset.tooltip;
const whole = cut?.isConnected && isTruncated(cut) ? cut.textContent!.trim().replace(/\s+/g, " ") : undefined;
if (!whole) return own ? { text: own, note: el.dataset.tooltipShortcut } : undefined;
return { text: whole, note: own && !own.includes(whole) ? own : undefined };
}
interface Shown {
text: string;
shortcut?: string;
@@ -49,15 +80,15 @@ export function TooltipHost() {
setShown(null);
};
const show = (el: HTMLElement) => {
const text = el.dataset.tooltipVerbatim === undefined ? tx(el.dataset.tooltip) : el.dataset.tooltip;
if (!text || !el.isConnected) return;
const show = (el: HTMLElement, cut: HTMLElement | null) => {
const shows = el.isConnected ? tooltipText(el, cut) : undefined;
if (!shows) return;
visible = true;
anchorRef.current = el;
const stripe = el.dataset.tooltipStripe;
setShown({
text,
shortcut: el.dataset.tooltipShortcut,
text: shows.text,
shortcut: shows.note,
x: -9999,
y: -9999,
placed: false,
@@ -66,16 +97,20 @@ export function TooltipHost() {
};
const onOver = (e: PointerEvent) => {
const el = (e.target as Element | null)?.closest?.<HTMLElement>("[data-tooltip]");
const target = e.target as Element | null;
const owner = target?.closest?.<HTMLElement>("[data-tooltip]") ?? null;
// Cut-off text inside an element with a tooltip shows in that tooltip.
const cut = truncatedText(target, owner);
const el = owner ?? cut;
if (!el) return;
if (el === anchorRef.current) return;
clearTimeout(timer);
if (visible) {
show(el);
show(el, cut);
return;
}
const warm = performance.now() - lastHidden < WARM_MS;
timer = setTimeout(() => show(el), warm ? 0 : TOOLTIP_DELAY);
timer = setTimeout(() => show(el, cut), warm ? 0 : TOOLTIP_DELAY);
anchorRef.current = el;
};