-
Notifications
You must be signed in to change notification settings - Fork 768
MediaObserver.isActive not working before subscribe MediaObserver.media$ #1025
Comments
I'm going to leave this up to @ThomasBurleson to investigate and drive towards a resolution. |
Hi, is this bug solved yet ? |
This one is killing us. One way to mitigate this was to add BreakpointObserver is sync but doesn't have an SSR shim so the page will initially deliver the menu open when on mobile. I believe the problem is caused by: flex-layout/src/lib/core/match-media/match-media.ts Lines 69 to 71 in d062708
The workaround I'm using is registering the mqList straight away above the observable. This patch-package will correct the initial isActive so you can do: this.isHandset$ = media.asObservable().pipe(
map(() => media.isActive('lt-lg')),
startWith(media.isActive('lt-lg')),
); patch-package:
It seems the problem is that the mqList is only loaded lazily and the observe that registers them is async using debounceTime(0). |
New patch. I can do a PR for something like this if wanted but not sure if it's the solution you're after @ThomasBurleson @angular+flex-layout+9.0.0-beta.29.patch
|
Alternatively, you can use an app initializer in providers: [
{
provide: APP_INITIALIZER,
useFactory: (media: MediaObserver) => {
return () => {
return media.asObservable()
.pipe(first())
.toPromise();
};
},
multi: true,
deps: [MediaObserver]
}
] |
Previously, only triggering `MatchMedia.observe` would correctly register media queries for later usage in the `MediaObserver`. This is incorrect, and queries should be registered if a) they are valid breakpoints in Angular Layout, and b) they are not currently registered. Fixes #1025
This issue has been automatically locked due to inactivity. Read more about our automatic conversation locking policy. This action has been performed automatically by a bot. |
Bug Report
What is the expected behavior?
this.mediaObserver.isActive('xs') should get true
What is the current behavior?
this.mediaObserver.isActive('xs') always return false
What are the steps to reproduce?
F12 see the console
https://stackblitz.com/github/keatkeat87/angular-flex-is-active-issue
https://github.com/keatkeat87/angular-flex-is-active-issue
What is the use-case or motivation for changing an existing behavior?
this is a bug.
Which versions of Angular, Material, OS, TypeScript, browsers are affected?
angular 7.x,
flex-layout 7.0.0-beta.23
Is there anything else we should know?
i think this is because of the rxjs behavior.
if no subscribe,
observe pipe won't run -> registerQuery no call -> _registry no set -> isActive always return false
workaround :
const isActive = await this.mediaObserver.media$.pipe(take(1), map(media => media.mqAlias === 'xs')).toPromise();
The text was updated successfully, but these errors were encountered: