How to use scrollStrategy in MatDialog?

79,102

Solution 1

If you want to scroll the content of the dialog then you have to use the <mat-dialog-content> tag, or use the directive mat-dialog-content in your div element. In your example try the following instead:

<h1 mat-dialog-title>Hi {{data.name}}</h1>
<mat-dialog-content> <!-- instead of your <div>  or use <div mat-dialog-content> -->
  <p>What's your favorite animal!!!!!!!</p>
  <p>What's your favorite animal?</p>
  <p>What's your favorite animal?</p>
  <p>What's your favorite animal?</p>
  <p>What's your favorite animal?</p>
  <p>What's your favorite animal?</p>
  <p>What's your favorite animal?</p>
  <p>What's your favorite animal?</p>
  <p>What's your favorite animal?</p>
  <p>What's your favorite animal?</p>
  <p>What's your favorite animal?</p>
  <p>What's your favorite animal?</p>
  <p>What's your favorite animal?</p>
  <p>What's your favorite animal?</p>
  <p>What's your favorite animal?</p>
  <p>What's your favorite animal?</p>
  <p>What's your favorite animal?</p>
  <p>What's your favorite animal?</p>
  <p>What's your favorite animal?</p>
  <p>What's your favorite animal?</p>
  <p>What's your favorite animal?</p>
  <p>What's your favorite animal?</p>
  <p>What's your favorite animal?</p>
  <p>What's your favorite animal?</p>
  <p>What's your favorite animal?</p>
  <p>What's your favorite animal!!!!!!</p>
  <mat-form-field>
    <input matInput [(ngModel)]="data.animal">
  </mat-form-field>
</mat-dialog-content> <!-- instead of your </div> -->
<div mat-dialog-actions>
  <button mat-button (click)="onNoClick()">No Thanks</button>
  <button mat-button [mat-dialog-close]="data.animal" cdkFocusInitial>Ok</button>
</div>

And now your dialog content should have a scroll on the side. Read more about the Scrollable content container of a dialog on:

https://material.angular.io/components/dialog/api#MatDialogContent

Solution 2

I tried this way,

const dialogRef = this.dialog.open(LoginModalComponent, {
      autoFocus: false,
      maxHeight: '90vh' //you can adjust the value as per your view
});

Solution 3

Hi try to put this on your style.css or style.scss

.cdk-global-overlay-wrapper {
  display: flex;
  position: absolute;
  z-index: 1000;
  overflow: auto;
  pointer-events: auto;  
}

Solution 4

Since https://github.com/angular/material2/pull/11235, .mat-dialog-container got max-height: inherit which should solve your problem.

Setting maxHeight: window.innerHeight + 'px' on the dialog configuration prevents the dialog from growing bigger than the screen.

Solution 5

I was looking for a similar solution as OP. The trick in the provided example is to set overflow:auto on the .cdk-global-overlay-wrapper.

Add this css to your styles (not inside the dialog's css):

Altough there's a slight issue, without auto pointer-events scrolling does not work on current chrome version and with auto pointer-events, closing via backdrop click does not work. (will update if i find a solution)

.cdk-global-overlay-wrapper {
  overflow: auto;
  pointer-events: auto;  // needed for chrome but prevents backdrop click close
}

If you want to prevent scrolling on the back scenery i.e. parent component you could set overflow: hidden on the parent as long as the dialog is open.

.. just remove the scroll strategy from the example.

Share:
79,102
constantant
Author by

constantant

I am a web developer with more than ten-year-experience

Updated on July 09, 2022

Comments

  • constantant
    constantant almost 2 years

    I tried to make a scroll for a dialog in reposition strategy, but it doesn't work for me.

    const scrollStrategy = this.overlay.scrollStrategies.reposition();
    const dialogRef = this.dialog.open( DialogOverviewExampleDialog, { scrollStrategy } );
    

    The full example

    I expect that during scrolling the whole dialog(element .cdk-overlay-pane) will move

    Almost right behavior