Comment
댓글 목록을 렌더링하고 페이지네이션·답글 버튼·스크롤 위치 복원 기능을 제공하는 컨테이너 컴포넌트
개요
Comment 컴포넌트는 작성자·작성일·답글 대상자·본문·답글 버튼으로 구성된 댓글 항목을 스크롤 가능한 목록으로 제공합니다.
주요 특징
- ✅ 댓글 목록 렌더링: 작성자·작성일·답글 대상자·본문 구조
- ✅ 더보기 페이지네이션: 버튼 클릭으로 추가 댓글 로드
- ✅ 로딩 상태: 스피너 오버레이 표시
- ✅ 빈 상태: 안내 문구 표시
- ✅ 답글 버튼: 클릭 시 댓글 데이터 콜백 전달
- ✅ 스크롤 위치 관리: initialScrollTop + ref.getScrollTop()
기본 사용
댓글 목록을 렌더링합니다.
Preview
빈 상태
댓글이 없을 때 안내 문구를 표시합니다.
Preview
아직 작성된 댓글이 없습니다.
로딩 상태
데이터를 불러오는 동안 스피너 오버레이를 표시합니다.
Preview
더보기 (페이지네이션)
hasMore=true이면 더보기 버튼이 표시되고, 클릭 시 onLoadMore가 호출됩니다.
Preview
표시 중: 2 / 5
답글 버튼 클릭
답글 버튼 클릭 시 onClickButton 콜백으로 해당 댓글의 index와 데이터가 전달됩니다.
Preview
스크롤 위치 관리
내부 스크롤 영역은 Foundation ScrollArea / ScrollBar를 사용합니다. 기존 height, initialScrollTop, ref.getScrollTop(), onLoadMore API는 그대로이며 실제 Viewport 위치를 조회·복원합니다. 더보기는 기존처럼 버튼 클릭으로 동작합니다.
아래 예제는 초기 64px 위치를 복원하고 현재 위치를 읽습니다. 보라색 12px 스크롤바는 List의 토큰 예제와 같은 방식으로 중첩된 [data-token="scroll-area"] 루트에 --scroll-area-scrollbar-width-vertical과 --scroll-area-thumb-background를 지정한 것입니다.
scrollTop: 0px
import { useRef } from "react"
import { Comment, type CommentRef } from "@vortex/ui-cals"
function MyPage() {
const commentRef = useRef<CommentRef>(null)
// 페이지 이탈 시 저장
const saveScrollPosition = () => {
const scrollTop = commentRef.current?.getScrollTop() ?? 0
sessionStorage.setItem("commentScroll", String(scrollTop))
}
// 복원
const savedTop = Number(sessionStorage.getItem("commentScroll") || 0)
return <Comment ref={commentRef} comments={comments} initialScrollTop={savedTop} />
}API Reference
Props
| Prop | Type | Default | Description |
|---|---|---|---|
comments | CommentItem[] | [] | 댓글 목록 데이터 |
loading | boolean | false | 로딩 상태 |
hasMore | boolean | false | 더 불러올 댓글 존재 여부 |
height | number | string | 250 | 스크롤 영역 높이 |
emptyText | string | "댓글이 없습니다." | 빈 상태 안내 문구 |
visible | boolean | true | 표시 여부 |
buttonLabel | string | "답글" | 답글 버튼 텍스트 |
showButton | boolean | true | 답글 버튼 표시 여부 |
pageSize | number | 20 | 한 번에 불러올 댓글 수 |
initialScrollTop | number | - | 초기 스크롤 위치 복원 |
className | string | - | 컨테이너 className |
style | React.CSSProperties | - | 인라인 스타일 |
Events
| Event | Type | Description |
|---|---|---|
onLoadMore | () => void | 더보기 버튼 클릭 시 호출 |
onClickButton | (data: CommentClickData) => void | 답글 버튼 클릭 시 호출 |
Ref (CommentRef)
| Method | Return Type | Description |
|---|---|---|
getScrollTop() | number | 현재 스크롤 위치 반환 |
Types
interface CommentItem {
id: string
author: string
date: string
replyTo?: string
content: string
}
interface CommentClickData {
index: number
comment: CommentItem
}접근성
권장 사항
- ✅ 댓글 목록에
role="list"자동 적용 - ✅ 각 댓글 항목에
role="listitem"자동 적용 - ✅ 로딩 시
aria-busy="true"적용 - ✅ 답글 버튼에 작성자 이름 포함 aria-label 제공
관련 컴포넌트
- CommentButton: 댓글 내 개별 버튼 컴포넌트
Last updated on