Question
How do I configure a material cdk overlay position strategy that works great, on big and small screens?
Goal
My objective is to create an overlay with the Angular overlay CDK, that follow these rules:
- Never position overlay outside the viewport
- Max height of 300 pixels
- Position overlay y with a priority of top, center bottom.
- On screens with a height below 300, use the available height.
- In other words, no minimum height.
What works π
I have accomplished some of the above requirements, as you can see here the positioning works great on devices with enough vertical space (in this case 512px):
The broken part π
However as you can tell from the following gif, this does not work on small devices with insufficient vertical space (in this case 255px). Actually, as you can tell from the GIF, in 2 of the cases it was very close. The position was correct, only the height was off.
Here the goal is to use the available space, as illustrated in red:
The code π€
I have a stack blitz, where you can experiment here.
component
openPopup(element: any) {
const overlayRef = this.overlay.create({
hasBackdrop: true,
positionStrategy: this.overlay.position()
.flexibleConnectedTo(element)
.withLockedPosition()
.withPositions([
{
panelClass: 'custom-panel1',
originX: 'center',
originY: 'center',
overlayX: 'start',
overlayY: 'top',
},
{
panelClass: 'custom-panel2',
originX: 'center',
originY: 'center',
overlayX: 'start',
overlayY: 'center',
},
{
panelClass: 'custom-panel3',
originX: 'center',
originY: 'center',
overlayX: 'start',
overlayY: 'bottom',
},
])
,
width: '200px',
maxHeight: 300,
});
const popupComponentPortal = new ComponentPortal(PopupComponent);
overlayRef.attach(popupComponentPortal);
overlayRef.backdropClick().subscribe(() => {
overlayRef.dispose();
});
}
global styles
.cdk-overlay-pane {
max-height: 300px;
}
Notes
I have been thinking about using the global positioning strategy when the height of the viewport is getting small. However, I would prefer to avoid this scenario, as I would love a solution that could tackle any height of the overlay (respecting the max height off-course).
I recommend using the "Open in New Window LIVE" feature of stackbllitz, when testing the stackblitz. Link is here again.
I would be SO grateful, if you could help solve this issue or point me in the right direction π€·πΎββοΈ