Skip to content

Commit e32e2e2

Browse files
authored
fix(google-maps): allow for dimensions to be controlled through CSS (#19790)
When the Google Map component was first introduced, it had inputs to control the `width` and `height`. This is somewhat inflexible, because it only handles pixel units and can't be controlled by media queries. Since changing the sizing approach now would be a breaking change, these changes allow for the consumer to set the `width` and `height` to `null` and to provide it themselves through CSS.
1 parent 210d054 commit e32e2e2

File tree

3 files changed

+25
-10
lines changed

3 files changed

+25
-10
lines changed

src/google-maps/google-map/google-map.spec.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,20 @@ describe('GoogleMap', () => {
116116
expect(container.style.width).toBe('600px');
117117
});
118118

119+
it('should be able to set null as the width/height', () => {
120+
mapSpy = createMapSpy(DEFAULT_OPTIONS);
121+
mapConstructorSpy = createMapConstructorSpy(mapSpy);
122+
123+
const fixture = TestBed.createComponent(TestApp);
124+
const instance = fixture.componentInstance;
125+
instance.height = instance.width = null;
126+
fixture.detectChanges();
127+
128+
const container = fixture.debugElement.query(By.css('div'))!.nativeElement;
129+
expect(container.style.height).toBeFalsy();
130+
expect(container.style.width).toBeFalsy();
131+
});
132+
119133
it('sets center and zoom of the map', () => {
120134
const options = {center: {lat: 3, lng: 5}, zoom: 7, mapTypeId: undefined};
121135
mapSpy = createMapSpy(options);
@@ -314,8 +328,8 @@ describe('GoogleMap', () => {
314328
})
315329
class TestApp {
316330
@ViewChild(GoogleMap) map: GoogleMap;
317-
height?: string | number;
318-
width?: string | number;
331+
height?: string | number | null;
332+
width?: string | number | null;
319333
center?: google.maps.LatLngLiteral;
320334
zoom?: number;
321335
options?: google.maps.MapOptions;

src/google-maps/google-map/google-map.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,11 @@ export class GoogleMap implements OnChanges, OnInit, OnDestroy {
7777
/** Whether we're currently rendering inside a browser. */
7878
_isBrowser: boolean;
7979

80-
/** Height of the map. */
81-
@Input() height: string | number = DEFAULT_HEIGHT;
80+
/** Height of the map. Set this to `null` if you'd like to control the height through CSS. */
81+
@Input() height: string | number | null = DEFAULT_HEIGHT;
8282

83-
/** Width of the map. */
84-
@Input() width: string | number = DEFAULT_WIDTH;
83+
/** Width of the map. Set this to `null` if you'd like to control the width through CSS. */
84+
@Input() width: string | number | null = DEFAULT_WIDTH;
8585

8686
/**
8787
* Type of map that should be rendered. E.g. hybrid map, terrain map etc.
@@ -443,8 +443,9 @@ export class GoogleMap implements OnChanges, OnInit, OnDestroy {
443443
private _setSize() {
444444
if (this._mapEl) {
445445
const styles = this._mapEl.style;
446-
styles.height = coerceCssPixelValue(this.height) || DEFAULT_HEIGHT;
447-
styles.width = coerceCssPixelValue(this.width) || DEFAULT_WIDTH;
446+
styles.height =
447+
this.height === null ? '' : (coerceCssPixelValue(this.height) || DEFAULT_HEIGHT);
448+
styles.width = this.width === null ? '' : (coerceCssPixelValue(this.width) || DEFAULT_WIDTH);
448449
}
449450
}
450451

tools/public_api_guard/google-maps/google-maps.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export declare class GoogleMap implements OnChanges, OnInit, OnDestroy {
77
get data(): google.maps.Data;
88
googleMap?: google.maps.Map;
99
headingChanged: Observable<void>;
10-
height: string | number;
10+
height: string | number | null;
1111
idle: Observable<void>;
1212
mapClick: Observable<google.maps.MouseEvent | google.maps.IconMouseEvent>;
1313
mapDblclick: Observable<google.maps.MouseEvent>;
@@ -26,7 +26,7 @@ export declare class GoogleMap implements OnChanges, OnInit, OnDestroy {
2626
projectionChanged: Observable<void>;
2727
tilesloaded: Observable<void>;
2828
tiltChanged: Observable<void>;
29-
width: string | number;
29+
width: string | number | null;
3030
set zoom(zoom: number);
3131
zoomChanged: Observable<void>;
3232
constructor(_elementRef: ElementRef, _ngZone: NgZone,

0 commit comments

Comments
 (0)