Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Auto-scroll behavior can interfere with user's scrolling #226

Open
kahkeng opened this issue Jun 6, 2023 · 2 comments
Open

Auto-scroll behavior can interfere with user's scrolling #226

kahkeng opened this issue Jun 6, 2023 · 2 comments
Assignees

Comments

@kahkeng
Copy link
Collaborator

kahkeng commented Jun 6, 2023

In MessageList.tsx, we have this snippet, which tries to get the newly appearing bot messages to appear on the screen (by automatically scrolling the page down to it).

  useEffect(() => {
    bottomRef.current?.scrollIntoView({ behavior: 'smooth' });
  }, [messages]);

However, if the user happens to scroll away while the bot is thinking, we will fight with the user to keep scrolling to the bottom.

I think we can make this apply only if the user is close to the bottom of the screen, and not otherwise.

@kahkeng
Copy link
Collaborator Author

kahkeng commented Jun 6, 2023

Here's a snippet that worked for me elsewhere (you might recognize some elements of this :) ):

export const Chats = () => {
  const { messages } = useChatContext();
  const outerRef = React.useRef<HTMLDivElement>(null);
  const innerRef = React.useRef<HTMLDivElement>(null);
  React.useEffect(() => {
    const div = outerRef.current;
    if (!div) return;

    // check if user is at the bottom
    const isAtBottom = div.scrollHeight - div.scrollTop <= div.clientHeight + 200;
    if (isAtBottom) {
      const timer = setTimeout(() => {
        innerRef.current?.scrollIntoView({ behavior: 'smooth' });
      }, 0);
      return () => clearTimeout(timer);
    }
  }, [messages]);

  return (
    <div ref={outerRef} className="w-full">
      <Wrapper>
        {
          messages.map((m, i) => (
            <div key={i} className="flex w-full">
              { m.actor === 'user' &&
                <User content={m.payload} type={m.type} />
              }
              { m.actor === 'bot' &&
                <Markdown content={m.payload} />
              }
            </div>
          ))
        }
        <div ref={innerRef}></div>
      </Wrapper>
    </div>
  );
};

@aniemerg
Copy link
Member

aniemerg commented Aug 3, 2023

Is this fixed @kahkeng?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants