Skip to content

Commit

Permalink
fix(select): lock dropdown position when scrolling (#9789)
Browse files Browse the repository at this point in the history
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`.
  • Loading branch information
crisbeto authored and andrewseguin committed Feb 20, 2018
1 parent ee582bd commit 30b90a2
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/cdk/overlay/overlay-directives.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ export class CdkConnectedOverlay implements OnDestroy, OnChanges {
private _overlayRef: OverlayRef;
private _templatePortal: TemplatePortal;
private _hasBackdrop = false;
private _lockPosition = false;
private _backdropSubscription = Subscription.EMPTY;
private _offsetX: number = 0;
private _offsetY: number = 0;
Expand Down Expand Up @@ -155,6 +156,11 @@ export class CdkConnectedOverlay implements OnDestroy, OnChanges {
get hasBackdrop() { return this._hasBackdrop; }
set hasBackdrop(value: any) { this._hasBackdrop = coerceBooleanProperty(value); }

/** Whether or not the overlay should be locked when scrolling. */
@Input('cdkConnectedOverlayLockPosition')
get lockPosition() { return this._lockPosition; }
set lockPosition(value: any) { this._lockPosition = coerceBooleanProperty(value); }

/**
* @deprecated
* @deletion-target 6.0.0
Expand Down Expand Up @@ -296,6 +302,10 @@ export class CdkConnectedOverlay implements OnDestroy, OnChanges {
this._position.withPositions(this.positions);
}

if (changes['lockPosition']) {
this._position.withLockedPosition(this.lockPosition);
}

if (changes['origin'] || changes['_deprecatedOrigin']) {
this._position.setOrigin(this.origin.elementRef);

Expand Down Expand Up @@ -359,7 +369,8 @@ export class CdkConnectedOverlay implements OnDestroy, OnChanges {
const strategy = this._overlay.position()
.connectedTo(this.origin.elementRef, originPoint, overlayPoint)
.withOffsetX(this.offsetX)
.withOffsetY(this.offsetY);
.withOffsetY(this.offsetY)
.withLockedPosition(this.lockPosition);

for (let i = 1; i < this.positions.length; i++) {
strategy.withFallbackPosition(
Expand Down
1 change: 1 addition & 0 deletions src/lib/select/select.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
<ng-template
cdk-connected-overlay
hasBackdrop
cdkConnectedOverlayLockPosition
backdropClass="cdk-overlay-transparent-backdrop"
[scrollStrategy]="_scrollStrategy"
[origin]="origin"
Expand Down

0 comments on commit 30b90a2

Please sign in to comment.