Skip to content

Commit 4d0d080

Browse files
crisbetojosephperrott
authored andcommitted
feat(bottom-sheet): allow for scroll strategy to be configured (#15535)
Allows for the scroll strategy of a bottom sheet to be changed. Fixes #15533.
1 parent c979484 commit 4d0d080

File tree

4 files changed

+24
-10
lines changed

4 files changed

+24
-10
lines changed

src/material/bottom-sheet/bottom-sheet-config.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9-
import {ViewContainerRef, InjectionToken} from '@angular/core';
109
import {Direction} from '@angular/cdk/bidi';
10+
import {ScrollStrategy} from '@angular/cdk/overlay';
11+
import {InjectionToken, ViewContainerRef} from '@angular/core';
1112

1213
/** Injection token that can be used to access the data that was passed in to a bottom sheet. */
1314
export const MAT_BOTTOM_SHEET_DATA = new InjectionToken<any>('MatBottomSheetData');
@@ -58,4 +59,7 @@ export class MatBottomSheetConfig<D = any> {
5859
* previously-focused element, after it's closed.
5960
*/
6061
restoreFocus?: boolean = true;
62+
63+
/** Scroll strategy to be used for the bottom sheet. */
64+
scrollStrategy?: ScrollStrategy;
6165
}

src/material/bottom-sheet/bottom-sheet.spec.ts

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import {Directionality} from '@angular/cdk/bidi';
22
import {A, ESCAPE} from '@angular/cdk/keycodes';
3-
import {OverlayContainer} from '@angular/cdk/overlay';
3+
import {OverlayContainer, ScrollStrategy} from '@angular/cdk/overlay';
44
import {ViewportRuler} from '@angular/cdk/scrolling';
55
import {dispatchKeyboardEvent} from '@angular/cdk/testing';
6+
import {Location} from '@angular/common';
7+
import {SpyLocation} from '@angular/common/testing';
68
import {
79
Component,
810
Directive,
@@ -22,10 +24,9 @@ import {
2224
TestBed,
2325
tick,
2426
} from '@angular/core/testing';
25-
import {Location} from '@angular/common';
26-
import {SpyLocation} from '@angular/common/testing';
2727
import {NoopAnimationsModule} from '@angular/platform-browser/animations';
28-
import {MatBottomSheet, MAT_BOTTOM_SHEET_DEFAULT_OPTIONS} from './bottom-sheet';
28+
29+
import {MAT_BOTTOM_SHEET_DEFAULT_OPTIONS, MatBottomSheet} from './bottom-sheet';
2930
import {MAT_BOTTOM_SHEET_DATA, MatBottomSheetConfig} from './bottom-sheet-config';
3031
import {MatBottomSheetModule} from './bottom-sheet-module';
3132
import {MatBottomSheetRef} from './bottom-sheet-ref';
@@ -405,6 +406,17 @@ describe('MatBottomSheet', () => {
405406
expect(overlayContainerElement.querySelector('mat-bottom-sheet-container')).toBeTruthy();
406407
}));
407408

409+
it('should be able to attach a custom scroll strategy', fakeAsync(() => {
410+
const scrollStrategy: ScrollStrategy = {
411+
attach: () => {},
412+
enable: jasmine.createSpy('scroll strategy enable spy'),
413+
disable: () => {}
414+
};
415+
416+
bottomSheet.open(PizzaMsg, {scrollStrategy});
417+
expect(scrollStrategy.enable).toHaveBeenCalled();
418+
}));
419+
408420
describe('passing in data', () => {
409421
it('should be able to pass in data', () => {
410422
const config = {

src/material/bottom-sheet/bottom-sheet.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -152,11 +152,8 @@ export class MatBottomSheet implements OnDestroy {
152152
hasBackdrop: config.hasBackdrop,
153153
disposeOnNavigation: config.closeOnNavigation,
154154
maxWidth: '100%',
155-
scrollStrategy: this._overlay.scrollStrategies.block(),
156-
positionStrategy: this._overlay.position()
157-
.global()
158-
.centerHorizontally()
159-
.bottom('0')
155+
scrollStrategy: config.scrollStrategy || this._overlay.scrollStrategies.block(),
156+
positionStrategy: this._overlay.position().global().centerHorizontally().bottom('0')
160157
});
161158

162159
if (config.backdropClass) {

tools/public_api_guard/material/bottom-sheet.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ export declare class MatBottomSheetConfig<D = any> {
2626
hasBackdrop?: boolean;
2727
panelClass?: string | string[];
2828
restoreFocus?: boolean;
29+
scrollStrategy?: ScrollStrategy;
2930
viewContainerRef?: ViewContainerRef;
3031
}
3132

0 commit comments

Comments
 (0)