Skip to content

Instantly share code, notes, and snippets.

@deebloo
Last active October 8, 2016 15:53
Show Gist options
  • Save deebloo/ea7e900ae3e1d8b7674ad99821b878ca to your computer and use it in GitHub Desktop.
Save deebloo/ea7e900ae3e1d8b7674ad99821b878ca to your computer and use it in GitHub Desktop.
Simple Modals with Angular2
import { Component, Input } from '@angular/core';
@Component({
selector: 'sweet-modal',
styleUrls: ['sweet-modal.component.css']
templateUrl: 'sweet-modal.component.html'
})
export class LsModalComponent {
@Input() width: string = '400px';
display: false;
open() {
this.display = true;
}
close(val = null) {
this.display = false;
}
}
<div class="overlay" *ngIf="display"></div>
<div *ngIf="display" [style.width]="width" class="modal-container">
<div class="modal-header">
<ng-content select=".sweet-modal-header"></ng-content>
</div>
<div class="modal-body">
<ng-content select=".sweet-modal-body"></ng-content>
</div>
<div class="modal-footer">
<ng-content select=".sweet-modal-footer"></ng-content>
</div>
</div>
@keyframes fadeIn {
0% {
opacity: 0;
transform: translate(-50%, -10%);
}
100% {
opacity: 1;
transform: translate(-50%, 0);
}
}
.overlay {
position: fixed;
display: block;
content: '';
background: #000;
top: 0;
left: 0;
right: 0;
bottom: 0;
opacity: .4;
z-index: 1000;
}
.modal-card {
animation-name: fadeIn;
animation-duration: .2s;
position: fixed;
left: 50%;
top: 10%;
transform: translate(-50%, 0);
z-index: 1001;
}
<!-- Button to open modal -->
<button (click)="modal.open()">Open Modal</button>
<!-- Define the modal and grab reference -->
<sweet-modal width="400px" #modal>
<section class=".sweet-modal-header">The Header</section>
<section class=".sweet-modal-body">The Body</section>
<section class=".sweet-modal-header">
<button (click)="modal.close()">Close</button>
</section>
</sweet-modal>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment