diff --git a/docs/widget/src/core/CWDComments.js b/docs/widget/src/core/CWDComments.js index 0cfe2cc..e2be762 100644 --- a/docs/widget/src/core/CWDComments.js +++ b/docs/widget/src/core/CWDComments.js @@ -179,8 +179,8 @@ export class CWDComments { typeof api.likeComment === 'function' ? api.likeComment.bind(api) : undefined, ); - this.unsubscribe = this.store.store.subscribe((state) => { - this._onStateChange(state); + this.unsubscribe = this.store.store.subscribe((state, prevState) => { + this._onStateChange(state, prevState); }); this._render(); @@ -496,6 +496,17 @@ export class CWDComments { currentUser: state.form, }); } + + const pageChanged = + prevState && + prevState.pagination && + state && + state.pagination && + state.pagination.page !== prevState.pagination.page; + + if (pageChanged) { + this._scrollToCommentsTop(); + } } /** @@ -553,8 +564,8 @@ export class CWDComments { this.store = createCommentStore(this.config, api.fetchComments.bind(api), api.submitComment.bind(api)); - this.unsubscribe = this.store.store.subscribe((state) => { - this._onStateChange(state); + this.unsubscribe = this.store.store.subscribe((state, prevState) => { + this._onStateChange(state, prevState); }); this.store.loadComments(); @@ -562,6 +573,17 @@ export class CWDComments { this._applyCustomCss(); } + _scrollToCommentsTop() { + if (typeof window === 'undefined') { + return; + } + const target = this.hostElement || this.mountPoint; + if (!target || typeof target.scrollIntoView !== 'function') { + return; + } + target.scrollIntoView({ behavior: 'smooth', block: 'start' }); + } + _applyCustomCss() { if (!this.shadowRoot) { return;