Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom parent container for Angular material overlay container?

Angular material creates overlay containers for various components such as their menu, snackbar and dialog components.

How can I, in an easy way, decide which element a cdk-overlay-container should be appended to?

Currently, it's appended to the body element. So if I trigger full screen mode for any other element than the body element, it won't be seen. Which of course is not what I want.

like image 750
Fredrik_Borgstrom Avatar asked May 20 '26 07:05

Fredrik_Borgstrom


1 Answers

Basically create a class that extends OverlayContainer. Override the getContainerElement method where you return your HTML element which should be appended with the overlay. If needed, You can also override _createContainer method, where you do your own logic to create the element.

Finally provide your CustomOverlayContainer class as Token for OverlayContainer like this:

@NgModule({
    providers: [{provide: OverlayContainer, 
                 useClass: CustomOverlayContainer}],
    // ...
})
export class MyModule { }

Just have a look at the origin file. It's quite easy: OverlayContainer

like image 106
Flash Avatar answered May 22 '26 10:05

Flash



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!