1
0
mirror of https://github.com/bitwarden/browser synced 2026-02-19 19:04:01 +00:00
Files
browser/libs/components/src/navigation/side-nav.service.ts
Mark Youssef 018b4d5eb4 [CL-609] Close side nav when breakpoint changes (#15062)
* Close side nav when breakpoint changes

* Leverage side-nave listener instead

* Remove effect inside pipe

* Reuse isSmallScreen
2025-09-29 07:19:52 -07:00

55 lines
1.3 KiB
TypeScript

import { Injectable } from "@angular/core";
import { takeUntilDestroyed } from "@angular/core/rxjs-interop";
import { BehaviorSubject, Observable, combineLatest, fromEvent, map, startWith } from "rxjs";
@Injectable({
providedIn: "root",
})
export class SideNavService {
private _open$ = new BehaviorSubject<boolean>(!window.matchMedia("(max-width: 768px)").matches);
open$ = this._open$.asObservable();
private isSmallScreen$ = media("(max-width: 768px)");
isOverlay$ = combineLatest([this.open$, this.isSmallScreen$]).pipe(
map(([open, isSmallScreen]) => open && isSmallScreen),
);
constructor() {
this.isSmallScreen$.pipe(takeUntilDestroyed()).subscribe((isSmallScreen) => {
if (isSmallScreen) {
this.setClose();
}
});
}
get open() {
return this._open$.getValue();
}
setOpen() {
this._open$.next(true);
}
setClose() {
this._open$.next(false);
}
toggle() {
const curr = this._open$.getValue();
if (curr) {
this.setClose();
} else {
this.setOpen();
}
}
}
export const media = (query: string): Observable<boolean> => {
const mediaQuery = window.matchMedia(query);
return fromEvent<MediaQueryList>(mediaQuery, "change").pipe(
startWith(mediaQuery),
map((list: MediaQueryList) => list.matches),
);
};