Skip to content

Commit

Permalink
feat(modal): add 'backdropClass' option
Browse files Browse the repository at this point in the history
Closes #2166
Closes #2248
  • Loading branch information
giampierobono authored and pkozlowski-opensource committed Mar 28, 2018
1 parent da96dec commit 5ffaabc
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ <h4 class="modal-title">Modal title</h4>
</div>
</ng-template>

<button class="btn btn-outline-primary" (click)="openCustomClass(content)">Modal with custom class</button>
<button class="btn btn-outline-primary" (click)="openSm(content)">Small modal</button>
<button class="btn btn-outline-primary" (click)="openLg(content)">Large modal</button>
<button class="btn btn-outline-primary" (click)="openVerticallyCentered(content)">Modal vertically centered</button>
<button class="btn btn-outline-primary mb-1" (click)="openWindowCustomClass(content)">Modal with window custom class</button>
<button class="btn btn-outline-primary mb-1" (click)="openBackDropCustomClass(content)">Modal with backdrop custom class</button>
<button class="btn btn-outline-primary mb-1" (click)="openSm(content)">Small modal</button>
<button class="btn btn-outline-primary mb-1" (click)="openLg(content)">Large modal</button>
<button class="btn btn-outline-primary mb-1" (click)="openVerticallyCentered(content)">Modal vertically centered</button>
9 changes: 8 additions & 1 deletion demo/src/app/components/modal/demos/options/modal-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,21 @@ import {NgbModal, ModalDismissReasons} from '@ng-bootstrap/ng-bootstrap';
.dark-modal .close {
color: white;
}
.light-blue-backdrop {
background-color: #5cb3fd;
}
`]
})
export class NgbdModalOptions {
closeResult: string;

constructor(private modalService: NgbModal) {}

openCustomClass(content) {
openBackDropCustomClass(content) {
this.modalService.open(content, {backdropClass: 'light-blue-backdrop'});
}

openWindowCustomClass(content) {
this.modalService.open(content, { windowClass: 'dark-modal' });
}

Expand Down
9 changes: 7 additions & 2 deletions src/modal/modal-backdrop.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import {Component} from '@angular/core';
import {Component, Input} from '@angular/core';

@Component({selector: 'ngb-modal-backdrop', template: '', host: {'class': 'modal-backdrop fade show'}})
@Component({
selector: 'ngb-modal-backdrop',
template: '',
host: {'[class]': '"modal-backdrop fade show" + (backdropClass ? " " + backdropClass : "")'}
})
export class NgbModalBackdrop {
@Input() backdropClass: string;
}
13 changes: 13 additions & 0 deletions src/modal/modal-stack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {NgbActiveModal, NgbModalRef} from './modal-ref';
export class NgbModalStack {
private _document: any;
private _windowAttributes = ['backdrop', 'centered', 'keyboard', 'size', 'windowClass'];
private _backdropAttributes = ['backdropClass'];

constructor(
private _applicationRef: ApplicationRef, private _injector: Injector,
Expand Down Expand Up @@ -49,6 +50,10 @@ export class NgbModalStack {
activeModal.dismiss = (reason: any) => { ngbModalRef.dismiss(reason); };

this._applyWindowOptions(windowCmptRef.instance, options);

if (backdropCmptRef && backdropCmptRef.instance) {
this._applyBackdropOptions(backdropCmptRef.instance, options);
}
return ngbModalRef;
}

Expand Down Expand Up @@ -77,6 +82,14 @@ export class NgbModalStack {
});
}

private _applyBackdropOptions(backdropInstance: NgbModalBackdrop, options: Object): void {
this._backdropAttributes.forEach((optionName: string) => {
if (isDefined(options[optionName])) {
backdropInstance[optionName] = options[optionName];
}
});
}

private _getContentRef(
moduleCFR: ComponentFactoryResolver, contentInjector: Injector, content: any,
context: NgbActiveModal): ContentRef {
Expand Down
19 changes: 17 additions & 2 deletions src/modal/modal.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -482,9 +482,9 @@ describe('ngb-modal', () => {

});

describe('custom class options', () => {
describe('window custom class options', () => {

it('should render modals with the correct custom classes', () => {
it('should render modals with the correct window custom classes', () => {
const modalInstance = fixture.componentInstance.open('foo', {windowClass: 'bar'});
fixture.detectChanges();
expect(fixture.nativeElement).toHaveModal('foo');
Expand All @@ -497,6 +497,21 @@ describe('ngb-modal', () => {

});

describe('backdrop custom class options', () => {

it('should render modals with the correct backdrop custom classes', () => {
const modalInstance = fixture.componentInstance.open('foo', {backdropClass: 'my-fancy-backdrop'});
fixture.detectChanges();
expect(fixture.nativeElement).toHaveModal('foo');
expect(document.querySelector('ngb-modal-backdrop')).toHaveCssClass('my-fancy-backdrop');

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

});

describe('custom injector option', () => {

it('should render modal with a custom injector', () => {
Expand Down
5 changes: 5 additions & 0 deletions src/modal/modal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ export interface NgbModalOptions {
* Custom class to append to the modal window
*/
windowClass?: string;

/**
* Custom class to append to the modal backdrop
*/
backdropClass?: string;
}

/**
Expand Down

0 comments on commit 5ffaabc

Please sign in to comment.