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

Commit f59ad1f

Browse files
fix(ObservableMedia): provide consistent reporting of active breakpoint
**ObservableMedia** event reports regarding current active breakpoint are not consistent: * during page startup - active breakpoint may show as "gt-sm" instead of "md", "lg", or "xl". * during manual resize - active breakpoint will as "xs", "sm", "md", "lg", or "xl". fixes #185.
1 parent eee20b2 commit f59ad1f

File tree

5 files changed

+29
-30
lines changed

5 files changed

+29
-30
lines changed

src/lib/flexbox/api/show-hide.ts

+6-20
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,10 @@ const FALSY = ['false', false, 0];
3030
/**
3131
* For fxHide selectors, we invert the 'value'
3232
* and assign to the equivalent fxShow selector cache
33+
* - When 'hide' === '' === true, do NOT show the element
34+
* - When 'hide' === false or 0... we WILL show the element
3335
*/
3436
export function negativeOf(hide: any) {
35-
// where 'hide' === '', do NOT show the element
36-
// where 'hide' === false or 0... we WILL show the element
3737
return (hide === "") ? false :
3838
((hide === "false") || (hide === 0)) ? true : !hide;
3939
}
@@ -45,25 +45,11 @@ export function negativeOf(hide: any) {
4545
@Directive({
4646
selector: `
4747
[fxShow],
48-
[fxShow.xs],
49-
[fxShow.gt-xs],
50-
[fxShow.sm],
51-
[fxShow.gt-sm],
52-
[fxShow.md],
53-
[fxShow.gt-md],
54-
[fxShow.lg],
55-
[fxShow.gt-lg],
56-
[fxShow.xl],
48+
[fxShow.xs],[fxShow.gt-xs],[fxShow.sm],[fxShow.gt-sm],
49+
[fxShow.md],[fxShow.gt-md],[fxShow.lg],[fxShow.gt-lg],[fxShow.xl],
5750
[fxHide],
58-
[fxHide.xs],
59-
[fxHide.gt-xs],
60-
[fxHide.sm],
61-
[fxHide.gt-sm],
62-
[fxHide.md],
63-
[fxHide.gt-md],
64-
[fxHide.lg],
65-
[fxHide.gt-lg],
66-
[fxHide.xl]
51+
[fxHide.xs],[fxHide.gt-xs],[fxHide.sm],[fxHide.gt-sm],
52+
[fxHide.md],[fxHide.gt-md],[fxHide.lg],[fxHide.gt-lg],[fxHide.xl]
6753
`
6854
})
6955
export class ShowHideDirective extends BaseFxDirective implements OnInit, OnChanges, OnDestroy {

src/lib/media-query/match-media.spec.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ describe('match-media-observable', () => {
223223

224224
// "all" mediaQuery is already active; total count should be (3)
225225

226-
expect(activationCount).toEqual(3);
226+
expect(activationCount).toEqual(2);
227227
expect(deactivationCount).toEqual(0);
228228

229229
subscription.unsubscribe();

src/lib/media-query/mock/mock-match-media.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ export class MockMatchMedia extends MatchMedia {
7575
* "xs" active == false
7676
*
7777
*/
78-
private _activateWithOverlaps(mediaQuery: string, useOverlaps: boolean) {
78+
private _activateWithOverlaps(mediaQuery: string, useOverlaps: boolean): boolean {
7979
if (useOverlaps) {
8080
let bp = this._breakpoints.findByQuery(mediaQuery);
8181
switch (bp ? bp.alias : 'unknown') {
@@ -96,7 +96,7 @@ export class MockMatchMedia extends MatchMedia {
9696
}
9797
}
9898
// Activate last since the responsiveActivation is watching *this* mediaQuery
99-
this._activateByQuery(mediaQuery);
99+
return this._activateByQuery(mediaQuery);
100100
}
101101

102102
/**

src/lib/media-query/observable-media-service.spec.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ describe('match-media-observable-provider', () => {
9494
subscription.unsubscribe();
9595
}));
9696

97-
it('can can subscribe to built-in mediaQueries', async(inject(
97+
it('can subscribe to built-in mediaQueries', inject(
9898
[ObservableMedia, MatchMedia],
9999
(media$, matchMedia) => {
100100
let current: MediaChange;
@@ -117,6 +117,9 @@ describe('match-media-observable-provider', () => {
117117
matchMedia.activate('md');
118118
expect(current.mediaQuery).toEqual(findMediaQuery('md'));
119119

120+
// Allow overlapping activations to be announced to observers
121+
media$.filterOverlaps = false;
122+
120123
matchMedia.activate('gt-lg');
121124
expect(current.mediaQuery).toEqual(findMediaQuery('gt-lg'));
122125

@@ -127,7 +130,7 @@ describe('match-media-observable-provider', () => {
127130
matchMedia.autoRegisterQueries = true;
128131
subscription.unsubscribe();
129132
}
130-
})));
133+
}));
131134

132135
it('can `.unsubscribe()` properly', async(inject(
133136
[ObservableMedia, MatchMedia],

src/lib/media-query/observable-media-service.ts

+15-5
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,12 @@ import {BreakPoint} from './breakpoints/break-point';
2424
*/
2525
export abstract class ObservableMedia implements Subscribable<MediaChange> {
2626
abstract isActive(query: string): boolean;
27+
2728
abstract asObservable(): Observable<MediaChange>;
29+
2830
abstract subscribe(next?: (value: MediaChange) => void,
29-
error?: (error: any) => void,
30-
complete?: () => void): Subscription;
31+
error?: (error: any) => void,
32+
complete?: () => void): Subscription;
3133
}
3234

3335
/**
@@ -74,12 +76,15 @@ export abstract class ObservableMedia implements Subscribable<MediaChange> {
7476
*/
7577
@Injectable()
7678
export class MediaService implements ObservableMedia {
77-
private observable$: Observable<MediaChange>;
79+
/**
80+
* Should we announce gt-<xxx> breakpoint activations ?
81+
*/
82+
public filterOverlaps = true;
7883

7984
constructor(private mediaWatcher: MatchMedia,
8085
private breakpoints: BreakPointRegistry) {
81-
this._registerBreakPoints();
8286
this.observable$ = this._buildObservable();
87+
this._registerBreakPoints();
8388
}
8489

8590
/**
@@ -137,6 +142,10 @@ export class MediaService implements ObservableMedia {
137142
.map((change: MediaChange) => {
138143
// Inject associated (if any) alias information into the MediaChange event
139144
return mergeAlias(change, this._findByQuery(change.mediaQuery));
145+
})
146+
.filter((change: MediaChange) => {
147+
let bp = this.breakpoints.findByQuery(change.mediaQuery);
148+
return !bp ? true : !(this.filterOverlaps && bp.overlapping);
140149
});
141150
}
142151

@@ -145,7 +154,7 @@ export class MediaService implements ObservableMedia {
145154
*/
146155
private _findByAlias(alias) {
147156
return this.breakpoints.findByAlias(alias);
148-
};
157+
}
149158

150159
/**
151160
* Breakpoint locator by mediaQuery
@@ -162,6 +171,7 @@ export class MediaService implements ObservableMedia {
162171
return bp ? bp.mediaQuery : query;
163172
};
164173

174+
private observable$: Observable<MediaChange>;
165175
}
166176

167177
/**

0 commit comments

Comments
 (0)