@@ -26,6 +26,7 @@ import { injectRouter } from './router';
26
26
'(touchstart)' : 'type() === "internal" && handleClick($event)' ,
27
27
'(mouseenter)' : 'type() === "internal" && handleMouseEnter($event)' ,
28
28
'(mouseleave)' : 'type() === "internal" && handleMouseLeave()' ,
29
+ '[class]' : '[isActive() ? activeClass() : ""]' ,
29
30
'[attr.data-active]' : 'isActive()' ,
30
31
'[attr.data-type]' : 'type()' ,
31
32
'[attr.data-transitioning]' :
@@ -41,14 +42,25 @@ export class Link {
41
42
alias : 'link' ,
42
43
transform : (
43
44
value :
44
- | ( Omit < LinkOptions , 'to' > & {
45
+ | ( Omit < LinkOptions , 'to' | 'activeOptions' > & {
45
46
to : NonNullable < LinkOptions [ 'to' ] > ;
46
47
} )
47
48
| NonNullable < LinkOptions [ 'to' ] >
48
49
) => {
49
50
return ( typeof value === 'object' ? value : { to : value } ) as LinkOptions ;
50
51
} ,
51
52
} ) ;
53
+ linkActiveOptions = input (
54
+ { class : 'active' } ,
55
+ {
56
+ alias : 'linkActive' ,
57
+ transform : (
58
+ value : ( LinkOptions [ 'activeOptions' ] & { class : string } ) | string
59
+ ) => {
60
+ return typeof value === 'string' ? { class : value } : value ;
61
+ } ,
62
+ }
63
+ ) ;
52
64
53
65
router = injectRouter ( ) ;
54
66
hostElement = inject < ElementRef < HTMLAnchorElement > > ( ElementRef ) ;
@@ -59,34 +71,34 @@ export class Link {
59
71
( ) => this . router . routerState ( ) . location . search
60
72
) ;
61
73
62
- private to = computed ( ( ) => this . linkOptions ( ) . to ) ;
63
74
protected disabled = computed ( ( ) => this . linkOptions ( ) . disabled ) ;
75
+ private to = computed ( ( ) => this . linkOptions ( ) . to ) ;
64
76
private userFrom = computed ( ( ) => this . linkOptions ( ) . from ) ;
65
77
private userReloadDocument = computed (
66
78
( ) => this . linkOptions ( ) . reloadDocument
67
79
) ;
68
80
private userPreload = computed ( ( ) => this . linkOptions ( ) . preload ) ;
69
81
private userPreloadDelay = computed ( ( ) => this . linkOptions ( ) . preloadDelay ) ;
70
- private exactActiveOptions = computed (
71
- ( ) => this . linkOptions ( ) . activeOptions ?. exact
72
- ) ;
82
+ private exactActiveOptions = computed ( ( ) => this . linkActiveOptions ( ) . exact ) ;
73
83
private includeHashActiveOptions = computed (
74
- ( ) => this . linkOptions ( ) . activeOptions ? .includeHash
84
+ ( ) => this . linkActiveOptions ( ) . includeHash
75
85
) ;
76
86
private includeSearchActiveOptions = computed (
77
- ( ) => this . linkOptions ( ) . activeOptions ? .includeSearch
87
+ ( ) => this . linkActiveOptions ( ) . includeSearch
78
88
) ;
79
89
private explicitUndefinedActiveOptions = computed (
80
- ( ) => this . linkOptions ( ) . activeOptions ? .explicitUndefined
90
+ ( ) => this . linkActiveOptions ( ) . explicitUndefined
81
91
) ;
92
+ protected activeClass = computed ( ( ) => this . linkActiveOptions ( ) . class ) ;
82
93
83
94
protected type = computed ( ( ) => {
84
95
const to = this . to ( ) ;
85
96
try {
86
97
new URL ( `${ to } ` ) ;
87
98
return 'external' ;
88
- } catch { }
89
- return 'internal' ;
99
+ } catch {
100
+ return 'internal' ;
101
+ }
90
102
} ) ;
91
103
92
104
private from = computed ( ( ) => {
@@ -200,6 +212,13 @@ export class Link {
200
212
this . doPreload ( ) ;
201
213
}
202
214
} ) ;
215
+
216
+ effect ( ( onCleanup ) => {
217
+ const unsub = this . router . subscribe ( 'onResolved' , ( ) => {
218
+ this . transitioning . set ( false ) ;
219
+ } ) ;
220
+ onCleanup ( ( ) => unsub ( ) ) ;
221
+ } ) ;
203
222
}
204
223
205
224
protected handleClick ( event : MouseEvent ) {
@@ -221,11 +240,6 @@ export class Link {
221
240
event . preventDefault ( ) ;
222
241
this . transitioning . set ( true ) ;
223
242
224
- const unsub = this . router . subscribe ( 'onResolved' , ( ) => {
225
- unsub ( ) ;
226
- this . transitioning . set ( false ) ;
227
- } ) ;
228
-
229
243
this . router . navigate ( this . navigateOptions ( ) ) ;
230
244
}
231
245
0 commit comments