0

Im looking for suggestions on how to make my angular project automatically detect and configure responsiveness.

Currently we are using a sort of matchmedia with queries such as "isMobile" etc. The project is responsive but doesnt detect a change in the responsiveness or size of the page automatically.

We want it to automatically detect a change in size that measures desktop, tablet or mobile and automatically update the responsiveness of the page to these configurations without needing to reload the page manually.

Any ideas or suggestions are welcome and please keep them coming cause this is a big project, thanks.

Example of a code piece we have in the project for this is down below.

    this.mobileQuery = media.matchMedia(`(max-width: ${MEDIA_BREAK_POINT}px)`);
    this._mobileQueryListener = () => changeDetectorRef.detectChanges();
    this.mobileQuery.addListener(this._mobileQueryListener);
    this.isMobile = this.service.isMobile;
    this.isTablet = this.service.isTablet;
    this.isDesktop = this.service.isDesktop;
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.7.5/angular.min.js"></script>

2
  • Hello Wanz, for this problem I think you should create a service that it subscribes to window resize event and check the device size. Commented Feb 4, 2022 at 9:22
  • Sounds like my kind of plan as well, do you have any suggestions on observables or other things that would be useful in order to achieve this? Not sure on HOW to write it and what to make use of.
    – Wanz
    Commented Feb 4, 2022 at 9:25

1 Answer 1

1

Yes of course:

enum DeviceSize='Mobile' | 'Tablet' | 'Desktop';
deviceSize$=new BehaviorSubject<DeviceSize>(DeviceSize.Desktop);
contructor(){
  this.initializeDeviceSize();
  fromEvent(window, 'resize')
      .pipe(
        startWith(null),
        debounceTime(1000),
      )
      .subscribe((c) => {
        this.onDeviceSizeChanged();
      });
}
initializeDeviceSize(){
 this.setDeviceSize();
}
onDeviceSizeChanged(){
 this.setDeviceSize();
}
setDeviceSize(){
 //check device size is which one of DeviceSize Enum
 if(isMobile){
   this.deviceSize$.next(DeviceSize.Mobile);
 }
 else if(isTablet)
 {
   this.deviceSizse$.next(DeviceSize.Tablet);
 }
 else {
   this.deviceSize$.next(DeviceSize.Desktop)
 }

}
5
  • Interesting, do you have a reason for using BehaviorSubject instead of example asObservable that you could explain perhaps?
    – Wanz
    Commented Feb 4, 2022 at 11:08
  • because when the component subscribe to deviceSize$ it need to know the last value of deviceSize. Commented Feb 4, 2022 at 12:17
  • Aha I see, is this example intended for a service or a component. The best way for me would be to have a service or something that I easily can implement into my components and set the responsiveness. Because there are a million components in the project so it would be nice to have something implemented in a service for example that can easily be imported and used by components without alot of code needed in the actual TS file of the component.
    – Wanz
    Commented Feb 4, 2022 at 12:32
  • this is implementation inside of your service. any component can subscribe to deviceSize$ and get the value. Commented Feb 4, 2022 at 12:36
  • thanks alot, I will look into it a bit more and take this into consideration!
    – Wanz
    Commented Feb 4, 2022 at 12:37

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.