Skip to content

Commit 48ef8b7

Browse files
H--o-lvalorkin
authored andcommitted
feat(modals): add support for nested modals (fix scroll)
Twitter bootstrap (and so ngx-boostrap) doesn't officialy support nested modals. You can however display nested modal but at least one issue appears: At first modal you close, scroll events and scrollbar go back to the page, so you can't scroll modals that are still displayed. I propose to improve it with following modifications in modal component: - at show, check if 'modal open' class is already attached to document body and set `isNested` boolean accordingly - at hide, if nested, do not remove 'modal open' class and do not reset scrollbar. Related to: #896 #1691
1 parent a8197e5 commit 48ef8b7

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

src/modal/modal.component.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,8 @@ export class ModalDirective implements AfterViewInit, OnDestroy {
8282
// todo: implement _dialog
8383
private _dialog: any;
8484

85+
private isNested: boolean = false;
86+
8587
@HostListener('click', ['$event'])
8688
public onClick(event: any): void {
8789
if (this.config.ignoreBackdropClick || this.config.backdrop === 'static' || event.target !== this._element.nativeElement) {
@@ -143,7 +145,11 @@ export class ModalDirective implements AfterViewInit, OnDestroy {
143145
this.setScrollbar();
144146

145147
if (document && document.body) {
146-
this._renderer.setElementClass(document.body, ClassName.OPEN, true);
148+
if (document.body.classList.contains(ClassName.OPEN)) {
149+
this.isNested = true;
150+
} else {
151+
this._renderer.setElementClass(document.body, ClassName.OPEN, true);
152+
}
147153
}
148154

149155
this.showBackdrop(() => {
@@ -233,11 +239,13 @@ export class ModalDirective implements AfterViewInit, OnDestroy {
233239
this._renderer.setElementAttribute(this._element.nativeElement, 'aria-hidden', 'true');
234240
this._renderer.setElementStyle(this._element.nativeElement, 'display', 'none');
235241
this.showBackdrop(() => {
236-
if (document && document.body) {
237-
this._renderer.setElementClass(document.body, ClassName.OPEN, false);
242+
if (!this.isNested) {
243+
if (document && document.body) {
244+
this._renderer.setElementClass(document.body, ClassName.OPEN, false);
245+
}
246+
this.resetScrollbar();
238247
}
239248
this.resetAdjustments();
240-
this.resetScrollbar();
241249
this.onHidden.emit(this);
242250
});
243251
}

0 commit comments

Comments
 (0)