Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix the issue of scrolling to the comment view after the initial loading of comments #98

Merged
merged 1 commit into from Mar 14, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 10 additions & 4 deletions packages/comment-widget/src/comment-widget.ts
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