Skip to content

Commit 30b90a2

Browse files
crisbetoandrewseguin
authored andcommitted
fix(select): lock dropdown position when scrolling (#9789)
This was the initial use-case for introducing the locked positioning to the `ConnectedPositionStrategy`. Since `mat-select` has some special positioning requirements, it would look weird if we kept repositioning it as the user scrolls. These changes add an input `CdkConnectedOverlay` that allows for the position to be locked and locks it for `mat-select`.
1 parent ee582bd commit 30b90a2

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

src/cdk/overlay/overlay-directives.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ export class CdkConnectedOverlay implements OnDestroy, OnChanges {
9797
private _overlayRef: OverlayRef;
9898
private _templatePortal: TemplatePortal;
9999
private _hasBackdrop = false;
100+
private _lockPosition = false;
100101
private _backdropSubscription = Subscription.EMPTY;
101102
private _offsetX: number = 0;
102103
private _offsetY: number = 0;
@@ -155,6 +156,11 @@ export class CdkConnectedOverlay implements OnDestroy, OnChanges {
155156
get hasBackdrop() { return this._hasBackdrop; }
156157
set hasBackdrop(value: any) { this._hasBackdrop = coerceBooleanProperty(value); }
157158

159+
/** Whether or not the overlay should be locked when scrolling. */
160+
@Input('cdkConnectedOverlayLockPosition')
161+
get lockPosition() { return this._lockPosition; }
162+
set lockPosition(value: any) { this._lockPosition = coerceBooleanProperty(value); }
163+
158164
/**
159165
* @deprecated
160166
* @deletion-target 6.0.0
@@ -296,6 +302,10 @@ export class CdkConnectedOverlay implements OnDestroy, OnChanges {
296302
this._position.withPositions(this.positions);
297303
}
298304

305+
if (changes['lockPosition']) {
306+
this._position.withLockedPosition(this.lockPosition);
307+
}
308+
299309
if (changes['origin'] || changes['_deprecatedOrigin']) {
300310
this._position.setOrigin(this.origin.elementRef);
301311

@@ -359,7 +369,8 @@ export class CdkConnectedOverlay implements OnDestroy, OnChanges {
359369
const strategy = this._overlay.position()
360370
.connectedTo(this.origin.elementRef, originPoint, overlayPoint)
361371
.withOffsetX(this.offsetX)
362-
.withOffsetY(this.offsetY);
372+
.withOffsetY(this.offsetY)
373+
.withLockedPosition(this.lockPosition);
363374

364375
for (let i = 1; i < this.positions.length; i++) {
365376
strategy.withFallbackPosition(

src/lib/select/select.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
<ng-template
1919
cdk-connected-overlay
2020
hasBackdrop
21+
cdkConnectedOverlayLockPosition
2122
backdropClass="cdk-overlay-transparent-backdrop"
2223
[scrollStrategy]="_scrollStrategy"
2324
[origin]="origin"

0 commit comments

Comments
 (0)