mirror of
https://github.com/bitwarden/browser
synced 2025-12-17 16:53:34 +00:00
modal component
This commit is contained in:
50
src/app/modal.component.ts
Normal file
50
src/app/modal.component.ts
Normal file
@@ -0,0 +1,50 @@
|
||||
import * as template from './modal.component.html';
|
||||
|
||||
import {
|
||||
Component,
|
||||
ComponentFactoryResolver,
|
||||
EventEmitter,
|
||||
Input,
|
||||
OnDestroy,
|
||||
Output,
|
||||
Type,
|
||||
ViewChild,
|
||||
ViewContainerRef,
|
||||
} from '@angular/core';
|
||||
|
||||
@Component({
|
||||
selector: 'app-modal',
|
||||
template: `<ng-template #container></ng-template>`,
|
||||
})
|
||||
export class ModalComponent implements OnDestroy {
|
||||
@ViewChild('container', { read: ViewContainerRef }) container: ViewContainerRef;
|
||||
parentContainer: ViewContainerRef = null;
|
||||
fade: boolean = true;
|
||||
|
||||
constructor(private componentFactoryResolver: ComponentFactoryResolver) { }
|
||||
|
||||
ngOnDestroy() {
|
||||
document.body.classList.remove('modal-open');
|
||||
document.body.removeChild(document.querySelector('.modal-backdrop'));
|
||||
}
|
||||
|
||||
show<T>(type: Type<T>, parentContainer: ViewContainerRef, fade: boolean = true): T {
|
||||
this.parentContainer = parentContainer;
|
||||
this.fade = fade;
|
||||
|
||||
document.body.classList.add('modal-open');
|
||||
const backdrop = document.createElement('div');
|
||||
backdrop.className = 'modal-backdrop' + (this.fade ? ' fade' : '');
|
||||
document.body.appendChild(backdrop);
|
||||
|
||||
const factory = this.componentFactoryResolver.resolveComponentFactory<T>(type);
|
||||
const componentRef = this.container.createComponent<T>(factory);
|
||||
return componentRef.instance;
|
||||
}
|
||||
|
||||
close() {
|
||||
if (this.parentContainer != null) {
|
||||
this.parentContainer.clear();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user