1- import { Inject , Injectable } from '@angular/core'
1+ import { Inject , Injectable , Renderer2 , RendererFactory2 } from '@angular/core'
22import { BehaviorSubject } from 'rxjs'
33import { NgxHrefServiceProvider } from './href.const'
44import { NgxHrefServiceConfig } from './href.interface'
@@ -7,27 +7,34 @@ import { NgxHrefServiceConfig } from './href.interface'
77 providedIn : 'root' ,
88} )
99export class NgxHrefService {
10+ private renderer : Renderer2
11+
1012 anchor$ : BehaviorSubject < string | null > = new BehaviorSubject < string | null > ( null )
1113 loadedAnchor$ : BehaviorSubject < string | null > = new BehaviorSubject < string | null > ( null ) // Trigger the scrollTo mechanism from outside
1214
1315 avoidSpam ?: boolean
1416 behavior ! : ScrollBehavior
15- defaultOffset ! : number
16- navbarOffset ! : number
17+ block ! : ScrollLogicalPosition
1718 defaultRelAttr ?: string
1819 defaultTargetAttr ! : string
20+ inline ! : ScrollLogicalPosition
1921 retryTimeout ?: number
2022
2123 private _actualAnchor ?: string
2224
23- constructor ( @Inject ( NgxHrefServiceProvider ) _config : NgxHrefServiceConfig ) {
25+ constructor (
26+ @Inject ( NgxHrefServiceProvider ) _config : NgxHrefServiceConfig ,
27+ _rendererFactory : RendererFactory2 ,
28+ ) {
29+ this . renderer = _rendererFactory . createRenderer ( null , null )
30+
2431 this . avoidSpam = _config . avoidSpam
2532 this . behavior = _config . behavior || 'auto'
26- this . defaultOffset = typeof _config . defaultOffset === 'number' ? _config . defaultOffset : 0
27- this . navbarOffset = typeof _config . navbarOffset === 'number' ? _config . navbarOffset : 0
33+ this . block = _config . block || 'start'
2834 this . defaultRelAttr = _config . defaultRelAttr
2935 this . defaultTargetAttr = _config . defaultTargetAttr || '_self'
30- this . retryTimeout = _config . retryTimeout
36+ this . inline = _config . inline || 'nearest'
37+ this . retryTimeout = _config . retryTimeout || 0
3138
3239 this . loadedAnchor$ . subscribe ( ( anchor ) => {
3340 if ( anchor === this . _actualAnchor ) {
@@ -55,28 +62,16 @@ export class NgxHrefService {
5562 this . setAnchor ( anchor )
5663 }
5764
58- const anchorRef = document . getElementById ( anchor )
65+ const anchorRef : HTMLElement = this . renderer . selectRootElement ( `# ${ anchor } ` , true )
5966
6067 if ( anchorRef ) {
61- const offsetPosition =
62- anchorRef . getBoundingClientRect ( ) . top +
63- window . scrollY -
64- ( this . navbarOffset + this . defaultOffset )
65-
66- window . scrollTo ( {
67- top : offsetPosition ,
68- behavior : this . behavior ,
69- } )
68+ anchorRef . scrollIntoView ( { behavior : this . behavior , block : this . block , inline : this . inline } )
7069
71- if ( this . retryTimeout )
72- setTimeout ( ( ) => {
73- window . scrollTo ( {
74- top : offsetPosition ,
75- behavior : this . behavior ,
76- } )
77- } , this . retryTimeout )
70+ setTimeout ( ( ) => {
71+ anchorRef . scrollIntoView ( { behavior : this . behavior } )
7872
79- this . _actualAnchor = undefined
73+ this . _actualAnchor = undefined
74+ } , this . retryTimeout )
8075 } else {
8176 setTimeout ( ( ) => {
8277 if ( anchor !== this . _actualAnchor ) return
0 commit comments