Skip to content

Commit

Permalink
Fix the issue of scrolling to the comment view after the initial load…
Browse files Browse the repository at this point in the history
…ing of comments

Signed-off-by: Ryan Wang <[email protected]>
  • Loading branch information
ruibaby committed Mar 9, 2024
1 parent 2360cf2 commit 1c13d16
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions packages/comment-widget/src/comment-widget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,9 @@ export class CommentWidget extends LitElement {

override render() {
return html`<div class="comment-widget">
<comment-form @reload="${() => this.fetchComments(1)}"></comment-form>
<comment-form
@reload="${() => this.fetchComments({ page: 1, scrollIntoView: true })}"
></comment-form>
${this.loading
? html`<loading-block></loading-block>`
: html`
Expand Down Expand Up @@ -135,7 +137,8 @@ export class CommentWidget extends LitElement {
this.currentUser = data.user.metadata.name === 'anonymousUser' ? undefined : data.user;
}

async fetchComments(page?: number) {
async fetchComments(options?: { page?: number; scrollIntoView?: boolean }) {
const { page, scrollIntoView } = options || {};
try {
if (this.comments.items.length === 0) {
this.loading = true;
Expand Down Expand Up @@ -170,14 +173,17 @@ export class CommentWidget extends LitElement {
}
} finally {
this.loading = false;
this.scrollIntoView({ block: 'start', inline: 'start', behavior: 'smooth' });

if (scrollIntoView) {
this.scrollIntoView({ block: 'start', inline: 'start', behavior: 'smooth' });
}
}
}

async onPageChange(e: CustomEvent) {
const data = e.detail;
this.comments.page = data.page;
await this.fetchComments();
await this.fetchComments({ scrollIntoView: true });
}

override connectedCallback(): void {
Expand Down

0 comments on commit 1c13d16

Please sign in to comment.