Skip to content

fix(cdk/layout): breakpoint observer not firing callback on chrome in specific cases #23571

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions src/cdk/layout/media-matcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,21 @@ export class MediaMatcher {
* MediaQueryList for the query provided.
*/
matchMedia(query: string): MediaQueryList {
if (this._platform.WEBKIT) {
if (this._platform.WEBKIT || this._platform.BLINK) {
createEmptyStyleRule(query);
}
return this._matchMedia(query);
}
}

/**
* For Webkit engines that only trigger the MediaQueryListListener when
* there is at least one CSS selector for the respective media query.
* Creates an empty stylesheet that is used to work around browser inconsistencies related to
* `matchMedia`. At the time of writing, it handles the following cases:
* 1. On WebKit browsers, a media query has to have at least one rule in order for `matchMedia`
* to fire. We work around it by declaring a dummy stylesheet with a `@media` declaration.
* 2. In some cases Blink browsers will stop firing the `matchMedia` listener if none of the rules
* inside the `@media` match existing elements on the page. We work around it by having one rule
* targeting the `body`. See https://github.com/angular/components/issues/23546.
*/
function createEmptyStyleRule(query: string) {
if (mediaQueriesForWebkitCompatibility.has(query)) {
Expand All @@ -59,8 +64,7 @@ function createEmptyStyleRule(query: string) {
}

if (mediaQueryStyleNode.sheet) {
(mediaQueryStyleNode.sheet as CSSStyleSheet)
.insertRule(`@media ${query} {.fx-query-test{ }}`, 0);
mediaQueryStyleNode.sheet.insertRule(`@media ${query} {body{ }}`, 0);
mediaQueriesForWebkitCompatibility.add(query);
}
} catch (e) {
Expand Down