Skip to content

Commit b0627a5

Browse files
committed
feat(logic): use Renderer2
1 parent 4b66bf6 commit b0627a5

File tree

5 files changed

+36
-67
lines changed

5 files changed

+36
-67
lines changed

README.md

Lines changed: 12 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,17 @@ A library that allows href to understand Angular's router while retaining its de
1111
2. Support scroll with the `#` attributes and let you configure the [scrolling logic](#scroll-logic)
1212
3. Automatically append `rel="nooepener"` & `target="_blank"` to external link [if wished so](#installation)
1313
4. Support using `href` with the html `button` [attribute](#directive)
14-
5. Enable easy `Scroll when ready` mechanism
14+
5. Enable easy `Scroll when ready` mechanism which works with `SSR`
1515
6. Let you transform text to well formatted `anchor`
1616

1717
## Demo
1818
- https://stackblitz.com/~/github.com/rbalet/ngx-href
1919

20+
## 19.0.0 Breaking change
21+
* **Now use `scrollIntoView` which render the `Offset` useless.**
22+
* Please use [scroll-margin-top](https://developer.mozilla.org/en-US/docs/Web/CSS/scroll-margin-top) instead.
23+
* Nows also work on SSR
24+
2025
## Installation
2126

2227
```sh
@@ -31,8 +36,8 @@ import { NgxHrefModule } from 'ngx-href'
3136
/** Default
3237
* avoidSpam="false"
3338
* behavior="auto"
34-
* defaultOffset="0"
35-
* navbarOffset="0"
39+
* block="start"
40+
* inline="nearest"
3641
* rel=undefined
3742
* retryTimeout=undefined
3843
* target="_self"
@@ -43,8 +48,8 @@ import { NgxHrefModule } from 'ngx-href'
4348
NgxHrefModule.forRoot({
4449
avoidSpam: true,
4550
behavior:"smooth",
46-
defaultOffset:"30",
47-
navbarOffset:"60",
51+
block:"center",
52+
inline:"nearest",
4853
rel:"noopener nofollow",
4954
retryTimeout: 300,
5055
target:"_blank",
@@ -69,39 +74,8 @@ Can also be passed individually directly through html
6974
<a href="https://my-external-url.com" behavior="instant">
7075
```
7176

72-
### defaultOffset
73-
The standard offset to be added to your website `scrollTo` logic
74-
75-
**Default:** `0`
76-
**Accepted value:** `number`
77-
Together with the `navbarOffset` will be the total offset for the scroll.
78-
79-
### navbarOffset
80-
An additional offset calculated base on your navbar height
81-
82-
**Default:** `0`
83-
**Accepted value:** `number`
84-
Together with the `defaultOffset` will be the total offset for the scroll.
85-
86-
You can update this value after the navbar is rendered.
87-
88-
```html
89-
<navbar #navbar>
90-
<!-- My html code -->
91-
</navbar>
92-
```
93-
94-
```typescript
95-
@ViewChild('navbar', { static: true }) navbar: ElementRef
96-
97-
constructor(
98-
private _ngxHrefService: NgxHrefService,
99-
) {}
100-
101-
ngAfterContentInit(): void {
102-
this._ngxHrefService.navbarOffset = this.navbar.nativeElement.offsetHeight
103-
}
104-
```
77+
### Offset
78+
If you wish to add offset, add `scroll-margin-top: $offset` to your targeted component -> [read more](https://developer.mozilla.org/en-US/docs/Web/CSS/scroll-margin-top)
10579

10680
### retryTimeout
10781
**Default:** `undefined`

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "ngx-href",
3-
"version": "18.2.0",
3+
"version": "19.0.0",
44
"license": "MIT",
55
"author": "Raphael Balet",
66
"maintainers": [

projects/ngx-href/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "ngx-href",
3-
"version": "18.1.2",
3+
"version": "19.0.0",
44
"license": "MIT",
55
"author": {
66
"name": "Raphaël Balet",
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
export interface NgxHrefServiceConfig {
22
avoidSpam?: boolean
33
behavior?: ScrollBehavior
4-
navbarOffset?: number
5-
defaultOffset?: number
4+
block?: ScrollLogicalPosition
65
defaultRelAttr?: string
76
defaultTargetAttr?: string
7+
inline?: ScrollLogicalPosition
88
retryTimeout?: number
99
}

projects/ngx-href/src/lib/href.service.ts

Lines changed: 20 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Inject, Injectable } from '@angular/core'
1+
import { Inject, Injectable, Renderer2, RendererFactory2 } from '@angular/core'
22
import { BehaviorSubject } from 'rxjs'
33
import { NgxHrefServiceProvider } from './href.const'
44
import { NgxHrefServiceConfig } from './href.interface'
@@ -7,27 +7,34 @@ import { NgxHrefServiceConfig } from './href.interface'
77
providedIn: 'root',
88
})
99
export 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

Comments
 (0)