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

Commit 66f3717

Browse files
ThomasBurlesontinayuangao
authored andcommitted
fix(ObservableMedia): properly announce 'xs' activation at startup (#396)
MatchMedia was not properly announcing XS activations for applications with startup viewports < 600px. Improve the specificity of the 'xs' media query enables proper MatchMedia notifications at startup. Fixes #388.
1 parent 7a48c25 commit 66f3717

File tree

5 files changed

+84
-47
lines changed

5 files changed

+84
-47
lines changed

src/lib/api/core/base.ts

+3-4
Original file line numberDiff line numberDiff line change
@@ -133,9 +133,8 @@ export abstract class BaseFxDirective implements OnDestroy, OnChanges {
133133
* Note: this allows use to preserve the original style
134134
* and optional restore it when the mediaQueries deactivate
135135
*/
136-
protected _getDisplayStyle(source?: HTMLElement): string {
137-
let element: HTMLElement = source || this.nativeElement;
138-
return lookupStyle(element, 'display');
136+
protected _getDisplayStyle(source: HTMLElement = this.nativeElement): string {
137+
return lookupStyle(source || this.nativeElement, 'display');
139138
}
140139

141140
/**
@@ -164,7 +163,7 @@ export abstract class BaseFxDirective implements OnDestroy, OnChanges {
164163
*/
165164
protected _applyStyleToElement(style: StyleDefinition,
166165
value?: string | number,
167-
nativeElement?: any) {
166+
nativeElement: any = this.nativeElement) {
168167
let element = nativeElement || this.nativeElement;
169168
applyStyleToElement(this._renderer, element, style, value);
170169
}

src/lib/media-query/breakpoints/data/break-points.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export const RESPONSIVE_ALIASES = [
1414
export const DEFAULT_BREAKPOINTS: BreakPoint[] = [
1515
{
1616
alias: 'xs',
17-
mediaQuery: '(max-width: 599px)'
17+
mediaQuery: '(min-width: 0px) and (max-width: 599px)'
1818
},
1919
{
2020
alias: 'gt-xs',

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

+17
Original file line numberDiff line numberDiff line change
@@ -237,4 +237,21 @@ describe('mock-match-media', () => {
237237

238238
subscription.unsubscribe();
239239
});
240+
241+
it('can observe a startup activation of XS', () => {
242+
let current: MediaChange,
243+
bpXS = breakPoints.findByAlias('xs');
244+
245+
matchMedia.activate(bpXS.mediaQuery);
246+
let subscription = matchMedia.observe(bpXS.mediaQuery)
247+
.subscribe((change: MediaChange) => {
248+
current = change;
249+
});
250+
251+
expect(current).toBeTruthy();
252+
expect(current.mediaQuery).toEqual(bpXS.mediaQuery);
253+
expect(matchMedia.isActive(bpXS.mediaQuery)).toBeTruthy();
254+
255+
subscription.unsubscribe();
256+
});
240257
});

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

+58-38
Original file line numberDiff line numberDiff line change
@@ -139,26 +139,46 @@ describe('observable-media', () => {
139139
}));
140140

141141
it('can `.unsubscribe()` properly', async(inject(
142-
[ObservableMedia, MatchMedia],
143-
(media, matchMedia) => {
144-
let current: MediaChange;
145-
let subscription = media.subscribe((change: MediaChange) => {
146-
current = change;
147-
});
148-
149-
// Activate mediaQuery associated with 'md' alias
150-
matchMedia.activate('md');
151-
expect(current.mediaQuery).toEqual(findMediaQuery('md'));
152-
153-
// Un-subscribe
154-
subscription.unsubscribe();
155-
156-
matchMedia.activate('lg');
157-
expect(current.mqAlias).toBe('md');
158-
159-
matchMedia.activate('xs');
160-
expect(current.mqAlias).toBe('md');
161-
})));
142+
[ObservableMedia, MatchMedia],
143+
(media, matchMedia) => {
144+
let current: MediaChange;
145+
let subscription = media.subscribe((change: MediaChange) => {
146+
current = change;
147+
});
148+
149+
// Activate mediaQuery associated with 'md' alias
150+
matchMedia.activate('md');
151+
expect(current.mediaQuery).toEqual(findMediaQuery('md'));
152+
153+
// Un-subscribe
154+
subscription.unsubscribe();
155+
156+
matchMedia.activate('lg');
157+
expect(current.mqAlias).toBe('md');
158+
159+
matchMedia.activate('xs');
160+
expect(current.mqAlias).toBe('md');
161+
})));
162+
163+
it('can observe a startup activation of XS', async(inject(
164+
[ObservableMedia, MatchMedia],
165+
(media, matchMedia) => {
166+
let current: MediaChange;
167+
let subscription = media.subscribe((change: MediaChange) => {
168+
current = change;
169+
});
170+
171+
// Activate mediaQuery associated with 'md' alias
172+
matchMedia.activate('xs');
173+
expect(current.mediaQuery).toEqual(findMediaQuery('xs'));
174+
175+
// Un-subscribe
176+
subscription.unsubscribe();
177+
178+
matchMedia.activate('lg');
179+
expect(current.mqAlias).toBe('xs');
180+
181+
})));
162182
});
163183

