Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(modal): pass an HTMLElement to modal container option (#3584) #3585

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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