Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -152,3 +152,25 @@ describe("citation comment source disappearance", () => {
expect(textarea()).toBeNull();
});
});

describe("citation comment popup placement", () => {
it("opens inside the window when the cited line ends in a code block's horizontal overflow", async () => {
const pre = document.createElement("pre");
pre.style.cssText = "width: 120px; overflow-x: auto; white-space: pre; margin: 0";
pre.textContent = `${"x".repeat(400)}\ny`;
document.body.append(pre);
const range = document.createRange();
range.setStart(pre.firstChild!, 0);
range.setEnd(pre.firstChild!, 401);
sourceAnchor = { source: pre, range, viewport: document.body } as AssistantCitationSourceAnchor;
// The anchor is the selection's last rect: a narrow line-end rect past the window edge.
const rects = range.getClientRects();
expect(rects.item(rects.length - 1)!.left).toBeGreaterThan(window.innerWidth);

await mount();

const popup = document.querySelector('[data-slot="popover-popup"]')!.getBoundingClientRect();
expect(popup.left).toBeGreaterThanOrEqual(0);
expect(popup.right).toBeLessThanOrEqual(window.innerWidth);
});
});
4 changes: 4 additions & 0 deletions apps/web/src/components/chat/AssistantCitationChip.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,10 @@ export function AssistantCitationChip({
{...composerFloatingLayerProps}
side={activeSourceAnchor ? "bottom" : "top"}
align="end"
// The anchor is the quote's last line rect, which can lie past the window edge
// inside a code block's horizontal overflow. Without sticky, Base UI keeps the
// popup attached to that rect instead of shifting it into view.
sticky
anchor={popupAnchor}
initialFocus={() => {
commentInputRef.current?.focus({ preventScroll: true });
Expand Down
3 changes: 3 additions & 0 deletions apps/web/src/components/ui/popover.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ function PopoverPopup({
sideOffset = 4,
alignOffset = 0,
collisionAvoidance,
sticky,
tooltipStyle = false,
keepMounted = false,
anchor,
Expand All @@ -53,6 +54,7 @@ function PopoverPopup({
sideOffset?: PopoverPrimitive.Positioner.Props["sideOffset"];
alignOffset?: PopoverPrimitive.Positioner.Props["alignOffset"];
collisionAvoidance?: PopoverPrimitive.Positioner.Props["collisionAvoidance"];
sticky?: PopoverPrimitive.Positioner.Props["sticky"];
tooltipStyle?: boolean;
keepMounted?: PopoverPrimitive.Portal.Props["keepMounted"];
anchor?: PopoverPrimitive.Positioner.Props["anchor"];
Expand All @@ -78,6 +80,7 @@ function PopoverPopup({
alignOffset={alignOffset}
anchor={anchor}
collisionAvoidance={collisionAvoidance}
sticky={sticky}
collisionPadding={variant === "panel" ? 0 : undefined}
className={cn(
"h-(--positioner-height) w-(--positioner-width) max-w-(--available-width) transition-transform data-instant:transition-none",
Expand Down
Loading