Skip to content

Commit e92868d

Browse files
committed
fix(breakpoint-observer): split comma separated queries into separte matchMedia queries.
1 parent 0a50158 commit e92868d

File tree

2 files changed

+27
-6
lines changed

2 files changed

+27
-6
lines changed

src/cdk/layout/breakpoints-observer.spec.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,16 @@ describe('BreakpointObserver', () => {
5050
expect(mediaMatcher.queryCount).toBe(2);
5151
});
5252

53+
it('splits combined query strings into individual matchMedia listeners', () => {
54+
expect(mediaMatcher.queryCount).toBe(0);
55+
breakpointManager.observe('query1, query2');
56+
expect(mediaMatcher.queryCount).toBe(2);
57+
breakpointManager.observe('query1');
58+
expect(mediaMatcher.queryCount).toBe(2);
59+
breakpointManager.observe('query2, query3');
60+
expect(mediaMatcher.queryCount).toBe(3);
61+
});
62+
5363
it('accepts an array of queries', () => {
5464
let queries = ['1 query', '2 query', 'red query', 'blue query'];
5565
breakpointManager.observe(queries);

src/cdk/layout/breakpoints-observer.ts

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,18 +45,19 @@ export class BreakpointObserver implements OnDestroy {
4545
* @returns Whether any of the media queries match.
4646
*/
4747
isMatched(value: string | string[]): boolean {
48-
let queries = coerceArray(value);
48+
const queries = splitQueries(coerceArray(value));
4949
return queries.some(mediaQuery => this._registerQuery(mediaQuery).mql.matches);
5050
}
5151

5252
/**
5353
* Gets an observable of results for the given queries that will emit new results for any changes
5454
* in matching of the given queries.
55+
* @param value One or more media queries to check.
5556
* @returns A stream of matches for the given queries.
5657
*/
5758
observe(value: string | string[]): Observable<BreakpointState> {
58-
let queries = coerceArray(value);
59-
let observables = queries.map(query => this._registerQuery(query).observable);
59+
const queries = splitQueries(coerceArray(value));
60+
const observables = queries.map(query => this._registerQuery(query).observable);
6061

6162
return combineLatest(observables, (a: BreakpointState, b: BreakpointState) => {
6263
return {
@@ -67,14 +68,15 @@ export class BreakpointObserver implements OnDestroy {
6768

6869
/** Registers a specific query to be listened for. */
6970
private _registerQuery(query: string): Query {
71+
query = query.trim();
7072
// Only set up a new MediaQueryList if it is not already being listened for.
7173
if (this._queries.has(query)) {
7274
return this._queries.get(query)!;
7375
}
7476

75-
let mql: MediaQueryList = this.mediaMatcher.matchMedia(query);
77+
const mql: MediaQueryList = this.mediaMatcher.matchMedia(query);
7678
// Create callback for match changes and add it is as a listener.
77-
let queryObservable = fromEventPattern(
79+
const queryObservable = fromEventPattern(
7880
// Listener callback methods are wrapped to be placed back in ngZone. Callbacks must be placed
7981
// back into the zone because matchMedia is only included in Zone.js by loading the
8082
// webapis-media-query.js file alongside the zone.js file. Additionally, some browsers do not
@@ -93,8 +95,17 @@ export class BreakpointObserver implements OnDestroy {
9395
);
9496

9597
// Add the MediaQueryList to the set of queries.
96-
let output = {observable: queryObservable, mql: mql};
98+
const output = {observable: queryObservable, mql: mql};
9799
this._queries.set(query, output);
98100
return output;
99101
}
100102
}
103+
104+
/**
105+
* Split each query string into separate query strings if two queries are provided as comma
106+
* separated.
107+
*/
108+
function splitQueries(queries: string[]): string[] {
109+
return queries.map((query: string) => query.split(','))
110+
.reduce((a1: string[], a2: string[]) => a1.concat(a2));
111+
}

0 commit comments

Comments
 (0)