Device detection service using Angular CDK breakpoints.
File: src/utils/services/device.service.ts
import { BreakpointObserver } from '@angular/cdk/layout';
import { Injectable, inject } from '@angular/core';
import { Observable, map } from 'rxjs';
@Injectable({ providedIn: 'root' })
export class DeviceService {
private readonly breakpointObserver = inject(BreakpointObserver);
isHandset$: Observable<boolean> = this.breakpointObserver
.observe('(max-width: 640px)')
.pipe(map(result => result.matches));
isTablet$: Observable<boolean> = this.breakpointObserver
.observe('(max-width: 960px)')
.pipe(map(result => result.matches));
isLessThanMD$: Observable<boolean> = this.breakpointObserver
.observe('(max-width: 768px)')
.pipe(map(result => result.matches));
canViewToc$ = this.breakpointObserver.observe('(max-width: 960px)').pipe(
map(result => {
return !result.matches;
})
);
}
The DeviceService provides reactive observables for detecting device screen sizes and adjusting UI accordingly.
isHandset$: Emits true when viewport width is 640px or less (mobile devices)
isTablet$: Emits true when viewport width is 960px or less (tablets and below)
isLessThanMD$: Emits true when viewport width is 768px or less (below medium breakpoint)
canViewToc$: Emits true when viewport width is greater than 960px (desktop - suitable for showing table of contents)
The service uses standard Tailwind CSS-like breakpoints:
All observables are reactive and automatically update when the viewport size changes, making them ideal for responsive UI adjustments.
Angular Material Dev UI (or Angular Material Blocks) is one place stop for developers to explore components and blocks for their Angular Material and Tailwind CSS based applications.
Find us on X (Twitter), LinkedIn, Instagram & Threads