Skip to content

Commit

Permalink
feat(module:modal): smart to determine whether to add padding-right
Browse files Browse the repository at this point in the history
  • Loading branch information
wilsoncook committed Jul 24, 2018
1 parent 8e7a6cb commit cc07d31
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions components/modal/nz-modal.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -414,13 +414,23 @@ export class NzModalComponent<T = any, R = any> extends NzModalRef<T, R> impleme
const openModals = this.modalControl.openModals;

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 {
if (this.hasScrollBar(this.document.body)) { // Adding padding-right only when body's scrollbar is shown up
this.renderer.setStyle(this.document.body, 'padding-right', `${this.nzMeasureScrollbarService.scrollBarWidth}px`);
this.renderer.setStyle(this.document.body, 'overflow', 'hidden');
}
} else { // NOTE: we need to always remove the padding due to the scroll bar may be disappear by window resizing before modal closed
this.renderer.removeStyle(this.document.body, 'padding-right');
this.renderer.removeStyle(this.document.body, 'overflow');
}
}

/**
* Check whether one element is able to has the scroll bar
* Exceptional Cases: users can show the scroll bar by their own permanently (eg. overflow: scroll)
*/
private hasScrollBar(element: HTMLElement): boolean {
return element.scrollHeight > element.clientHeight;
}
}

////////////
Expand Down

0 comments on commit cc07d31

Please sign in to comment.