mirror of
https://github.com/paperclipai/paperclip.git
synced 2026-12-04 04:29:01 +01:00
fix(ui): never strand mobile task chrome hidden at the latest message
Measure the thread against its expanded viewport in both states, so hiding the composer and nav never clamps the reader to the latest message and reaching the real bottom always brings them back. Re-show the chrome when the keyboard opens. Document the task-conversation padding exception in DESIGN.md. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
012311e8ec
commit
2e83e3466b
4 files changed
+141
-42
No files matched your search
@@ -75,6 +75,14 @@ Mobile task conversations size their shell to the visible viewport, with one
|
||||
message scroller and a normal flex footer for the composer. Navigation stays at
|
||||
the layout viewport bottom, behind the keyboard.
|
||||
|
||||
A task conversation is the one exception to stable page padding. Its message
|
||||
scroller drives the navigation, measured from the latest message. Reading back
|
||||
through the thread slides the composer and the navigation away together, and
|
||||
the thread takes the freed height. The chrome hides only after the reader is
|
||||
farther back than that freed height, so hiding never snaps the thread to the
|
||||
latest message. Moving back toward the latest message, or reaching it, brings
|
||||
both back. An open keyboard always keeps the composer on screen.
|
||||
|
||||
Cold task opens reveal the instruction and recent conversation independently of
|
||||
recovery, related metadata, and historical run activity. Saved document cards,
|
||||
the saved plan, and the bounded latest turn arrive before first reveal so they
|
||||
|
||||
@@ -10,11 +10,20 @@ function Host({ enabled = true }: { enabled?: boolean }) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// The composer dock (50px) plus the nav offset padding (100px): how much the
|
||||
// thread viewport grows at its bottom edge when the chrome tucks away.
|
||||
const DOCK_HEIGHT = 50;
|
||||
const NAV_PADDING = 100;
|
||||
const VISIBLE_HEIGHT = 500;
|
||||
const HIDDEN_HEIGHT = VISIBLE_HEIGHT + DOCK_HEIGHT + NAV_PADDING;
|
||||
|
||||
let root: Root;
|
||||
let container: HTMLDivElement;
|
||||
let frames: FrameRequestCallback[] = [];
|
||||
let main: HTMLElement;
|
||||
let thread: HTMLDivElement;
|
||||
let geometry: { scrollHeight: number; clientHeight: number; bottom: number };
|
||||
let geometry: { scrollHeight: number; clientHeight: number };
|
||||
let rawScrollTop = 0;
|
||||
|
||||
function flushFrames() {
|
||||
const pending = frames;
|
||||
@@ -22,17 +31,30 @@ function flushFrames() {
|
||||
act(() => pending.forEach((callback) => callback(0)));
|
||||
}
|
||||
|
||||
/** Scroll the thread to `fromBottom` CSS pixels above its latest message. */
|
||||
function scrollThread(fromBottom: number) {
|
||||
thread.scrollTop = geometry.scrollHeight - geometry.clientHeight - fromBottom;
|
||||
const maxScrollTop = () => Math.max(0, geometry.scrollHeight - geometry.clientHeight);
|
||||
const fromBottom = () => maxScrollTop() - thread.scrollTop;
|
||||
|
||||
function emitScroll() {
|
||||
thread.dispatchEvent(new Event("scroll"));
|
||||
flushFrames();
|
||||
}
|
||||
|
||||
/** Grow or shrink the thread viewport at its bottom edge, as the dock does. */
|
||||
function resizeThread(clientHeight: number) {
|
||||
geometry.bottom += clientHeight - geometry.clientHeight;
|
||||
geometry.clientHeight = clientHeight;
|
||||
/** Scroll the thread to `distance` CSS pixels above its latest message. */
|
||||
function scrollThread(distance: number) {
|
||||
thread.scrollTop = maxScrollTop() - distance;
|
||||
emitScroll();
|
||||
}
|
||||
|
||||
/**
|
||||
* Apply the layout for the current chrome state, as the CSS does: hiding grows
|
||||
* the thread viewport at its bottom edge, and the browser clamps `scrollTop`
|
||||
* to the new maximum.
|
||||
*/
|
||||
function settleChrome() {
|
||||
geometry.clientHeight = visible ? VISIBLE_HEIGHT : HIDDEN_HEIGHT;
|
||||
main.style.paddingBottom = visible ? `${NAV_PADDING}px` : "0px";
|
||||
thread.scrollTop = rawScrollTop;
|
||||
emitScroll();
|
||||
}
|
||||
|
||||
beforeEach(() => {
|
||||
@@ -44,15 +66,29 @@ beforeEach(() => {
|
||||
tokens.setProperty("--mobile-nav-scroll-hide-distance", "32px");
|
||||
tokens.setProperty("--mobile-nav-scroll-show-distance", "16px");
|
||||
|
||||
geometry = { scrollHeight: 4000, clientHeight: 500, bottom: 560 };
|
||||
geometry = { scrollHeight: 4000, clientHeight: VISIBLE_HEIGHT };
|
||||
rawScrollTop = 0;
|
||||
main = document.createElement("main");
|
||||
main.id = "main-content";
|
||||
main.style.paddingBottom = `${NAV_PADDING}px`;
|
||||
const shell = document.createElement("div");
|
||||
shell.dataset.testid = "task-chat-thread";
|
||||
thread = document.createElement("div");
|
||||
thread.setAttribute("data-mobile-chrome-scroll", "bottom");
|
||||
Object.defineProperty(thread, "scrollHeight", { configurable: true, get: () => geometry.scrollHeight });
|
||||
let scrollTop = 0;
|
||||
Object.defineProperty(thread, "scrollTop", { configurable: true, get: () => scrollTop, set: (value: number) => { scrollTop = value; } });
|
||||
// Browsers clamp scrollTop to the live scroll range.
|
||||
Object.defineProperty(thread, "scrollTop", {
|
||||
configurable: true,
|
||||
get: () => Math.max(0, Math.min(rawScrollTop, maxScrollTop())),
|
||||
set: (value: number) => { rawScrollTop = Math.max(0, Math.min(value, maxScrollTop())); },
|
||||
});
|
||||
Object.defineProperty(thread, "clientHeight", { configurable: true, get: () => geometry.clientHeight });
|
||||
thread.getBoundingClientRect = () => ({ bottom: geometry.bottom, top: geometry.bottom - geometry.clientHeight } as DOMRect);
|
||||
document.body.append(thread);
|
||||
const dock = document.createElement("div");
|
||||
dock.dataset.testid = "task-chat-composer-dock";
|
||||
dock.getBoundingClientRect = () => ({ height: DOCK_HEIGHT } as DOMRect);
|
||||
shell.append(thread, dock);
|
||||
main.append(shell);
|
||||
document.body.append(main);
|
||||
|
||||
container = document.createElement("div");
|
||||
document.body.append(container);
|
||||
@@ -62,7 +98,7 @@ beforeEach(() => {
|
||||
afterEach(() => {
|
||||
act(() => root.unmount());
|
||||
container.remove();
|
||||
thread.remove();
|
||||
main.remove();
|
||||
document.documentElement.removeAttribute("style");
|
||||
document.body.querySelectorAll(".paperclip-app-shell").forEach((node) => node.remove());
|
||||
frames = [];
|
||||
@@ -76,12 +112,17 @@ describe("useMobileNavVisibility with a bottom-anchored task thread", () => {
|
||||
scrollThread(0);
|
||||
expect(visible).toBe(true);
|
||||
|
||||
// Small drags near the latest message keep the composer reachable.
|
||||
scrollThread(20);
|
||||
scrollThread(50);
|
||||
// Reading within the space the chrome would free never hides it, so the
|
||||
// thread can never be clamped to the latest message by hiding.
|
||||
scrollThread(100);
|
||||
scrollThread(170);
|
||||
scrollThread(200);
|
||||
expect(visible).toBe(true);
|
||||
scrollThread(60);
|
||||
scrollThread(210);
|
||||
expect(visible).toBe(false);
|
||||
settleChrome();
|
||||
expect(visible).toBe(false);
|
||||
expect(fromBottom()).toBe(60);
|
||||
|
||||
// Keep reading older messages: the chrome stays out of the way.
|
||||
scrollThread(600);
|
||||
@@ -92,6 +133,10 @@ describe("useMobileNavVisibility with a bottom-anchored task thread", () => {
|
||||
expect(visible).toBe(false);
|
||||
scrollThread(580);
|
||||
expect(visible).toBe(true);
|
||||
|
||||
// The viewport shrinking back is not a swipe away from the latest message.
|
||||
settleChrome();
|
||||
expect(visible).toBe(true);
|
||||
});
|
||||
|
||||
it("returns the chrome when the reader reaches the latest message", () => {
|
||||
@@ -99,26 +144,32 @@ describe("useMobileNavVisibility with a bottom-anchored task thread", () => {
|
||||
scrollThread(0);
|
||||
scrollThread(400);
|
||||
expect(visible).toBe(false);
|
||||
settleChrome();
|
||||
scrollThread(300);
|
||||
scrollThread(320);
|
||||
scrollThread(10);
|
||||
scrollThread(0);
|
||||
expect(visible).toBe(true);
|
||||
});
|
||||
|
||||
it("does not bounce back when tucking the composer away grows the thread viewport", () => {
|
||||
it("never strands the chrome hidden at the latest message after a short drag", () => {
|
||||
act(() => root.render(<Host />));
|
||||
scrollThread(0);
|
||||
scrollThread(80);
|
||||
expect(visible).toBe(false);
|
||||
// A short drag up stays inside the space the chrome would free.
|
||||
scrollThread(40);
|
||||
scrollThread(120);
|
||||
expect(visible).toBe(true);
|
||||
|
||||
// The dock and nav collapse: the viewport grows 150px at its bottom while
|
||||
// the reading position holds, so the newest text slides into view.
|
||||
const scrollTop = thread.scrollTop;
|
||||
resizeThread(650);
|
||||
thread.scrollTop = scrollTop;
|
||||
thread.dispatchEvent(new Event("scroll"));
|
||||
flushFrames();
|
||||
// Just past that space the chrome hides; the larger viewport keeps the
|
||||
// reading position, and dragging to the latest message brings it back.
|
||||
scrollThread(190);
|
||||
expect(visible).toBe(true);
|
||||
scrollThread(210);
|
||||
expect(visible).toBe(false);
|
||||
settleChrome();
|
||||
expect(visible).toBe(false);
|
||||
expect(fromBottom()).toBeGreaterThan(0);
|
||||
scrollThread(0);
|
||||
expect(visible).toBe(true);
|
||||
});
|
||||
|
||||
it("keeps the composer on screen while the keyboard is open", () => {
|
||||
@@ -132,6 +183,24 @@ describe("useMobileNavVisibility with a bottom-anchored task thread", () => {
|
||||
expect(visible).toBe(true);
|
||||
});
|
||||
|
||||
it("shows the chrome when the keyboard opens and keeps it after the keyboard closes", async () => {
|
||||
const shell = document.createElement("div");
|
||||
shell.className = "paperclip-app-shell";
|
||||
document.body.append(shell);
|
||||
act(() => root.render(<Host />));
|
||||
scrollThread(0);
|
||||
scrollThread(400);
|
||||
expect(visible).toBe(false);
|
||||
settleChrome();
|
||||
|
||||
await act(async () => { shell.dataset.keyboardOpen = "true"; });
|
||||
expect(visible).toBe(true);
|
||||
await act(async () => { delete shell.dataset.keyboardOpen; });
|
||||
settleChrome();
|
||||
scrollThread(fromBottom() + 10);
|
||||
expect(visible).toBe(true);
|
||||
});
|
||||
|
||||
it("ignores threads that did not opt in and stays visible on desktop", () => {
|
||||
thread.removeAttribute("data-mobile-chrome-scroll");
|
||||
act(() => root.render(<Host />));
|
||||
|
||||
@@ -40,21 +40,32 @@ export function useMobileNavVisibility(enabled: boolean, pathname: string): bool
|
||||
let current = true;
|
||||
// A bottom-anchored thread is measured from its latest message, so the
|
||||
// tracker's resting zone is the bottom of the thread instead of the top.
|
||||
// Tucking the composer away grows the thread viewport; measuring against
|
||||
// the bottom edge it had while the chrome was visible keeps that growth
|
||||
// from reading as a return to the latest message (which would flicker).
|
||||
const restingBottoms = new WeakMap<HTMLElement, number>();
|
||||
// Tucking the chrome away grows the thread viewport at its bottom edge by
|
||||
// the composer dock plus the nav offset. Measuring against that expanded
|
||||
// viewport in both states keeps the reported position continuous across
|
||||
// the change: nothing hides until the reader is far enough back that the
|
||||
// growth cannot clamp them to the latest message, and once hidden,
|
||||
// reaching the real bottom of the thread brings the chrome back.
|
||||
const collapseGrowth = (scroller: HTMLElement) => {
|
||||
if (!current) return 0;
|
||||
const dock = scroller
|
||||
.closest<HTMLElement>('[data-testid="task-chat-thread"]')
|
||||
?.querySelector<HTMLElement>('[data-testid="task-chat-composer-dock"]');
|
||||
const main = scroller.closest<HTMLElement>("#main-content");
|
||||
const dockHeight = dock?.getBoundingClientRect().height ?? 0;
|
||||
const padding = main ? Number.parseFloat(getComputedStyle(main).paddingBottom) || 0 : 0;
|
||||
return dockHeight + padding;
|
||||
};
|
||||
const threadBounds = (scroller: HTMLElement) => {
|
||||
const bottom = scroller.getBoundingClientRect().bottom;
|
||||
if (current || !restingBottoms.has(scroller)) restingBottoms.set(scroller, bottom);
|
||||
const restingHeight = scroller.clientHeight - (bottom - restingBottoms.get(scroller)!);
|
||||
const scrollTop = Math.max(0, Math.min(scroller.scrollTop, scroller.scrollHeight - scroller.clientHeight));
|
||||
const max = Math.max(0, scroller.scrollHeight - restingHeight);
|
||||
return { top: max - scrollTop, max };
|
||||
const liveMax = Math.max(0, scroller.scrollHeight - scroller.clientHeight);
|
||||
const scrollTop = Math.max(0, Math.min(scroller.scrollTop, liveMax));
|
||||
const growth = collapseGrowth(scroller);
|
||||
const max = Math.max(0, liveMax - growth);
|
||||
return { top: Math.max(0, liveMax - scrollTop - growth), max };
|
||||
};
|
||||
const initial = documentBounds();
|
||||
const documentTracker = new MobileNavScrollTracker(options, initial.top, initial.max);
|
||||
const threadTrackers = new WeakMap<HTMLElement, MobileNavScrollTracker>();
|
||||
let threadTrackers = new WeakMap<HTMLElement, MobileNavScrollTracker>();
|
||||
// Typing in the composer keeps it on screen; the keyboard resize is not a swipe.
|
||||
const keyboardOpen = () => document.querySelector('.paperclip-app-shell[data-keyboard-open="true"]') !== null;
|
||||
|
||||
@@ -99,12 +110,23 @@ export function useMobileNavVisibility(enabled: boolean, pathname: string): bool
|
||||
});
|
||||
};
|
||||
|
||||
// Opening the keyboard shows the chrome; re-baseline the trackers so
|
||||
// closing it again does not tuck the composer away under the reader.
|
||||
const keyboardObserver = typeof MutationObserver === "undefined" ? null : new MutationObserver(() => {
|
||||
if (!keyboardOpen()) return;
|
||||
documentTracker.visible = true;
|
||||
threadTrackers = new WeakMap();
|
||||
apply(true);
|
||||
});
|
||||
keyboardObserver?.observe(document.documentElement, { attributes: true, attributeFilter: ["data-keyboard-open"], subtree: true });
|
||||
|
||||
window.addEventListener("scroll", onDocumentScroll, { passive: true });
|
||||
// Element scroll events do not bubble, so observe nested threads in capture.
|
||||
document.addEventListener("scroll", onNestedScroll, { passive: true, capture: true });
|
||||
return () => {
|
||||
window.removeEventListener("scroll", onDocumentScroll);
|
||||
document.removeEventListener("scroll", onNestedScroll, { capture: true });
|
||||
keyboardObserver?.disconnect();
|
||||
if (documentFrame !== null) cancelAnimationFrame(documentFrame);
|
||||
if (threadFrame !== null) cancelAnimationFrame(threadFrame);
|
||||
};
|
||||
|
||||
@@ -130,7 +130,7 @@ export const ReadingBack: Story = {
|
||||
name: "02 · Reading back hides composer and nav",
|
||||
play: async ({ canvasElement }) => {
|
||||
const element = await settleAtLatest(canvasElement);
|
||||
await dragThread(element, -240);
|
||||
await dragThread(element, -480);
|
||||
await waitFor(() => {
|
||||
const state = chromeState(canvasElement);
|
||||
expect(state.main).toBe("hidden");
|
||||
@@ -145,7 +145,7 @@ export const HeadingBack: Story = {
|
||||
name: "03 · Heading back restores them",
|
||||
play: async ({ canvasElement }) => {
|
||||
const element = await settleAtLatest(canvasElement);
|
||||
await dragThread(element, -240);
|
||||
await dragThread(element, -480);
|
||||
await waitFor(() => expect(chromeState(canvasElement).main).toBe("hidden"));
|
||||
await dragThread(element, 40);
|
||||
await waitFor(() => {
|
||||
|
||||
Reference in new issue
Block a user