perf(GradientBlinds): decouple pointer events from render loop

- Move scroll position and bounding client rect calculations from `pointermove` handler to `requestAnimationFrame` loop.
- Use `pointerPosRef` to store raw event coordinates, reducing overhead in high-frequency event handlers.
- Ensure spotlight effect correctly accounts for scroll position updates during animation frames.
- Add regression test to verify `scrollX` is not accessed during pointer events.

Co-authored-by: ragusa-it <196988693+ragusa-it@users.noreply.github.com>
This commit is contained in:
google-labs-jules[bot]
2026-01-29 01:40:38 +00:00
parent 6b8f54072e
commit bac867f228
2 changed files with 58 additions and 28 deletions

View File

@@ -176,17 +176,11 @@ describe('GradientBlinds', () => {
unmount();
expect(removeEventListenerSpy).toHaveBeenCalledWith('pointermove', expect.any(Function));
});
it('minimizes getBoundingClientRect calls during pointer move', () => {
const { unmount } = render(<GradientBlinds />);
// Spy on getBoundingClientRect
// Note: In jsdom, canvas is an HTMLCanvasElement which inherits from HTMLElement
const spy = vi.spyOn(HTMLElement.prototype, 'getBoundingClientRect');
// Trigger pointer move to clear any initial calls or verify baseline
// The initial render calls resize(), which calls getBoundingClientRect on container
// Clear spy history from initial render
spy.mockClear();
act(() => {
@@ -198,9 +192,37 @@ describe('GradientBlinds', () => {
window.dispatchEvent(event);
});
// EXPECTATION: It should NOT be called because the listener shouldn't be attached (not visible)
expect(spy).not.toHaveBeenCalled();
unmount();
});
it('avoids expensive DOM reads (scrollX/Y) in pointermove handler when visible', () => {
const { unmount } = render(<GradientBlinds />);
// Spy on scrollX/scrollY getters
// Note: In jsdom, these are properties on window.
const scrollSpy = vi.spyOn(window, 'scrollX', 'get');
// Make visible to attach listener
act(() => {
if (ioCallback) {
ioCallback([{ isIntersecting: true } as IntersectionObserverEntry]);
}
});
scrollSpy.mockClear();
// Trigger pointer move
act(() => {
window.dispatchEvent(new PointerEvent('pointermove', { clientX: 100, clientY: 100 }));
});
// With the optimization (moving to RAF loop), this should be 0.
// Without optimization, this will be > 0.
// Since we are mocking RAF and not running the loop, if it's in the loop, it won't be called.
expect(scrollSpy).not.toHaveBeenCalled();
unmount();
});
});