In the last post, we covered setting up and using the Angular Flex-Layout library. In that post, we looked at the responsive aspect of the library using directives and suffixing them with breakpoints where required.
To recap, add the fxLayout directive to a DOM element in the HTML. Then, add the abbreviated breakpoint depending on where the view size should change. For example, fxLayout.md or fxFlexAlign.lt-lg etc…
This post covers how to use the MediaObserver service.
The mediaObserver is installed from the Angular Flex-Layout library as shown below. In the projects terminal window type:
And then import it to our app.module.ts file.
The MediaObserver service is an Observable that exposes features to subscribe to mediaQuery changes. It also serves as an isActive() validator method to check if a mediaQuery is currently active.
The MediaObserver service has two API’s.
isActive(query: string): boolean
We use Angular Dependency Injection to inject a reference to the MediaObserver as a constructor parameter to use this service.
The current example on the Angular Flex-Layout’s wiki page is a little out of date. The media$ observable is now deprecated and replaced with mediaObserver. There appears to be a small bug when reporting the change detection as it duplicates the results, as shown below.
To get around this I’ve added a call to the distinctUntilChanged() RXJS operator (lines 29–31). The operator now only emits a single change.
Resize the browser to log the current breakpoint.
Here the screen size is small (sm), but also shows us that the screen is less than medium (lt-md), less than large (lt-lg), less than extra-large (lt-xl), and greater than extra-small (gt-xs). In most cases, we really only need to know the actual size, but it’s good to know the others just in case.
So how do we use the MediaObserver?
When designing our applications, we need to know when the screen view size changes so that we can change our UI to make optimal use of the space. This might mean hiding an element or switching from rows to columns, for example:
When the browser hits the ‘md’ breakpoint, the DIV (starting on line 2) is removed from the DOM.
To use the mediaObserver in your HTML (as shown above) it needs to be accessed by a get() for it to be visible.
Let’s try out another example, you will need to install Angular Material for this to work:
Let’s walk through the above code:
We inject the mediObserver into the constructor as before, then create a new Map of the breakpoints. We set up an observable to the mediaObserver using the asObservable(). When the browser is resized, we get the currently set breakpoint from mqAlias, get the matching value from the grid array, and assign all of this to the cols variable.
In the HTML we set up an Angular Material grid list component and use the async pipe operator to subscribe to the cols observable. The grid shrinks as the browser width decreases. Tile rows wrap to the row below until only one tile can fit on a page. When the browser width increases, the tiles move up to form one long row.
The number we get back from the grid Map denotes the number of columns shown in our grid list, here the browser size is ‘md’ or ‘3’ columns.
Using the mediaObserver we now have an observable that we can subscribe to in our application and track when our viewport changes. The mediaObserver can be used in both the HTML or the Typescript code behind and we walked through a couple of examples in this post.
Thanks for reading.