Skip to content

Commit d43c010

Browse files
authored
fix(isActive): Do not match when path is not at the beginning (#90)
1 parent 91e9c79 commit d43c010

File tree

3 files changed

+13
-2
lines changed

3 files changed

+13
-2
lines changed

.changeset/brave-ideas-scream.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@layerstack/utils': patch
3+
---
4+
5+
fix(isActive): Do not match when path is not at the beginning

packages/utils/src/lib/routing.test.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,10 @@ describe('isActive()', () => {
2626
const path = '/foo';
2727
expect(isActive(currentUrl, path)).false;
2828
});
29+
30+
it('path should not match if not at beginning', () => {
31+
const currentUrl = new URL('http://localhost/foo/bar');
32+
const path = '/bar';
33+
expect(isActive(currentUrl, path)).false;
34+
});
2935
});

packages/utils/src/lib/routing.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ export function isActive(currentUrl: URL, path: string) {
3535
// home must be direct match (otherwise matches all)
3636
return currentUrl.pathname === path;
3737
} else {
38-
// Matches full path next character is `/`
39-
return currentUrl.pathname.match(path + '($|\\/)') != null;
38+
// Matches path at start of pathname, followed by end or slash
39+
return currentUrl.pathname.match('^' + path + '($|\\/)') != null;
4040
}
4141
}

0 commit comments

Comments
 (0)