I think the easiest and fastest is to implement media query css, but that's what you don't want. Then you can create a service that manages breakpoints and reuse it where you need it. It would be something similar to this:
import { Injectable } from '@angular/core';
import { BreakpointObserver, BreakpointState, Breakpoints } from '@angular/cdk/layout';
import { Subject } from 'rxjs';
@Injectable({
providedIn: 'root'
})
export class BreakpointsService {
isMobile: Subject<boolean> = new Subject();
constructor(
private breakpointObserver: BreakpointObserver,
) {
this.breakpointObserver
.observe([
Breakpoints.XSmall
])
.subscribe((state: BreakpointState) => {
if (state.matches) {
// console.log('isMobile');
this.isMobile.next(true);
} else {
// console.log('not isMobile');
this.isMobile.next(false);
}
});
}
isMobile$() {
return this.isMobile.asObservable();
}
isMatchedMobile() {
const rta = this.breakpointObserver.isMatched([Breakpoints.XSmall]);
this.isMobile.next(rta);
}
}
And in yours components use like this:
this.breakpointsService.isMobile$()
.pipe(
takeUntil(this.unsubscribe$)
)
.subscribe(value => {
this.isMobile = value;
});