[RAPID-fix] 채팅방 스크롤 점프 제거: useEffect → useLayoutEffect (페인트 전 처리)
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
"use client";
|
"use client";
|
||||||
|
|
||||||
import React, { useEffect, useRef, useState } from "react";
|
import React, { useEffect, useLayoutEffect, useRef, useState } from "react";
|
||||||
import { MessageSquare, Pencil, Check, X, ChevronsDown } from "lucide-react";
|
import { MessageSquare, Pencil, Check, X, ChevronsDown } from "lucide-react";
|
||||||
import { useMessages, useMarkAsRead, useUpdateRoom } from "@/hooks/useMessenger";
|
import { useMessages, useMarkAsRead, useUpdateRoom } from "@/hooks/useMessenger";
|
||||||
import { useAuth } from "@/hooks/useAuth";
|
import { useAuth } from "@/hooks/useAuth";
|
||||||
@@ -36,21 +36,18 @@ export function ChatPanel({ room }: ChatPanelProps) {
|
|||||||
|
|
||||||
const lastMessageId = messages?.[messages.length - 1]?.id;
|
const lastMessageId = messages?.[messages.length - 1]?.id;
|
||||||
|
|
||||||
// Hide messages until scrolled to bottom (prevents visible jump on room open)
|
// useLayoutEffect: fires before browser paint → hide + scroll synchronously
|
||||||
useEffect(() => {
|
useLayoutEffect(() => {
|
||||||
|
const el = scrollRef.current;
|
||||||
|
if (!el) return;
|
||||||
setScrollReady(false);
|
setScrollReady(false);
|
||||||
}, [selectedRoomId]);
|
el.scrollTop = el.scrollHeight;
|
||||||
|
// Reveal after scroll is applied
|
||||||
|
requestAnimationFrame(() => setScrollReady(true));
|
||||||
|
}, [selectedRoomId, lastMessageId]);
|
||||||
|
|
||||||
// Scroll to bottom when messages load or room changes
|
// Second pass: re-scroll after async images load
|
||||||
// Two-pass: immediate rAF + 600ms delayed (for async image loads)
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
requestAnimationFrame(() => {
|
|
||||||
const el = scrollRef.current;
|
|
||||||
if (el) {
|
|
||||||
el.scrollTop = el.scrollHeight;
|
|
||||||
setScrollReady(true);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
const t = setTimeout(() => {
|
const t = setTimeout(() => {
|
||||||
const el = scrollRef.current;
|
const el = scrollRef.current;
|
||||||
if (el) el.scrollTop = el.scrollHeight;
|
if (el) el.scrollTop = el.scrollHeight;
|
||||||
|
|||||||
Reference in New Issue
Block a user