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

fix(fxLayoutGap): account for responsive fxHide on children elements #931

Merged
merged 2 commits into from
Dec 18, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions src/lib/flex/layout-gap/layout-gap.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,36 @@ describe('layout-gap directive', () => {
expectEl(nodes[1]).toHaveStyle({'margin-bottom': '24px'}, styler);
expectEl(nodes[2]).not.toHaveStyle({'margin-bottom': '*'}, styler);
});

it('should work with responsive fxHide', () => {
let template = `
<div fxLayoutAlign="center center" fxLayoutGap="13px">
<div fxFlex="15" class="sec1" fxFlex.xs="55"></div>
<div fxFlex="30" class="sec2" fxFlex.sm></div>
<div fxFlex="55" class="sec3" fxShow fxHide.sm></div>
</div>
`;
createTestComponent(template);
fixture.detectChanges();

let nodes = queryFor(fixture, '[fxFlex]');
expect(nodes.length).toEqual(3);
expectEl(nodes[0]).toHaveStyle({'margin-right': '13px'}, styler);
expectEl(nodes[1]).toHaveStyle({'margin-right': '13px'}, styler);
expectEl(nodes[2]).not.toHaveStyle({'margin-right': '*'}, styler);

matchMedia.activate('sm');
fixture.detectChanges();
expectEl(nodes[0]).toHaveStyle({'margin-right': '13px'}, styler);
expectEl(nodes[1]).not.toHaveStyle({'margin-right': '*'}, styler);
expectEl(nodes[2]).not.toHaveStyle({'margin-right': '*'}, styler);

matchMedia.activate('lg');
fixture.detectChanges();
expectEl(nodes[0]).toHaveStyle({'margin-right': '13px'}, styler);
expectEl(nodes[1]).toHaveStyle({'margin-right': '13px'}, styler);
expectEl(nodes[2]).not.toHaveStyle({'margin-right': '*'}, styler);
});
});

describe('rtl support', () => {
Expand Down
15 changes: 6 additions & 9 deletions src/lib/flex/layout-gap/layout-gap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ export class LayoutGapDirective extends BaseDirective2 implements AfterContentIn
}
// Gather all non-hidden Element nodes
const items = this.childrenNodes
.filter(el => el.nodeType === 1 && this.getDisplayStyle(el) !== 'none')
.filter(el => el.nodeType === 1 && this.willDisplay(el))
.sort((a, b) => {
const orderA = +this.styler.lookupStyle(a, 'order');
const orderB = +this.styler.lookupStyle(b, 'order');
Expand All @@ -200,14 +200,11 @@ export class LayoutGapDirective extends BaseDirective2 implements AfterContentIn
}
}

/**
* Quick accessor to the current HTMLElement's `display` style
* Note: this allows us to preserve the original style
* and optional restore it when the mediaQueries deactivate
*/
protected getDisplayStyle(source: HTMLElement = this.nativeElement): string {
const query = 'display';
return this.styler.lookupStyle(source, query);
/** Determine if an element will show or hide based on current activation */
protected willDisplay(source: HTMLElement): boolean {
const value = this.marshal.getValue(source, 'show-hide');
Copy link
Contributor

@ThomasBurleson ThomasBurleson Dec 17, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about this for better clarity:

const value = this.marshal.getValue(source, 'show-hide');
if ( value == '' || value == undefined) {
  // Current value is not explicitly set (to 'false' or 'true'), so lookStyle() 
  value = this.styleUtils.lookupStyle(source, 'display') !== 'none';
}
return value;

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think the value gain is great in terms of clarity: the value returned from the marshaller will never be undefined.

return value === true ||
(value === '' && this.styleUtils.lookupStyle(source, 'display') !== 'none');
}

protected buildChildObservable(): void {
Expand Down