Skip to content

Commit cf9620f

Browse files
atscottleonsenft
authored andcommitted
feat(router): Make match options optional in isActive
The behavior now matches RouterLinkActive.
1 parent 0c2efc0 commit cf9620f

File tree

5 files changed

+36
-29
lines changed

5 files changed

+36
-29
lines changed

goldens/public-api/router/index.api.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,7 @@ export interface InMemoryScrollingOptions {
370370
}
371371

372372
// @public
373-
export function isActive(url: string | UrlTree, router: Router, matchOptions: IsActiveMatchOptions): Signal<boolean>;
373+
export function isActive(url: string | UrlTree, router: Router, matchOptions?: IsActiveMatchOptions): Signal<boolean>;
374374

375375
// @public
376376
export interface IsActiveMatchOptions {

packages/router/src/directives/router_link_active.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ import {from, of, Subscription} from 'rxjs';
2727
import {mergeAll} from 'rxjs/operators';
2828

2929
import {Event, NavigationEnd} from '../events';
30-
import {exactMatchOptions, Router, subsetMatchOptions} from '../router';
31-
import {isActive, IsActiveMatchOptions} from '../url_tree';
30+
import {Router} from '../router';
31+
import {isActive, IsActiveMatchOptions, exactMatchOptions, subsetMatchOptions} from '../url_tree';
3232

3333
import {RouterLink} from './router_link';
3434

packages/router/src/router.ts

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,10 @@ import {StateManager} from './statemanager/state_manager';
5959
import {UrlHandlingStrategy} from './url_handling_strategy';
6060
import {
6161
containsTree,
62+
exactMatchOptions,
6263
IsActiveMatchOptions,
6364
isUrlTree,
65+
subsetMatchOptions,
6466
UrlSegmentGroup,
6567
UrlSerializer,
6668
UrlTree,
@@ -69,28 +71,6 @@ import {validateConfig} from './utils/config';
6971
import {afterNextNavigation} from './utils/navigations';
7072
import {RouterState} from './router_state';
7173

72-
/**
73-
* The equivalent `IsActiveMatchOptions` options for `isActive` is called with `true`
74-
* (exact = true).
75-
*/
76-
export const exactMatchOptions: IsActiveMatchOptions = {
77-
paths: 'exact',
78-
fragment: 'ignored',
79-
matrixParams: 'ignored',
80-
queryParams: 'exact',
81-
};
82-
83-
/**
84-
* The equivalent `IsActiveMatchOptions` options for `isActive` is called with `false`
85-
* (exact = false).
86-
*/
87-
export const subsetMatchOptions: IsActiveMatchOptions = {
88-
paths: 'subset',
89-
fragment: 'ignored',
90-
matrixParams: 'ignored',
91-
queryParams: 'subset',
92-
};
93-
9474
/**
9575
* @description
9676
*

packages/router/src/url_tree.ts

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,28 @@ const paramCompareMap: Record<ParamMatchOptions, ParamCompareFn> = {
8080
'ignored': () => true,
8181
};
8282

83+
/**
84+
* The equivalent `IsActiveMatchOptions` options for `isActive` is called with `true`
85+
* (exact = true).
86+
*/
87+
export const exactMatchOptions: IsActiveMatchOptions = {
88+
paths: 'exact',
89+
fragment: 'ignored',
90+
matrixParams: 'ignored',
91+
queryParams: 'exact',
92+
};
93+
94+
/**
95+
* The equivalent `IsActiveMatchOptions` options for `isActive` is called with `false`
96+
* (exact = false).
97+
*/
98+
export const subsetMatchOptions: IsActiveMatchOptions = {
99+
paths: 'subset',
100+
fragment: 'ignored',
101+
matrixParams: 'ignored',
102+
queryParams: 'subset',
103+
};
104+
83105
/**
84106
* Returns a computed signal of whether the given url is activated in the Router.
85107
*
@@ -91,14 +113,14 @@ const paramCompareMap: Record<ParamMatchOptions, ParamCompareFn> = {
91113
export function isActive(
92114
url: string | UrlTree,
93115
router: Router,
94-
matchOptions: IsActiveMatchOptions,
116+
matchOptions?: IsActiveMatchOptions,
95117
): Signal<boolean> {
96118
const urlTree = url instanceof UrlTree ? url : router.parseUrl(url);
97119
return computed(() =>
98120
containsTree(
99121
router.lastSuccessfulNavigation()?.finalUrl ?? new UrlTree(),
100122
urlTree,
101-
matchOptions,
123+
matchOptions ?? subsetMatchOptions,
102124
),
103125
);
104126
}

packages/router/test/url_tree.spec.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,13 @@
77
*/
88

99
import {TestBed} from '@angular/core/testing';
10-
import {exactMatchOptions, Router, subsetMatchOptions} from '../src/router';
11-
import {containsTree, DefaultUrlSerializer} from '../src/url_tree';
10+
import {Router} from '../src/router';
11+
import {
12+
containsTree,
13+
DefaultUrlSerializer,
14+
exactMatchOptions,
15+
subsetMatchOptions,
16+
} from '../src/url_tree';
1217

1318
describe('UrlTree', () => {
1419
const serializer = new DefaultUrlSerializer();

0 commit comments

Comments
 (0)