Comment

A comment thread with an inline composer. Comments render with avatar, author, timestamp, likes, and per-comment actions. Deleting is restricted to the current user.

Usage

<script>
  import { CommentSection } from '@vultra/ui';

  let comments = $state([
    { id: 1, author: 'Ada', text: 'First!', time: '2m', likes: 3 },
    { id: 2, author: 'Grace', text: 'Nice work.', time: '1h', likes: 1 }
  ]);

  function addComment(text) {
    comments = [...comments, { id: Date.now(), author: 'You', text, time: 'now' }];
  }

  function likeComment(id) {
    comments = comments.map((c) =>
      c.id === id ? { ...c, likes: (c.likes ?? 0) + 1 } : c
    );
  }

  function deleteComment(id) {
    comments = comments.filter((c) => c.id !== id);
  }
</script>

<CommentSection
  {comments}
  currentUser="You"
  onAddComment={addComment}
  onLike={likeComment}
  onDelete={deleteComment}
/>

Props

PropTypeDefaultDescription
commentsCommentItem[]List of comments to render
onAddComment(text: string) => voidCalled with the composer text when submitted
onLike(id: string \| number) => voidCalled when a comment is liked
onDelete(id: string \| number) => voidCalled when a comment is deleted
currentUserstringAuthor name used to show delete controls
placeholderstring'Add a comment…'Composer placeholder
classstringAdditional CSS classes
commentClassstringAdditional CSS classes per comment
childrenSnippetExtra content below the thread

CommentItem

{
  id: string | number;
  author: string;
  avatar?: string;
  text: string;
  time: string;
  likes?: number;
}

Features

  • Composer with submit handling; empty drafts are ignored.
  • Like and delete callbacks per comment.
  • Delete controls only for the current user’s comments.
  • Optional avatar image per comment.