A way to recalculate the position of a mat-menu panel if it changes its height after opened. I don't need to do it automatically, I'm just seeking a way to ask mat-menu to recalculate the position.
Wasn't able to achieve it without dirty tricks (for example triggering a window resize event). Asked in StackOverflow with no luck: https://stackoverflow.com/questions/58375853/angular-material-recalculate-position-of-mat-menu-on-height-change
Steps to reproduce:
I don't think it's a good user experience if suddenly the menu changes it's size and position. I would hate it.
@manklu What'd you do then if you need to load data to populate the panel? We change its height because we show a spinner while loading it.
I would show a spinner (if the data is loaded via network) and delay the display of the menu until the data has been loaded.
We should be able to expose a method that tells the menu to reposition itself.
Made a global search for the resize event handlers, and found one of them in flexible-connected-position-strategy.ts, which should be the default position strategy with mat-menu.
We could manually run the code block inside the resize event handler. However, the code mostly touches on the private properties, and hence we will need to forcibly bypass the TypeScript checking with as any.
function recalculateMenu(menuTrigger: MatMenuTrigger) {
// get the OverlayRef from MatMenuTrigger
const overlayRef: OverlayRef = (menuTrigger as any)._overlayRef;
// get the FlexibleConnectedPositionStrategy from OverlayRef
const overlayConfig = overlayRef.getConfig();
// repeat what was done in the resize event handler
(overlayConfig.positionStrategy as any)._isInitialRender = true;
overlayConfig.positionStrategy.apply();
}
And with that in mind, here is the revised StackBlitz.
https://stackblitz.com/edit/angular-lorwfh-sfgdnm?file=app%2Fmenu-overview-example.ts
I would consider this a dirty trick as well, but at least it is a temporary solution.