6
6
* found in the LICENSE file at https://angular.dev/license
7
7
*/
8
8
9
- import { Injectable , OnDestroy , inject } from '@angular/core' ;
9
+ import { Injectable , inject } from '@angular/core' ;
10
10
import { Event , NavigationEnd , Router } from '@angular/router' ;
11
11
import { filter , skip } from 'rxjs/operators' ;
12
- import { Subscription } from 'rxjs' ;
12
+ import { takeUntilDestroyed } from '@angular/core/ rxjs-interop ' ;
13
13
14
14
@Injectable ( {
15
15
providedIn : 'root' ,
16
16
} )
17
- export class NavigationFocusService implements OnDestroy {
17
+ export class NavigationFocusService {
18
18
private _router = inject ( Router ) ;
19
- private _subscriptions = new Subscription ( ) ;
20
19
private _navigationFocusRequests : HTMLElement [ ] = [ ] ;
21
20
private _skipLinkFocusRequests : HTMLElement [ ] = [ ] ;
22
21
private _skipLinkHref : string | null | undefined ;
@@ -27,24 +26,18 @@ export class NavigationFocusService implements OnDestroy {
27
26
readonly softNavigations = this . navigationEndEvents . pipe ( skip ( 1 ) ) ;
28
27
29
28
constructor ( ) {
30
- this . _subscriptions . add (
31
- this . softNavigations . subscribe ( ( ) => {
32
- // focus if url does not have fragment
33
- if ( ! this . _router . url . split ( '#' ) [ 1 ] ) {
34
- setTimeout ( ( ) => {
35
- if ( this . _navigationFocusRequests . length ) {
36
- this . _navigationFocusRequests [ this . _navigationFocusRequests . length - 1 ] . focus ( {
37
- preventScroll : true ,
38
- } ) ;
39
- }
40
- } , 100 ) ;
41
- }
42
- } ) ,
43
- ) ;
44
- }
45
-
46
- ngOnDestroy ( ) {
47
- this . _subscriptions . unsubscribe ( ) ;
29
+ this . softNavigations . pipe ( takeUntilDestroyed ( ) ) . subscribe ( ( ) => {
30
+ // focus if url does not have fragment
31
+ if ( ! this . _router . url . split ( '#' ) [ 1 ] ) {
32
+ setTimeout ( ( ) => {
33
+ if ( this . _navigationFocusRequests . length ) {
34
+ this . _navigationFocusRequests [ this . _navigationFocusRequests . length - 1 ] . focus ( {
35
+ preventScroll : true ,
36
+ } ) ;
37
+ }
38
+ } , 100 ) ;
39
+ }
40
+ } ) ;
48
41
}
49
42
50
43
requestFocusOnNavigation ( el : HTMLElement ) {
0 commit comments