Skip to content

Commit 7c1549a

Browse files
josephperrottmmalerba
authored andcommitted
fix(breakpoint-observer): split comma separated queries into separte queries (#10789)
1 parent 6a82c65 commit 7c1549a

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 {
@@ -72,9 +73,9 @@ export class BreakpointObserver implements OnDestroy {
7273
return this._queries.get(query)!;
7374
}
7475

75-
let mql: MediaQueryList = this.mediaMatcher.matchMedia(query);
76+
const mql: MediaQueryList = this.mediaMatcher.matchMedia(query);
7677
// Create callback for match changes and add it is as a listener.
77-
let queryObservable = fromEventPattern(
78+
const queryObservable = fromEventPattern(
7879
// Listener callback methods are wrapped to be placed back in ngZone. Callbacks must be placed
7980
// back into the zone because matchMedia is only included in Zone.js by loading the
8081
// webapis-media-query.js file alongside the zone.js file. Additionally, some browsers do not
@@ -93,8 +94,18 @@ export class BreakpointObserver implements OnDestroy {
9394
);
9495

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

0 commit comments

Comments
 (0)