Skip to content

Commit

Permalink
feat(modal): pass an HTMLElement to modal container option (#3585)
Browse files Browse the repository at this point in the history
Fixes #3584
  • Loading branch information
drobins29 committed Feb 13, 2020
1 parent 2e0918a commit 3c65b21
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 3 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
5 changes: 3 additions & 2 deletions src/modal/modal-stack.ts
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 = options.container instanceof HTMLElement ? 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

0 comments on commit 3c65b21

Please sign in to comment.