Skip to content

Commit 159f2b8

Browse files
EtanHeyclaude
andauthored
fix(web): quote comment box stays on screen for long code lines (pingdotgg#22149)
Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent bee9ce8 commit 159f2b8

2 files changed

Lines changed: 59 additions & 1 deletion

File tree

‎apps/web/src/components/chat/AssistantCitationChip.browser.test.tsx‎

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@ import {
66
} from "@t3tools/contracts";
77
import { useState, type ReactNode } from "react";
88
import { describe, expect, it, vi } from "vite-plus/test";
9+
import { userEvent } from "vite-plus/test/browser";
910

1011
import type { AssistantCitationSourceAnchor } from "~/lib/assistantTextSelection";
12+
import "~/index.css";
1113
import { act, render } from "../../../test/render";
1214

1315
const mocks = vi.hoisted(() => ({ observeSource: vi.fn(), dispose: vi.fn() }));
@@ -125,3 +127,51 @@ describe("citation comment source disappearance", () => {
125127
expect(textarea()).toBeNull();
126128
});
127129
});
130+
131+
describe("citation comment popup position", () => {
132+
it("stays on screen when the cited text ends in a code block's hidden overflow", async () => {
133+
using _source = setup();
134+
const message = document.createElement("div");
135+
message.className = "chat-markdown";
136+
message.style.width = "240px";
137+
const pre = document.createElement("pre");
138+
const code = document.createElement("code");
139+
// Highlighted code is split into token spans, so the range's last client rect
140+
// covers only the final token, wholly inside the hidden overflow.
141+
for (let index = 0; index < 40; index += 1) {
142+
const token = document.createElement("span");
143+
token.textContent = `token${index} `;
144+
code.append(token);
145+
}
146+
pre.append(code);
147+
message.append(pre);
148+
document.body.prepend(message);
149+
using _message = { [Symbol.dispose]: () => message.remove() };
150+
const range = document.createRange();
151+
range.selectNodeContents(code);
152+
const rects = range.getClientRects();
153+
expect(rects.item(rects.length - 1)!.left).toBeGreaterThan(window.innerWidth);
154+
const sourceAnchor = {
155+
source: message,
156+
range,
157+
viewport: document.body,
158+
} as AssistantCitationSourceAnchor;
159+
160+
await using _screen = await mount(
161+
sourceAnchor,
162+
vi.fn(() => true),
163+
);
164+
const popup = () => document.querySelector<HTMLElement>('[data-slot="popover-popup"]');
165+
await vi.waitFor(() => expect(popup()?.getBoundingClientRect().width).toBeGreaterThan(0));
166+
// Typing scrolls ancestors to reveal the caret, which shifts the whole app
167+
// sideways when the box is off screen.
168+
await userEvent.keyboard("note");
169+
170+
expect(textarea()?.value).toBe("note");
171+
expect(document.scrollingElement?.scrollLeft).toBe(0);
172+
expect(document.body.scrollLeft).toBe(0);
173+
const rect = popup()!.getBoundingClientRect();
174+
expect(rect.left).toBeGreaterThanOrEqual(0);
175+
expect(rect.right).toBeLessThanOrEqual(window.innerWidth);
176+
});
177+
});

‎apps/web/src/components/chat/AssistantCitationChip.tsx‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,15 @@ export function AssistantCitationChip({
9494
contextElement: activeSourceAnchor.source,
9595
getBoundingClientRect: () => {
9696
const rects = activeSourceAnchor.range.getClientRects();
97-
return rects.item(rects.length - 1) ?? activeSourceAnchor.range.getBoundingClientRect();
97+
const rect =
98+
rects.item(rects.length - 1) ?? activeSourceAnchor.range.getBoundingClientRect();
99+
// Code blocks and tables scroll sideways inside the message, so the cited
100+
// text can end in their hidden overflow. Base UI only shifts the popup while
101+
// it still touches the anchor, so clamp the anchor to the message's width.
102+
const bounds = activeSourceAnchor.source.getBoundingClientRect();
103+
const left = Math.min(Math.max(rect.left, bounds.left), bounds.right);
104+
const right = Math.max(Math.min(rect.right, bounds.right), left);
105+
return new DOMRect(left, rect.top, right - left, rect.height);
98106
},
99107
}
100108
: undefined;

0 commit comments

Comments
 (0)