Created
March 5, 2019 09:47
-
-
Save jpzwarte/5b6f848a8cbc1488779181894382f306 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
/** | |
* Sets the appropriate positions on a position strategy | |
* so the overlay connects with the trigger correctly. | |
* @param positionStrategy Strategy whose position to update. | |
*/ | |
private setPosition(positionStrategy: FlexibleConnectedPositionStrategy) { | |
let positions: ConnectedPosition[]; | |
if (this.triggersSubmenu()) { | |
// When the menu is a sub-menu, it should always align itself | |
// to the edges of the trigger, instead of overlapping it. | |
positions = convertMenuPositionToConnectedPositions(['right', 'top'], -MENU_PANEL_TOP_PADDING); | |
} else { | |
positions = convertMenuPositionToConnectedPositions(this.menu.position); | |
} | |
positionStrategy.withPositions(positions); | |
} |
Hi, this looks great, can you tell us how to use this? The trigger directive doesn't look like a regular angular directive, it's not clear how we should be getting the reference to the FlexibleConnectedPositionStrategy?
@chriszrc I don't know if you still need the answer, but below is the way I used it if other people need it:
@ViewChildren(MatMenuTrigger)
menuTriggers: QueryList<MatMenuTrigger>;
constructor(
private ngZone: NgZone
) { }
ngAfterViewInit(): void {
this.menuTriggers.forEach(item => {
item.menuOpened.pipe(
// delay(1000)
).subscribe(() => {
this.menuOpened = item;
this.ngZone.onStable.pipe(first()).subscribe(() => {
this.recalculateMenu(item)
})
});
});
}
recalculateMenu(menuTrigger: MatMenuTrigger) {
const overlayRef: OverlayRef = (menuTrigger as any)._overlayRef;
const overlayConfig = overlayRef.getConfig();
(overlayConfig.positionStrategy as any)._isInitialRender = true;
this.setPosition(menuTrigger, overlayConfig.positionStrategy as FlexibleConnectedPositionStrategy);
overlayConfig.positionStrategy.apply();
}
setPosition(menuTrigger: MatMenuTrigger, positionStrategy: FlexibleConnectedPositionStrategy) {
let positions: ConnectedPosition[];
if (menuTrigger.triggersSubmenu()) {
// When the menu is a sub-menu, it should always align itself
// to the edges of the trigger, instead of overlapping it.
positions = convertMenuPositionToConnectedPositions(["right", "top"], -8);
} else {
positions = convertMenuPositionToConnectedPositions(["right", "top"]);
}
positionStrategy.withPositions(positions);
}
what is this.menuOpened?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
RTL is no requirement for me, so the code doesn't take that into account.