Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/main' into feat/comment-with-r…
Browse files Browse the repository at this point in the history
…eplies
  • Loading branch information
ruibaby committed Mar 18, 2024
2 parents 046dcb6 + d29197e commit ab0919b
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 5 deletions.
4 changes: 4 additions & 0 deletions packages/comment-widget/src/base-form.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,10 @@ export class BaseForm extends LitElement {
form?.reset();
}

setFocus() {
this.textareaRef.value?.focus();
}

static override styles = [
varStyles,
baseStyles,
Expand Down
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 @@ -94,7 +94,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 @@ -145,7 +147,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 @@ -182,14 +185,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
2 changes: 1 addition & 1 deletion packages/comment-widget/src/emoji-button.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ export class EmojiButton extends LitElement {
position: relative;
}
.emoji-button:hover {
.emoji-button:hover icon-emoji {
opacity: 0.8;
}
Expand Down
9 changes: 9 additions & 0 deletions packages/comment-widget/src/reply-form.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,15 @@ export class ReplyForm extends LitElement {

baseFormRef: Ref<BaseForm> = createRef<BaseForm>();

override connectedCallback(): void {
super.connectedCallback();

setTimeout(() => {
this.scrollIntoView({ block: 'center', inline: 'start', behavior: 'smooth' });
this.baseFormRef.value?.setFocus();
}, 0);
}

override render() {
return html`<base-form ${ref(this.baseFormRef)} @submit="${this.onSubmit}"></base-form>`;
}
Expand Down

0 comments on commit ab0919b

Please sign in to comment.