Skip to content

Instantly share code, notes, and snippets.

View GreenFlag31's full-sized avatar

Manu Claeys GreenFlag31

View GitHub Profile
@GreenFlag31
GreenFlag31 / app.component.ts
Last active August 13, 2023 09:00
app component class
openModalComponent() {
this.modalService.open(ModalContentComponent, {
animations: {
modal: {
enter: 'enter-scaling 0.3s ease-out',
leave: 'fade-out 0.3s forwards',
},
overlay: {
enter: 'fade-in 1s',
leave: 'fade-out 0.3s forwards',
@GreenFlag31
GreenFlag31 / modal.service.ts
Last active August 13, 2023 09:18
modal service class
private openWithComponent(component: Type<unknown>) {
// create the desired component, the content of the modal box
const newComponent = createComponent(component, {
environmentInjector: this.injector,
});
// create the modal component and project the instance of the desired component in the ng-content
this.newModalComponent = createComponent(ModalComponent, {
environmentInjector: this.injector,
projectableNodes: [[newComponent.location.nativeElement]],
});
@GreenFlag31
GreenFlag31 / app.component.ts
Last active August 13, 2023 08:24
app component class
export class AppComponent {
// Query the #view element in the template, expecting it to be of type ViewContainerRef
// and store the reference in the variable vcr.
@ViewChild('view', { static: true, read: ViewContainerRef })
vcr!: ViewContainerRef;
constructor(private modalService: ModalService) {}
openModalTemplate(view: TemplateRef<Element>) {
this.modalService.open(this.vcr, view, {
@GreenFlag31
GreenFlag31 / modal-animations.css
Created August 13, 2023 08:12
modal animation styles
@keyframes enter-slide-down {
from {
transform: translate(-50%, -60%);
}
to {
transform: translate(-50%, -50%);
}
}
@keyframes enter-scaling {
@GreenFlag31
GreenFlag31 / modal.component.ts
Last active August 13, 2023 08:06
modal component class
export class ModalComponent implements AfterViewInit {
@ViewChild('modal') modal!: ElementRef<HTMLDivElement>;
@ViewChild('overlay') overlay!: ElementRef<HTMLDivElement>;
options!: Options | undefined;
modalAnimationEnd!: Observable<Event>;
modalLeaveAnimation!: string;
overlayLeaveAnimation!: string;
overlayAnimationEnd!: Observable<Event>;
modalLeaveTiming!: number;
overlayLeaveTiming!: number;
@GreenFlag31
GreenFlag31 / modal-options.ts
Last active August 11, 2023 19:53
modal options
export interface Options {
animations?: {
modal?: {
enter?: string;
leave?: string;
};
overlay?: {
enter?: string;
leave?: string;
};
@GreenFlag31
GreenFlag31 / modal.service.ts
Last active August 13, 2023 09:53
Modal service
@Injectable({
providedIn: 'root',
})
export class ModalService {
// Create a reference to our modal component
newModalComponent!: ComponentRef<ModalComponent>;
// Optional content passed at the creation : animation, size, ...
options!: Options | undefined;
constructor(
@GreenFlag31
GreenFlag31 / app.component.html
Last active August 13, 2023 09:52
app component HTML :
<div class="container">
<!-- Add a TemplateRef on ng-template -->
<ng-template #view>
<div class="modal-container">
<div class="title">
<h2>How to create component dynamically in Angular ?</h2>
</div>
<div class="content">
<p class="current">
This is the first method. First method is to use a ViewContainerRef
@GreenFlag31
GreenFlag31 / modal.component.html
Last active August 11, 2023 18:32
HTML of modal component
<div class="modal-wrapper">
<section class="modal" #modal>
<!-- Content will be projected here -->
<ng-content></ng-content>
</section>
<div class="modal-overlay" #overlay (click)="onClose()"></div>
</div>
@GreenFlag31
GreenFlag31 / modal.component.css
Created August 11, 2023 18:25
Modal component CSS
.modal-overlay {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: rgba(0, 0, 0, 0.4);
z-index: 1;
}
.modal {