Skip to content

Commit

Permalink
feat(chat): add scrollBottom chat option (#1001)
Browse files Browse the repository at this point in the history
So that it is possible to control `scroll to bottom` reaction on a new message
Also closes #921
  • Loading branch information
nnixaa authored Nov 20, 2018
1 parent a00d57c commit d393f33
Showing 1 changed file with 28 additions and 7 deletions.
35 changes: 28 additions & 7 deletions src/framework/theme/components/chat/chat.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ import {
HostBinding,
ViewChild,
ElementRef,
AfterViewChecked,
ContentChildren,
QueryList, AfterViewInit,
} from '@angular/core';
import { convertToBoolProperty } from '../helpers';
import { NbChatMessageComponent } from './chat-message.component';

/**
Expand Down Expand Up @@ -143,7 +143,7 @@ import { NbChatMessageComponent } from './chat-message.component';
</div>
`,
})
export class NbChatComponent implements AfterViewChecked, AfterViewInit {
export class NbChatComponent implements AfterViewInit {

static readonly SIZE_XXSMALL = 'xxsmall';
static readonly SIZE_XSMALL = 'xsmall';
Expand All @@ -164,6 +164,7 @@ export class NbChatComponent implements AfterViewChecked, AfterViewInit {
size: string;
status: string;
accent: string;
scrollBottom: boolean = true;

@Input() title: string;

Expand Down Expand Up @@ -262,15 +263,35 @@ export class NbChatComponent implements AfterViewChecked, AfterViewInit {
this.status = val;
}

/**
* Scroll chat to the bottom of the list when a new message arrives
* @param {boolean} val
*/
@Input('scrollBottom')
private set setScrollBottom(val: boolean) {
this.scrollBottom = convertToBoolProperty(val);
}

@ViewChild('scrollable') scrollable: ElementRef;
@ContentChildren(NbChatMessageComponent) messages: QueryList<NbChatMessageComponent>;

ngAfterViewChecked() {
this.scrollable.nativeElement.scrollTop = this.scrollable.nativeElement.scrollHeight;
}

ngAfterViewInit() {
this.messages.changes
.subscribe((messages) => this.messages = messages);
.subscribe((messages) => {
this.messages = messages;
this.updateView();
});

this.updateView();
}

updateView() {
if (this.scrollBottom) {
this.scrollListBottom();
}
}

scrollListBottom() {
this.scrollable.nativeElement.scrollTop = this.scrollable.nativeElement.scrollHeight;
}
}

0 comments on commit d393f33

Please sign in to comment.