Skip to content

Commit

Permalink
fix(modal): pass an HTMLElement to modal container option (ng-boots…
Browse files Browse the repository at this point in the history
  • Loading branch information
drobins29 committed Feb 12, 2020
1 parent 59581c7 commit 6059ee1
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 4 deletions.
4 changes: 3 additions & 1 deletion src/modal/modal-config.ts
Expand Up @@ -44,9 +44,11 @@ export interface NgbModalOptions {
/**
* A selector specifying the element all new modal windows should be appended to.
*
* If passed a `HTMLElement`, all new modal windows should be appended to it.
*
* If not specified, will be `body`.
*/
container?: string;
container?: string | HTMLElement;

/**
* The `Injector` to use for modal content.
Expand Down
7 changes: 4 additions & 3 deletions src/modal/modal-stack.ts
Expand Up @@ -15,7 +15,7 @@ import {Subject} from 'rxjs';
import {ngbFocusTrap} from '../util/focus-trap';
import {ContentRef} from '../util/popup';
import {ScrollBar} from '../util/scrollbar';
import {isDefined, isString} from '../util/util';
import {isDefined, isDomObject, isString} from '../util/util';
import {NgbModalBackdrop} from './modal-backdrop';
import {NgbModalOptions} from './modal-config';
import {NgbActiveModal, NgbModalRef} from './modal-ref';
Expand Down Expand Up @@ -46,8 +46,9 @@ export class NgbModalStack {
}

open(moduleCFR: ComponentFactoryResolver, contentInjector: Injector, content: any, options): NgbModalRef {
const containerEl =
isDefined(options.container) ? this._document.querySelector(options.container) : this._document.body;
const containerEl = isDomObject(options.container) ? options.container : isDefined(options.container) ?
this._document.querySelector(options.container) :
this._document.body;
const renderer = this._rendererFactory.createRenderer(null, null);

const revertPaddingForScrollBar = this._scrollBar.compensate();
Expand Down
11 changes: 11 additions & 0 deletions src/modal/modal.spec.ts
Expand Up @@ -461,6 +461,17 @@ describe('ngb-modal', () => {
expect(fixture.nativeElement).not.toHaveModal();
});

it('should attach window and backdrop elements to the specified container DOM element', () => {
const containerDomEl = document.querySelector('div#testContainer');
const modalInstance = fixture.componentInstance.open('foo', {container: containerDomEl});
fixture.detectChanges();
expect(fixture.nativeElement).toHaveModal('foo', '#testContainer');

modalInstance.close();
fixture.detectChanges();
expect(fixture.nativeElement).not.toHaveModal();
});

it('should throw when the specified container element doesn\'t exist', () => {
const brokenSelector = '#notInTheDOM';
expect(() => {
Expand Down
4 changes: 4 additions & 0 deletions src/util/util.ts
Expand Up @@ -26,6 +26,10 @@ export function isDefined(value: any): boolean {
return value !== undefined && value !== null;
}

export function isDomObject(value: any): boolean {
return isDefined(value) && isDefined(value.nodeType);
}

export function padNumber(value: number) {
if (isNumber(value)) {
return `0${value}`.slice(-2);
Expand Down

0 comments on commit 6059ee1

Please sign in to comment.