-
-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathns-router-link-active.ts
More file actions
245 lines (221 loc) · 8.46 KB
/
Copy pathns-router-link-active.ts
File metadata and controls
245 lines (221 loc) · 8.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
import { AfterContentInit, ChangeDetectorRef, ContentChildren, Directive, ElementRef, EventEmitter, inject, Input, OnChanges, OnDestroy, Output, QueryList, Renderer2, SimpleChanges, untracked } from '@angular/core';
import { from, of, Subscription } from 'rxjs';
import { mergeAll } from 'rxjs/operators';
import { IsActiveMatchOptions, NavigationEnd, Router, isActive } from '@angular/router';
import { NSRouterLink } from './ns-router-link';
// Inline equivalent of upstream's exactMatchOptions
const exactMatchOptions: IsActiveMatchOptions = {
paths: 'exact',
fragment: 'ignored',
matrixParams: 'ignored',
queryParams: 'exact',
};
// Inline equivalent of upstream's subsetMatchOptions
const subsetMatchOptions: IsActiveMatchOptions = {
paths: 'subset',
fragment: 'ignored',
matrixParams: 'ignored',
queryParams: 'subset',
};
/**
* Use instead of `'paths' in options` to be compatible with property renaming
*/
function isActiveMatchOptions(options: { exact: boolean } | Partial<IsActiveMatchOptions>): options is Partial<IsActiveMatchOptions> {
const o = options as Partial<IsActiveMatchOptions>;
return !!(o.paths || o.matrixParams || o.queryParams || o.fragment);
}
/**
* The NSRouterLinkActive directive lets you add a CSS class to an element when the link's route
* becomes active.
*
* Consider the following example:
*
* ```
* <Label [nsRouterLink]="/user/bob" [nsRouterLinkActive]="'active-link'" text="Bob"></Label>
* ```
*
* When the url is either "/user" or "/user/bob", the active-link class will
* be added to the component. If the url changes, the class will be removed.
*
* You can set more than one class, as follows:
*
* ```
* <Label [nsRouterLink]="/user/bob" [nsRouterLinkActive]="'class1 class2'" text="Bob"></Label>
* <Label [nsRouterLink]="/user/bob" [nsRouterLinkActive]="['class1', 'class2']" text="Bob"></Label>
* ```
*
* You can configure NSRouterLinkActive by passing `exact: true`. This will add the
* classes only when the url matches the link exactly.
*
* ```
* <Label [nsRouterLink]="/user/bob" [nsRouterLinkActive]="'active-link'"
* [nsRouterLinkActiveOptions]="{exact: true}" text="Bob"></Label>
* ```
*
* To directly check the `isActive` status of the link, assign the `NSRouterLinkActive`
* instance to a template variable.
* For example, the following checks the status without assigning any CSS classes:
*
* ```
* <Label [nsRouterLink]="/user/bob" nsRouterLinkActive #rla="routerLinkActive"
* [text]="'Bob ' + (rla.isActive ? '(already open)' : '')"></Label>
* ```
*
* You can apply the NSRouterLinkActive directive to an ancestor of a RouterLink.
*
* ```
* <StackLayout [nsRouterLinkActive]="'active-link'" [nsRouterLinkActiveOptions]="{exact: true}">
* <Label [nsRouterLink]="/user/jim" text="Jim"></Label>
* <Label [nsRouterLink]="/user/bob" text="Bob"></Label>
* </StackLayout>
* ```
*
* This will set the active-link class on the StackLayout if the url is either "/user/jim" or
* "/user/bob".
*
* The `NSRouterLinkActive` directive can also be used to set the aria-current attribute
* to provide an alternative distinction for active elements to visually impaired users.
*
* For example, the following code adds the 'active' class to the Home Page link when it is
* indeed active and in such case also sets its aria-current attribute to 'page':
*
* ```
* <Label nsRouterLink="/" [nsRouterLinkActive]="'active'" ariaCurrentWhenActive="page" text="Home Page"></Label>
* ```
*/
@Directive({
selector: '[nsRouterLinkActive]',
exportAs: 'routerLinkActive',
standalone: true,
})
export class NSRouterLinkActive implements OnChanges, OnDestroy, AfterContentInit {
@ContentChildren(NSRouterLink, { descendants: true }) links!: QueryList<NSRouterLink>;
private classes: string[] = [];
private routerEventsSubscription: Subscription;
private linkInputChangesSubscription?: Subscription;
private _isActive = false;
get isActive(): boolean {
return this._isActive;
}
/**
* Options to configure how to determine if the router link is active.
*
* These options are passed to the `isActive()` function.
*
* @see {@link isActive}
*/
@Input() nsRouterLinkActiveOptions: { exact: boolean } | Partial<IsActiveMatchOptions> = { exact: false };
/**
* Aria-current attribute to apply when the router link is active.
*
* Possible values: `'page'` | `'step'` | `'location'` | `'date'` | `'time'` | `true` | `false`.
*
* @see {@link https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/Attributes/aria-current}
*/
@Input() ariaCurrentWhenActive?: 'page' | 'step' | 'location' | 'date' | 'time' | true | false;
/**
*
* You can use the output `isActiveChange` to get notified each time the link becomes
* active or inactive.
*
* Emits:
* true -> Route is active
* false -> Route is inactive
*
* ```html
* <Label
* [nsRouterLink]="/user/bob"
* [nsRouterLinkActive]="'active-link'"
* (isActiveChange)="this.onRouterLinkActive($event)" text="Bob"></Label>
* ```
*/
@Output() readonly isActiveChange: EventEmitter<boolean> = new EventEmitter();
private readonly link = inject(NSRouterLink, { optional: true });
private readonly router = inject(Router);
private readonly element = inject(ElementRef);
private readonly renderer = inject(Renderer2);
private readonly cdr = inject(ChangeDetectorRef);
constructor() {
this.routerEventsSubscription = this.router.events.subscribe((s) => {
if (s instanceof NavigationEnd) {
this.update();
}
});
}
ngAfterContentInit(): void {
// `of(null)` is used to force subscribe body to execute once immediately (like `startWith`).
of(this.links.changes, of(null))
.pipe(mergeAll())
.subscribe(() => {
this.update();
this.subscribeToEachLinkOnChanges();
});
}
private subscribeToEachLinkOnChanges() {
this.linkInputChangesSubscription?.unsubscribe();
const allLinkChanges = [...this.links.toArray(), this.link]
.filter((link): link is NSRouterLink => !!link)
.map((link) => link.onChanges);
this.linkInputChangesSubscription = from(allLinkChanges)
.pipe(mergeAll())
.subscribe((link) => {
if (this._isActive !== this.isLinkActive(this.router)(link)) {
this.update();
}
});
}
@Input()
set nsRouterLinkActive(data: string[] | string) {
const classes = Array.isArray(data) ? data : data.split(' ');
this.classes = classes.filter((c) => !!c);
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
ngOnChanges(_changes: SimpleChanges): void {
this.update();
}
ngOnDestroy(): void {
this.routerEventsSubscription.unsubscribe();
this.linkInputChangesSubscription?.unsubscribe();
}
private update(): void {
if (!this.links || !this.router.navigated) return;
queueMicrotask(() => {
const hasActiveLinks = this.hasActiveLinks();
this.classes.forEach((c) => {
if (hasActiveLinks) {
this.renderer.addClass(this.element.nativeElement, c);
} else {
this.renderer.removeClass(this.element.nativeElement, c);
}
});
if (hasActiveLinks && this.ariaCurrentWhenActive !== undefined) {
this.renderer.setAttribute(this.element.nativeElement, 'aria-current', this.ariaCurrentWhenActive.toString());
} else {
this.renderer.removeAttribute(this.element.nativeElement, 'aria-current');
}
// Only emit change if the active state changed.
if (this._isActive !== hasActiveLinks) {
this._isActive = hasActiveLinks;
this.cdr.markForCheck();
// Emit on isActiveChange after classes are updated
this.isActiveChange.emit(hasActiveLinks);
}
});
}
private isLinkActive(router: Router): (link: NSRouterLink) => boolean {
const options: Partial<IsActiveMatchOptions> = isActiveMatchOptions(this.nsRouterLinkActiveOptions)
? this.nsRouterLinkActiveOptions
: // While the types should disallow `undefined` here, it's possible without strict inputs
(this.nsRouterLinkActiveOptions.exact ?? false)
? { ...exactMatchOptions }
: { ...subsetMatchOptions };
return (link: NSRouterLink) => {
const urlTree = link.urlTree;
return urlTree ? untracked(isActive(urlTree, router, options)) : false;
};
}
private hasActiveLinks(): boolean {
const isActiveCheckFn = this.isLinkActive(this.router);
return (this.link && isActiveCheckFn(this.link)) || this.links.some(isActiveCheckFn);
}
}