Skip to content
This repository was archived by the owner on Jan 6, 2025. It is now read-only.

Commit 676ddf7

Browse files
authored
fix(fxLayoutGap): respond correctly to layout changes (#919)
1 parent b8c040b commit 676ddf7

File tree

2 files changed

+88
-7
lines changed

2 files changed

+88
-7
lines changed

src/lib/core/media-marshaller/media-marshaller.ts

+7-4
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ export class MediaMarshaller {
112112
getValue(element: HTMLElement, key: string, bp?: string): any {
113113
const bpMap = this.elementMap.get(element);
114114
if (bpMap) {
115-
const values = bp !== undefined ? bpMap.get(bp) : this.getFallback(bpMap);
115+
const values = bp !== undefined ? bpMap.get(bp) : this.getFallback(bpMap, key);
116116
if (values) {
117117
const value = values.get(key);
118118
return value !== undefined ? value : '';
@@ -129,7 +129,7 @@ export class MediaMarshaller {
129129
hasValue(element: HTMLElement, key: string): boolean {
130130
const bpMap = this.elementMap.get(element);
131131
if (bpMap) {
132-
const values = this.getFallback(bpMap);
132+
const values = this.getFallback(bpMap, key);
133133
if (values) {
134134
return values.get(key) !== undefined || false;
135135
}
@@ -214,13 +214,16 @@ export class MediaMarshaller {
214214
/**
215215
* get the fallback breakpoint for a given element, starting with the current breakpoint
216216
* @param bpMap
217+
* @param key
217218
*/
218-
private getFallback(bpMap: BreakpointMap): ValueMap | undefined {
219+
private getFallback(bpMap: BreakpointMap, key?: string): ValueMap | undefined {
219220
for (let i = 0; i < this.activatedBreakpoints.length; i++) {
220221
const activatedBp = this.activatedBreakpoints[i];
221222
const valueMap = bpMap.get(activatedBp.alias);
222223
if (valueMap) {
223-
return valueMap;
224+
if (key === undefined || valueMap.has(key)) {
225+
return valueMap;
226+
}
224227
}
225228
}
226229
return bpMap.get('');

src/lib/flex/layout-gap/layout-gap.spec.ts

+81-3
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import {CommonModule, isPlatformServer} from '@angular/common';
1010
import {TestBed, ComponentFixture, async, inject} from '@angular/core/testing';
1111
import {DIR_DOCUMENT} from '@angular/cdk/bidi';
1212
import {
13+
MatchMedia,
14+
MockMatchMedia,
1315
MockMatchMediaProvider,
1416
SERVER_TOKEN,
1517
StyleBuilder,
@@ -32,9 +34,12 @@ describe('layout-gap directive', () => {
3234
let fakeDocument: {body: {dir?: string}, documentElement: {dir?: string}};
3335
let styler: StyleUtils;
3436
let platformId: Object;
37+
let matchMedia: MockMatchMedia;
3538
let createTestComponent = (template: string, styles?: any) => {
3639
fixture = makeCreateTestComponent(() => TestLayoutGapComponent)(template, styles);
37-
inject([StyleUtils, PLATFORM_ID], (_styler: StyleUtils, _platformId: Object) => {
40+
inject([MatchMedia, StyleUtils, PLATFORM_ID],
41+
(_matchMedia: MockMatchMedia, _styler: StyleUtils, _platformId: Object) => {
42+
matchMedia = _matchMedia;
3843
styler = _styler;
3944
platformId = _platformId;
4045
})();
@@ -49,6 +54,7 @@ describe('layout-gap directive', () => {
4954
imports: [CommonModule, FlexLayoutModule],
5055
declarations: [TestLayoutGapComponent],
5156
providers: [
57+
MockMatchMediaProvider,
5258
{provide: DIR_DOCUMENT, useValue: fakeDocument},
5359
{provide: SERVER_TOKEN, useValue: true}
5460
]
@@ -332,7 +338,80 @@ describe('layout-gap directive', () => {
332338
});
333339

334340
describe('with responsive features', () => {
335-
// TODO(ThomasBurleson): add tests here
341+
it('should set gap on breakpoint change', () => {
342+
let template = `
343+
<div fxLayoutAlign='center center' fxLayoutGap='13px' fxLayoutGap.md="24px">
344+
<div fxFlex></div>
345+
<div fxFlex></div>
346+
<div fxFlex></div>
347+
</div>
348+
`;
349+
createTestComponent(template);
350+
fixture.detectChanges();
351+
352+
let nodes = queryFor(fixture, '[fxFlex]');
353+
expect(nodes.length).toEqual(3);
354+
expectEl(nodes[0]).toHaveStyle({'margin-right': '13px'}, styler);
355+
expectEl(nodes[1]).toHaveStyle({'margin-right': '13px'}, styler);
356+
expectEl(nodes[2]).not.toHaveStyle({'margin-right': '13px'}, styler);
357+
expectEl(nodes[2]).not.toHaveStyle({'margin-right': '0px'}, styler);
358+
359+
matchMedia.activate('md');
360+
fixture.detectChanges();
361+
expectEl(nodes[0]).toHaveStyle({'margin-right': '24px'}, styler);
362+
expectEl(nodes[1]).toHaveStyle({'margin-right': '24px'}, styler);
363+
expectEl(nodes[2]).not.toHaveStyle({'margin-right': '24px'}, styler);
364+
expectEl(nodes[2]).not.toHaveStyle({'margin-right': '0px'}, styler);
365+
});
366+
367+
it('should set gap without fallback', () => {
368+
let template = `
369+
<div fxLayoutAlign='center center' fxLayoutGap.md="24px">
370+
<div fxFlex></div>
371+
<div fxFlex></div>
372+
<div fxFlex></div>
373+
</div>
374+
`;
375+
createTestComponent(template);
376+
fixture.detectChanges();
377+
378+
let nodes = queryFor(fixture, '[fxFlex]');
379+
expect(nodes.length).toEqual(3);
380+
expectEl(nodes[0]).not.toHaveStyle({'margin-right': '*'}, styler);
381+
expectEl(nodes[1]).not.toHaveStyle({'margin-right': '*'}, styler);
382+
expectEl(nodes[2]).not.toHaveStyle({'margin-right': '*'}, styler);
383+
384+
matchMedia.activate('md');
385+
fixture.detectChanges();
386+
expectEl(nodes[0]).toHaveStyle({'margin-right': '24px'}, styler);
387+
expectEl(nodes[1]).toHaveStyle({'margin-right': '24px'}, styler);
388+
expectEl(nodes[2]).not.toHaveStyle({'margin-right': '24px'}, styler);
389+
expectEl(nodes[2]).not.toHaveStyle({'margin-right': '0px'}, styler);
390+
});
391+
392+
it('should set gap with responsive layout change', () => {
393+
let template = `
394+
<div fxLayout="row" fxLayout.xs="column" fxLayoutGap="24px">
395+
<div fxFlex></div>
396+
<div fxFlex></div>
397+
<div fxFlex></div>
398+
</div>
399+
`;
400+
createTestComponent(template);
401+
fixture.detectChanges();
402+
403+
let nodes = queryFor(fixture, '[fxFlex]');
404+
expect(nodes.length).toEqual(3);
405+
expectEl(nodes[0]).toHaveStyle({'margin-right': '24px'}, styler);
406+
expectEl(nodes[1]).toHaveStyle({'margin-right': '24px'}, styler);
407+
expectEl(nodes[2]).not.toHaveStyle({'margin-right': '*'}, styler);
408+
409+
matchMedia.activate('xs');
410+
fixture.detectChanges();
411+
expectEl(nodes[0]).toHaveStyle({'margin-bottom': '24px'}, styler);
412+
expectEl(nodes[1]).toHaveStyle({'margin-bottom': '24px'}, styler);
413+
expectEl(nodes[2]).not.toHaveStyle({'margin-bottom': '*'}, styler);
414+
});
336415
});
337416

338417
describe('rtl support', () => {
@@ -412,7 +491,6 @@ describe('layout-gap directive', () => {
412491
],
413492
declarations: [],
414493
providers: [
415-
MockMatchMediaProvider,
416494
{
417495
provide: LayoutGapStyleBuilder,
418496
useClass: MockLayoutGapStyleBuilder,

0 commit comments

Comments
 (0)