Skip to content

Commit b09806b

Browse files
committed
feat(directive): signal compatible
1 parent 1f71a78 commit b09806b

File tree

3 files changed

+32
-26
lines changed

3 files changed

+32
-26
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "ngx-href",
3-
"version": "19.0.1",
3+
"version": "19.1.0",
44
"license": "MIT",
55
"author": "Raphael Balet",
66
"maintainers": [

projects/ngx-href/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "ngx-href",
3-
"version": "19.0.1",
3+
"version": "19.1.0",
44
"license": "MIT",
55
"author": {
66
"name": "Raphaël Balet",

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

+30-24
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,30 @@
1-
import { Directive, ElementRef, HostBinding, HostListener, Input, OnDestroy } from '@angular/core'
1+
import { Directive, ElementRef, HostListener, Input, OnDestroy, signal } from '@angular/core'
22
import { Router } from '@angular/router'
33
import { NgxHrefService } from './href.service'
44

55
@Directive({
66
standalone: true,
77
selector: 'a[href], button[href]',
8+
host: {
9+
'[attr.rel]': 'rel$()',
10+
'[attr.target]': 'target$()',
11+
'[attr.href]': 'href$()',
12+
},
813
})
914
export class NgxHrefDirective implements OnDestroy {
1015
private _tagName: 'BUTTON' | 'A' = this._elementRef.nativeElement.tagName
1116

12-
@HostBinding('attr.rel') relAttr?: string
13-
@HostBinding('attr.target') targetAttr?: string
14-
@HostBinding('attr.href') hrefAttr: string | null = ''
17+
rel$ = signal<string>('')
18+
target$ = signal<string>('')
19+
href$ = signal<string | null>('')
1520

1621
@HostListener('click', ['$event']) onClick(event: PointerEvent) {
17-
if (!this.hrefAttr || !this._routeOnClick) return
22+
if (!this.href$() || !this._routeOnClick) return
1823

1924
event.preventDefault()
2025

21-
const fragments = this.hrefAttr.split('#')
26+
const fragments = this.href$()?.split('#')
27+
if (!fragments) return
2228

2329
if (fragments.length >= 2) {
2430
const urlFragments = this._router.url.split('#')
@@ -35,19 +41,19 @@ export class NgxHrefDirective implements OnDestroy {
3541
}
3642

3743
@Input() set rel(value: string) {
38-
this.relAttr = value
44+
this.rel$.set(value)
3945
}
4046
@Input() set target(value: string) {
41-
this.targetAttr = value
47+
this.target$.set(value)
4248
}
4349
@Input() set href(value: string) {
4450
if (!value) {
45-
this.hrefAttr = null
51+
this.href$.set(null)
4652
return
4753
}
4854

4955
try {
50-
this.hrefAttr = value?.replace(/ /g, '') || ''
56+
this.href$.set(value?.replace(/ /g, '') || '')
5157
} catch (error) {
5258
console.error('ngx-href: Expecting a string', '\n', error)
5359
}
@@ -63,7 +69,7 @@ export class NgxHrefDirective implements OnDestroy {
6369
}
6470
}
6571

66-
private _hrefAttr?: string // spam protection
72+
private _hrefAttr?: string | null // spam protection
6773
private _mouseenterListener: any = null // EventListener | null
6874
private _clickListener: any = null // EventListener | null
6975
private _routeOnClick = false
@@ -75,15 +81,15 @@ export class NgxHrefDirective implements OnDestroy {
7581
) {}
7682

7783
private _isLinkMailOrPhone(): boolean {
78-
if (this.hrefAttr?.startsWith('mailto') || this.hrefAttr?.startsWith('tel')) {
84+
if (this.href$()?.startsWith('mailto') || this.href$()?.startsWith('tel')) {
7985
if (this._ngxHrefService.avoidSpam) {
80-
this._hrefAttr = this.hrefAttr
86+
this._hrefAttr = this.href$()
8187

82-
if (this.hrefAttr?.startsWith('mailto')) this.hrefAttr = 'mailto:obfuscated'
83-
else this.hrefAttr = 'tel:obfuscated'
88+
if (this.href$()?.startsWith('mailto')) this.href$.set('mailto:obfuscated')
89+
else this.href$.set('tel:obfuscated')
8490

8591
this._mouseenterListener = () => {
86-
if (this._hrefAttr) this.hrefAttr = this._hrefAttr
92+
if (this._hrefAttr) this.href$.set(this._hrefAttr)
8793
}
8894

8995
this._elementRef.nativeElement.addEventListener('mouseenter', this._mouseenterListener)
@@ -97,32 +103,32 @@ export class NgxHrefDirective implements OnDestroy {
97103
}
98104

99105
private _isLinkExternal(): boolean {
100-
return this.hrefAttr?.startsWith('http') ? true : false
106+
return this.href$()?.startsWith('http') ? true : false
101107
}
102108
private _prepareOpenLink() {
103-
if (!this.relAttr && this._ngxHrefService.defaultRelAttr)
104-
this.relAttr = this._ngxHrefService.defaultRelAttr
105-
if (!this.targetAttr) this.targetAttr = this._ngxHrefService.defaultTargetAttr
109+
if (!this.rel$() && this._ngxHrefService.defaultRelAttr)
110+
this.rel$.set(this._ngxHrefService.defaultRelAttr)
111+
if (!this.target$()) this.target$.set(this._ngxHrefService.defaultTargetAttr)
106112

107113
this._clickListener = (event: PointerEvent) => {
108114
event.preventDefault()
109115

110-
const hrefAttr = this._hrefAttr || this.hrefAttr
111-
if (hrefAttr) window.open(hrefAttr, this.targetAttr, this.relAttr)
116+
const hrefAttr = this._hrefAttr || this.href$()
117+
if (hrefAttr) window.open(hrefAttr, this.target$(), this.rel$())
112118
}
113119

114120
this._elementRef.nativeElement.addEventListener('click', this._clickListener)
115121
}
116122

117123
private _isSamePageLink(): boolean {
118-
return this.hrefAttr && this.hrefAttr[0] === '#' ? true : false
124+
return this.href$() && (this.href$() as any)[0] === '#' ? true : false
119125
}
120126

121127
private _prepareScrollToLink() {
122128
this._clickListener = (event: PointerEvent) => {
123129
event.preventDefault()
124130

125-
if (this.hrefAttr) this._ngxHrefService.scrollTo(this.hrefAttr.substring(1))
131+
if (this.href$()) this._ngxHrefService.scrollTo(this.href$()?.substring(1))
126132
}
127133

128134
this._elementRef.nativeElement.addEventListener('click', this._clickListener)

0 commit comments

Comments
 (0)