@@ -10,52 +10,143 @@ tags:
1010 - path
1111 - conditional
1212 - links
13- version : ' 1.0'
13+ version : " 1.0"
1414description : >-
1515 Check if paths match current route with useMatch - create active navigation
1616 links, conditional rendering based on route matching.
1717---
1818
19- ` useMatch ` takes an accessor that returns the path and creates a Memo that returns match information if the current path matches the provided path.
20- Useful for determining if a given path matches the current route.
19+ The ` useMatch ` function checks whether the current path matches a provided path pattern.
2120
22- ``` js
23- const match = useMatch (() => props .href );
21+ ## Import
2422
25- return < div classList= {{ active: Boolean (match ()) }} / > ;
23+ ``` ts
24+ import { useMatch } from " @solidjs/router" ;
2625```
2726
28- As a second parameter, ` useMatch ` also accepts a group of ` MatchFilters ` .
29- These filteres allow for a more granular check.
27+ ## Type
3028
31- The filters are the same used by the ` <Router> ` itself and they accept either a an array of strings, or a regular expression. Additionally, there's a ` boolean ` option to match a route only if it has, or doesn't have, the HTML extension.
29+ ``` ts
30+ const useMatch: <S extends string >(
31+ path : () => S ,
32+ matchFilters ? : MatchFilters <S >
33+ ): Accessor <PathMatch | undefined >;
3234
33- ``` js
34- const filters: MatchFilters = {
35- parent: [" mom" , " dad" ]
36- id: / ^ \d + $ / ,
37- withHtmlExtension : (v : string ) => v .length > 5 && v .endsWith (" .html" )
35+ type MatchFilters <P extends string | readonly string [] = any > = P extends string
36+ ? { [K in PathParams <P >[number ]]? : MatchFilter }
37+ : Record <string , MatchFilter >;
38+
39+ interface PathMatch {
40+ params: Params ;
41+ path: string ;
42+ }
43+ ```
44+
45+ ## Parameters
46+
47+ ### ` path `
48+
49+ - ** Type:** ` () => S `
50+ - ** Required:** Yes
51+
52+ An accessor function that returns the path pattern to match against the current route.
53+ Uses the same syntax as the ` path ` prop in the [ ` <Route> ` ] ( /solid-router/reference/components/route ) component.
54+ Supports [ path parameters] ( /solid-router/concepts/path-parameters ) , [ optional parameters] ( /solid-router/concepts/path-parameters#optional-parameters ) , and [ wildcard parameters] ( /solid-router/concepts/path-parameters#wildcard-routes ) .
55+
56+ ### ` filters `
57+
58+ - ** Type:** ` MatchFilters<S> `
59+ - ** Required:** No
60+
61+ An object where keys correspond to route parameter names and values define match filters.
62+ Each filter can be:
63+
64+ - An array of allowed strings
65+ - A regular expression pattern
66+ - A function that receives the parameter value as a string and returns true if the parameter should match
67+
68+ ## Return value
69+
70+ ` useMatch ` returns a memo containing a ` PathMatch ` object when the path matches, or ` undefined ` when there's no match.
71+
72+ The ` PathMatch ` object contains:
73+
74+ ### ` params `
75+
76+ - ** Type:** ` Record<string, string> `
77+
78+ An object containing the matched path parameters as key-value pairs.
79+
80+ ### ` path `
81+
82+ - ** Type:** ` string `
83+
84+ The matched path.
85+
86+ ## Examples
87+
88+ ### Basic usage
89+
90+ ``` tsx
91+ import { useMatch } from " @solidjs/router" ;
92+ import { type JSXElement } from " solid-js" ;
93+
94+ type NavLinkProps = {
95+ href: string ;
96+ children: JSXElement ;
3897};
98+
99+ function NavLink(props : NavLinkProps ) {
100+ const match = useMatch (() => props .href );
101+
102+ return (
103+ <a href = { props .href } classList = { { active: Boolean (match ()) }} >
104+ { props .children }
105+ </a >
106+ );
107+ }
39108```
40109
41- Finally, any parameter can be determined optional by adding a ` ? ` at the end of the parameter name.
110+ ### With filters
111+
112+ ``` tsx
113+ import { useMatch } from " @solidjs/router" ;
114+ import { Show } from " solid-js" ;
42115
43- ``` js
44- const isReference = useMatch (() => " /:project?/reference/*?" , {
45- project: [" solid-router" , " solid-meta" , " solid-start" ],
116+ function BlogPost() {
117+ const match = useMatch (() => " /:lang?/blog/:slug" , {
118+ lang: [" en" , " es" , " fr" ],
119+ slug: / ^ [a-z0-9 -] + $ / , // Only allow lowercase letters, numbers, and hyphens
46120 });
121+
122+ const lang = () => match ()?.params .lang || " en" ;
123+
124+ return (
125+ <Show when = { match ()} >
126+ <article lang = { lang ()} >
127+ <p >Blog slug: { match ()?.params .slug } </p >
128+ </article >
129+ </Show >
130+ );
131+ }
47132```
48133
49- The check above will match:
134+ ### With custom filter functions
50135
51- ``` text
52- /reference
53- /solid-router/reference
54- /solid-meta/reference
55- /solid-start/reference
136+ ``` tsx
137+ import { useMatch } from " @solidjs/router" ;
56138
57- /reference/...
58- /solid-router/reference/...
59- /solid-meta/reference/...
60- /solid-start/reference/...
139+ function FileInfo() {
140+ const match = useMatch (() => " /files/:type/:name" , {
141+ type: [" images" , " documents" , " videos" ],
142+ name : (name ) => name .length > 5 && name .endsWith (" .html" ),
143+ });
144+
145+ return <div >File: { match ()?.params .name } </div >;
146+ }
61147```
148+
149+ ## Related
150+
151+ - [ ` useParams ` ] ( /reference/router-primitives/use-params )
152+ - [ ` useLocation ` ] ( /reference/router-primitives/use-location )
0 commit comments