@@ -48,32 +48,33 @@ const comparableQuery = (search: string) => {
4848} ;
4949
5050/**
51- * The link-state rule shared by claimed anchors and `useLinkState`, given
52- * the location and a link's already-resolved target (path, optional query
53- * and hash):
51+ * The link-state rule shared by claimed anchors and `useLinkState`, bound to
52+ * a location so links matched against the same one share its parse. The
53+ * returned matcher takes a link's already-resolved target (path, optional
54+ * query and hash):
5455 *
5556 * - `current` — same pathname and same query (parameter order and hash aside)
5657 * - `active` — same pathname or one under it; the router's root (`base`, or
5758 * `/` without one) only matches exactly, since it is a prefix of every
5859 * page. `end` makes every link exact-only.
5960 */
60- export function matchLink (
61- location : { pathname : string ; search : string } ,
62- target : string | undefined ,
61+ export function linkMatcher (
62+ { pathname, search } : { pathname : string ; search : string } ,
6363 base : string ,
6464 end ?: boolean
65- ) : { active : boolean ; current : boolean } {
66- if ( target === undefined ) return { active : false , current : false } ;
67- const loc = comparablePath ( location . pathname ) ;
68- const path = comparablePath ( target ) ;
69- const exact = loc === path ;
70- const hashless = target . split ( "#" , 1 ) [ 0 ] ;
71- const q = hashless . indexOf ( "?" ) ;
72- return {
73- active :
74- exact || ( ! end && path !== "" && path !== comparablePath ( base ) && loc . startsWith ( path + "/" ) ) ,
75- current :
76- exact && comparableQuery ( location . search ) === comparableQuery ( q < 0 ? "" : hashless . slice ( q ) )
65+ ) {
66+ const loc = comparablePath ( pathname ) ;
67+ const root = comparablePath ( base ) ;
68+ return ( target : string | undefined ) : { active : boolean ; current : boolean } => {
69+ if ( target === undefined ) return { active : false , current : false } ;
70+ const path = comparablePath ( target ) ;
71+ const exact = loc === path ;
72+ const hashless = target . split ( "#" , 1 ) [ 0 ] ;
73+ const q = hashless . indexOf ( "?" ) ;
74+ return {
75+ active : exact || ( ! end && path !== "" && path !== root && loc . startsWith ( path + "/" ) ) ,
76+ current : exact && comparableQuery ( search ) === comparableQuery ( q < 0 ? "" : hashless . slice ( q ) )
77+ } ;
7778 } ;
7879}
7980
0 commit comments