Skip to content

Commit 3b49387

Browse files
committed
fix(overlay): make config immutable for existing refs
This changes makes the OverlayConfig instance in OverlatRef immutable. Calling `getConfig` will now return a clone of the config to make clear that it cannot be modified directly to change the state of the overlay. This also updates the `updateSize` method to accept a partial config to apply to the existing config (and make a new config instance) BREAKING CHANGE: OverlayRef.getConfig returns a clone of the config object. BREAKING CHANGE: OverlayRef.updateSize now accepts a OverlaySizeConfig rather than being based on the existing config object.
1 parent 0d9f786 commit 3b49387

File tree

4 files changed

+51
-30
lines changed

4 files changed

+51
-30
lines changed

src/cdk/overlay/overlay-ref.ts

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

9-
import {NgZone} from '@angular/core';
10-
import {PortalHost, Portal} from '@angular/cdk/portal';
9+
import {NgZone, ComponentRef, EmbeddedViewRef} from '@angular/core';
10+
import {PortalHost, Portal, ComponentPortal, TemplatePortal} from '@angular/cdk/portal';
1111
import {OverlayConfig} from './overlay-config';
1212
import {Observable} from 'rxjs/Observable';
1313
import {Subject} from 'rxjs/Subject';
@@ -39,8 +39,14 @@ export class OverlayRef implements PortalHost {
3939
return this._pane;
4040
}
4141

42+
attach<T>(portal: ComponentPortal<T>): ComponentRef<T>;
43+
attach<T>(portal: TemplatePortal<T>): EmbeddedViewRef<T>;
44+
attach(portal: any): any;
45+
4246
/**
43-
* Attaches the overlay to a portal instance and adds the backdrop.
47+
* Attaches content, given via a Portal, to the overlay.
48+
* If the overlay is configured to have a backdrop, it will be created.
49+
*
4450
* @param portal Portal instance to which to attach the overlay.
4551
* @returns The portal attachment result.
4652
*/
@@ -53,8 +59,8 @@ export class OverlayRef implements PortalHost {
5359

5460
// Update the pane element with the given configuration.
5561
this._updateStackingOrder();
56-
this.updateSize();
57-
this.updateDirection();
62+
this._updateElementSize();
63+
this._updateDirection();
5864
this.updatePosition();
5965

6066
if (this._config.scrollStrategy) {
@@ -107,9 +113,7 @@ export class OverlayRef implements PortalHost {
107113
return detachmentResult;
108114
}
109115

110-
/**
111-
* Cleans up the overlay from the DOM.
112-
*/
116+
/** Cleans up the overlay from the DOM. */
113117
dispose(): void {
114118
if (this._config.positionStrategy) {
115119
this._config.positionStrategy.dispose();
@@ -127,35 +131,30 @@ export class OverlayRef implements PortalHost {
127131
this._detachments.complete();
128132
}
129133

130-
/**
131-
* Checks whether the overlay has been attached.
132-
*/
134+
/** Whether the overlay has attached content. */
133135
hasAttached(): boolean {
134136
return this._portalHost.hasAttached();
135137
}
136138

137-
/**
138-
* Returns an observable that emits when the backdrop has been clicked.
139-
*/
139+
/** Gets an observable that emits when the backdrop has been clicked. */
140140
backdropClick(): Observable<void> {
141141
return this._backdropClick.asObservable();
142142
}
143143

144-
/** Returns an observable that emits when the overlay has been attached. */
144+
/** Gets an observable that emits when the overlay has been attached. */
145145
attachments(): Observable<void> {
146146
return this._attachments.asObservable();
147147
}
148148

149-
/** Returns an observable that emits when the overlay has been detached. */
149+
/** Gets an observable that emits when the overlay has been detached. */
150150
detachments(): Observable<void> {
151151
return this._detachments.asObservable();
152152
}
153153

154-
/**
155-
* Gets the current config of the overlay.
156-
*/
154+
/** Gets a clone of the the current overlay configuration. */
157155
getConfig(): OverlayConfig {
158-
return this._config;
156+
// Clone the config so that it cannot be modified outside of this class.
157+
return {...this._config};
159158
}
160159

161160
/** Updates the position of the overlay based on the position strategy. */
@@ -165,13 +164,19 @@ export class OverlayRef implements PortalHost {
165164
}
166165
}
167166

167+
/** Update the size properties of the overlay. */
168+
updateSize(sizeConfig: OverlaySizeConfig) {
169+
this._config = {...this._config, ...sizeConfig};
170+
this._updateElementSize();
171+
}
172+
168173
/** Updates the text direction of the overlay panel. */
169-
private updateDirection() {
174+
private _updateDirection() {
170175
this._pane.setAttribute('dir', this._config.direction!);
171176
}
172177

173-
/** Updates the size of the overlay based on the overlay config. */
174-
updateSize() {
178+
/** Updates the size of the overlay element based on the overlay config. */
179+
private _updateElementSize() {
175180
if (this._config.width || this._config.width === 0) {
176181
this._pane.style.width = formatCssUnit(this._config.width);
177182
}
@@ -220,10 +225,12 @@ export class OverlayRef implements PortalHost {
220225
this._backdropElement.addEventListener('click', () => this._backdropClick.next(null));
221226

222227
// Add class to fade-in the backdrop after one frame.
223-
requestAnimationFrame(() => {
224-
if (this._backdropElement) {
225-
this._backdropElement.classList.add('cdk-overlay-backdrop-showing');
226-
}
228+
this._ngZone.runOutsideAngular(() => {
229+
requestAnimationFrame(() => {
230+
if (this._backdropElement) {
231+
this._backdropElement.classList.add('cdk-overlay-backdrop-showing');
232+
}
233+
});
227234
});
228235
}
229236

@@ -284,3 +291,14 @@ export class OverlayRef implements PortalHost {
284291
function formatCssUnit(value: number | string) {
285292
return typeof value === 'string' ? value as string : `${value}px`;
286293
}
294+
295+
296+
/** Size properties for an overlay. */
297+
export interface OverlaySizeConfig {
298+
width?: number | string;
299+
height?: number | string;
300+
minWidth?: number | string;
301+
minHeight?: number | string;
302+
maxWidth?: number | string;
303+
maxHeight?: number | string;
304+
};

src/cdk/portal/portal.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,10 @@ export abstract class BasePortalHost implements PortalHost {
179179
return !!this._attachedPortal;
180180
}
181181

182+
attach<T>(portal: ComponentPortal<T>): ComponentRef<T>;
183+
attach<T>(portal: TemplatePortal<T>): EmbeddedViewRef<T>;
184+
attach(portal: any): any;
185+
182186
attach(portal: Portal<any>): any {
183187
if (!portal) {
184188
throwNullPortalError();

src/cdk/scrolling/scrolling.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
### Scrolling
22

3-
Some things to help with scrollling.
3+
Some things to help with scrollling.

src/lib/autocomplete/autocomplete-trigger.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -451,8 +451,7 @@ export class MdAutocompleteTrigger implements ControlValueAccessor, OnDestroy {
451451
this._overlayRef = this._overlay.create(this._getOverlayConfig());
452452
} else {
453453
/** Update the panel width, in case the host width has changed */
454-
this._overlayRef.getConfig().width = this._getHostWidth();
455-
this._overlayRef.updateSize();
454+
this._overlayRef.updateSize({width: this._getHostWidth()});
456455
}
457456

458457
if (this._overlayRef && !this._overlayRef.hasAttached()) {

0 commit comments

Comments
 (0)