Skip to content

Instantly share code, notes, and snippets.

@serjKim
Last active January 5, 2021 19:01
Show Gist options
  • Save serjKim/1771bd324bf783f072919a6ec79efff7 to your computer and use it in GitHub Desktop.
Save serjKim/1771bd324bf783f072919a6ec79efff7 to your computer and use it in GitHub Desktop.
cdkPortal: popup
<ng-template cdkPortal>
<div class="popup">
<div class="arrow"></div>
<ng-content></ng-content>
</div>
</ng-template>
@use 'variables' as *;
.popup {
width: 280px;
height: 224px;
background-color: white;
transform: translateY(100%);
border: 1px solid $graphical-divider-gray-color;
border-radius: 4px;
box-shadow: 0px 2px 6px rgba(0, 0, 0, 0.2);
.arrow {
width: 100px;
height: 25px;
position: absolute;
bottom: 100%;
overflow: hidden;
&::after {
content: "";
position: absolute;
width: 6px;
height: 20px;
background: white;
transform: translateY(100%) rotate(45deg);
left: 20px;
box-shadow: 1px 1px 3px 2px rgb(0 0 0 / 15%);
}
}
}
class PopupComponent {
@Input()
public offsetX = -16;
@Input()
public offsetY = 18;
@ViewChild(CdkPortal)
private templatePortal: CdkPortal;
private overlayRef: OverlayRef | undefined;
@ViewChild(CdkPortal)
private templatePortal: CdkPortal;
public showPopup(e: MouseEvent): void {
e.stopPropagation();
const positionStrategy = this.overlay
.position()
.flexibleConnectedTo(this.hostElement)
.withPositions([
{
originX: 'start',
originY: 'top',
overlayX: 'start',
overlayY: 'bottom',
offsetX: this.offsetX,
offsetY: this.offsetY,
},
]);
const overlayConfig = new OverlayConfig({
// hasBackdrop: config.hasBackdrop,
// backdropClass: config.backdropClass,
// panelClass: config.panelClass,
positionStrategy,
});
this.overlayRef = this.overlay.create(overlayConfig);
this.overlayRef.attach(this.templatePortal);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment