Last active
November 11, 2020 09:11
-
-
Save talamaska/ae5572d58d0e5cebcb32b23964ddb464 to your computer and use it in GitHub Desktop.
template modals
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface CustomOverlayConfig { | |
hasBackdropClick?: boolean; | |
isCentered?: boolean; | |
size?: any; | |
top?: string; | |
} | |
const DEFAULT_CUSTOM_CONFIG: CustomOverlayConfig = { | |
hasBackdropClick: false, | |
isCentered: true, | |
size: null | |
}; | |
@Injectable() | |
export class TemplateModalService { | |
public dialogRef: TemplateModalOverlayRef; | |
public customConfig: CustomOverlayConfig; | |
constructor( | |
private injector: Injector, | |
private overlay: Overlay | |
) { } | |
open(templateRef: CdkPortal, config: FormModalConfig = {}, customConfig: CustomOverlayConfig = {}) {...} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public dialogRef: TemplateModalOverlayRef; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@ViewChild('ratingModal') ratingModal: TemplatePortal<any>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public closeModal() { | |
this.dialogRef.close(); | |
this.modalData.next({}); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<ng-template cdkPortal #ratingModal="cdkPortal"> | |
<rating-modal [modalData]="modalData | async" (close)="closeModal()" (ratings)="updateRating($event)" (rate)="rateEvent($event)"></rating-modal> | |
</ng-template> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
this.dialogRef = this.modalService.open(this.ratingModal, {}, { | |
isCentered: false | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { OverlayRef } from '@angular/cdk/overlay'; | |
export class TemplateModalOverlayRef { | |
constructor(private overlayRef: OverlayRef) { } | |
close(): void { | |
this.overlayRef.dispose(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private attachModalContainer( | |
overlayRef: OverlayRef, | |
config: FormModalConfig, | |
dialogRef: TemplateModalOverlayRef, | |
templateRef: CdkPortal // this was added | |
) { | |
const containerRef = overlayRef.attach(templateRef); // this was changed | |
return containerRef; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private getOverlayConfig(config: FormModalConfig): OverlayConfig { | |
let positionStrategy = this.overlay.position() | |
.global(); | |
if (this.customConfig.isCentered) { | |
positionStrategy = positionStrategy | |
.centerHorizontally() | |
.centerVertically(); | |
} | |
if (this.customConfig.size) { | |
positionStrategy = positionStrategy | |
.width(this.customConfig.size.width) | |
.height(this.customConfig.size.height); | |
} | |
if (this.customConfig.top) { | |
positionStrategy = positionStrategy | |
.top(this.customConfig.top) | |
.centerHorizontally(); | |
} |
I'm also getting error 'there is no directive with exportAs set to cdkPortal'. I've made all of the adjustments, and I'm importing PortalModule. Was this every resolved?
I'm also getting error 'there is no directive with exportAs set to cdkPortal'. I've made all of the adjustments, and I'm importing PortalModule. Was this every resolved?
Gotta love it - a minute after I submit this, I found my error. I was not, in fact, importing Portal Module in the correct dependent component module.
Anyway, thanks for the great tutorial!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@capaneus
the remote controlling of the template modal is performed via the ViewChild ratingModal