Skip to content

fix(chat): smooth scrollbar behavior during AI streaming output #86

Closed

Description

@larryro

Problem Statement

When AI continuously outputs streaming content in the chat page (e.g., "write an article about environment with 1000 words"), the browser scrollbar position jumps and content moves up and down, creating a non-smooth user experience.

Current State

  1. Layout height mismatch in streaming markdown: In incremental-markdown.tsx, the streaming-text-layout-reference (hidden layer) renders full content while streaming-text-reveal (visible layer) renders partial content. When markdown rendering produces different heights, the visible layer's height doesn't match, causing layout shifts.

  2. No continuous auto-scroll during streaming: Current implementation only scrolls ONCE when isLoading becomes true, but as content grows during streaming, the scroll doesn't follow the new content.

  3. Scroll position conflicts: When content height changes, the browser adjusts scroll position, combined with no auto-scroll tracking, creating visible jumping.

Proposed Solution

  1. Fix layout stability: Ensure consistent height between layout reference and reveal layers by rendering full content in both layers and using CSS masking for reveal
  2. Implement continuous auto-scroll: Use ResizeObserver to detect content growth and scroll smoothly
  3. Add CSS scrollbar-gutter: stable: Reserve space for scrollbar to prevent layout shifts
  4. Respect user intent: If user scrolls up, disable auto-scroll; re-enable when scrolling to bottom

Affected Components

  • services/platform/app/(app)/dashboard/[id]/chat/components/incremental-markdown.tsx
  • services/platform/app/(app)/globals.css
  • services/platform/hooks/use-auto-scroll.ts (new)
  • services/platform/app/(app)/dashboard/[id]/chat/components/chat-interface.tsx

Acceptance Criteria

  • Content height stays stable during streaming (no jumping)
  • Auto-scroll follows content during streaming
  • User can scroll up without being pulled back down
  • Scroll-to-bottom button re-enables auto-scroll
  • No visible scrollbar jumping when content grows
  • Performance: no jank during streaming
  • Long articles (1000+ words) render smoothly

🤖 Generated with Claude Code

Activity

  1. larryro commented on Jan 5, 2026

    @larryro
    ContributorAuthor

    Implementation Plan

    Step 1: Fix Layout Stability in Streaming Markdown

    File: services/platform/app/(app)/dashboard/[id]/chat/components/incremental-markdown.tsx

    Problem: The streaming-text-reveal layer uses position: absolute but its height differs from streaming-text-layout-reference because they render different content (partial vs full).

    Solution: Change the architecture to ensure height stability:

    • Use overflow: hidden on container instead of absolute positioning
    • Both layers should contribute to layout, with reveal clipped to show only revealed content
    • Use CSS clip-path or mask to reveal content without changing layout
    // Current (problematic):
    <div className="streaming-text-layout-reference"> {/* visibility: hidden */}
      <Markdown>{content}</Markdown>  {/* Full content */}
    </div>
    <div className="streaming-text-reveal"> {/* position: absolute */}
      <Markdown>{revealedContent}</Markdown>  {/* Partial - different height! */}
    </div>
    
    // Fixed approach:
    <div className="streaming-text-container" style={{ overflow: 'hidden' }}>
      <div className="streaming-text-layout-reference"> {/* Sets height */}
        <Markdown>{content}</Markdown>
      </div>
      <div className="streaming-text-reveal"> {/* Same content, clipped via CSS mask */}
        <Markdown>{content}</Markdown>  {/* Full content, masked to revealPosition */}
      </div>
    </div>

    Step 2: Update CSS for Clip-Based Reveal

    File: services/platform/app/(app)/globals.css

    .streaming-text-container {
      position: relative;
      overflow: hidden;
    }
    
    .streaming-text-layout-reference {
      visibility: hidden;
      pointer-events: none;
      user-select: none;
    }
    
    .streaming-text-reveal {
      position: absolute;
      inset: 0;
    }
    
    /* Add scrollbar stability */
    .scrollbar-gutter-stable {
      scrollbar-gutter: stable;
    }

    Step 3: Create use-auto-scroll.ts Hook

    File: services/platform/hooks/use-auto-scroll.ts (NEW)

    Key features:

    • Track content height changes using ResizeObserver
    • Detect user scroll intent (if user scrolls up, disable auto-scroll)
    • Re-enable auto-scroll when user scrolls back to bottom
    • Use requestAnimationFrame for smooth scrolling
    • Passive event listeners for performance

    Step 4: Update chat-interface.tsx

    File: services/platform/app/(app)/dashboard/[id]/chat/components/chat-interface.tsx

    Changes:

    1. Add ref to the messages container div for ResizeObserver
    2. Import and use the new useAutoScroll hook
    3. Update scroll-to-bottom button to use new auto-scroll logic
    4. Apply scrollbar-gutter-stable class to scroll container
    5. Keep the initial scroll-to-AI-response behavior, but add continuous tracking

    Files to Modify

    File Changes
    incremental-markdown.tsx Fix layout stability - render full content in both layers, use CSS masking
    globals.css Update streaming CSS, add scrollbar-gutter
    use-auto-scroll.ts New hook for intelligent auto-scrolling with ResizeObserver
    chat-interface.tsx Integrate auto-scroll hook, apply CSS classes

    Potential Risks

    • Performance: Rendering full content in both layers may increase rendering overhead → Mitigation: The layout reference is already rendering full content, so no additional parsing cost
    • CSS Masking Complexity: Character-based masking in CSS is complex → Mitigation: May need to keep partial rendering but ensure both layers have same height

    Testing Strategy

    • Test with prompt: "write an article about environment with 1000 words and don't generate document"
    • Test scrolling up during streaming (should disable auto-scroll)
    • Test clicking scroll-to-bottom button (should re-enable auto-scroll)
    • Test tab switching during streaming
    • Test with code blocks and tables in streaming content
    • Performance profiling during long streaming sessions

    🤖 Generated with Claude Code

  2. Israeltheminer commented on Jan 5, 2026

    @Israeltheminer
    Contributor

    Implementation Summary

    Fixed the scroll jumping issue with a simpler approach than originally proposed.

    Root Cause

    Browser scroll anchoring was causing scroll position jumps when content height changed during streaming. The streaming-text-layout-reference layer grows as new text arrives, and the browser's scroll anchoring tries to "fix" the scroll position, causing visible jumps.

    Solution

    1. Disabled browser scroll anchoring (globals.css)

    .streaming-text-container {
      overflow-anchor: none;
    }

    2. Created useAutoScroll hook (hooks/use-auto-scroll.ts)

    • Uses ResizeObserver to detect content growth
    • Only auto-scrolls when user is at/near bottom
    • Respects user intent: stops auto-scrolling if user scrolls up
    • Re-enables auto-scroll when user returns to bottom or clicks scroll-to-bottom button

    3. Updated chat-interface.tsx

    • Integrated useAutoScroll hook (replaced useThrottledScroll)
    • Added initial scroll-to-bottom when navigating to existing conversations
    • Fixed ThinkingAnimation to hide once AI text content starts appearing

    Why This Approach vs. Original Proposal

    The issue proposed rendering full content in both layers with CSS clip-path masking. A simpler approach was chosen:

    Aspect Original Proposal This Implementation
    Complexity Modify markdown rendering + clip-path 1 CSS property + auto-scroll hook
    Performance Render full content twice Keep partial rendering
    Root cause Eliminate height mismatch Prevent browser reaction to height changes

    Both approaches solve the problem, but this one is simpler and avoids doubling the markdown rendering work.

    PR: #92

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    priority: medium

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions