Skip to content

Commit ef0229c

Browse files
crisbetojelbourn
authored andcommitted
feat(overlay): expose flexible overlay features through CdkConnectedOverlay (#11069)
Exposes all of the new flexible positioning features through the `CdkConnectedOverlay` directive. They're still all set to be opt-in for now, in order to avoid breaking changes.
1 parent 61dd937 commit ef0229c

File tree

3 files changed

+72
-9
lines changed

3 files changed

+72
-9
lines changed

src/cdk/overlay/overlay-directives.spec.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,42 @@ describe('Overlay directives', () => {
359359
expect(Math.floor(overlayRect.left)).toBe(Math.floor(triggerRect.left) + 20);
360360
});
361361

362+
it('should be able to set the viewport margin', () => {
363+
expect(fixture.componentInstance.connectedOverlayDirective.viewportMargin).not.toBe(10);
364+
365+
fixture.componentInstance.viewportMargin = 10;
366+
fixture.detectChanges();
367+
368+
expect(fixture.componentInstance.connectedOverlayDirective.viewportMargin).toBe(10);
369+
});
370+
371+
it('should allow for flexible positioning to be enabled', () => {
372+
expect(fixture.componentInstance.connectedOverlayDirective.flexibleDiemsions).not.toBe(true);
373+
374+
fixture.componentInstance.flexibleDimensions = true;
375+
fixture.detectChanges();
376+
377+
expect(fixture.componentInstance.connectedOverlayDirective.flexibleDiemsions).toBe(true);
378+
});
379+
380+
it('should allow for growing after open to be enabled', () => {
381+
expect(fixture.componentInstance.connectedOverlayDirective.growAfterOpen).not.toBe(true);
382+
383+
fixture.componentInstance.growAfterOpen = true;
384+
fixture.detectChanges();
385+
386+
expect(fixture.componentInstance.connectedOverlayDirective.growAfterOpen).toBe(true);
387+
});
388+
389+
it('should allow for pushing to be enabled', () => {
390+
expect(fixture.componentInstance.connectedOverlayDirective.push).not.toBe(true);
391+
392+
fixture.componentInstance.push = true;
393+
fixture.detectChanges();
394+
395+
expect(fixture.componentInstance.connectedOverlayDirective.push).toBe(true);
396+
});
397+
362398
});
363399

