Skip to content

Commit

Permalink
feat(dialog): allow for single dialog scroll strategy to be overwritt…
Browse files Browse the repository at this point in the history
…en (#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.
  • Loading branch information
crisbeto authored and tinayuangao committed Jan 10, 2018
1 parent 4055a4c commit 6cf3f15
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 {
Expand Down Expand Up @@ -605,6 +605,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 @@ -8,7 +8,6 @@

import {Directionality} from '@angular/cdk/bidi';
import {
BlockScrollStrategy,
Overlay,
OverlayConfig,
OverlayRef,
Expand Down Expand Up @@ -49,7 +48,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 @@ -185,7 +184,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 6cf3f15

Please sign in to comment.