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

Commit 69a4d28

Browse files
committed
fix(layout-gap): apply correct gaps based on flex order
* Sort the elements by flex order before applying gap stylings Fixes #608
1 parent cf5266a commit 69a4d28

File tree

2 files changed

+36
-10
lines changed

2 files changed

+36
-10
lines changed

src/lib/api/flexbox/layout-gap.spec.ts

+19
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,25 @@ describe('layout-gap directive', () => {
103103
expectEl(nodes[2]).not.toHaveStyle({'margin-right': '0px'}, styler);
104104
});
105105

106+
it('should add gap styles in proper order when order style is applied', () => {
107+
let template = `
108+
<div fxLayoutAlign='center center' fxLayoutGap='13px'>
109+
<div fxFlex fxFlexOrder="3"></div>
110+
<div fxFlex fxFlexOrder="2"></div>
111+
<div fxFlex fxFlexOrder="1"></div>
112+
</div>
113+
`;
114+
createTestComponent(template);
115+
fixture.detectChanges();
116+
117+
let nodes = queryFor(fixture, '[fxFlex]');
118+
expect(nodes.length).toEqual(3);
119+
expectEl(nodes[2]).toHaveStyle({'margin-right': '13px'}, styler);
120+
expectEl(nodes[1]).toHaveStyle({'margin-right': '13px'}, styler);
121+
expectEl(nodes[0]).not.toHaveStyle({'margin-right': '13px'}, styler);
122+
expectEl(nodes[0]).not.toHaveStyle({'margin-right': '0px'}, styler);
123+
});
124+
106125
it('should add gap styles to dynamics rows EXCEPT first', () => {
107126
let template = `
108127
<div fxLayoutAlign='center center' fxLayoutGap='13px'>

src/lib/api/flexbox/layout-gap.ts

+17-10
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ import {StyleUtils} from '../../utils/styling/style-utils';
3939
[fxLayoutGap.gt-xs], [fxLayoutGap.gt-sm], [fxLayoutGap.gt-md], [fxLayoutGap.gt-lg]
4040
`
4141
})
42-
export class LayoutGapDirective extends BaseFxDirective implements AfterContentInit, OnChanges,
43-
OnDestroy {
42+
export class LayoutGapDirective extends BaseFxDirective
43+
implements AfterContentInit, OnChanges, OnDestroy {
4444
protected _layout = 'row'; // default flex-direction
4545
protected _layoutWatcher: Subscription;
4646
protected _observer: MutationObserver;
@@ -128,7 +128,7 @@ export class LayoutGapDirective extends BaseFxDirective implements AfterContentI
128128

129129
if (typeof MutationObserver !== 'undefined') {
130130
this._observer = new MutationObserver((mutations: MutationRecord[]) => {
131-
let validatedChanges = (it: MutationRecord): boolean => {
131+
const validatedChanges = (it: MutationRecord): boolean => {
132132
return (it.addedNodes && it.addedNodes.length > 0) ||
133133
(it.removedNodes && it.removedNodes.length > 0);
134134
};
@@ -164,16 +164,23 @@ export class LayoutGapDirective extends BaseFxDirective implements AfterContentI
164164
}
165165

166166
// Gather all non-hidden Element nodes
167-
let items = this.childrenNodes
168-
.filter(el => el.nodeType === 1 && this._getDisplayStyle(el) != 'none');
169-
let numItems = items.length;
170-
171-
if (numItems > 0) {
172-
let lastItem = items[numItems - 1];
167+
const items = this.childrenNodes
168+
.filter(el => el.nodeType === 1 && this._getDisplayStyle(el) != 'none')
169+
.sort((a, b) => {
170+
const orderA = +this._styler.lookupStyle(a, 'order');
171+
const orderB = +this._styler.lookupStyle(b, 'order');
172+
if (isNaN(orderA) || isNaN(orderB) || orderA === orderB) {
173+
return 0;
174+
} else {
175+
return orderA > orderB ? 1 : -1;
176+
}
177+
});
178+
179+
if (items.length > 0) {
180+
const lastItem = items.pop();
173181

174182
// For each `element` children EXCEPT the last,
175183
// set the margin right/bottom styles...
176-
items = items.filter((_, j) => j < numItems - 1);
177184
this._applyStyleToElements(this._buildCSS(value), items);
178185

179186
// Clear all gaps for all visible elements

0 commit comments

Comments
 (0)