364400
describe('outputs', () => {
@@ -421,6 +457,10 @@ describe('Overlay directives', () => {
421457
[cdkConnectedOverlayHeight]="height"
422458
[cdkConnectedOverlayOrigin]="triggerOverride || trigger"
423459
[cdkConnectedOverlayHasBackdrop]="hasBackdrop"
460+
[cdkConnectedOverlayViewportMargin]="viewportMargin"
461+
[cdkConnectedOverlayFlexibleDimensions]="flexibleDimensions"
462+
[cdkConnectedOverlayGrowAfterOpen]="growAfterOpen"
463+
[cdkConnectedOverlayPush]="push"
424464
cdkConnectedOverlayBackdropClass="mat-test-class"
425465
(backdropClick)="backdropClickHandler($event)"
426466
[cdkConnectedOverlayOffsetX]="offsetX"
@@ -448,6 +488,10 @@ class ConnectedOverlayDirectiveTest {
448488
offsetY: number;
449489
triggerOverride: CdkOverlayOrigin;
450490
hasBackdrop: boolean;
491+
viewportMargin: number;
492+
flexibleDimensions: boolean;
493+
growAfterOpen: boolean;
494+
push: boolean;
451495
backdropClickHandler = jasmine.createSpy('backdropClick handler');
452496
positionChangeHandler = jasmine.createSpy('positionChangeHandler');
453497
positionOverrides: ConnectionPositionPair[];

src/cdk/overlay/overlay-directives.ts

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,8 @@ export class CdkOverlayOrigin {
106106

107107

108108
/**
109-
* Directive to facilitate declarative creation of an Overlay using a ConnectedPositionStrategy.
109+
* Directive to facilitate declarative creation of an
110+
* Overlay using a FlexibleConnectedPositionStrategy.
110111
*/
111112
@Directive({
112113
selector: '[cdk-connected-overlay], [connected-overlay], [cdkConnectedOverlay]',
@@ -117,6 +118,9 @@ export class CdkConnectedOverlay implements OnDestroy, OnChanges {
117118
private _templatePortal: TemplatePortal;
118119
private _hasBackdrop = false;
119120
private _lockPosition = false;
121+
private _growAfterOpen = false;
122+
private _flexibleDimensions = false;
123+
private _push = false;
120124
private _backdropSubscription = Subscription.EMPTY;
121125
private _offsetX: number;
122126
private _offsetY: number;
@@ -165,6 +169,9 @@ export class CdkConnectedOverlay implements OnDestroy, OnChanges {
165169
/** The custom class to be set on the backdrop element. */
166170
@Input('cdkConnectedOverlayBackdropClass') backdropClass: string;
167171

172+
/** Margin between the overlay and the viewport edges. */
173+
@Input('cdkConnectedOverlayViewportMargin') viewportMargin: number = 0;
174+
168175
/** Strategy to be used when handling scroll events while the overlay is open. */
169176
@Input('cdkConnectedOverlayScrollStrategy') scrollStrategy: ScrollStrategy =
170177
this._scrollStrategy();
@@ -182,6 +189,21 @@ export class CdkConnectedOverlay implements OnDestroy, OnChanges {
182189
get lockPosition() { return this._lockPosition; }
183190
set lockPosition(value: any) { this._lockPosition = coerceBooleanProperty(value); }
184191

192+
/** Whether the overlay's width and height can be constrained to fit within the viewport. */
193+
@Input('cdkConnectedOverlayFlexibleDimensions')
194+
get flexibleDiemsions() { return this._flexibleDimensions; }
195+
set flexibleDiemsions(value: boolean) { this._flexibleDimensions = coerceBooleanProperty(value); }
196+
197+
/** Whether the overlay can grow after the initial open when flexible positioning is turned on. */
198+
@Input('cdkConnectedOverlayGrowAfterOpen')
199+
get growAfterOpen() { return this._growAfterOpen; }
200+
set growAfterOpen(value: boolean) { this._growAfterOpen = coerceBooleanProperty(value); }
201+
202+
/** Whether the overlay can be pushed on-screen if none of the provided positions fit. */
203+
@Input('cdkConnectedOverlayPush')
204+
get push() { return this._push; }
205+
set push(value: boolean) { this._push = coerceBooleanProperty(value); }
206+
185207
/** Event emitted when the backdrop is clicked. */
186208
@Output() backdropClick = new EventEmitter<MouseEvent>();
187209

@@ -288,13 +310,10 @@ export class CdkConnectedOverlay implements OnDestroy, OnChanges {
288310
private _createPositionStrategy(): FlexibleConnectedPositionStrategy {
289311
const strategy = this._overlay.position()
290312
.flexibleConnectedTo(this.origin.elementRef)
291-
// Turn off all of the flexible positioning features for now to have it behave
292-
// the same way as the old ConnectedPositionStrategy and to avoid breaking changes.
293-
// TODO(crisbeto): make these on by default and add inputs for them
294-
// next time we do breaking changes.
295-
.withFlexibleDimensions(false)
296-
.withPush(false)
297-
.withGrowAfterOpen(false)
313+
.withFlexibleDimensions(this.flexibleDiemsions)
314+
.withPush(this.push)
315+
.withGrowAfterOpen(this.growAfterOpen)
316+
.withViewportMargin(this.viewportMargin)
298317
.withLockedPosition(this.lockPosition);
299318

300319
this._setPositions(strategy);

src/cdk/overlay/position/flexible-connected-position-strategy.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ export class FlexibleConnectedPositionStrategy implements PositionStrategy {
137137
overlayRef.hostElement.classList.add('cdk-overlay-connected-position-bounding-box');
138138

139139
this._overlayRef = overlayRef;
140-
this._boundingBox = overlayRef.hostElement!;
140+
this._boundingBox = overlayRef.hostElement;
141141
this._pane = overlayRef.overlayElement;
142142
this._resizeSubscription.unsubscribe();
143143
this._resizeSubscription = this._viewportRuler.change().subscribe(() => this.apply());

0 commit comments

Comments
 (0)