Skip to content
Merged
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
228 changes: 228 additions & 0 deletions packages/ui/components/CommentPopover.overlayGutter.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,228 @@
/**
* Composer overlay gutter (#1525).
*
* The composer paints its glyphs with a mirror <div> over a transparent-text
* textarea; only the caret comes from the textarea itself. When a classic,
* layout-consuming scrollbar appears (Windows/Linux), the textarea's line box
* narrows and the `overflow: hidden` mirror's does not, so the two wrap at
* different points and the caret is drawn away from the glyph. The failures
* guarded here:
*
* - the mirror not picking up the scrollbar width (the bug itself),
* - the fix writing an inline style on platforms with overlay scrollbars,
* where the measured width is 0 — macOS/iOS/touch must stay byte-identical,
* - a stale gutter left behind when the scrollbar goes away, and
* - a resize that adds or drops the scrollbar without a keystroke (the
* ResizeObserver path; `syncScroll` alone only runs on scroll/value change).
*
* happy-dom has no layout engine, so the textarea's box metrics are stubbed.
*
* DOM-gated (DOM_TESTS=1), same harness as CommentPopover.skillReferences.test.tsx.
*/
import { afterEach, beforeEach, describe, expect, test } from 'bun:test';
import React from 'react';
import { createRoot, type Root } from 'react-dom/client';
import { act } from 'react';

import {
resetSkillCatalogCache,
resetSkillCatalogTransport,
setSkillCatalogTransport,
} from '../utils/skillCatalog';
import type { SkillCatalogEntry } from '../utils/skillReferences';

const hasDom = typeof document !== 'undefined';

const popoverMod = hasDom ? await import('./CommentPopover') : null;
const CommentPopover =
popoverMod?.CommentPopover as typeof import('./CommentPopover')['CommentPopover'];

const catalog: SkillCatalogEntry[] = [
{ name: 'animate', root: 'claude', description: 'Motion design', humanOnly: false },
];

let root: Root | null = null;
let host: HTMLElement | null = null;

async function mountPopover() {
host = document.createElement('div');
document.body.appendChild(host);
await act(async () => {
root = createRoot(host!);
root.render(
<CommentPopover
anchorRect={new DOMRect(100, 100, 60, 20)}
contextText="selected text"
isGlobal={false}
onSubmit={() => {}}
onClose={() => {}}
skillReferences
/>,
);
});
await act(async () => {}); // flush the catalog fetch effect
}

beforeEach(() => {
if (!hasDom) return;
resetSkillCatalogCache();
setSkillCatalogTransport(async () => catalog);
});

afterEach(async () => {
if (root) {
await act(async () => {
root!.unmount();
});
root = null;
}
host?.remove();
host = null;
if (hasDom) document.body.innerHTML = '';
resetSkillCatalogCache();
resetSkillCatalogTransport();
});

function textarea(): HTMLTextAreaElement {
const el = document.querySelector<HTMLTextAreaElement>('[data-comment-popover] textarea');
if (!el) throw new Error('CommentPopover textarea did not render');
return el;
}

function overlay(): HTMLElement {
const el = document.querySelector<HTMLElement>('[data-skill-ref-overlay]');
if (!el) throw new Error('composer overlay did not render');
return el;
}

/** Give the layout-less textarea a content box, with `scrollbar` px eaten. */
function stubBox(el: HTMLTextAreaElement, scrollbar: number, border = 0): void {
const width = 320;
Object.defineProperty(el, 'offsetWidth', { value: width, configurable: true });
Object.defineProperty(el, 'clientWidth', {
value: width - scrollbar - border * 2,
configurable: true,
});
el.style.paddingRight = '4px'; // stands in for the composer's `px-1`
if (border) el.style.border = `${border}px solid black`;
}

async function type(el: HTMLTextAreaElement, value: string): Promise<void> {
const setter = Object.getOwnPropertyDescriptor(Object.getPrototypeOf(el), 'value')?.set;
await act(async () => {
if (setter) setter.call(el, value);
else el.value = value;
el.selectionStart = el.selectionEnd = value.length;
el.dispatchEvent(new Event('input', { bubbles: true }));
});
}

