Created
March 4, 2019 10:18
-
-
Save rekoch/47c8fd5001f5287b2b199c69ff78b943 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import {Injectable} from '@angular/core'; | |
import {BreakpointObserver, BreakpointState} from '@angular/cdk/layout'; | |
import {BehaviorSubject} from 'rxjs'; | |
export enum Breakpoints { | |
'XS' = 'xs', | |
'SM' = 'sm', | |
'MD' = 'md', | |
'LG' = 'lg', | |
'XL' = 'xl' | |
} | |
@Injectable({ | |
providedIn: 'root' | |
}) | |
export class ScreenBreakpointObserverService { | |
xsBreakpoint = ['(max-width:575.98px)']; | |
smBreakpoint = ['(min-width:576px) and (max-width:767.98px)']; | |
mdBreakpoint = ['(min-width:768px) and (max-width:991.98px)']; | |
lgBreakpoint = ['(min-width:992px) and (max-width:1199.98px)']; | |
xlBreakpoint = '(min-width:1200px)'; | |
public screenSizeObserver = new BehaviorSubject<Breakpoints[]>([Breakpoints.XS]); | |
constructor(private breakpointObserver: BreakpointObserver) { | |
this.initObservers(); | |
} | |
private initObservers() { | |
this.breakpointObserver.observe(this.xsBreakpoint).subscribe((state: BreakpointState) => { | |
if (state.matches) { | |
this.screenSizeObserver.next([Breakpoints.XS]); | |
} | |
}); | |
this.breakpointObserver.observe(this.smBreakpoint).subscribe((state: BreakpointState) => { | |
if (state.matches) { | |
this.screenSizeObserver.next([Breakpoints.XS, Breakpoints.SM]); | |
} | |
}); | |
this.breakpointObserver.observe(this.mdBreakpoint).subscribe((state: BreakpointState) => { | |
if (state.matches) { | |
this.screenSizeObserver.next([Breakpoints.XS, Breakpoints.SM, Breakpoints.MD]); | |
} | |
}); | |
this.breakpointObserver.observe(this.lgBreakpoint).subscribe((state: BreakpointState) => { | |
if (state.matches) { | |
this.screenSizeObserver.next([Breakpoints.XS, Breakpoints.SM, Breakpoints.MD, Breakpoints.LG]); | |
} | |
}); | |
this.breakpointObserver.observe(this.xlBreakpoint).subscribe((state: BreakpointState) => { | |
if (state.matches) { | |
this.screenSizeObserver.next([Breakpoints.XS, Breakpoints.SM, Breakpoints.MD, Breakpoints.LG, Breakpoints.XL]); | |
} | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment