-
Notifications
You must be signed in to change notification settings - Fork 0
/
viewport.service.ts
40 lines (32 loc) · 1.16 KB
/
viewport.service.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import { BreakpointObserver, BreakpointState } from '@angular/cdk/layout';
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';
export enum BREAKPOINT {
XS = '(min-width: 0)',
SM = '(min-width: 576px)',
MD = '(min-width: 768px)',
LG = '(min-width: 992px)',
XL = '(min-width: 1200px)',
XXL = '(min-width: 1400px)',
};
@Injectable({
providedIn: 'root'
})
export class ViewportService {
constructor(
private breakpointObserver: BreakpointObserver,
) { }
// NOTE: Kill the subscription properly.
public observe(...breakpoint: BREAKPOINT[]): Observable<BREAKPOINT[]> {
return this.breakpointObserver
.observe(Object.values(breakpoint))
.pipe(map(breakpointsState => this.filterBrakpoints(breakpointsState)));
}
private filterBrakpoints(breakpointsState: BreakpointState): BREAKPOINT[] {
if (!breakpointsState || !breakpointsState.matches) return [];
return Object.entries(breakpointsState.breakpoints)
.filter(([, state]) => state)
.map(([breakpoint]) => breakpoint as BREAKPOINT);
}
}