@@ -42,6 +42,13 @@ export class NavigationComponent implements OnDestroy {
4242 }
4343 }
4444
45+ onBackdropKeyDown ( event : KeyboardEvent ) : void {
46+ if ( event . key === 'Enter' || event . key === ' ' ) {
47+ event . preventDefault ( ) ;
48+ this . closeMobileMenu ( ) ;
49+ }
50+ }
51+
4552 @HostListener ( 'window:scroll' )
4653 onWindowScroll ( ) : void {
4754 if ( ! isPlatformBrowser ( this . platformId ) ) {
@@ -52,6 +59,16 @@ export class NavigationComponent implements OnDestroy {
5259 this . isScrolled . set ( scrollY > 10 ) ;
5360 }
5461
62+ @HostListener ( 'window:resize' )
63+ onWindowResize ( ) : void {
64+ if ( ! isPlatformBrowser ( this . platformId ) ) {
65+ return ;
66+ }
67+ if ( window . innerWidth >= 1024 && this . isMobileMenuOpen ( ) ) {
68+ this . closeMobileMenu ( ) ;
69+ }
70+ }
71+
5572 private setupRouteTracking ( ) : void {
5673 this . router . events
5774 . pipe (
@@ -60,6 +77,9 @@ export class NavigationComponent implements OnDestroy {
6077 )
6178 . subscribe ( ( ) => {
6279 this . currentRoute . set ( this . router . url ) ;
80+ if ( this . isMobileMenuOpen ( ) ) {
81+ this . closeMobileMenu ( ) ;
82+ }
6383 } ) ;
6484 }
6585
@@ -76,11 +96,25 @@ export class NavigationComponent implements OnDestroy {
7696 }
7797
7898 toggleMobileMenu ( ) : void {
79- this . isMobileMenuOpen . update ( ( open ) => ! open ) ;
99+ if ( this . isMobileMenuOpen ( ) ) {
100+ this . closeMobileMenu ( ) ;
101+ } else {
102+ this . openMobileMenu ( ) ;
103+ }
104+ }
105+
106+ openMobileMenu ( ) : void {
107+ if ( this . isMobileMenuOpen ( ) ) {
108+ return ;
109+ }
110+ this . isMobileMenuOpen . set ( true ) ;
80111 this . updateBodyScroll ( ) ;
81112 }
82113
83114 closeMobileMenu ( ) : void {
115+ if ( ! this . isMobileMenuOpen ( ) ) {
116+ return ;
117+ }
84118 this . isMobileMenuOpen . set ( false ) ;
85119 this . updateBodyScroll ( ) ;
86120 }
@@ -92,32 +126,42 @@ export class NavigationComponent implements OnDestroy {
92126
93127 const body = document . body ;
94128 if ( this . isMobileMenuOpen ( ) ) {
129+ const scrollY = window . scrollY ;
95130 body . style . overflow = 'hidden' ;
96131 body . style . position = 'fixed' ;
97132 body . style . width = '100%' ;
98- body . style . top = `-${ window . scrollY } px` ;
133+ body . style . top = `-${ scrollY } px` ;
134+ body . setAttribute ( 'data-scroll-y' , scrollY . toString ( ) ) ;
99135 } else {
100- const scrollY = body . style . top ;
101- body . style . overflow = '' ;
102- body . style . position = '' ;
103- body . style . width = '' ;
104- body . style . top = '' ;
105- if ( scrollY ) {
106- window . scrollTo ( 0 , parseInt ( scrollY || '0' ) * - 1 ) ;
136+ this . restoreBodyScroll ( ) ;
137+ }
138+ }
139+
140+ private restoreBodyScroll ( ) : void {
141+ if ( ! isPlatformBrowser ( this . platformId ) ) {
142+ return ;
143+ }
144+ const body = document . body ;
145+ const scrollY = body . getAttribute ( 'data-scroll-y' ) ;
146+ body . style . overflow = '' ;
147+ body . style . position = '' ;
148+ body . style . width = '' ;
149+ body . style . top = '' ;
150+ body . removeAttribute ( 'data-scroll-y' ) ;
151+ if ( scrollY ) {
152+ const scrollPosition = parseInt ( scrollY , 10 ) ;
153+ if ( ! isNaN ( scrollPosition ) && scrollPosition >= 0 ) {
154+ window . scrollTo ( 0 , scrollPosition ) ;
107155 }
108156 }
109157 }
110158
111159 ngOnDestroy ( ) : void {
112160 this . destroy$ . next ( ) ;
113161 this . destroy$ . complete ( ) ;
114-
115162 if ( isPlatformBrowser ( this . platformId ) && this . isMobileMenuOpen ( ) ) {
116- const body = document . body ;
117- body . style . overflow = '' ;
118- body . style . position = '' ;
119- body . style . width = '' ;
120- body . style . top = '' ;
163+ this . isMobileMenuOpen . set ( false ) ;
164+ this . restoreBodyScroll ( ) ;
121165 }
122166 }
123167
0 commit comments