Skip to content

Commit

Permalink
fix(media-observer): return correct value for isActive on init (#1244)
Browse files Browse the repository at this point in the history
  • Loading branch information
CaerusKaru authored May 8, 2020
1 parent bf2355b commit bf069af
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/lib/core/match-media/match-media.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export class MatchMedia {
*/
isActive(mediaQuery: string): boolean {
const mql = this.registry.get(mediaQuery);
return !!mql ? mql.matches : false;
return !!mql ? mql.matches : this.registerQuery(mediaQuery).some(m => m.matches);
}

/**
Expand Down
5 changes: 1 addition & 4 deletions src/lib/core/media-observer/media-observer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ describe('media-observer', () => {
it('can supports the `.isActive()` API', () => {
expect(media$).toBeDefined();

mediaController.autoRegisterQueries = false;
// Activate mediaQuery associated with 'md' alias
mediaController.activate('md');
expect(mediaObserver.isActive('md')).toBeTruthy();
Expand Down Expand Up @@ -126,10 +127,6 @@ describe('media-observer', () => {

activateQuery('gt-lg');
expect(current.mediaQuery).toEqual(findMediaQuery('gt-lg'));

activateQuery('unknown');
expect(current.mediaQuery).toEqual(findMediaQuery('gt-lg'));

} finally {
mediaController.autoRegisterQueries = true;
subscription.unsubscribe();
Expand Down
4 changes: 2 additions & 2 deletions src/lib/core/media-observer/media-observer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ export class MediaObserver implements OnDestroy {
const aliases = splitQueries(coerceArray(value));
return aliases.some(alias => {
const query = toMediaQuery(alias, this.breakpoints);
return this.matchMedia.isActive(query);
return query !== null && this.matchMedia.isActive(query);
});
}

Expand Down Expand Up @@ -201,7 +201,7 @@ export class MediaObserver implements OnDestroy {
*/
function toMediaQuery(query: string, locator: BreakPointRegistry) {
const bp = locator.findByAlias(query) || locator.findByQuery(query);
return bp ? bp.mediaQuery : query;
return bp ? bp.mediaQuery : null;
}

/**
Expand Down

0 comments on commit bf069af

Please sign in to comment.