Skip to content

Commit 541684a

Browse files
committed
feat(overlay): add support for flexible connected positioning
* Adds the `FlexibleConnectedPositionStrategy` that builds on top of the `ConnectedPositionStrategy` adds the following: * The ability to have overlays with flexible sizing. * Being able to push overlays into the viewport if they don't fit. * Having a margin between the overlay and the viewport edge. * Being able to assign weights to the overlay positions. * Refactors the `ConnectedPositionStrategy` to use the `FlexibleConnectedPositionStrategy` in order to avoid breaking API changes. * Switches all of the components to the new position strategy. * Adds an API to the `OverlayRef` that allows for the consumer to wrap the pane in a div. This is a common requirement between the `GlobalPositionStrategy` and the `FlexibleConnectedPositionStrategy`, and it's easier to keep track of the elements when the attaching and detaching is done in the `OverlayRef`. Fixes #6534. Fixes #2725. Fixes #5267.
1 parent 60b0625 commit 541684a

31 files changed

+3141
-741
lines changed

.github/CODEOWNERS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@
9494
/src/demo-app/card/** @jelbourn
9595
/src/demo-app/checkbox/** @tinayuangao @devversion
9696
/src/demo-app/chips/** @tinayuangao
97+
/src/demo-app/connected-overlay/** @jelbourn @crisbeto
9798
/src/demo-app/dataset/** @andrewseguin
9899
/src/demo-app/datepicker/** @mmalerba
99100
/src/demo-app/demo-app/** @jelbourn

src/cdk/overlay/_overlay.scss

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,16 @@ $backdrop-animation-timing-function: cubic-bezier(0.25, 0.8, 0.25, 1) !default;
4747
// A single overlay pane.
4848
.cdk-overlay-pane {
4949
position: absolute;
50+
5051
pointer-events: auto;
5152
box-sizing: border-box;
5253
z-index: $cdk-z-index-overlay;
54+
55+
// For connected-position overlays, we set `display: flex` in
56+
// order to force `max-width` and `max-height` to take effect.
57+
display: flex;
58+
max-width: 100%;
59+
max-height: 100%;
5360
}
5461

5562
.cdk-overlay-backdrop {
@@ -79,6 +86,20 @@ $backdrop-animation-timing-function: cubic-bezier(0.25, 0.8, 0.25, 1) !default;
7986
background: none;
8087
}
8188

89+
// Overlay parent element used with the connected position strategy. Used to constrain the
90+
// overlay element's size to fit within the viewport.
91+
.cdk-overlay-connected-position-bounding-box {
92+
position: absolute;
93+
z-index: $cdk-z-index-overlay;
94+
95+
// We use `display: flex` on this element exclusively for centering connected overlays.
96+
// When *not* centering, a top/left/bottom/right will be set which overrides the normal
97+
// flex layout.
98+
display: flex;
99+
justify-content: center;
100+
align-items: center;
101+
}
102+
82103
// Used when disabling global scrolling.
83104
.cdk-global-scrollblock {
84105
position: fixed;

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

Lines changed: 14 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {dispatchKeyboardEvent} from '@angular/cdk/testing';
66
import {ESCAPE} from '@angular/cdk/keycodes';
77
import {CdkConnectedOverlay, OverlayModule, CdkOverlayOrigin} from './index';
88
import {OverlayContainer} from './overlay-container';
9-
import {ConnectedPositionStrategy} from './position/connected-position-strategy';
9+
import {FlexibleConnectedPositionStrategy} from './position/flexible-connected-position-strategy';
1010
import {ConnectedOverlayPositionChange} from './position/connected-position';
1111

1212

@@ -80,13 +80,11 @@ describe('Overlay directives', () => {
8080
let testComponent: ConnectedOverlayDirectiveTest =
8181
fixture.debugElement.componentInstance;
8282
let overlayDirective = testComponent.connectedOverlayDirective;
83-
8483
let strategy =
85-
<ConnectedPositionStrategy> overlayDirective.overlayRef.getConfig().positionStrategy;
86-
expect(strategy instanceof ConnectedPositionStrategy).toBe(true);
84+
overlayDirective.overlayRef.getConfig().positionStrategy as FlexibleConnectedPositionStrategy;
8785

88-
let positions = strategy.positions;
89-
expect(positions.length).toBeGreaterThan(0);
86+
expect(strategy instanceof FlexibleConnectedPositionStrategy).toBe(true);
87+
expect(strategy.positions.length).toBeGreaterThan(0);
9088
});
9189

9290
it('should set and update the `dir` attribute', () => {
@@ -139,7 +137,7 @@ describe('Overlay directives', () => {
139137
fixture.componentInstance.isOpen = true;
140138
fixture.detectChanges();
141139

142-
const pane = overlayContainerElement.children[0] as HTMLElement;
140+
const pane = overlayContainerElement.querySelector('.cdk-overlay-pane') as HTMLElement;
143141
expect(pane.style.width).toEqual('250px');
144142
});
145143

@@ -148,7 +146,7 @@ describe('Overlay directives', () => {
148146
fixture.componentInstance.isOpen = true;
149147
fixture.detectChanges();
150148

151-
const pane = overlayContainerElement.children[0] as HTMLElement;
149+
const pane = overlayContainerElement.querySelector('.cdk-overlay-pane') as HTMLElement;
152150
expect(pane.style.height).toEqual('100vh');
153151
});
154152

@@ -157,7 +155,7 @@ describe('Overlay directives', () => {
157155
fixture.componentInstance.isOpen = true;
158156
fixture.detectChanges();
159157

160-
const pane = overlayContainerElement.children[0] as HTMLElement;
158+
const pane = overlayContainerElement.querySelector('.cdk-overlay-pane') as HTMLElement;
161159
expect(pane.style.minWidth).toEqual('250px');
162160
});
163161

@@ -166,7 +164,7 @@ describe('Overlay directives', () => {
166164
fixture.componentInstance.isOpen = true;
167165
fixture.detectChanges();
168166

169-
const pane = overlayContainerElement.children[0] as HTMLElement;
167+
const pane = overlayContainerElement.querySelector('.cdk-overlay-pane') as HTMLElement;
170168
expect(pane.style.minHeight).toEqual('500px');
171169
});
172170

@@ -198,18 +196,13 @@ describe('Overlay directives', () => {
198196
});
199197

200198
it('should set the offsetX', () => {
201-
const trigger = fixture.debugElement.query(By.css('button')).nativeElement;
202-
const startX = trigger.getBoundingClientRect().left;
203-
204199
fixture.componentInstance.offsetX = 5;
205200
fixture.componentInstance.isOpen = true;
206201
fixture.detectChanges();
207202

208-
const pane = overlayContainerElement.children[0] as HTMLElement;
203+
const pane = overlayContainerElement.querySelector('.cdk-overlay-pane') as HTMLElement;
209204

210-
expect(pane.style.left)
211-
.toBe(startX + 5 + 'px',
212-
`Expected overlay translateX to equal the original X + the offsetX.`);
205+
expect(pane.style.transform).toContain('translateX(5px)');
213206

214207
fixture.componentInstance.isOpen = false;
215208
fixture.detectChanges();
@@ -218,9 +211,7 @@ describe('Overlay directives', () => {
218211
fixture.componentInstance.isOpen = true;
219212
fixture.detectChanges();
220213

221-
expect(pane.style.left)
222-
.toBe(startX + 15 + 'px',
223-
`Expected overlay directive to reflect new offsetX if it changes.`);
214+
expect(pane.style.transform).toContain('translateX(15px)');
224215
});
225216

226217
it('should set the offsetY', () => {
@@ -233,21 +224,17 @@ describe('Overlay directives', () => {
233224
fixture.componentInstance.isOpen = true;
234225
fixture.detectChanges();
235226

236-
// expected y value is the starting y + trigger height + offset y
237-
// 30 + 20 + 45 = 95px
238-
const pane = overlayContainerElement.children[0] as HTMLElement;
227+
const pane = overlayContainerElement.querySelector('.cdk-overlay-pane') as HTMLElement;
239228

240-
expect(pane.style.top)
241-
.toBe('95px', `Expected overlay translateY to equal the start Y + height + offsetY.`);
229+
expect(pane.style.transform).toContain('translateY(45px)');
242230

243231
fixture.componentInstance.isOpen = false;
244232
fixture.detectChanges();
245233

246234
fixture.componentInstance.offsetY = 55;
247235
fixture.componentInstance.isOpen = true;
248236
fixture.detectChanges();
249-
expect(pane.style.top)
250-
.toBe('105px', `Expected overlay directive to reflect new offsetY if it changes.`);
237+
expect(pane.style.transform).toContain('translateY(55px)');
251238
});
252239

253240
});

src/cdk/overlay/overlay-directives.ts

Lines changed: 66 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -30,27 +30,39 @@ import {Overlay} from './overlay';
3030
import {OverlayConfig} from './overlay-config';
3131
import {OverlayRef} from './overlay-ref';
3232
import {
33-
ConnectedOverlayPositionChange,
34-
ConnectionPositionPair,
35-
} from './position/connected-position';
36-
import {ConnectedPositionStrategy} from './position/connected-position-strategy';
33+
FlexibleConnectedPositionStrategy,
34+
ConnectedPosition,
35+
} from './position/flexible-connected-position-strategy';
36+
import {ConnectedOverlayPositionChange} from './position/connected-position';
3737
import {RepositionScrollStrategy, ScrollStrategy} from './scroll/index';
3838

3939

4040
/** Default set of positions for the overlay. Follows the behavior of a dropdown. */
41-
const defaultPositionList = [
42-
new ConnectionPositionPair(
43-
{originX: 'start', originY: 'bottom'},
44-
{overlayX: 'start', overlayY: 'top'}),
45-
new ConnectionPositionPair(
46-
{originX: 'start', originY: 'top'},
47-
{overlayX: 'start', overlayY: 'bottom'}),
48-
new ConnectionPositionPair(
49-
{originX: 'end', originY: 'top'},
50-
{overlayX: 'end', overlayY: 'bottom'}),
51-
new ConnectionPositionPair(
52-
{originX: 'end', originY: 'bottom'},
53-
{overlayX: 'end', overlayY: 'top'}),
41+
const defaultPositionList: ConnectedPosition[] = [
42+
{
43+
originX: 'start',
44+
originY: 'bottom',
45+
overlayX: 'start',
46+
overlayY: 'top'
47+
},
48+
{
49+
originX: 'start',
50+
originY: 'top',
51+
overlayX: 'start',
52+
overlayY: 'bottom'
53+
},
54+
{
55+
originX: 'end',
56+
originY: 'top',
57+
overlayX: 'end',
58+
overlayY: 'bottom'
59+
},
60+
{
61+
originX: 'end',
62+
originY: 'bottom',
63+
overlayX: 'end',
64+
overlayY: 'top'
65+
}
5466
];
5567

5668
/** Injection token that determines the scroll handling while the connected overlay is open. */
@@ -101,21 +113,22 @@ export class CdkConnectedOverlay implements OnDestroy, OnChanges {
101113
private _positionSubscription = Subscription.EMPTY;
102114
private _offsetX: number = 0;
103115
private _offsetY: number = 0;
104-
private _position: ConnectedPositionStrategy;
116+
private _position: FlexibleConnectedPositionStrategy;
105117

106118
/** Origin for the connected overlay. */
107119
@Input('cdkConnectedOverlayOrigin') origin: CdkOverlayOrigin;
108120

109121
/** Registered connected position pairs. */
110-
@Input('cdkConnectedOverlayPositions') positions: ConnectionPositionPair[];
122+
@Input('cdkConnectedOverlayPositions') positions: ConnectedPosition[];
111123

112124
/** The offset in pixels for the overlay connection point on the x-axis */
113125
@Input('cdkConnectedOverlayOffsetX')
114126
get offsetX(): number { return this._offsetX; }
115127
set offsetX(offsetX: number) {
116128
this._offsetX = offsetX;
129+
117130
if (this._position) {
118-
this._position.withOffsetX(offsetX);
131+
this._setPositions(this._position);
119132
}
120133
}
121134

@@ -124,8 +137,9 @@ export class CdkConnectedOverlay implements OnDestroy, OnChanges {
124137
get offsetY() { return this._offsetY; }
125138
set offsetY(offsetY: number) {
126139
this._offsetY = offsetY;
140+
127141
if (this._position) {
128-
this._position.withOffsetY(offsetY);
142+
this._setPositions(this._position);
129143
}
130144
}
131145

@@ -163,8 +177,8 @@ export class CdkConnectedOverlay implements OnDestroy, OnChanges {
163177

164178
/** @deprecated */
165179
@Input('positions')
166-
get _deprecatedPositions(): ConnectionPositionPair[] { return this.positions; }
167-
set _deprecatedPositions(_positions: ConnectionPositionPair[]) { this.positions = _positions; }
180+
get _deprecatedPositions(): ConnectedPosition[] { return this.positions; }
181+
set _deprecatedPositions(_positions: ConnectedPosition[]) { this.positions = _positions; }
168182

169183
/** @deprecated */
170184
@Input('offsetX')
@@ -303,31 +317,40 @@ export class CdkConnectedOverlay implements OnDestroy, OnChanges {
303317
}
304318

305319
/** Returns the position strategy of the overlay to be set on the overlay config */
306-
private _createPositionStrategy(): ConnectedPositionStrategy {
307-
const pos = this.positions[0];
308-
const originPoint = {originX: pos.originX, originY: pos.originY};
309-
const overlayPoint = {overlayX: pos.overlayX, overlayY: pos.overlayY};
310-
320+
private _createPositionStrategy(): FlexibleConnectedPositionStrategy {
311321
const strategy = this._overlay.position()
312-
.connectedTo(this.origin.elementRef, originPoint, overlayPoint)
313-
.withOffsetX(this.offsetX)
314-
.withOffsetY(this.offsetY);
315-
316-
this._handlePositionChanges(strategy);
322+
.flexibleConnectedTo(this.origin.elementRef)
323+
// Turn off all of the flexible positioning features for now to have it behave
324+
// the same way as the old ConnectedPositionStrategy and to avoid breaking changes.
325+
// TODO(crisbeto): make these on by default and add inputs for them
326+
// next time we do breaking changes.
327+
.withFlexibleHeight(false)
328+
.withFlexibleWidth(false)
329+
.withPush(false)
330+
.withGrowAfterOpen(false);
331+
332+
this._setPositions(strategy);
333+
this._positionSubscription =
334+
strategy.positionChanges.subscribe(p => this.positionChange.emit(p));
317335

318336
return strategy;
319337
}
320338

321-
private _handlePositionChanges(strategy: ConnectedPositionStrategy): void {
322-
for (let i = 1; i < this.positions.length; i++) {
323-
strategy.withFallbackPosition(
324-
{originX: this.positions[i].originX, originY: this.positions[i].originY},
325-
{overlayX: this.positions[i].overlayX, overlayY: this.positions[i].overlayY}
326-
);
327-
}
328-
329-
this._positionSubscription =
330-
strategy.onPositionChange.subscribe(pos => this.positionChange.emit(pos));
339+
/**
340+
* Sets the primary and fallback positions of a positions strategy,
341+
* based on the current directive inputs.
342+
*/
343+
private _setPositions(positionStrategy: FlexibleConnectedPositionStrategy) {
344+
const positions: ConnectedPosition[] = this.positions.map(pos => ({
345+
originX: pos.originX,
346+
originY: pos.originY,
347+
overlayX: pos.overlayX,
348+
overlayY: pos.overlayY,
349+
offsetX: this.offsetX,
350+
offsetY: this.offsetY
351+
}));
352+
353+
positionStrategy.withPositions(positions);
331354
}
332355

333356
/** Attaches the overlay and subscribes to backdrop clicks if backdrop exists */
@@ -342,7 +365,6 @@ export class CdkConnectedOverlay implements OnDestroy, OnChanges {
342365
});
343366
}
344367

345-
this._position.withDirection(this.dir);
346368
this._overlayRef.setDirection(this.dir);
347369

348370
if (!this._overlayRef.hasAttached()) {

0 commit comments

Comments
 (0)