Skip to content
TypeScript

fix(web): quote comment box stays on screen for long code lines (#22149)

Etan Heyman159f2b8cc33a
diff --git a/apps/web/src/components/chat/AssistantCitationChip.browser.test.tsx b/apps/web/src/components/chat/AssistantCitationChip.browser.test.tsx
index 8dc91214b50..deee8d7afa6 100644
--- a/apps/web/src/components/chat/AssistantCitationChip.browser.test.tsx
+++ b/apps/web/src/components/chat/AssistantCitationChip.browser.test.tsx
@@ -6,8 +6,10 @@ import {
} from "@t3tools/contracts";
import { useState, type ReactNode } from "react";
import { describe, expect, it, vi } from "vite-plus/test";
+import { userEvent } from "vite-plus/test/browser";
import type { AssistantCitationSourceAnchor } from "~/lib/assistantTextSelection";
+import "~/index.css";
import { act, render } from "../../../test/render";
const mocks = vi.hoisted(() => ({ observeSource: vi.fn(), dispose: vi.fn() }));
@@ -125,3 +127,51 @@ describe("citation comment source disappearance", () => {
expect(textarea()).toBeNull();
});
});
+
+describe("citation comment popup position", () => {
+ it("stays on screen when the cited text ends in a code block's hidden overflow", async () => {
+ using _source = setup();
+ const message = document.createElement("div");
+ message.className = "chat-markdown";
+ message.style.width = "240px";
+ const pre = document.createElement("pre");
+ const code = document.createElement("code");
+ // Highlighted code is split into token spans, so the range's last client rect
+ // covers only the final token, wholly inside the hidden overflow.
+ for (let index = 0; index < 40; index += 1) {
+ const token = document.createElement("span");
+ token.textContent = `token${index} `;
+ code.append(token);
+ }
+ pre.append(code);
+ message.append(pre);
+ document.body.prepend(message);
+ using _message = { [Symbol.dispose]: () => message.remove() };
+ const range = document.createRange();
+ range.selectNodeContents(code);
+ const rects = range.getClientRects();
+ expect(rects.item(rects.length - 1)!.left).toBeGreaterThan(window.innerWidth);
+ const sourceAnchor = {
+ source: message,
+ range,
+ viewport: document.body,
+ } as AssistantCitationSourceAnchor;
+
+ await using _screen = await mount(
+ sourceAnchor,
+ vi.fn(() => true),
+ );
+ const popup = () => document.querySelector<HTMLElement>('[data-slot="popover-popup"]');
+ await vi.waitFor(() => expect(popup()?.getBoundingClientRect().width).toBeGreaterThan(0));
+ // Typing scrolls ancestors to reveal the caret, which shifts the whole app
+ // sideways when the box is off screen.
+ await userEvent.keyboard("note");
+
+ expect(textarea()?.value).toBe("note");
+ expect(document.scrollingElement?.scrollLeft).toBe(0);
+ expect(document.body.scrollLeft).toBe(0);
+ const rect = popup()!.getBoundingClientRect();
+ expect(rect.left).toBeGreaterThanOrEqual(0);
+ expect(rect.right).toBeLessThanOrEqual(window.innerWidth);
+ });
+});
diff --git a/apps/web/src/components/chat/AssistantCitationChip.tsx b/apps/web/src/components/chat/AssistantCitationChip.tsx
index 741880d4116..dd21f7c4668 100644
--- a/apps/web/src/components/chat/AssistantCitationChip.tsx
+++ b/apps/web/src/components/chat/AssistantCitationChip.tsx
@@ -94,7 +94,15 @@ export function AssistantCitationChip({
contextElement: activeSourceAnchor.source,
getBoundingClientRect: () => {
const rects = activeSourceAnchor.range.getClientRects();
- return rects.item(rects.length - 1) ?? activeSourceAnchor.range.getBoundingClientRect();
+ const rect =
+ rects.item(rects.length - 1) ?? activeSourceAnchor.range.getBoundingClientRect();
+ // Code blocks and tables scroll sideways inside the message, so the cited
+ // text can end in their hidden overflow. Base UI only shifts the popup while
+ // it still touches the anchor, so clamp the anchor to the message's width.
+ const bounds = activeSourceAnchor.source.getBoundingClientRect();
+ const left = Math.min(Math.max(rect.left, bounds.left), bounds.right);
+ const right = Math.max(Math.min(rect.right, bounds.right), left);
+ return new DOMRect(left, rect.top, right - left, rect.height);
},
}
: undefined;