Skip to content

Commit

Permalink
Add scroll new message into view
Browse files Browse the repository at this point in the history
  • Loading branch information
jloh02 committed Jan 20, 2024
1 parent 1d65f7c commit ecd33b5
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion frontend/src/Game.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
import { Chessboard } from "react-chessboard";
import "./Game.css";
import { Message } from "./types/message";
import { useCallback, useEffect, useState } from "react";
import { useCallback, useEffect, useRef, useState } from "react";
import { move, onUpdate } from "./utils/socket";

export default function Game({
Expand All @@ -21,6 +21,7 @@ export default function Game({
isWhite: boolean;
isTurn: boolean;
}) {
const bottomChat = useRef<HTMLDivElement>(null);
const [text, setText] = useState("");
const [messages, setMessages] = useState<Message[]>([]);
const [isLoading, setLoading] = useState(false);
Expand Down Expand Up @@ -60,6 +61,11 @@ export default function Game({
setText("");
}, [text]);

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

return (
<Grid columns={2} style={{ height: "80vh", width: "80vw" }}>
<Grid.Column>
Expand Down Expand Up @@ -94,6 +100,7 @@ export default function Game({
<div>{m.text}</div>
</div>
))}
<div ref={bottomChat}></div>
</Container>
<Input
onKeyDown={(event: KeyboardEvent) => {
Expand Down

0 comments on commit ecd33b5

Please sign in to comment.