describe('composer overlay gutter', () => {
test.skipIf(!hasDom)('mirrors a classic scrollbar onto the overlay padding', async () => {
await mountPopover();
const el = textarea();
stubBox(el, 12);
await type(el, 'a long enough comment to wrap');
// 4px class padding + the 12px the scrollbar took from the textarea.
expect(overlay().style.paddingRight).toBe('16px');
});

test.skipIf(!hasDom)('counts borders as borders, never as scrollbar', async () => {
await mountPopover();
const el = textarea();
stubBox(el, 12, 1); // offsetWidth - clientWidth === 14 here
await type(el, 'a long enough comment to wrap');
expect(overlay().style.paddingRight).toBe('16px');
});

test.skipIf(!hasDom)(
'writes no inline style at all when the scrollbar takes no layout (macOS/touch)',
async () => {
await mountPopover();
const el = textarea();
stubBox(el, 0);
await type(el, 'a long enough comment to wrap');
await type(el, 'a long enough comment to wrap, and then some more of it');
const ov = overlay();
expect(ov.style.paddingRight).toBe('');
// Stronger than an empty value: the overlay carries no style attribute,
// so the rendered DOM is identical to the pre-fix build.
expect(ov.getAttribute('style')).toBeNull();
},
);

test.skipIf(!hasDom)('drops the gutter again when the scrollbar goes away', async () => {
await mountPopover();
const el = textarea();
stubBox(el, 12);
await type(el, 'a long enough comment to wrap');
expect(overlay().style.paddingRight).toBe('16px');

stubBox(el, 0);
await type(el, 'short');
expect(overlay().style.paddingRight).toBe('');
expect(overlay().getAttribute('style')).toBeNull();
});

test.skipIf(!hasDom)(
'a resize that adds the scrollbar re-syncs without a keystroke',
async () => {
const realRO = globalThis.ResizeObserver;
const callbacks: ResizeObserverCallback[] = [];
let disconnected = 0;
class FakeResizeObserver {
constructor(cb: ResizeObserverCallback) {
callbacks.push(cb);
}
observe() {}
unobserve() {}
disconnect() {
disconnected++;
}
}
globalThis.ResizeObserver = FakeResizeObserver as unknown as typeof ResizeObserver;
try {
await mountPopover();
const el = textarea();
stubBox(el, 0);
await type(el, 'a long enough comment to wrap');
expect(overlay().getAttribute('style')).toBeNull();

// The panel narrows and the textarea grows a scrollbar — no keystroke,
// no scroll event; only the observer can notice.
stubBox(el, 12);
expect(callbacks.length).toBeGreaterThan(0);
await act(async () => {
for (const cb of callbacks) {
cb([] as unknown as ResizeObserverEntry[], null as unknown as ResizeObserver);
}
});
expect(overlay().style.paddingRight).toBe('16px');

await act(async () => {
root!.unmount();
root = null;
});
expect(disconnected).toBeGreaterThan(0);
} finally {
globalThis.ResizeObserver = realRO;
}
},
);

test.skipIf(!hasDom)('runs without a ResizeObserver at all', async () => {
const realRO = globalThis.ResizeObserver;
// Simulate an environment without the API.
delete (globalThis as { ResizeObserver?: unknown }).ResizeObserver;
try {
await mountPopover();
const el = textarea();
stubBox(el, 12);
await type(el, 'a long enough comment to wrap');
// The scroll/value-change path still carries the fix.
expect(overlay().style.paddingRight).toBe('16px');
} finally {
globalThis.ResizeObserver = realRO;
}
});
});
93 changes: 91 additions & 2 deletions packages/ui/components/CommentPopover.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -849,6 +849,77 @@ export const CommentPopover: React.FC<CommentPopoverProps> = ({
* and box metrics MUST stay identical or the overlay drifts out of alignment. */
const COMPOSER_TEXT_CLASSES = 'w-full bg-transparent text-sm px-1 py-0.5';

/** Per-composer memo for the overlay gutter sync below. */
interface OverlayGutterState {
/** The overlay the applied value belongs to (a remount invalidates it). */
overlay: HTMLElement | null;
/** Last `offsetWidth - clientWidth` seen on the textarea. */
raw: number;
/** Extra right padding currently written inline on the overlay, in px. */
applied: number;
}

const cssPx = (value: string): number => {
const n = parseFloat(value);
return Number.isFinite(n) ? n : 0;
};

/**
* Keep the mirror's text column exactly as wide as the textarea's (#1525).
*
* A layout-consuming vertical scrollbar (Windows/Linux classic scrollbars; the
* app's `scrollbar-width: thin` makes them ~12px) narrows the textarea's
* content box once the composer hits its max height — but the overlay is
* `overflow: hidden` and keeps its full width, so from the first line that
* wraps differently the painted glyphs drift away from the native caret, which
* is the one thing the textarea itself still draws. `scrollbar-gutter: stable`
* cannot fix this: it is ignored on an `overflow: hidden` box. Mirroring the
* measured scrollbar width onto the overlay's right padding makes both boxes
* wrap identically again (and stops the overlay painting under the scrollbar).
*
* Overlay scrollbars (macOS, iOS, any touch surface) consume no layout, so the
* measured width is 0 — and then this writes NOTHING at all, leaving the
* class-driven padding untouched exactly as before the fix.
*/
function syncOverlayGutter(
el: HTMLTextAreaElement,
overlay: HTMLElement,
state: OverlayGutterState,
raw: number,
): void {
// Nothing measurable changed since the last sync — the common case.
if (overlay === state.overlay && raw === state.raw) return;
state.overlay = overlay;
state.raw = raw;

const clear = () => {
if (state.applied === 0) return; // never wrote anything; leave the DOM alone
overlay.style.removeProperty('padding-right');
state.applied = 0;
};

const style = getComputedStyle(el);
// `offsetWidth - clientWidth` is scrollbar + left/right borders. The borders
// are shared with the overlay (both boxes are border-box), so only the
// scrollbar part is the extra gutter the mirror is missing.
const borders = cssPx(style.borderLeftWidth) + cssPx(style.borderRightWidth);
const scrollbar = Math.max(0, raw - borders);
if (scrollbar === 0) {
clear();
return;
}
// Base padding is read from the textarea (never the overlay, whose computed
// value may already include a gutter we wrote), so the two can never drift
// apart if COMPOSER_TEXT_CLASSES changes.
const base = parseFloat(style.paddingRight);
if (!Number.isFinite(base)) {
clear(); // unmeasurable: fall back to the pre-fix behavior
return;
}
overlay.style.paddingRight = `${base + scrollbar}px`;
state.applied = scrollbar;
}

interface ComposerTextareaProps {
value: string;
onChange: (e: React.ChangeEvent<HTMLTextAreaElement>) => void;
Expand Down Expand Up @@ -897,12 +968,17 @@ const ComposerTextarea: React.FC<ComposerTextareaProps> = ({
}) => {
const overlayRef = useRef<HTMLDivElement>(null);
const [composing, setComposing] = useState(false);
const gutterRef = useRef<OverlayGutterState>({ overlay: null, raw: 0, applied: 0 });

const syncScroll = useCallback((el: HTMLTextAreaElement) => {
const overlay = overlayRef.current;
if (!overlay) return;
overlay.scrollTop = el.scrollTop;
overlay.scrollLeft = el.scrollLeft;
// Reads before writes so a sync costs at most one forced layout.
const { scrollTop, scrollLeft } = el;
const raw = el.offsetWidth - el.clientWidth;
overlay.scrollTop = scrollTop;
overlay.scrollLeft = scrollLeft;
syncOverlayGutter(el, overlay, gutterRef.current, raw);
}, []);

const innerRef = useRef<HTMLTextAreaElement | null>(null);
Expand All @@ -920,6 +996,19 @@ const ComposerTextarea: React.FC<ComposerTextareaProps> = ({
if (innerRef.current) syncScroll(innerRef.current);
}, [value, syncScroll]);

// `syncScroll` otherwise only runs on scroll and on value change, so a
// window/panel resize that grows or drops the textarea's scrollbar without a
// keystroke would leave the gutter stale until the next one.
useEffect(() => {
const el = innerRef.current;
if (!el || typeof ResizeObserver === 'undefined') return;
const observer = new ResizeObserver(() => {
if (innerRef.current) syncScroll(innerRef.current);
});
observer.observe(el);
return () => observer.disconnect();
}, [skillReferences, syncScroll]);

if (!skillReferences) {
return (
<textarea
Expand Down