TypeScript
fix(web): keep the last message visible when the resting composer expands (#9553)
Theo Browne9e1bc36a0843
diff --git a/apps/web/src/components/ChatView.tsx b/apps/web/src/components/ChatView.tsxindex 0141dd4903f..c4ed99c0e23 100644--- a/apps/web/src/components/ChatView.tsx+++ b/apps/web/src/components/ChatView.tsx@@ -298,6 +298,7 @@ import { PullRequestThreadDialog } from "./PullRequestThreadDialog";import { MessagesTimeline } from "./chat/MessagesTimeline";import type { AssistantCitationRequest } from "./chat/AssistantCitationSource";import { resolveTimelineIsAtEnd } from "./chat/MessagesTimeline.logic";+import { resolveComposerTimelineInset } from "./composerFooterLayout";import { ChatHeader } from "./chat/ChatHeader";import { PanelLayoutControls, RightPanelMaximizeControl } from "./chat/PanelLayoutControls";import { expandedImageKey, type ExpandedImagePreview } from "./chat/ExpandedImagePreview";@@ -1628,6 +1629,12 @@ function ChatViewContent(props: ChatViewProps) {const [composerOverlayElement, setComposerOverlayElement] = useState<HTMLDivElement | null>(null);const [composerOverlayHeight, setComposerOverlayHeight] = useState(0);const composerOverlayHeightRef = useRef(0);+ // Space the timeline keeps clear above its end. Tracks the overlay while the+ // composer is expanded and holds that height while it rests, so the resting+ // composer never exposes rows that its expansion will cover.+ const [composerTimelineInset, setComposerTimelineInset] = useState(0);+ const composerTimelineInsetRef = useRef(0);+ const composerRestingRef = useRef(false);const [scrollToEndClearance, setScrollToEndClearance] = useState(0);const isAtEndRef = useRef(true);const isTimelineAtLogicalEnd = useCallback(() => isAtEndRef.current, []);@@ -4459,11 +4466,11 @@ function ChatViewContent(props: ChatViewProps) {return getAnchoredTurnMetrics({state,anchorIndex,- composerOverlayHeight,+ composerOverlayHeight: composerTimelineInset,anchorOffset: CHAT_TIMELINE_ANCHOR_OFFSET,});},- [composerOverlayHeight],+ [composerTimelineInset],);const timelineRealContentOverflowsViewport = useCallback((list?: LegendListRef | null) => {@@ -4488,11 +4495,11 @@ function ChatViewContent(props: ChatViewProps) {const realContentBottom = lastRowTop + Math.max(1, lastRowHeight);const visibleScrollLength = Math.max(0,- (state.scrollLength ?? 0) - composerOverlayHeight - CHAT_TIMELINE_ANCHOR_OFFSET,+ (state.scrollLength ?? 0) - composerTimelineInset - CHAT_TIMELINE_ANCHOR_OFFSET,);return realContentBottom > visibleScrollLength;},- [composerOverlayHeight],+ [composerTimelineInset],);const pageScrollControllerRef = useRef<ReturnType<typeof createPageScrollController> | null>(null,@@ -4964,10 +4971,35 @@ function ChatViewContent(props: ChatViewProps) {composerOverlayHeightRef.current = nextHeight;setComposerOverlayHeight(nextHeight);}+ const nextInset = resolveComposerTimelineInset({+ currentInset: composerTimelineInsetRef.current,+ overlayHeight: nextHeight,+ isResting: composerRestingRef.current,+ });+ if (composerTimelineInsetRef.current !== nextInset) {+ composerTimelineInsetRef.current = nextInset;+ setComposerTimelineInset(nextInset);+ }setScrollToEndClearance((currentClearance) =>currentClearance === nextHeight ? currentClearance : nextHeight,);}, []);+ // The composer reports its resting flag from a layout effect, which runs+ // before this component's own layout effects and before any resize+ // observation, so every measurement below sees the flag for its layout.+ // Only the flag is stored here: the stored height still belongs to the+ // previous layout, and the composer publishes the new layout's height+ // itself once it has measured it.+ const onComposerRestingChange = useCallback((resting: boolean) => {+ composerRestingRef.current = resting;+ }, []);+ // A held reservation belongs to the previous thread's draft. Rebuild it from+ // this thread's overlay so a tall draft elsewhere does not pad this one.+ useLayoutEffect(() => {+ if (!composerOverlayElement) return;+ composerTimelineInsetRef.current = 0;+ publishComposerOverlayHeight(composerOverlayElement.getBoundingClientRect().height);+ }, [activeThreadKey, composerOverlayElement, publishComposerOverlayHeight]);useLayoutEffect(() => {if (!composerOverlayElement) return;@@ -7775,7 +7807,7 @@ function ChatViewContent(props: ChatViewProps) {}anchorMessageId={timelineAnchorMessageId}onAnchorReady={onTimelineAnchorReady}- contentInsetEndAdjustment={composerOverlayHeight}+ contentInsetEndAdjustment={composerTimelineInset}liveFollowEnabled={timelineLiveFollowEnabled}onIsAtEndChange={onIsAtEndChange}onManualNavigation={cancelTimelineLiveFollowForUserNavigation}@@ -7923,6 +7955,7 @@ function ChatViewContent(props: ChatViewProps) {getTimelineScrollableNode={getTimelineScrollableNode}isTimelineAtLogicalEnd={isTimelineAtLogicalEnd}onComposerOverlayHeightChange={publishComposerOverlayHeight}+ onRestingChange={onComposerRestingChange}promptRef={promptRef}composerImagesRef={composerImagesRef}composerFilesRef={composerFilesRef}diff --git a/apps/web/src/components/chat/ChatComposer.tsx b/apps/web/src/components/chat/ChatComposer.tsxindex 589ef7350ec..e2d4bda1b1d 100644--- a/apps/web/src/components/chat/ChatComposer.tsx+++ b/apps/web/src/components/chat/ChatComposer.tsx@@ -251,12 +251,14 @@ const COMPOSER_RESTING_CONTROLS_ARRIVAL_DRIFT_PX = 4;function useComposerRestingTransition(isCollapsed: boolean,+ isResting: boolean,restingControlsRef: React.RefObject<HTMLDivElement | null>,onOverlayHeightChange: (height: number) => void,) {const elementRef = useRef<HTMLDivElement>(null);const isCollapsedRef = useRef(isCollapsed);const previousCollapsedRef = useRef(isCollapsed);+ const previousRestingRef = useRef(isResting);const previousHeightRef = useRef<number | null>(null);const previousContentOffsetsRef = useRef<{promptFromTop: number | null;@@ -355,6 +357,15 @@ function useComposerRestingTransition(const nextRect = element.getBoundingClientRect();const nextHeight = nextRect.height;+ // The chat view resize-observes the overlay to place the timeline+ // inset, the scroll-to-end pill, and the mini player. Publishing the+ // destination height here turns that feedback into one update instead+ // of a ChatView re-render on every animation frame.+ const overlay = element.closest<HTMLElement>('[data-chat-composer-overlay="true"]');+ const overlayHeight = overlay?.getBoundingClientRect().height ?? null;+ if (overlayHeight !== null) {+ onOverlayHeightChange(overlayHeight);+ }const nextPromptRect = prompt?.getBoundingClientRect() ?? null;const nextPromptTop = nextPromptRect?.top ?? null;const nextActionTop = action?.getBoundingClientRect().top ?? null;@@ -385,18 +396,13 @@ function useComposerRestingTransition(element.style.overflow = "clip";surface.style.height = "100%";- // The chat view resize-observes the overlay to place the timeline- // inset, the scroll-to-end pill, and the mini player. Pinning the- // overlay at the destination height turns that feedback into one- // update instead of a ChatView re-render on every animation frame;- // bottom alignment keeps the animating surface glued to the overlay's- // stable bottom edge. The pin lasts only for the tween so later- // attachment, thread, font, and viewport changes remain natural.- const overlay = element.closest<HTMLElement>('[data-chat-composer-overlay="true"]');- let pinnedOverlayHeight: number | null = null;- if (overlay) {- pinnedOverlayHeight = overlay.getBoundingClientRect().height;- overlay.style.height = `${String(pinnedOverlayHeight)}px`;+ // Pinning the overlay at the destination height keeps the resize+ // observer quiet for the tween; bottom alignment keeps the animating+ // surface glued to the overlay's stable bottom edge. The pin lasts+ // only for the tween so later attachment, thread, font, and viewport+ // changes remain natural.+ if (overlay && overlayHeight !== null) {+ overlay.style.height = `${String(overlayHeight)}px`;overlay.style.display = "flex";overlay.style.flexDirection = "column";overlay.style.justifyContent = "flex-end";@@ -430,11 +436,6 @@ function useComposerRestingTransition();animationRef.current = animation;animationTargetHeightRef.current = nextHeight;- // Publish the destination overlay geometry in the same layout pass;- // ResizeObserver remains the fallback for non-transition changes.- if (pinnedOverlayHeight !== null) {- onOverlayHeightChange(pinnedOverlayHeight);- }const animatedRect = element.getBoundingClientRect();const previousPromptTop =@@ -612,6 +613,18 @@ function useComposerRestingTransition(};}, [isCollapsed, transitionToCurrentGeometry]);+ // The resting flag can change while the collapsed layout stays the same,+ // for example when an unfocused thread crosses the phone breakpoint. The+ // chat view pairs overlay heights with that flag, so republish the natural+ // height for the new flag. A transition in flight publishes its own.+ useLayoutEffect(() => {+ if (previousRestingRef.current === isResting) return;+ previousRestingRef.current = isResting;+ if (animationRef.current) return;+ const overlay = elementRef.current?.closest<HTMLElement>('[data-chat-composer-overlay="true"]');+ if (overlay) onOverlayHeightChange(overlay.getBoundingClientRect().height);+ }, [isResting, onOverlayHeightChange]);+useLayoutEffect(() => {const element = elementRef.current;if (!element || typeof ResizeObserver === "undefined") return;@@ -1232,6 +1245,11 @@ export interface ChatComposerProps {getTimelineScrollableNode: () => HTMLElement | null;isTimelineAtLogicalEnd: () => boolean;onComposerOverlayHeightChange: (height: number) => void;+ /**+ * Whether the desktop resting layout is active. Reported from a layout+ * effect, so it is current before the chat view measures the overlay.+ */+ onRestingChange: (resting: boolean) => void;// Refs the parent needs kept in syncpromptRef: React.RefObject<string>;@@ -1336,6 +1354,7 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)getTimelineScrollableNode,isTimelineAtLogicalEnd,onComposerOverlayHeightChange,+ onRestingChange,promptRef,composerRef,composerImagesRef,@@ -3584,6 +3603,9 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)useLayoutEffect(() => {onRestingControlsVisibilityChange(composerControlsVisibleInStrip);}, [composerControlsVisibleInStrip, onRestingControlsVisibilityChange]);+ useLayoutEffect(() => {+ onRestingChange(isComposerResting);+ }, [isComposerResting, onRestingChange]);const restingImagePreviewCounts = getRestingComposerImagePreviewCounts(standaloneComposerImages.length,);@@ -3639,6 +3661,7 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)) : null;const composerMainSurfaceRef = useComposerRestingTransition(composerControlsInStrip,+ isComposerResting,restingComposerControlsRef,onComposerOverlayHeightChange,);diff --git a/apps/web/src/components/composerFooterLayout.test.ts b/apps/web/src/components/composerFooterLayout.test.tsindex e260de3eb37..82ef5850cc1 100644--- a/apps/web/src/components/composerFooterLayout.test.ts+++ b/apps/web/src/components/composerFooterLayout.test.ts@@ -4,7 +4,9 @@ import { resolveContextStripLabelsCompact } from "./BranchToolbar.logic";import {COMPOSER_FOOTER_COMPACT_BREAKPOINT_PX,COMPOSER_FOOTER_WIDE_ACTIONS_COMPACT_BREAKPOINT_PX,+ COMPOSER_RESTING_EXPANSION_MIN_PX,getRestingComposerImagePreviewCounts,+ resolveComposerTimelineInset,resolveRestingComposerControlsLayout,resolveRestingComposerControlsNaturalWidth,shouldAnimateComposerRestingTransition,@@ -74,6 +76,26 @@ describe("shouldUseCompactComposerPrimaryActions", () => {});});+describe("resolveComposerTimelineInset", () => {+ it("follows the expanded overlay height", () => {+ expect(+ resolveComposerTimelineInset({ currentInset: 160, overlayHeight: 140, isResting: false }),+ ).toBe(140);+ });++ it("keeps a larger expanded reservation while resting", () => {+ expect(+ resolveComposerTimelineInset({ currentInset: 200, overlayHeight: 60, isResting: true }),+ ).toBe(200);+ });++ it("reserves the empty expansion when no larger height is known", () => {+ expect(+ resolveComposerTimelineInset({ currentInset: 0, overlayHeight: 60, isResting: true }),+ ).toBe(60 + COMPOSER_RESTING_EXPANSION_MIN_PX);+ });+});+describe("shouldUseRestingComposerLayout", () => {const resting = {isExistingThread: true,diff --git a/apps/web/src/components/composerFooterLayout.ts b/apps/web/src/components/composerFooterLayout.tsindex 53fef1181d1..b7b7d91a033 100644--- a/apps/web/src/components/composerFooterLayout.ts+++ b/apps/web/src/components/composerFooterLayout.ts@@ -48,6 +48,36 @@ export function shouldUseRestingComposerLayout(input: {return input.isExistingThread && !input.isMobileViewport && collapsed && !input.hasExpandedChrome;}+/**+ * How much taller the empty expanded composer is than its resting row on+ * desktop widths, from the layout classes in ChatComposer: the body loses+ * 8px of top padding, the prompt clamps from min-h-17.5 (70px) to 32px, and+ * the 48px footer leaves flow.+ */+export const COMPOSER_RESTING_EXPANSION_MIN_PX = 94;++/**+ * The space the timeline reserves at its end for the composer overlay.+ *+ * The overlay is measured live, but a resting composer is much shorter than+ * an expanded one. Reserving only the resting height lets a scroll to the end+ * land flush against the short composer, and the expansion that follows then+ * covers the last rows because the timeline never moves for footer growth.+ * While resting, the reservation keeps the last expanded height, or at least+ * the resting height plus the empty expansion, so expanding again changes+ * nothing above the composer. An expanded measurement is authoritative and+ * may shrink it.+ */+export function resolveComposerTimelineInset(input: {+ currentInset: number;+ overlayHeight: number;+ isResting: boolean;+}): number {+ return input.isResting+ ? Math.max(input.currentInset, input.overlayHeight + COMPOSER_RESTING_EXPANSION_MIN_PX)+ : input.overlayHeight;+}+export function shouldAnimateComposerRestingTransition(input: {hasCompletedInitialLayout: boolean;stateChanged: boolean;diff --git a/docs/user/composer.md b/docs/user/composer.mdindex b844a34d137..fce133cd27d 100644--- a/docs/user/composer.md+++ b/docs/user/composer.md@@ -55,8 +55,9 @@ On web and desktop, an existing thread settles its composer into a single-line rthe composer loses focus. At wider sizes, scrolling the conversation also rests a focused composer,except when scrolling toward the end while already there. When the thread-context strip has room,the model and mode controls stay available beside the thread context; otherwise they return when the-composer is focused. Focus the composer or start typing to expand it again. New-thread layouts keep-the full composer. **Settings → General → Collapse composer** chooses which triggers rest it:+composer is focused. Focus the composer or start typing to expand it again. The conversation keeps+the expanded composer's space clear above its last message while the composer rests, so expanding it+again never covers what you scrolled to. New-thread layouts keep the full composer. **Settings → General → Collapse composer** chooses which triggers rest it:**On unfocus**, **On scroll**, both, or neither. With neither selected the composer stays expanded.At phone-sized web or desktop window widths, existing threads animate between their compact and