diff --git a/apps/desktop/src/components/ui/LightTooltip.vue b/apps/desktop/src/components/ui/LightTooltip.vue index 6cf14f12d1..a8982a889e 100644 --- a/apps/desktop/src/components/ui/LightTooltip.vue +++ b/apps/desktop/src/components/ui/LightTooltip.vue @@ -34,6 +34,10 @@ const show = ref(false); const x = ref(0); const y = ref(0); const arrowOffset = ref(); +// `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 | null = null; let closeTimer: ReturnType | null = null; 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); } +/** + * 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() { clearTimer(); clearCloseTimer(); show.value = false; + wrapNowrapContent.value = false; openSource = null; removeGlobalListeners(); } @@ -238,8 +257,15 @@ function open(source: "hover" | "focus") { if (source === "focus" ? !hasFocusVisible() : !isPointerActive()) return; updatePosition(); openSource = source; + wrapNowrapContent.value = false; show.value = true; - void nextTick(fitPositionToViewport); + void nextTick(() => { + if (switchToWrappingWhenOverflowing()) { + void nextTick(fitPositionToViewport); + return; + } + fitPositionToViewport(); + }); addGlobalListeners(); } @@ -281,7 +307,7 @@ watch( v-if="show" ref="tooltipRef" 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` }" role="tooltip" @mouseenter="clearCloseTimer" diff --git a/apps/desktop/src/components/ui/__tests__/LightTooltip.spec.ts b/apps/desktop/src/components/ui/__tests__/LightTooltip.spec.ts index 04589ff067..5f6abadbb2 100644 --- a/apps/desktop/src/components/ui/__tests__/LightTooltip.spec.ts +++ b/apps/desktop/src/components/ui/__tests__/LightTooltip.spec.ts @@ -9,6 +9,34 @@ afterEach(() => { vi.useRealTimers(); }); +async function mountAndHover(props: Record) { + 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("#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("[role=tooltip]") }; +} + describe("LightTooltip scrolling", () => { it("stays open for content scrolling and closes for outside scrolling", async () => { vi.useFakeTimers(); @@ -52,34 +80,6 @@ describe("LightTooltip scrolling", () => { }); describe("LightTooltip contentClass", () => { - async function mountAndHover(props: Record) { - 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("#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("[role=tooltip]") }; - } - it("merges contentClass into the teleported tooltip and drops the default max-w-xs", async () => { vi.useFakeTimers(); const { app, tooltip } = await mountAndHover({ text: "Conflict hint", contentClass: "max-w-[320px]" }); @@ -103,3 +103,36 @@ describe("LightTooltip contentClass", () => { 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(); + }); +});