Skip to content

Commit 29be96f

Browse files
committed
feat(dialog): allow for single dialog scroll strategy to be overwritten
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 b4ce5ec commit 29be96f

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 {MAT_DIALOG_DATA, MatDialog, MatDialogModule, MatDialogRef} from './index';
@@ -592,6 +592,17 @@ describe('MatDialog', () => {
592592
expect(spy).toHaveBeenCalled();
593593
}));
594594

595+
it('should be able to attach a custom scroll strategy', fakeAsync(() => {
596+
const scrollStrategy: ScrollStrategy = {
597+
attach: () => {},
598+
enable: jasmine.createSpy('scroll strategy enable spy'),
599+
disable: () => {}
600+
};
601+
602+
dialog.open(PizzaMsg, {scrollStrategy});
603+
expect(scrollStrategy.enable).toHaveBeenCalled();
604+
}));
605+
595606
describe('passing in data', () => {
596607
it('should be able to pass in data', () => {
597608
let config = {

src/lib/dialog/dialog.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
import {Directionality} from '@angular/cdk/bidi';
1010
import {ESCAPE} from '@angular/cdk/keycodes';
1111
import {
12-
BlockScrollStrategy,
1312
Overlay,
1413
OverlayConfig,
1514
OverlayRef,
@@ -47,7 +46,7 @@ export const MAT_DIALOG_SCROLL_STRATEGY =
4746

4847
/** @docs-private */
4948
export function MAT_DIALOG_SCROLL_STRATEGY_PROVIDER_FACTORY(overlay: Overlay):
50-
() => BlockScrollStrategy {
49+
() => ScrollStrategy {
5150
return () => overlay.scrollStrategies.block();
5251
}
5352

@@ -175,7 +174,7 @@ export class MatDialog {
175174
private _getOverlayConfig(dialogConfig: MatDialogConfig): OverlayConfig {
176175
const state = new OverlayConfig({
177176
positionStrategy: this._overlay.position().global(),
178-
scrollStrategy: this._scrollStrategy(),
177+
scrollStrategy: dialogConfig.scrollStrategy || this._scrollStrategy(),
179178
panelClass: dialogConfig.panelClass,
180179
hasBackdrop: dialogConfig.hasBackdrop,
181180
direction: dialogConfig.direction,

0 commit comments

Comments
 (0)