Skip to content

Commit

Permalink
refactor(module:modal): show/hide scrollbar more smoothly (#1491)
Browse files Browse the repository at this point in the history
  • Loading branch information
wilsoncook authored and vthinkxie committed May 24, 2018
1 parent 6f394a4 commit e9806c6
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions components/modal/nz-modal.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -253,16 +253,21 @@ export class NzModalComponent<T = any, R = any> extends NzModalRef<T, R> impleme

// Do rest things when visible state changed
private handleVisibleStateChange(visible: boolean, animation: boolean = true, closeResult?: R): Promise<void> {
if (visible) { // Hide scrollbar at the first time when shown up
this.changeBodyOverflow(1);
}

return Promise
.resolve(animation && this.animateTo(visible))
.then(() => { // Emit open/close event after animations over
if (visible) {
this.nzAfterOpen.emit();
} else {
this.nzAfterClose.emit(closeResult);
this.changeBodyOverflow(); // Show/hide scrollbar when animation is over
}
})
.then(() => this.changeBodyOverflow());
});
// .then(() => this.changeBodyOverflow());
}

// Lookup a button's property, if the prop is a function, call & then return the result, otherwise, return itself.
Expand Down Expand Up @@ -373,10 +378,14 @@ export class NzModalComponent<T = any, R = any> extends NzModalRef<T, R> impleme
// }
}

private changeBodyOverflow(): void {
/**
* Take care of the body's overflow to decide the existense of scrollbar
* @param plusNum The number that the openModals.length will increase soon
*/
private changeBodyOverflow(plusNum: number = 0): void {
const openModals = this.modalControl.openModals;

if (openModals.length) {
if (openModals.length + plusNum > 0) {
this.renderer.setStyle(this.document.body, 'padding-right', `${this.nzMeasureScrollbarService.scrollBarWidth}px`);
this.renderer.setStyle(this.document.body, 'overflow', 'hidden');
} else {
Expand Down

0 comments on commit e9806c6

Please sign in to comment.