Skip to content

Commit 6cf3f15

Browse files
crisbetotinayuangao
authored andcommitted
feat(dialog): allow for single dialog scroll strategy to be overwritten (#8726)
Currently we have the `MAT_DIALOG_SCROLL_STRATEGY` token which allows consumers to specify the scroll strategy for all dialogs, however there's no way do so for a single dialog. These changes add an extra property to the dialog config that allow for the scroll strategy to be set. Fixes #8706.
1 parent 4055a4c commit 6cf3f15

File tree

3 files changed

+18
-4
lines changed

3 files changed

+18
-4
lines changed

src/lib/dialog/dialog-config.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
import {ViewContainerRef} from '@angular/core';
1010
import {Direction} from '@angular/cdk/bidi';
11+
import {ScrollStrategy} from '@angular/cdk/overlay';
1112

1213
/** Valid ARIA roles for a dialog element. */
1314
export type DialogRole = 'dialog' | 'alertdialog';
@@ -94,5 +95,8 @@ export class MatDialogConfig<D = any> {
9495
/** Whether the dialog should focus the first focusable element on open. */
9596
autoFocus?: boolean = true;
9697

98+
/** Scroll strategy to be used for the dialog. */
99+
scrollStrategy?: ScrollStrategy;
100+
97101
// TODO(jelbourn): add configuration for lifecycle hooks, ARIA labelling.
98102
}

src/lib/dialog/dialog.spec.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import {Location} from '@angular/common';
2424
import {SpyLocation} from '@angular/common/testing';
2525
import {Directionality} from '@angular/cdk/bidi';
2626
import {MatDialogContainer} from './dialog-container';
27-
import {OverlayContainer} from '@angular/cdk/overlay';
27+
import {OverlayContainer, ScrollStrategy} from '@angular/cdk/overlay';
2828
import {A, ESCAPE} from '@angular/cdk/keycodes';
2929
import {dispatchKeyboardEvent} from '@angular/cdk/testing';
3030
import {
@@ -605,6 +605,17 @@ describe('MatDialog', () => {
605605
expect(spy).toHaveBeenCalled();
606606
}));
607607

608+
it('should be able to attach a custom scroll strategy', fakeAsync(() => {
609+
const scrollStrategy: ScrollStrategy = {
610+
attach: () => {},
611+
enable: jasmine.createSpy('scroll strategy enable spy'),
612+
disable: () => {}
613+
};
614+
615+
dialog.open(PizzaMsg, {scrollStrategy});
616+
expect(scrollStrategy.enable).toHaveBeenCalled();
617+
}));
618+
608619
describe('passing in data', () => {
609620
it('should be able to pass in data', () => {
610621
let config = {

src/lib/dialog/dialog.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99
import {Directionality} from '@angular/cdk/bidi';
1010
import {
11-
BlockScrollStrategy,
1211
Overlay,
1312
OverlayConfig,
1413
OverlayRef,
@@ -49,7 +48,7 @@ export const MAT_DIALOG_SCROLL_STRATEGY =
4948

5049
/** @docs-private */
5150
export function MAT_DIALOG_SCROLL_STRATEGY_PROVIDER_FACTORY(overlay: Overlay):
52-
() => BlockScrollStrategy {
51+
() => ScrollStrategy {
5352
return () => overlay.scrollStrategies.block();
5453
}
5554

@@ -185,7 +184,7 @@ export class MatDialog {
185184
private _getOverlayConfig(dialogConfig: MatDialogConfig): OverlayConfig {
186185
const state = new OverlayConfig({
187186
positionStrategy: this._overlay.position().global(),
188-
scrollStrategy: this._scrollStrategy(),
187+
scrollStrategy: dialogConfig.scrollStrategy || this._scrollStrategy(),
189188
panelClass: dialogConfig.panelClass,
190189
hasBackdrop: dialogConfig.hasBackdrop,
191190
direction: dialogConfig.direction,

0 commit comments

Comments
 (0)