Skip to content

Commit

Permalink
feat(dialog): allow for single dialog scroll strategy to be overwritten
Browse files Browse the repository at this point in the history
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 angular#8706.
  • Loading branch information
crisbeto committed Dec 2, 2017
1 parent 5210b3e commit f0720e9
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 4 deletions.
4 changes: 4 additions & 0 deletions src/lib/dialog/dialog-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import {ViewContainerRef} from '@angular/core';
import {Direction} from '@angular/cdk/bidi';
import {ScrollStrategy} from '@angular/cdk/overlay';

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

/** Scroll strategy to be used for the dialog. */
scrollStrategy?: ScrollStrategy;

// TODO(jelbourn): add configuration for lifecycle hooks, ARIA labelling.
}
13 changes: 12 additions & 1 deletion src/lib/dialog/dialog.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import {Location} from '@angular/common';
import {SpyLocation} from '@angular/common/testing';
import {Directionality} from '@angular/cdk/bidi';
import {MatDialogContainer} from './dialog-container';
import {OverlayContainer} from '@angular/cdk/overlay';
import {OverlayContainer, ScrollStrategy} from '@angular/cdk/overlay';
import {A, ESCAPE} from '@angular/cdk/keycodes';
import {dispatchKeyboardEvent} from '@angular/cdk/testing';
import {MAT_DIALOG_DATA, MatDialog, MatDialogModule, MatDialogRef} from './index';
Expand Down Expand Up @@ -599,6 +599,17 @@ describe('MatDialog', () => {
expect(spy).toHaveBeenCalled();
}));

it('should be able to attach a custom scroll strategy', fakeAsync(() => {
const scrollStrategy: ScrollStrategy = {
attach: () => {},
enable: jasmine.createSpy('scroll strategy enable spy'),
disable: () => {}
};

dialog.open(PizzaMsg, {scrollStrategy});
expect(scrollStrategy.enable).toHaveBeenCalled();
}));

describe('passing in data', () => {
it('should be able to pass in data', () => {
let config = {
Expand Down
5 changes: 2 additions & 3 deletions src/lib/dialog/dialog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import {Directionality} from '@angular/cdk/bidi';
import {ESCAPE} from '@angular/cdk/keycodes';
import {
BlockScrollStrategy,
Overlay,
OverlayConfig,
OverlayRef,
Expand Down Expand Up @@ -47,7 +46,7 @@ export const MAT_DIALOG_SCROLL_STRATEGY =

/** @docs-private */
export function MAT_DIALOG_SCROLL_STRATEGY_PROVIDER_FACTORY(overlay: Overlay):
() => BlockScrollStrategy {
() => ScrollStrategy {
return () => overlay.scrollStrategies.block();
}

Expand Down Expand Up @@ -175,7 +174,7 @@ export class MatDialog {
private _getOverlayConfig(dialogConfig: MatDialogConfig): OverlayConfig {
const state = new OverlayConfig({
positionStrategy: this._overlay.position().global(),
scrollStrategy: this._scrollStrategy(),
scrollStrategy: dialogConfig.scrollStrategy || this._scrollStrategy(),
panelClass: dialogConfig.panelClass,
hasBackdrop: dialogConfig.hasBackdrop,
direction: dialogConfig.direction,
Expand Down

0 comments on commit f0720e9

Please sign in to comment.