1234567891011121314151617181920212223242526272829303132333435363738 |
- import { DestroyRef, Injectable, inject } from '@angular/core';
- import { takeUntilDestroyed } from '@angular/core/rxjs-interop';
- import { BehaviorSubject, fromEvent } from 'rxjs';
- import { debounceTime, distinctUntilChanged, map, startWith, tap } from 'rxjs/operators';
- import * as i0 from "@angular/core";
- export class ScreenSizeService {
- constructor() {
- this.destroyRef = inject(DestroyRef);
- this.screenSizeSubject = new BehaviorSubject(this.getScreenSize());
- this.screenSize$ = this.screenSizeSubject.asObservable();
- fromEvent(window, 'resize')
- .pipe(distinctUntilChanged(), debounceTime(200), map(() => this.getScreenSize()), startWith(this.getScreenSize()), tap(() => console.log('test updated')), takeUntilDestroyed(this.destroyRef))
- .subscribe(this.screenSizeSubject);
- }
- getScreenSize() {
- const width = window.innerWidth;
- if (width < 640)
- return 'xs';
- if (width >= 640 && width < 768)
- return 'sm';
- if (width >= 768 && width < 1024)
- return 'md';
- if (width >= 1024 && width < 1280)
- return 'lg';
- if (width >= 1280 && width < 1536)
- return 'xl';
- return '2xl';
- }
- static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "17.3.5", ngImport: i0, type: ScreenSizeService, deps: [], target: i0.ɵɵFactoryTarget.Injectable }); }
- static { this.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "17.3.5", ngImport: i0, type: ScreenSizeService, providedIn: 'root' }); }
- }
- i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "17.3.5", ngImport: i0, type: ScreenSizeService, decorators: [{
- type: Injectable,
- args: [{
- providedIn: 'root',
- }]
- }], ctorParameters: () => [] });
|