164184
describe('with custom BreakPoints', () => {
@@ -176,30 +196,30 @@ describe('observable-media', () => {
176196
providers: [
177197
BreakPointRegistry, // Registry of known/used BreakPoint(s)
178198
MockMatchMediaProvider,
179-
CUSTOM_BREAKPOINTS_PROVIDER_FACTORY(CUSTOM_BREAKPOINTS, {defaults: excludeDefaults} ),
199+
CUSTOM_BREAKPOINTS_PROVIDER_FACTORY(CUSTOM_BREAKPOINTS, {defaults: excludeDefaults}),
180200
OBSERVABLE_MEDIA_PROVIDER,
181201
]
182202
});
183203
});
184204

185205
it('can activate custom alias with custom mediaQueries', async(inject(
186-
[ObservableMedia, MatchMedia],
187-
(media, matchMedia) => {
188-
let current: MediaChange;
189-
let subscription = media.subscribe((change: MediaChange) => {
190-
current = change;
191-
});
192-
193-
// Activate mediaQuery associated with 'md' alias
194-
matchMedia.activate('print.md');
195-
expect(current.mediaQuery).toEqual(mdMediaQuery);
196-
197-
matchMedia.activate('tablet-gt-xs');
198-
expect(current.mqAlias).toBe('tablet-gt-xs');
199-
expect(current.mediaQuery).toBe(gtXsMediaQuery);
200-
201-
subscription.unsubscribe();
202-
})));
206+
[ObservableMedia, MatchMedia],
207+
(media, matchMedia) => {
208+
let current: MediaChange;
209+
let subscription = media.subscribe((change: MediaChange) => {
210+
current = change;
211+
});
212+
213+
// Activate mediaQuery associated with 'md' alias
214+
matchMedia.activate('print.md');
215+
expect(current.mediaQuery).toEqual(mdMediaQuery);
216+
217+
matchMedia.activate('tablet-gt-xs');
218+
expect(current.mqAlias).toBe('tablet-gt-xs');
219+
expect(current.mediaQuery).toBe(gtXsMediaQuery);
220+
221+
subscription.unsubscribe();
222+
})));
203223

204224
});
205225

src/lib/utils/testing/custom-matchers.ts

+5-4
Original file line numberDiff line numberDiff line change
@@ -224,12 +224,13 @@ function buildCompareStyleFunction(inlineOnly = true) {
224224
return {
225225
pass: allPassed,
226226
get message() {
227-
const expectedValueStr = (typeof styles === 'string') ? styles : JSON.stringify(styles);
227+
const expectedValueStr = (typeof styles === 'string') ? styles :
228+
JSON.stringify(styles, null, 2);
228229
const foundValueStr = inlineOnly ? actual.outerHTML : JSON.stringify(found);
229230
return `
230-
Expected ${foundValueStr} ${!allPassed ? ' ' : 'not '} to contain the
231-
CSS ${typeof styles === 'string' ? 'property' : 'styles'} '${expectedValueStr}'
232-
`;
231+
Expected ${foundValueStr} ${!allPassed ? ' ' : 'not '} to contain the
232+
CSS ${typeof styles === 'string' ? 'property' : 'styles'} '${expectedValueStr}'
233+
`;
233234
}
234235
};
235236
};

0 commit comments

Comments
 (0)