|
| 1 | +The injectable **MediaObserver** service will provide mediaQuery **activations** notifications for all |
| 2 | +[registered BreakPoints](https://github.com/angular/flex-layout/wiki/Custom-Breakpoints). |
| 3 | + |
| 4 | +This service is essentially an Observable that exposes both features to subscribe to mediaQuery |
| 5 | +changes and a validator method `.isActive()` to test if a mediaQuery (or alias) is |
| 6 | +currently active. |
| 7 | + |
| 8 | +> Only mediaChange activations (not deactivations) are announced by the MediaObserver |
| 9 | +
|
| 10 | +--- |
| 11 | + |
| 12 | +### API Summary |
| 13 | + |
| 14 | +The injectable **MediaObserver** service has two (2) APIs: |
| 15 | + |
| 16 | +- `asObservable(): Observable<MediaChange>` |
| 17 | +- `isActive(query: string): boolean` |
| 18 | + |
| 19 | +--- |
| 20 | + |
| 21 | +#### 1. **`MediaObserver::asObservable`** |
| 22 | + |
| 23 | +Developers use Angular DI to inject a reference to the **MediaObserver** service as a **constructor** parameter. |
| 24 | + |
| 25 | +The **MediaObserver** is not an actual **Observable**. It is a wrapper of an Observable used to publish additional methods like `isActive(<alias>). |
| 26 | + |
| 27 | +```typescript |
| 28 | +asObservable(): Observable<MediaChange> |
| 29 | +``` |
| 30 | + |
| 31 | +Use the `.asObservable()` accessor method to access the **Observable** and use **RxJS** operators; such as `media.asObservable().filter(....)`. |
| 32 | + |
| 33 | +> Do not forget to **import** the specific RxJS operators you wish to use! |
| 34 | +
|
| 35 | +```typescript |
| 36 | +import { Component, OnDestroy, OnInit } from '@angular/core'; |
| 37 | +import { MediaChange, MediaObserver } from '@angular/flex-layout'; |
| 38 | +import { Subscription } from 'rxjs'; |
| 39 | +import { distinctUntilChanged } from 'rxjs/operators'; |
| 40 | + |
| 41 | +@Component({ |
| 42 | + selector: 'app-root', |
| 43 | + templateUrl: './app.component.html', |
| 44 | + styleUrls: ['./app.component.css'], |
| 45 | +}) |
| 46 | +export class AppComponent implements OnInit, OnDestroy { |
| 47 | + constructor(private mediaObserver: MediaObserver) {} |
| 48 | + private mediaSubscription: Subscription; |
| 49 | + private activeMediaQuery = ''; |
| 50 | + |
| 51 | + ngOnInit(): void { |
| 52 | + this.mediaSubscription = this.mediaObserver |
| 53 | + .asObservable() |
| 54 | + .subscribe((change) => { |
| 55 | + change.forEach((item) => { |
| 56 | + this.activeMediaQuery = item |
| 57 | + ? `'${item.mqAlias}' = (${item.mediaQuery})` |
| 58 | + : ''; |
| 59 | + if (item.mqAlias === 'xs') { |
| 60 | + this.loadMobileContent(); |
| 61 | + } |
| 62 | + console.log('activeMediaQuery', this.activeMediaQuery); |
| 63 | + }); |
| 64 | + }); |
| 65 | + } |
| 66 | + |
| 67 | + loadMobileContent() { |
| 68 | + // Do something special since the viewport is currently |
| 69 | + // using mobile display sizes. |
| 70 | + } |
| 71 | + |
| 72 | + ngOnDestroy(): void { |
| 73 | + this.mediaSubscription.unsubscribe(); |
| 74 | + } |
| 75 | +} |
| 76 | +``` |
| 77 | + |
| 78 | +This class uses the BreakPoint Registry to inject alias information into the raw MediaChange |
| 79 | +notification. For custom mediaQuery notifications, alias information will not be injected and |
| 80 | +those fields will be an empty string ['']. |
| 81 | + |
| 82 | +> This method is useful when the developer is not interested in using RxJS operators (eg `.map()`, `.filter()`). |
| 83 | +
|
| 84 | +#### 2. **`MediaObserver::asObservable()`** |
| 85 | + |
| 86 | +```typescript |
| 87 | +import { Component } from '@angular/core'; |
| 88 | +import { Subscription } from 'rxjs/Subscription'; |
| 89 | +import { filter } from 'rxjs/operators/filter'; |
| 90 | + |
| 91 | +import { MediaChange, MediaObserver } from '@angular/flex-layout'; |
| 92 | + |
| 93 | +@Component({ |
| 94 | + selector: 'responsive-component', |
| 95 | +}) |
| 96 | +export class MyDemo { |
| 97 | + constructor(media: MediaObserver) { |
| 98 | + media |
| 99 | + .asObservable() |
| 100 | + .pipe(filter((change: MediaChange[]) => change[0].mqAlias == 'xs')) |
| 101 | + .subscribe(() => { |
| 102 | + this.loadMobileContent(); |
| 103 | + }); |
| 104 | + |
| 105 | + loadMobileContent() {} |
| 106 | +} |
| 107 | +``` |
| 108 | +
|
| 109 | +#### 3. **`MediaObserver::isActive()`** |
| 110 | +
|
| 111 | +```typescript |
| 112 | +isActive(query: string): boolean |
| 113 | +``` |
| 114 | +
|
| 115 | +This method is useful both for expressions in component templates and in component imperative logic. The query can be an alias or a mediaQuery. |
| 116 | +
|
| 117 | +For example: |
| 118 | +
|
| 119 | +- `print and (max-width: 600px)` is a mediaQuery for printing with mobile viewport sizes. |
| 120 | +- `xs` is an alias associated with the mediaQuery for mobile viewport sizes. |
| 121 | + |
| 122 | +```typescript |
| 123 | +import { Component, OnInit } from '@angular/core'; |
| 124 | +import { MediaChange, MediaObserver } from '@angular/flex-layout'; |
| 125 | +
|
| 126 | +const PRINT_MOBILE = 'print and (max-width: 600px)'; |
| 127 | +
|
| 128 | +@Component({ |
| 129 | + selector: 'responsive-component', |
| 130 | + template: ` |
| 131 | + <div class="ad-content" *ngIf="media.isActive('xs')"> |
| 132 | + Only shown if on mobile viewport sizes |
| 133 | + </div> |
| 134 | + `, |
| 135 | +}) |
| 136 | +export class MyDemo implements OnInit { |
| 137 | + constructor(public media: MediaObserver) {} |
| 138 | +
|
| 139 | + ngOnInit() { |
| 140 | + if (this.media.isActive('xs') && !this.media.isActive(PRINT_MOBILE)) { |
| 141 | + this.loadMobileContent(); |
| 142 | + } |
| 143 | + } |
| 144 | +
|
| 145 | + loadMobileContent() { |
| 146 | + /* .... */ |
| 147 | + } |
| 148 | +} |
| 149 | +``` |
0 commit comments