fix(ui): wrap oversized single-line tooltips instead of clipping

This commit is contained in:
zipg authored and GitHub committed 2026-09-28 21:03:57 +08:00
1 parent e444c15e0c
commit bf3a475997
2 files changed
+89 -30

No files matched your search

@@ -34,6 +34,10 @@ const show = ref(false);
const x = ref(0); const x = ref(0);
const y = ref(0); const y = ref(0);
const arrowOffset = ref<number>(); const arrowOffset = ref<number>();
// `nowrap` keeps short hints on one line. Anything wider than the surface cap
// (max-w-xs) cannot stay on one line without spilling past the bubble, so the
// content switches to wrapping once the single-line layout overflows (#10451).
const wrapNowrapContent = ref(false);
let timer: ReturnType<typeof setTimeout> | null = null; let timer: ReturnType<typeof setTimeout> | null = null;
let closeTimer: ReturnType<typeof setTimeout> | null = null; let closeTimer: ReturnType<typeof setTimeout> | null = null;
let suppressOpenUntil = 0; let suppressOpenUntil = 0;
@@ -162,10 +166,25 @@ function fitPositionToViewport() {
arrowOffset.value = props.side === "left" || props.side === "right" ? floatingArrowOffset(rect.height, shift.y) : floatingArrowOffset(rect.width, shift.x); arrowOffset.value = props.side === "left" || props.side === "right" ? floatingArrowOffset(rect.height, shift.y) : floatingArrowOffset(rect.width, shift.x);
} }
/**
* Detects a single-line tooltip whose text is wider than the surface. CSS
* cannot express "one line unless it does not fit", so the measurement decides
* between `whitespace-nowrap` and wrapping. Returns true when the caller must
* re-position after the text reflowed.
*/
function switchToWrappingWhenOverflowing(): boolean {
const tooltip = tooltipRef.value;
if (!tooltip || !props.nowrap || wrapNowrapContent.value) return false;
if (tooltip.scrollWidth - tooltip.clientWidth <= 1) return false;
wrapNowrapContent.value = true;
return true;
}
function close() { function close() {
clearTimer(); clearTimer();
clearCloseTimer(); clearCloseTimer();
show.value = false; show.value = false;
wrapNowrapContent.value = false;
openSource = null; openSource = null;
removeGlobalListeners(); removeGlobalListeners();
} }
@@ -238,8 +257,15 @@ function open(source: "hover" | "focus") {
if (source === "focus" ? !hasFocusVisible() : !isPointerActive()) return; if (source === "focus" ? !hasFocusVisible() : !isPointerActive()) return;
updatePosition(); updatePosition();
openSource = source; openSource = source;
wrapNowrapContent.value = false;
show.value = true; show.value = true;
void nextTick(fitPositionToViewport); void nextTick(() => {
if (switchToWrappingWhenOverflowing()) {
void nextTick(fitPositionToViewport);
return;
}
fitPositionToViewport();
});
addGlobalListeners(); addGlobalListeners();
} }
@@ -281,7 +307,7 @@ watch(
v-if="show" v-if="show"
ref="tooltipRef" ref="tooltipRef"
class="fixed z-50 rounded-md text-xs" class="fixed z-50 rounded-md text-xs"
:class="cn([tooltipSurfaceClass, slots.content ? '' : ['inline-flex w-fit max-w-xs items-center gap-1.5 px-3 py-1.5', nowrap ? 'whitespace-nowrap' : ''], tooltipTransformClass], contentClass)" :class="cn([tooltipSurfaceClass, slots.content ? '' : ['inline-flex w-fit max-w-xs items-center gap-1.5 px-3 py-1.5', nowrap && !wrapNowrapContent ? 'whitespace-nowrap' : 'break-words'], tooltipTransformClass], contentClass)"
:style="{ left: `${x}px`, top: `${y}px` }" :style="{ left: `${x}px`, top: `${y}px` }"
role="tooltip" role="tooltip"
@mouseenter="clearCloseTimer" @mouseenter="clearCloseTimer"
+61 -28
View File
@@ -9,6 +9,34 @@ afterEach(() => {
vi.useRealTimers(); vi.useRealTimers();
}); });
async function mountAndHover(props: Record<string, unknown>) {
const root = defineComponent({
setup() {
return () =>
h(
LightTooltip,
{ delay: 0, ...props },
{
default: () => h("span", { id: "content-class-trigger" }, "pill"),
},
);
},
});
const container = document.createElement("div");
document.body.append(container);
const app = createApp(root);
app.mount(container);
const trigger = container.querySelector<HTMLElement>("#content-class-trigger");
expect(trigger).toBeTruthy();
vi.spyOn(trigger!, "matches").mockImplementation((selector) => selector === ":hover");
trigger?.dispatchEvent(new MouseEvent("mouseenter", { bubbles: true }));
await vi.runAllTimersAsync();
await nextTick();
return { app, tooltip: document.querySelector<HTMLElement>("[role=tooltip]") };
}
describe("LightTooltip scrolling", () => { describe("LightTooltip scrolling", () => {
it("stays open for content scrolling and closes for outside scrolling", async () => { it("stays open for content scrolling and closes for outside scrolling", async () => {
vi.useFakeTimers(); vi.useFakeTimers();
@@ -52,34 +80,6 @@ describe("LightTooltip scrolling", () => {
}); });
describe("LightTooltip contentClass", () => { describe("LightTooltip contentClass", () => {
async function mountAndHover(props: Record<string, unknown>) {
const root = defineComponent({
setup() {
return () =>
h(
LightTooltip,
{ delay: 0, ...props },
{
default: () => h("span", { id: "content-class-trigger" }, "pill"),
},
);
},
});
const container = document.createElement("div");
document.body.append(container);
const app = createApp(root);
app.mount(container);
const trigger = container.querySelector<HTMLElement>("#content-class-trigger");
expect(trigger).toBeTruthy();
vi.spyOn(trigger!, "matches").mockImplementation((selector) => selector === ":hover");
trigger?.dispatchEvent(new MouseEvent("mouseenter", { bubbles: true }));
await vi.runAllTimersAsync();
await nextTick();
return { app, tooltip: document.querySelector<HTMLElement>("[role=tooltip]") };
}
it("merges contentClass into the teleported tooltip and drops the default max-w-xs", async () => { it("merges contentClass into the teleported tooltip and drops the default max-w-xs", async () => {
vi.useFakeTimers(); vi.useFakeTimers();
const { app, tooltip } = await mountAndHover({ text: "Conflict hint", contentClass: "max-w-[320px]" }); const { app, tooltip } = await mountAndHover({ text: "Conflict hint", contentClass: "max-w-[320px]" });
@@ -103,3 +103,36 @@ describe("LightTooltip contentClass", () => {
app.unmount(); app.unmount();
}); });
}); });
describe("LightTooltip nowrap overflow", () => {
it("wraps a single-line tooltip that is wider than the surface instead of spilling past it", async () => {
vi.useFakeTimers();
vi.spyOn(HTMLElement.prototype, "scrollWidth", "get").mockReturnValue(400);
vi.spyOn(HTMLElement.prototype, "clientWidth", "get").mockReturnValue(320);
const { app, tooltip } = await mountAndHover({
text: "1.2 按月看话单量 (确认没有整月空洞;单条超时时按月拆量重跑)",
nowrap: true,
});
expect(tooltip).toBeTruthy();
expect(tooltip!.classList.contains("whitespace-nowrap")).toBe(false);
expect(tooltip!.classList.contains("break-words")).toBe(true);
app.unmount();
});
it("keeps short single-line tooltips on one line", async () => {
vi.useFakeTimers();
vi.spyOn(HTMLElement.prototype, "scrollWidth", "get").mockReturnValue(120);
vi.spyOn(HTMLElement.prototype, "clientWidth", "get").mockReturnValue(120);
const { app, tooltip } = await mountAndHover({ text: "复制", nowrap: true });
expect(tooltip).toBeTruthy();
expect(tooltip!.classList.contains("whitespace-nowrap")).toBe(true);
expect(tooltip!.classList.contains("break-words")).toBe(false);
app.unmount();
});
});