Files
PaperClipAI/ui/src/hooks/useMobileVisualViewport.test.tsx
T
commitperclip[bot] 47ce5459c9 fix: Keep tablet task composers above the keyboard and release sizing during pinch zoom (#19725)
> [!NOTE]
> Opened by Prospector's automated issue-fix pipeline. A maintainer
reviews and decides; nothing here merges on its own.

> [!WARNING]
> This change crosses a trust boundary. See Risks.

## Thinking Path

- Issue #17866 reports a defect in paperclipai/paperclip.
- Root cause, as the fixing agent read it: Viewport observation and
conversation sizing were tied to the mobile navigation breakpoint. The
earlier attempt also retained keyboard state during zoom and excluded
classic task threads from keyboard sizing.
- This pull request: Keep tablet task composers above the keyboard and
release sizing during pinch zoom

## What Changed

- `ui/src/components/Layout.tsx`: Retain viewport observation
independent of navigation width.
- `ui/src/components/Layout.production.tsx`: Apply the same viewport
observation to the production layout.
- `ui/src/hooks/useMobileVisualViewport.ts`: Clear keyboard state during
pinch zoom and handle Safari document panning for classic conversation
shells.
- `ui/src/pages/IssueDetail.tsx`: Mark desktop-width classic chat pages
for keyboard sizing while preserving phone layouts and other tabs.
- `ui/src/index.css`: Constrain classic conversation shells during
keyboard display, retain main-content scrolling, and keep their sticky
composer inside the visible area.
- `ui/src/hooks/useMobileVisualViewport.test.tsx`: Reproduce stale
keyboard state after zoom and verify recovery.
- `ui/src/components/Layout.viewport.test.tsx`: Cover both layouts, both
conversation shells, zoom recovery, and document pan correction.
- `ui/src/pages/IssueDetail.test.tsx`: Verify classic sizing eligibility
at desktop and phone widths. Regression tests, existing thread tests,
and typecheck passed.

## Verification

- Reproducing test(s), added in this PR:
`ui/src/hooks/useMobileVisualViewport.test.tsx`,
`ui/src/pages/IssueDetail.test.tsx`,
`ui/src/components/Layout.viewport.test.tsx`,
`ui/src/components/Layout.test.tsx`
- They fail on the base commit and pass with the fix, each twice in a
fresh sandbox.
- 3 independent attempts wrote 3 reproductions; this fix passes every
one of them.
- The repository's compile command passes with the fix applied.
- 10 existing test file(s) near the change passed.
- The full test suite passes except for the 15 file(s) that already fail
on the base commit.
- Revised in 1 round(s) of automated code review.

## Risks

- Written by an automated pipeline, not a person: review it as you would
an outside contribution.
- Risk tier of the touched paths: 3.
- The scope reviewer found it also changes, beyond the report: Enabled
hook callers entering zoom after keyboard detection: both Layout
variants, hook test hosts, and task-chat-mobile-follow Storybook
- The scope reviewer found it also changes, beyond the report: Missing
visualViewport or shell; disabled hook; zero, negative, non-finite, or
missing viewport height; missing or malformed scale
- It crosses a trust boundary. Check each crossing against what the
issue asked for, and against who asked:
- validation (ui/src/components/Layout.tsx:155): Passing true removes
the mobile-width eligibility check for viewport keyboard handling.
- validation (ui/src/components/Layout.production.tsx:182): Passing true
removes the mobile-width eligibility check in the production layout.
- validation (ui/src/hooks/useMobileVisualViewport.ts:12, not asked for
by the report): The scroll-reset guard now accepts classic task shells
as well as task-chat shells.

## Model Used

- codex:gpt-6.1-sol, opus, sonnet (Anthropic Claude, via Claude Code;
OpenAI, via the Codex CLI), writing and checking the change in an
isolated sandbox.
- opus (Anthropic Claude, via Claude Code), reviewing it independently.

## Linked Issues

Fixes #17866

A search of paperclipai/paperclip's open and closed pull requests found
no other pull request that names #17866.

## Checklist

- [x] I have included a thinking path that traces from project context
to this change
- [x] I have specified the model used (with version and capability
details)
- [ ] I have checked ROADMAP.md and confirmed this PR does not duplicate
planned core work
- [x] I have searched GitHub for duplicate or related PRs and linked
them above
- [x] I have either (a) linked existing issues with 'Fixes: #' / 'Closes
#' / 'Refs #' OR (b) described the issue in-PR following the relevant
issue template
- [x] I have not referenced internal/instance-local Paperclip issues or
links (only public GitHub '#NNN' / 'github.com/paperclipai/paperclip'
URLs)
- [ ] My branch name describes the change (e.g. 'docs/...', 'fix/...')
and contains no internal Paperclip ticket id or instance-derived details
- [x] I have run tests locally and they pass
- [x] I have added or updated tests where applicable
- [ ] I have updated relevant documentation to reflect my changes
- [x] I have considered and documented any risks above
- [ ] All Paperclip CI gates are green
- [ ] Greptile is 5/5 with no open P2s, recommendations, or follow-ups
- [ ] I will address all Greptile and reviewer comments before
requesting merge

<!-- prospector:issue-fix v1 issue=17866 base=f8cb4957dcd9
report=8607f5fafe77c2be -->

Co-authored-by: commitperclip[bot] <284812584+commitperclip[bot]@users.noreply.github.com>
2026-11-13 12:57:20 -06:00

141 lines
5.7 KiB
TypeScript

// @vitest-environment jsdom
import { act } from "react";
import { createRoot, type Root } from "react-dom/client";
import { afterEach, beforeEach, expect, it, vi } from "vitest";
import { useMobileVisualViewport } from "./useMobileVisualViewport";
function Host({ mobile = true, conversation = true }: { mobile?: boolean; conversation?: boolean }) {
const ref = useMobileVisualViewport(mobile);
return <div ref={ref}><div data-task-chat-shell={conversation ? "" : undefined}>Chat shell</div></div>;
}
let root: Root;
let container: HTMLDivElement;
let viewport: EventTarget & { height: number; offsetTop: number; scale: number };
beforeEach(() => {
vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
vi.stubGlobal("innerHeight", 695);
Object.defineProperty(document.documentElement, "clientHeight", { configurable: true, value: 695 });
viewport = Object.assign(new EventTarget(), { height: 695, offsetTop: 0, scale: 1 });
vi.stubGlobal("visualViewport", viewport);
container = document.createElement("div");
document.body.append(container);
root = createRoot(container);
});
afterEach(() => {
act(() => root.unmount());
container.remove();
Reflect.deleteProperty(document.documentElement, "clientHeight");
document.documentElement.scrollTop = 0;
vi.unstubAllGlobals();
});
it("resizes the shell from the visible height when iOS innerHeight shrinks too", () => {
act(() => root.render(<Host />));
const shell = container.firstElementChild as HTMLElement;
act(() => {
vi.stubGlobal("innerHeight", 358);
viewport.height = 358;
viewport.offsetTop = 337;
viewport.dispatchEvent(new Event("resize"));
});
expect(shell.style.getPropertyValue("--mobile-chat-viewport-height")).toBe("358px");
expect(shell.dataset.keyboardOpen).toBe("true");
// Panning must not translate or resize the shell a second time.
act(() => { viewport.offsetTop = 185; viewport.dispatchEvent(new Event("scroll")); });
expect(shell.style.top).toBe("");
expect(shell.style.transform).toBe("");
expect(shell.style.getPropertyValue("--mobile-chat-viewport-height")).toBe("358px");
act(() => { viewport.height = 695; viewport.offsetTop = 0; viewport.dispatchEvent(new Event("resize")); });
expect(shell.style.getPropertyValue("--mobile-chat-viewport-height")).toBe("695px");
expect(shell.dataset.keyboardOpen).toBeUndefined();
});
it("preserves pinch zoom and leaves desktop sizing to CSS", () => {
act(() => root.render(<Host />));
const shell = container.firstElementChild as HTMLElement;
act(() => { viewport.scale = 2; viewport.height = 350; viewport.dispatchEvent(new Event("resize")); });
expect(shell.style.getPropertyValue("--mobile-chat-viewport-height")).toBe("695px");
act(() => root.render(<Host mobile={false} />));
expect(shell.style.getPropertyValue("--mobile-chat-viewport-height")).toBe("");
expect(shell.dataset.keyboardOpen).toBeUndefined();
});
it("does not invalidate shell styles for typing, scrolling, or unchanged viewport events", () => {
act(() => root.render(<Host />));
const shell = container.firstElementChild as HTMLElement;
const observer = new MutationObserver(() => {});
observer.observe(shell, { attributes: true });
act(() => {
for (let i = 0; i < 30; i++) {
shell.dispatchEvent(new Event("input", { bubbles: true }));
viewport.dispatchEvent(new Event("scroll"));
viewport.dispatchEvent(new Event("resize"));
}
});
expect(observer.takeRecords()).toHaveLength(0);
observer.disconnect();
});
it("rejects Safari's stale document focus scroll after the keyboard has resized the conversation", () => {
act(() => root.render(<Host />));
act(() => {
viewport.height = 358;
// Some openings pan before the resize event; others pan one frame later.
document.documentElement.scrollTop = 337;
viewport.dispatchEvent(new Event("resize"));
});
expect(document.documentElement.scrollTop).toBe(0);
document.documentElement.scrollTop = 337;
document.dispatchEvent(new Event("scroll"));
expect(document.documentElement.scrollTop).toBe(0);
});
it("leaves ordinary pages and inner message scrolling alone", () => {
viewport.height = 358;
act(() => root.render(<Host conversation={false} />));
document.documentElement.scrollTop = 337;
document.dispatchEvent(new Event("scroll"));
expect(document.documentElement.scrollTop).toBe(337);
act(() => root.render(<Host />));
const messages = container.querySelector('[data-task-chat-shell]') as HTMLElement;
messages.scrollTop = 250;
messages.dispatchEvent(new Event("scroll"));
expect(messages.scrollTop).toBe(250);
});
it("does not suppress zoom panning when the keyboard was already open", () => {
viewport.height = 358;
act(() => root.render(<Host />));
viewport.scale = 2;
viewport.height = 179;
viewport.dispatchEvent(new Event("resize"));
document.documentElement.scrollTop = 120;
document.dispatchEvent(new Event("scroll"));
expect(document.documentElement.scrollTop).toBe(120);
});
it("releases keyboard sizing when zoom starts and the keyboard closes while zoomed", () => {
viewport.height = 358;
act(() => root.render(<Host />));
const shell = container.firstElementChild as HTMLElement;
expect(shell.dataset.keyboardOpen).toBe("true");
act(() => {
viewport.scale = 2;
viewport.height = 179;
viewport.dispatchEvent(new Event("resize"));
});
expect(shell.dataset.keyboardOpen).toBeUndefined();
act(() => {
viewport.height = 347.5;
viewport.dispatchEvent(new Event("resize"));
});
expect(shell.dataset.keyboardOpen).toBeUndefined();
act(() => {
viewport.scale = 1;
viewport.height = 695;
viewport.dispatchEvent(new Event("resize"));
});
expect(shell.style.getPropertyValue("--mobile-chat-viewport-height")).toBe("695px");
});