Skip to content

Commit 3ebc71b

Browse files
authored
Merge pull request #495 from marvin-j97/docs
Add JSdoc
2 parents ebdde8f + fe5c83e commit 3ebc71b

File tree

4 files changed

+161
-7
lines changed

4 files changed

+161
-7
lines changed

.changeset/big-badgers-learn.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@solidjs/router": patch
3+
---
4+
5+
Add JSdoc

README.md

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -818,7 +818,7 @@ if (unauthorized) {
818818

819819
### useLocation
820820

821-
Retrieves reactive `location` object useful for getting things like `pathname`
821+
Retrieves reactive `location` object useful for getting things like `pathname`.
822822

823823
```js
824824
const location = useLocation();
@@ -880,7 +880,8 @@ return <div classList={{ active: Boolean(match()) }} />;
880880
For example if you stored breadcrumbs on your route definition you could retrieve them like so:
881881
```js
882882
const matches = useCurrentMatches();
883-
const breadcrumbs = createMemo(() => matches().map(m => m.route.info.breadcrumb))
883+
884+
const breadcrumbs = createMemo(() => matches().map(m => m.route.info.breadcrumb));
884885
```
885886

886887
### usePreloadRoute
@@ -898,11 +899,11 @@ preload(`/users/settings`, { preloadData: true });
898899
`useBeforeLeave` takes a function that will be called prior to leaving a route. The function will be called with:
899900

900901
- from (_Location_): current location (before change).
901-
- to (_string | number_}: path passed to `navigate`.
902-
- options (_NavigateOptions_}: options passed to `navigate`.
903-
- preventDefault (_void function_): call to block the route change.
904-
- defaultPrevented (_readonly boolean_): true if any previously called leave handlers called preventDefault().
905-
- retry (_void function_, _force?: boolean_ ): call to retry the same navigation, perhaps after confirming with the user. Pass `true` to skip running the leave handlers again (ie force navigate without confirming).
902+
- to (_string | number_): path passed to `navigate`.
903+
- options (_NavigateOptions_): options passed to `navigate`.
904+
- preventDefault (_function_): call to block the route change.
905+
- defaultPrevented (_readonly boolean_): `true` if any previously called leave handlers called `preventDefault`.
906+
- retry (_function_, _force?: boolean_ ): call to retry the same navigation, perhaps after confirming with the user. Pass `true` to skip running the leave handlers again (i.e. force navigate without confirming).
906907

907908
Example usage:
908909

src/data/query.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ function getCache() {
3535
return (req.router || (req.router = {})).cache || (req.router.cache = new Map());
3636
}
3737

38+
/**
39+
* Revalidates the given cache entry/entries.
40+
*/
3841
export function revalidate(key?: string | string[] | void, force = true) {
3942
return startTransition(() => {
4043
const now = Date.now();

src/routing.ts

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,15 +76,84 @@ export const useHref = (to: () => string | undefined) => {
7676
});
7777
};
7878

79+
/**
80+
* Retrieves method to do navigation. The method accepts a path to navigate to and an optional object with the following options:
81+
*
82+
* - resolve (*boolean*, default `true`): resolve the path against the current route
83+
* - replace (*boolean*, default `false`): replace the history entry
84+
* - scroll (*boolean*, default `true`): scroll to top after navigation
85+
* - state (*any*, default `undefined`): pass custom state to `location.state`
86+
*
87+
* **Note**: The state is serialized using the structured clone algorithm which does not support all object types.
88+
*
89+
* @example
90+
* ```js
91+
* const navigate = useNavigate();
92+
*
93+
* if (unauthorized) {
94+
* navigate("/login", { replace: true });
95+
* }
96+
* ```
97+
*/
7998
export const useNavigate = () => useRouter().navigatorFactory();
99+
100+
/**
101+
* Retrieves reactive `location` object useful for getting things like `pathname`.
102+
*
103+
* @example
104+
* ```js
105+
* const location = useLocation();
106+
*
107+
* const pathname = createMemo(() => parsePath(location.pathname));
108+
* ```
109+
*/
80110
export const useLocation = <S = unknown>() => useRouter().location as Location<S>;
111+
112+
/**
113+
* Retrieves signal that indicates whether the route is currently in a *Transition*.
114+
* Useful for showing stale/pending state when the route resolution is *Suspended* during concurrent rendering.
115+
*
116+
* @example
117+
* ```js
118+
* const isRouting = useIsRouting();
119+
*
120+
* return (
121+
* <div classList={{ "grey-out": isRouting() }}>
122+
* <MyAwesomeContent />
123+
* </div>
124+
* );
125+
* ```
126+
*/
81127
export const useIsRouting = () => useRouter().isRouting;
128+
129+
/**
130+
* usePreloadRoute returns a function that can be used to preload a route manual.
131+
* This is what happens automatically with link hovering and similar focus based behavior, but it is available here as an API.
132+
*
133+
* @example
134+
* ```js
135+
* const preload = usePreloadRoute();
136+
*
137+
* preload(`/users/settings`, { preloadData: true });
138+
* ```
139+
*/
82140
export const usePreloadRoute = () => {
83141
const pre = useRouter().preloadRoute
84142
return (url: string | URL, options: { preloadData?: boolean } = {} ) =>
85143
pre(url instanceof URL ? url : new URL(url, mockBase), options.preloadData)
86144
}
87145

146+
/**
147+
* `useMatch` takes an accessor that returns the path and creates a `Memo` that returns match information if the current path matches the provided path.
148+
* Useful for determining if a given path matches the current route.
149+
*
150+
* @example
151+
* ```js
152+
* const match = useMatch(() => props.href);
153+
*
154+
* return <div classList={{ active: Boolean(match()) }} />;
155+
* ```
156+
*/
88157
export const useMatch = <S extends string>(path: () => S, matchFilters?: MatchFilters<S>) => {
89158
const location = useLocation();
90159
const matchers = createMemo(() =>
@@ -98,10 +167,59 @@ export const useMatch = <S extends string>(path: () => S, matchFilters?: MatchFi
98167
});
99168
};
100169

170+
/**
171+
* `useCurrentMatches` returns all the matches for the current matched route.
172+
* Useful for getting all the route information.
173+
*
174+
* @example
175+
* ```js
176+
* const matches = useCurrentMatches();
177+
*
178+
* const breadcrumbs = createMemo(() => matches().map(m => m.route.info.breadcrumb))
179+
* ```
180+
*/
101181
export const useCurrentMatches = () => useRouter().matches;
102182

183+
/**
184+
* Retrieves a reactive, store-like object containing the current route path parameters as defined in the Route.
185+
*
186+
* @example
187+
* ```js
188+
* const params = useParams();
189+
*
190+
* // fetch user based on the id path parameter
191+
* const [user] = createResource(() => params.id, fetchUser);
192+
* ```
193+
*/
103194
export const useParams = <T extends Params>() => useRouter().params as T;
104195

196+
/**
197+
* Retrieves a tuple containing a reactive object to read the current location's query parameters and a method to update them.
198+
* The object is a proxy so you must access properties to subscribe to reactive updates.
199+
* **Note** that values will be strings and property names will retain their casing.
200+
*
201+
* The setter method accepts an object whose entries will be merged into the current query string.
202+
* Values `''`, `undefined` and `null` will remove the key from the resulting query string.
203+
* Updates will behave just like a navigation and the setter accepts the same optional second parameter as `navigate` and auto-scrolling is disabled by default.
204+
*
205+
* @examples
206+
* ```js
207+
* const [searchParams, setSearchParams] = useSearchParams();
208+
*
209+
* return (
210+
* <div>
211+
* <span>Page: {searchParams.page}</span>
212+
* <button
213+
* onClick={() =>
214+
* setSearchParams({ page: (parseInt(searchParams.page) || 0) + 1 })
215+
* }
216+
* >
217+
* Next Page
218+
* </button>
219+
* </div>
220+
* );
221+
* ```
222+
*/
105223
export const useSearchParams = <T extends SearchParams>(): [
106224
Partial<T>,
107225
(params: SetSearchParams, options?: Partial<NavigateOptions>) => void
@@ -119,6 +237,33 @@ export const useSearchParams = <T extends SearchParams>(): [
119237
return [location.query as Partial<T>, setSearchParams];
120238
};
121239

240+
/**
241+
* useBeforeLeave takes a function that will be called prior to leaving a route.
242+
* The function will be called with:
243+
*
244+
* - from (*Location*): current location (before change).
245+
* - to (*string | number*): path passed to `navigate`.
246+
* - options (*NavigateOptions*): options passed to navigate.
247+
* - preventDefault (*function*): call to block the route change.
248+
* - defaultPrevented (*readonly boolean*): `true` if any previously called leave handlers called `preventDefault`.
249+
* - retry (*function*, force?: boolean ): call to retry the same navigation, perhaps after confirming with the user. Pass `true` to skip running the leave handlers again (i.e. force navigate without confirming).
250+
*
251+
* @example
252+
* ```js
253+
* useBeforeLeave((e: BeforeLeaveEventArgs) => {
254+
* if (form.isDirty && !e.defaultPrevented) {
255+
* // preventDefault to block immediately and prompt user async
256+
* e.preventDefault();
257+
* setTimeout(() => {
258+
* if (window.confirm("Discard unsaved changes - are you sure?")) {
259+
* // user wants to proceed anyway so retry with force=true
260+
* e.retry(true);
261+
* }
262+
* }, 100);
263+
* }
264+
* });
265+
* ```
266+
*/
122267
export const useBeforeLeave = (listener: (e: BeforeLeaveEventArgs) => void) => {
123268
const s = useRouter().beforeLeave.subscribe({
124269
listener,

0 commit comments

Comments
 (0)