Skip to content

🐛 Bug: Scroll to bottom button not appearing and user message shifts after streaming finishes in "pin to send" autoscroll mode #5355

Closed

Description

@kwang1083

♻️ Reproduction Steps

We are using <CopilotPopup /> at the base level to reproduce:

<CopilotPopup 
  scrollView={{
  	autoScroll: 'pin-to-send',
  }}
/>

We are seeing the same errors in the base level reproduction as we see using our full overrides:

<CopilotPopup
	header={ResolvedHeader}
	input={ResolvedInput}
	scrollView={{
		scrollToBottomButton: ScrollButton,
        autoScroll: 'pin-to-send',
	}}
	toggleButton={hideToggle ? HideToggleOverride : undefined}
	messageView={ResolvedMessageView}
	suggestionView={{
		suggestion: SuggestionPill,
		container: suggestionContainerCss,
	}}
	welcomeScreen={false}
	width={isResizable ? width : defaultWidth}
	height={isResizable ? height : defaultHeight}
	id={id}
	{...dataProps}
/>

To reproduce, chat with the agent in pin to send autoscroll mode. We see that the scroll to bottom button does not appear and that the user messages shifts abruptly after the agent finishes streaming.

Reproduction with CPK chat with overriden slots:

Screen.Recording.2026-06-10.at.11.07.12.AM.mov

Reproduction with base CPK chat:

Screen.Recording.2026-06-10.at.11.05.30.AM.mov

✅ Expected Behavior

Scroll to bottom button should be present in pin to send mode and chat message should remain in the same position after streaming

Root cause analysis

For bottom scroll button not appearing, I believe the reason is that the useEffect in ScrollView that attaches scroll listeners has [scrollRef, mode] as dependencies. scrollRef is a stable useRef object, so the effect only runs on the initial render when scrollRef.current is still null (because the hasMounted guard renders a placeholder on the first pass, before the ref callback div mounts). The effect early-returns and never re-runs, leaving showScrollButton permanently false. (

)

I'm unsure what the root cause for the flash of content for user message is. Looking for help from CPK team on this!

Activity

  1. copilotkit-support-bot commented on Jun 10, 2026

    @copilotkit-support-bot

    The issues you've identified are indeed caused by a lack of reactive dependencies in the scroll-monitoring logic and a specific interaction between the "shrink-only" spacer and layout changes that occur when an agent finishes streaming (like suggestions appearing or the input container resizing).

    The root cause for the button's absence in pin-to-send (and none) mode is that the useEffect responsible for attaching scroll listeners depends on the stable scrollRef object rather than the element itself.

    Details

    The issues you've identified are indeed caused by a lack of reactive dependencies in the scroll-monitoring logic and a specific interaction between the "shrink-only" spacer and layout changes that occur when an agent finishes streaming (like suggestions appearing or the input container resizing).

    1. Scroll to Bottom Button Not Appearing

    The root cause for the button's absence in pin-to-send (and none) mode is that the useEffect responsible for attaching scroll listeners depends on the stable scrollRef object rather than the element itself. Since the chat renders a placeholder until mounting, scrollRef.current is null on the first run and the effect never re-synchronizes.

    Solution:
    Update the useEffect dependencies in packages/react-core/src/v2/components/chat/CopilotChatView.tsx to use the state-tracked nonAutoScrollEl instead of the raw ref.

    // packages/react-core/src/v2/components/chat/CopilotChatView.tsx
    
    // Change the dependency from [scrollRef, mode] to [nonAutoScrollEl, mode]
    useEffect(() => {
      if (mode === "pin-to-bottom") return;
    
      const scrollElement = nonAutoScrollEl; // Use the state variable
      if (!scrollElement) return;
    
      const checkScroll = () => {
        const atBottom =
          scrollElement.scrollHeight -
            scrollElement.scrollTop -
            scrollElement.clientHeight < 10;
        setShowScrollButton(!atBottom);
      };
    
      checkScroll();
      scrollElement.addEventListener("scroll", checkScroll);
      const resizeObserver = new ResizeObserver(checkScroll);
      resizeObserver.observe(scrollElement);
    
      return () => {
        scrollElement.removeEventListener("scroll", checkScroll);
        resizeObserver.disconnect();
      };
    }, [nonAutoScrollEl, mode]); // Now correctly re-runs when the element mounts

    2. User Message Shifting After Streaming

    The "shift" occurs because when isRunning flips to false, several layout changes happen simultaneously:

    • The inputContainerHeight often changes (e.g., transition from "Stop" button to the full Input).
    • The hasSuggestions flag becomes true, which adds the suggestions component and reduces the paddingBottom of the scroll content (from 32px to 4px).

    The usePinToSend hook's ResizeObserver is "shrink-only." When height is added (suggestions), it shrinks the spacer to compensate. However, if height is removed (the 28px padding reduction), the "shrink-only" guard prevents the spacer from growing to compensate. This discrepancy causes a jump in the total scrollable height, yanking the pinned message.

    Solution:
    The usePinToSend logic should allow the spacer to grow if the content height decreases, as long as the total "available space" remains consistent with the initial anchoring.

    Modification to use-pin-to-send.ts:

    // packages/react-core/src/v2/hooks/use-pin-to-send.ts
    
    const ro = new ResizeObserver(() => {
      if (!contentEl || !spacerEl || !scrollEl) return;
      const contentHeight = contentEl.getBoundingClientRect().height;
      const targetOffsetWithinContent = computeOffsetTop(targetEl, contentEl);
      const consumedBelow = contentHeight - targetOffsetWithinContent - userMessageHeight;
      
      // instead of if (remaining < currentSpacerHeightRef.current)
      // allow the spacer to adjust in both directions to maintain the pin
      const remaining = Math.max(0, spacerHeight - consumedBelow);
      spacerEl.style.height = `${remaining}px`;
      currentSpacerHeightRef.current = remaining;
    });

    Summary of Fixes

    • Reactive Scroll Listeners: Ensure ScrollView re-attaches listeners once the DOM element is actually available by depending on element state.
    • Bi-directional Spacer Adjustment: Allow the pin-to-send spacer to grow or shrink in response to internal content height changes (like padding adjustments) to maintain the visual anchor point.

    Confidence Assessment: High. The user's analysis of the scrollRef dependency is correct, and the "flash" behavior perfectly matches a layout conflict where paddingBottom is removed but the spacer is blocked from growing to fill the gap.

    Documentation References:


    Was this helpful?

    If this solution worked for you, please click on the appropriate option below to help us improve:

    ✅ This was helpful | ❌ Need more help

  2. Vishal0203 commented on Jun 10, 2026

    @Vishal0203
    Contributor

    Tagging Nathan for visibility on this - @NathanTarbert

  3. added a commit that references this issue on Jun 12, 2026
    930e182
  4. tylerslaton commented on Jun 12, 2026

    @tylerslaton
    Contributor

    Thanks to @westkite1201 for the fix 😄

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions