@studiolambda/router 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +15 -0
- package/dist/matcher-CSJ3hjzA.cjs +2 -0
- package/dist/matcher-CSJ3hjzA.cjs.map +1 -0
- package/dist/matcher-XNPYU-tD.js +69 -0
- package/dist/matcher-XNPYU-tD.js.map +1 -0
- package/dist/router.cjs +1 -0
- package/dist/router.js +2 -0
- package/dist/router_react.cjs +6 -0
- package/dist/router_react.cjs.map +1 -0
- package/dist/router_react.js +506 -0
- package/dist/router_react.js.map +1 -0
- package/dist/src/react/ExampleMain.d.ts +7 -0
- package/dist/src/react/components/Link.d.ts +91 -0
- package/dist/src/react/components/Middlewares.d.ts +28 -0
- package/dist/src/react/components/NotFound.d.ts +6 -0
- package/dist/src/react/components/Router.d.ts +81 -0
- package/dist/src/react/context/MatcherContext.d.ts +12 -0
- package/dist/src/react/context/NavigationContext.d.ts +11 -0
- package/dist/src/react/context/NavigationSignalContext.d.ts +7 -0
- package/dist/src/react/context/NavigationTypeContext.d.ts +6 -0
- package/dist/src/react/context/PathnameContext.d.ts +10 -0
- package/dist/src/react/context/PropsContext.d.ts +10 -0
- package/dist/src/react/context/TransitionContext.d.ts +19 -0
- package/dist/src/react/createRouter.d.ts +187 -0
- package/dist/src/react/example.d.ts +1 -0
- package/dist/src/react/examples/Auth.d.ts +11 -0
- package/dist/src/react/examples/HelloWorld.d.ts +5 -0
- package/dist/src/react/examples/Other.d.ts +6 -0
- package/dist/src/react/examples/User.d.ts +6 -0
- package/dist/src/react/extractPathname.d.ts +17 -0
- package/dist/src/react/hooks/useActiveLinkProps.d.ts +73 -0
- package/dist/src/react/hooks/useBack.d.ts +47 -0
- package/dist/src/react/hooks/useForward.d.ts +47 -0
- package/dist/src/react/hooks/useIsPending.d.ts +29 -0
- package/dist/src/react/hooks/useNavigate.d.ts +10 -0
- package/dist/src/react/hooks/useNavigation.d.ts +13 -0
- package/dist/src/react/hooks/useNavigationEvents.d.ts +43 -0
- package/dist/src/react/hooks/useNavigationHandlers.d.ts +47 -0
- package/dist/src/react/hooks/useNavigationSignal.d.ts +13 -0
- package/dist/src/react/hooks/useNavigationType.d.ts +12 -0
- package/dist/src/react/hooks/useNextMatch.d.ts +26 -0
- package/dist/src/react/hooks/useParams.d.ts +19 -0
- package/dist/src/react/hooks/usePathname.d.ts +23 -0
- package/dist/src/react/hooks/usePrefetch.d.ts +27 -0
- package/dist/src/react/hooks/usePrefetchEffect.d.ts +69 -0
- package/dist/src/react/hooks/useSearchParams.d.ts +58 -0
- package/dist/src/react/index.d.ts +31 -0
- package/dist/src/react/navigation/createMemoryNavigation.d.ts +52 -0
- package/dist/src/react/router.d.ts +139 -0
- package/dist/src/react/test-helpers.d.ts +50 -0
- package/dist/src/router/index.d.ts +1 -0
- package/dist/src/router/matcher.d.ts +127 -0
- package/package.json +107 -0
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
import { ComponentType } from 'react';
|
|
2
|
+
import { Matcher } from '../router/matcher';
|
|
3
|
+
import { FormHandler, Handler, MiddlewareProps, PrefetchFunc, RedirectTarget } from './router';
|
|
4
|
+
/**
|
|
5
|
+
* Chainable route builder returned by the `route()` factory.
|
|
6
|
+
* Accumulates handler configuration through method calls and
|
|
7
|
+
* terminates with `.render()`, `.redirect()`, or `.group()`.
|
|
8
|
+
*
|
|
9
|
+
* Chainable methods (return the builder for further chaining):
|
|
10
|
+
* - `.middleware()` — appends middleware components
|
|
11
|
+
* - `.prefetch()` — adds a prefetch function to the chain
|
|
12
|
+
* - `.scroll()` — sets scroll restoration behavior
|
|
13
|
+
* - `.focusReset()` — sets focus reset behavior
|
|
14
|
+
* - `.formHandler()` — sets the form submission handler
|
|
15
|
+
*
|
|
16
|
+
* Terminal methods:
|
|
17
|
+
* - `.render()` — registers a route with a component
|
|
18
|
+
* - `.redirect()` — registers a precommit redirect
|
|
19
|
+
* - `.group()` — returns a scoped `RouteFactory` that
|
|
20
|
+
* inherits this builder's config
|
|
21
|
+
*/
|
|
22
|
+
export interface RouteBuilder {
|
|
23
|
+
/**
|
|
24
|
+
* Appends middleware components to this route or group.
|
|
25
|
+
* Middlewares are applied outermost-first: inherited group
|
|
26
|
+
* middlewares wrap before this route's middlewares, and
|
|
27
|
+
* within the array the first element wraps outermost.
|
|
28
|
+
*
|
|
29
|
+
* @param list - Middleware components to append.
|
|
30
|
+
* @returns The builder for further chaining.
|
|
31
|
+
*/
|
|
32
|
+
middleware(list: ComponentType<MiddlewareProps>[]): RouteBuilder;
|
|
33
|
+
/**
|
|
34
|
+
* Adds a prefetch function to the chain for this route
|
|
35
|
+
* or group. When multiple prefetch functions are chained
|
|
36
|
+
* (from parent groups and the route itself), they execute
|
|
37
|
+
* sequentially: parent prefetches run first, then this
|
|
38
|
+
* route's, in the order they were added.
|
|
39
|
+
*
|
|
40
|
+
* @param fn - The prefetch function to add.
|
|
41
|
+
* @returns The builder for further chaining.
|
|
42
|
+
*/
|
|
43
|
+
prefetch(fn: PrefetchFunc): RouteBuilder;
|
|
44
|
+
/**
|
|
45
|
+
* Sets the scroll restoration behavior for this route.
|
|
46
|
+
* - `'after-transition'` — browser handles scroll after
|
|
47
|
+
* the handler promise resolves (default).
|
|
48
|
+
* - `'manual'` — disables automatic scrolling so the
|
|
49
|
+
* route component can call `event.scroll()` manually.
|
|
50
|
+
*
|
|
51
|
+
* @param behavior - The scroll behavior to use.
|
|
52
|
+
* @returns The builder for further chaining.
|
|
53
|
+
*/
|
|
54
|
+
scroll(behavior: NavigationScrollBehavior): RouteBuilder;
|
|
55
|
+
/**
|
|
56
|
+
* Sets the focus reset behavior for this route.
|
|
57
|
+
* - `'after-transition'` — focuses the first autofocus
|
|
58
|
+
* element after the handler resolves (default).
|
|
59
|
+
* - `'manual'` — disables automatic focus reset.
|
|
60
|
+
*
|
|
61
|
+
* @param behavior - The focus reset behavior to use.
|
|
62
|
+
* @returns The builder for further chaining.
|
|
63
|
+
*/
|
|
64
|
+
focusReset(behavior: NavigationFocusReset): RouteBuilder;
|
|
65
|
+
/**
|
|
66
|
+
* Sets the form submission handler for this route. When
|
|
67
|
+
* a navigation includes FormData and matches this route,
|
|
68
|
+
* the form handler is called instead of rendering the
|
|
69
|
+
* component.
|
|
70
|
+
*
|
|
71
|
+
* @param fn - The form handler function.
|
|
72
|
+
* @returns The builder for further chaining.
|
|
73
|
+
*/
|
|
74
|
+
formHandler(fn: FormHandler): RouteBuilder;
|
|
75
|
+
/**
|
|
76
|
+
* Terminal method that registers this route on the matcher
|
|
77
|
+
* with the given component. Merges inherited and local
|
|
78
|
+
* configuration into a `Handler` and registers it at the
|
|
79
|
+
* full path (group prefix + route path).
|
|
80
|
+
*
|
|
81
|
+
* @param component - The React component to render.
|
|
82
|
+
* @throws When no path was provided to the `route()` call
|
|
83
|
+
* and no group prefix exists.
|
|
84
|
+
*/
|
|
85
|
+
render(component: ComponentType): void;
|
|
86
|
+
/**
|
|
87
|
+
* Terminal method that registers a precommit redirect.
|
|
88
|
+
* When the Navigation API matches this route, the
|
|
89
|
+
* precommit handler calls `controller.redirect(target)`
|
|
90
|
+
* before the URL commits, avoiding any component render
|
|
91
|
+
* or visual flash.
|
|
92
|
+
*
|
|
93
|
+
* The target can be a static absolute path string, or a
|
|
94
|
+
* callback that receives the prefetch context and returns
|
|
95
|
+
* the path. The callback form enables dynamic redirects
|
|
96
|
+
* that carry route parameters to the new location.
|
|
97
|
+
*
|
|
98
|
+
* The resolved target path is absolute and is NOT prefixed
|
|
99
|
+
* by parent groups.
|
|
100
|
+
*
|
|
101
|
+
* @param target - The redirect target: a static path or a
|
|
102
|
+
* callback receiving `PrefetchContext` and returning one.
|
|
103
|
+
* @throws When no path was provided to the `route()` call
|
|
104
|
+
* and no group prefix exists.
|
|
105
|
+
*
|
|
106
|
+
* @example
|
|
107
|
+
* ```ts
|
|
108
|
+
* // Static redirect
|
|
109
|
+
* route('/old').redirect('/new')
|
|
110
|
+
*
|
|
111
|
+
* // Dynamic redirect using route params
|
|
112
|
+
* route('/old-user/:id').redirect(({ params }) => `/user/${params.id}`)
|
|
113
|
+
* ```
|
|
114
|
+
*/
|
|
115
|
+
redirect(target: RedirectTarget): void;
|
|
116
|
+
/**
|
|
117
|
+
* Terminal method that creates a nested route scope.
|
|
118
|
+
* Returns a `RouteFactory` whose routes inherit this
|
|
119
|
+
* builder's middleware and prefetch configuration. When
|
|
120
|
+
* a path was provided, it is prepended to all child
|
|
121
|
+
* route paths as a prefix.
|
|
122
|
+
*
|
|
123
|
+
* @returns A scoped `RouteFactory` for defining child
|
|
124
|
+
* routes that inherit this builder's config.
|
|
125
|
+
*
|
|
126
|
+
* @example
|
|
127
|
+
* ```ts
|
|
128
|
+
* const admin = route('/admin').middleware([Auth]).group()
|
|
129
|
+
* admin('/dashboard').render(Dashboard)
|
|
130
|
+
* admin('/settings').render(Settings)
|
|
131
|
+
* ```
|
|
132
|
+
*/
|
|
133
|
+
group(): RouteFactory;
|
|
134
|
+
}
|
|
135
|
+
/**
|
|
136
|
+
* Factory function that creates a route builder for a given
|
|
137
|
+
* path. When path is omitted, the builder is used purely for
|
|
138
|
+
* group-level configuration (middleware, prefetch) without
|
|
139
|
+
* contributing a path segment.
|
|
140
|
+
*
|
|
141
|
+
* @param path - Optional path pattern for this route. May
|
|
142
|
+
* contain dynamic (`:param`) and wildcard (`*param`)
|
|
143
|
+
* segments.
|
|
144
|
+
* @returns A chainable route builder.
|
|
145
|
+
*/
|
|
146
|
+
export type RouteFactory = (path?: string) => RouteBuilder;
|
|
147
|
+
/**
|
|
148
|
+
* Creates a route matcher using a declarative builder API.
|
|
149
|
+
* Routes are defined inside a callback that receives a
|
|
150
|
+
* `route` factory function for chainable route configuration.
|
|
151
|
+
*
|
|
152
|
+
* The builder supports middleware inheritance, prefetch
|
|
153
|
+
* chaining, nested groups with path prefixing, redirects,
|
|
154
|
+
* and all handler options (scroll, focusReset, formHandler).
|
|
155
|
+
*
|
|
156
|
+
* Returns a `Matcher<Handler>` that plugs directly into the
|
|
157
|
+
* `<Router matcher={...}>` component.
|
|
158
|
+
*
|
|
159
|
+
* @param callback - A function that defines routes using the
|
|
160
|
+
* provided `route` factory.
|
|
161
|
+
* @returns A populated matcher ready for the Router.
|
|
162
|
+
*
|
|
163
|
+
* @example
|
|
164
|
+
* ```tsx
|
|
165
|
+
* const router = createRouter(function (route) {
|
|
166
|
+
* route('/').render(Home)
|
|
167
|
+
* route('/old').redirect('/new')
|
|
168
|
+
*
|
|
169
|
+
* route('/other')
|
|
170
|
+
* .prefetch(prefetchOther)
|
|
171
|
+
* .scroll('after-transition')
|
|
172
|
+
* .render(Other)
|
|
173
|
+
*
|
|
174
|
+
* const authed = route().middleware([Auth]).group()
|
|
175
|
+
* authed('/dashboard').render(Dashboard)
|
|
176
|
+
* authed('/user/:id').render(User)
|
|
177
|
+
*
|
|
178
|
+
* const admin = authed('/admin')
|
|
179
|
+
* .middleware([AdminOnly])
|
|
180
|
+
* .group()
|
|
181
|
+
* admin('/settings').render(Settings)
|
|
182
|
+
* })
|
|
183
|
+
*
|
|
184
|
+
* <Router matcher={router} />
|
|
185
|
+
* ```
|
|
186
|
+
*/
|
|
187
|
+
export declare function createRouter(callback: (route: RouteFactory) => void): Matcher<Handler>;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { PropsWithChildren } from 'react';
|
|
2
|
+
/**
|
|
3
|
+
* Example authentication middleware component that suspends
|
|
4
|
+
* while verifying credentials. Uses React's `use()` hook to
|
|
5
|
+
* suspend rendering until the auth promise resolves, causing
|
|
6
|
+
* the nearest Suspense boundary to show its fallback.
|
|
7
|
+
*
|
|
8
|
+
* In a real application, this would check session tokens or
|
|
9
|
+
* call an authentication API.
|
|
10
|
+
*/
|
|
11
|
+
export default function Auth({ children }: PropsWithChildren): import('react').ReactNode;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Extracts the pathname portion from a URL string. Uses a
|
|
3
|
+
* dummy base URL to handle both absolute and relative paths
|
|
4
|
+
* correctly. Returns `'/'` when the input is null, undefined,
|
|
5
|
+
* or an empty string.
|
|
6
|
+
*
|
|
7
|
+
* Used by the Router (to extract pathname from navigation
|
|
8
|
+
* destination URLs), Link (for active link comparison), and
|
|
9
|
+
* usePrefetch (to match URLs against registered routes).
|
|
10
|
+
*
|
|
11
|
+
* @param url - The URL string to extract a pathname from.
|
|
12
|
+
* May be absolute (`https://example.com/foo`), relative
|
|
13
|
+
* (`/foo/bar`), or nullish.
|
|
14
|
+
* @returns The pathname string, or `'/'` when no URL is
|
|
15
|
+
* provided.
|
|
16
|
+
*/
|
|
17
|
+
export declare function extractPathname(url: string | null | undefined): string;
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Options for the `useActiveLinkProps` hook.
|
|
3
|
+
*/
|
|
4
|
+
export interface ActiveLinkOptions {
|
|
5
|
+
/**
|
|
6
|
+
* When true, the link is only considered active when
|
|
7
|
+
* the current pathname exactly matches the href
|
|
8
|
+
* pathname. When false, the link is active when the
|
|
9
|
+
* current pathname starts with the href pathname,
|
|
10
|
+
* which is useful for parent navigation items that
|
|
11
|
+
* should highlight when any child route is active.
|
|
12
|
+
*
|
|
13
|
+
* Defaults to `true`.
|
|
14
|
+
*/
|
|
15
|
+
exact?: boolean;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Props returned by the hook to spread onto an anchor
|
|
19
|
+
* element for active link styling and accessibility.
|
|
20
|
+
*/
|
|
21
|
+
export interface ActiveLinkProps {
|
|
22
|
+
/**
|
|
23
|
+
* Present (set to `true`) when the link is active.
|
|
24
|
+
* Absent (`undefined`) when inactive. Use the
|
|
25
|
+
* `a[data-active]` CSS selector for styling.
|
|
26
|
+
*/
|
|
27
|
+
'data-active': true | undefined;
|
|
28
|
+
/**
|
|
29
|
+
* Set to `'page'` when the link is active to indicate
|
|
30
|
+
* to assistive technologies that this link represents
|
|
31
|
+
* the current page. Absent when inactive.
|
|
32
|
+
*/
|
|
33
|
+
'aria-current': 'page' | undefined;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Computes active link attributes for an anchor element
|
|
37
|
+
* by comparing its href against the current pathname from
|
|
38
|
+
* the Router's PathnameContext.
|
|
39
|
+
*
|
|
40
|
+
* Returns a props object containing `data-active` and
|
|
41
|
+
* `aria-current` attributes ready to spread onto an `<a>`
|
|
42
|
+
* element or any element that should reflect active state.
|
|
43
|
+
*
|
|
44
|
+
* Also returns an `isActive` boolean for conditional logic
|
|
45
|
+
* like dynamic class names.
|
|
46
|
+
*
|
|
47
|
+
* Must be used inside a `<Router>` component tree where
|
|
48
|
+
* `PathnameContext` is provided.
|
|
49
|
+
*
|
|
50
|
+
* @param href - The href to compare against the current
|
|
51
|
+
* pathname. When undefined, the link is never active.
|
|
52
|
+
* @param options - Optional configuration for exact vs
|
|
53
|
+
* prefix matching.
|
|
54
|
+
* @returns An object with `isActive` boolean and `props`
|
|
55
|
+
* to spread onto the element.
|
|
56
|
+
*
|
|
57
|
+
* @example
|
|
58
|
+
* ```tsx
|
|
59
|
+
* function NavItem({ href, children }: NavItemProps) {
|
|
60
|
+
* const { isActive, props } = useActiveLinkProps(href)
|
|
61
|
+
*
|
|
62
|
+
* return (
|
|
63
|
+
* <a href={href} className={isActive ? 'active' : ''} {...props}>
|
|
64
|
+
* {children}
|
|
65
|
+
* </a>
|
|
66
|
+
* )
|
|
67
|
+
* }
|
|
68
|
+
* ```
|
|
69
|
+
*/
|
|
70
|
+
export declare function useActiveLinkProps(href: string | undefined, options?: ActiveLinkOptions): {
|
|
71
|
+
isActive: boolean;
|
|
72
|
+
props: ActiveLinkProps;
|
|
73
|
+
};
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Return value from the `useBack` hook, providing both
|
|
3
|
+
* a traversal function and a boolean indicating whether
|
|
4
|
+
* backward navigation is possible.
|
|
5
|
+
*/
|
|
6
|
+
export interface UseBackResult {
|
|
7
|
+
/**
|
|
8
|
+
* Navigates backward in the session history. Delegates
|
|
9
|
+
* to `navigation.back()` from the Navigation API.
|
|
10
|
+
*
|
|
11
|
+
* @param options - Optional navigation options such as
|
|
12
|
+
* `info` to pass data to the navigate event handler.
|
|
13
|
+
* @returns The NavigationResult with `committed` and
|
|
14
|
+
* `finished` promises.
|
|
15
|
+
*/
|
|
16
|
+
readonly back: (options?: NavigationOptions) => NavigationResult;
|
|
17
|
+
/**
|
|
18
|
+
* Whether backward navigation is possible. Mirrors
|
|
19
|
+
* `navigation.canGoBack` from the Navigation API.
|
|
20
|
+
* When false, calling `back()` will throw.
|
|
21
|
+
*/
|
|
22
|
+
readonly canGoBack: boolean;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Provides backward navigation capabilities using the
|
|
26
|
+
* Navigation API. Returns a `back` function and a
|
|
27
|
+
* `canGoBack` boolean that reflects whether the history
|
|
28
|
+
* stack has a previous entry to traverse to.
|
|
29
|
+
*
|
|
30
|
+
* Must be used inside a `<Router>` component tree.
|
|
31
|
+
*
|
|
32
|
+
* @returns An object with `back` and `canGoBack`.
|
|
33
|
+
*
|
|
34
|
+
* @example
|
|
35
|
+
* ```tsx
|
|
36
|
+
* function BackButton() {
|
|
37
|
+
* const { back, canGoBack } = useBack()
|
|
38
|
+
*
|
|
39
|
+
* return (
|
|
40
|
+
* <button onClick={back} disabled={!canGoBack}>
|
|
41
|
+
* Go Back
|
|
42
|
+
* </button>
|
|
43
|
+
* )
|
|
44
|
+
* }
|
|
45
|
+
* ```
|
|
46
|
+
*/
|
|
47
|
+
export declare function useBack(): UseBackResult;
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Return value from the `useForward` hook, providing both
|
|
3
|
+
* a traversal function and a boolean indicating whether
|
|
4
|
+
* forward navigation is possible.
|
|
5
|
+
*/
|
|
6
|
+
export interface UseForwardResult {
|
|
7
|
+
/**
|
|
8
|
+
* Navigates forward in the session history. Delegates
|
|
9
|
+
* to `navigation.forward()` from the Navigation API.
|
|
10
|
+
*
|
|
11
|
+
* @param options - Optional navigation options such as
|
|
12
|
+
* `info` to pass data to the navigate event handler.
|
|
13
|
+
* @returns The NavigationResult with `committed` and
|
|
14
|
+
* `finished` promises.
|
|
15
|
+
*/
|
|
16
|
+
readonly forward: (options?: NavigationOptions) => NavigationResult;
|
|
17
|
+
/**
|
|
18
|
+
* Whether forward navigation is possible. Mirrors
|
|
19
|
+
* `navigation.canGoForward` from the Navigation API.
|
|
20
|
+
* When false, calling `forward()` will throw.
|
|
21
|
+
*/
|
|
22
|
+
readonly canGoForward: boolean;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Provides forward navigation capabilities using the
|
|
26
|
+
* Navigation API. Returns a `forward` function and a
|
|
27
|
+
* `canGoForward` boolean that reflects whether the history
|
|
28
|
+
* stack has a next entry to traverse to.
|
|
29
|
+
*
|
|
30
|
+
* Must be used inside a `<Router>` component tree.
|
|
31
|
+
*
|
|
32
|
+
* @returns An object with `forward` and `canGoForward`.
|
|
33
|
+
*
|
|
34
|
+
* @example
|
|
35
|
+
* ```tsx
|
|
36
|
+
* function ForwardButton() {
|
|
37
|
+
* const { forward, canGoForward } = useForward()
|
|
38
|
+
*
|
|
39
|
+
* return (
|
|
40
|
+
* <button onClick={forward} disabled={!canGoForward}>
|
|
41
|
+
* Go Forward
|
|
42
|
+
* </button>
|
|
43
|
+
* )
|
|
44
|
+
* }
|
|
45
|
+
* ```
|
|
46
|
+
*/
|
|
47
|
+
export declare function useForward(): UseForwardResult;
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Returns whether a navigation transition is currently pending.
|
|
3
|
+
* This reflects the `isPending` value from the `useTransition`
|
|
4
|
+
* tuple managed by the Router component.
|
|
5
|
+
*
|
|
6
|
+
* Useful for displaying loading indicators, progress bars, or
|
|
7
|
+
* adjusting UI opacity while a route transition is in progress.
|
|
8
|
+
*
|
|
9
|
+
* Must be used inside a `<Router>` component tree or a
|
|
10
|
+
* `<TransitionContext>` provider.
|
|
11
|
+
*
|
|
12
|
+
* @returns `true` while a navigation transition is pending,
|
|
13
|
+
* `false` otherwise.
|
|
14
|
+
* @throws When used outside a TransitionContext provider.
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* ```tsx
|
|
18
|
+
* function NavBar() {
|
|
19
|
+
* const isPending = useIsPending()
|
|
20
|
+
*
|
|
21
|
+
* return (
|
|
22
|
+
* <nav style={{ opacity: isPending ? 0.7 : 1 }}>
|
|
23
|
+
* ...
|
|
24
|
+
* </nav>
|
|
25
|
+
* )
|
|
26
|
+
* }
|
|
27
|
+
* ```
|
|
28
|
+
*/
|
|
29
|
+
export declare function useIsPending(): boolean;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Returns a function to programmatically navigate to a URL
|
|
3
|
+
* with full Navigation API options support (`state`, `info`,
|
|
4
|
+
* `history`). This is a thin convenience wrapper around
|
|
5
|
+
* `navigation.navigate()`.
|
|
6
|
+
*
|
|
7
|
+
* @returns A navigate function that accepts a URL string and
|
|
8
|
+
* optional `NavigationNavigateOptions`.
|
|
9
|
+
*/
|
|
10
|
+
export declare function useNavigate(): (url: string, options?: NavigationNavigateOptions) => NavigationResult;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Returns the native browser Navigation object from context.
|
|
3
|
+
* Gives full access to the Navigation API: `entries()`,
|
|
4
|
+
* `traverseTo()`, `updateCurrentEntry()`, `canGoBack`,
|
|
5
|
+
* `canGoForward`, `transition`, `currentEntry`, etc.
|
|
6
|
+
*
|
|
7
|
+
* Must be used inside a `<Router>` or a component tree
|
|
8
|
+
* that provides a `<NavigationContext>` value.
|
|
9
|
+
*
|
|
10
|
+
* @returns The Navigation object from the nearest provider.
|
|
11
|
+
* @throws When used outside a NavigationContext provider.
|
|
12
|
+
*/
|
|
13
|
+
export declare function useNavigation(): Navigation;
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Callbacks for navigation lifecycle events. All callbacks
|
|
3
|
+
* are optional — only provided callbacks are subscribed.
|
|
4
|
+
*/
|
|
5
|
+
export interface NavigationEventHandlers {
|
|
6
|
+
/**
|
|
7
|
+
* Called when a same-document navigation is initiated.
|
|
8
|
+
* The Router uses this to intercept the event, match
|
|
9
|
+
* the destination URL, and trigger a React transition.
|
|
10
|
+
*/
|
|
11
|
+
readonly onNavigate?: (event: NavigateEvent) => void;
|
|
12
|
+
/**
|
|
13
|
+
* Called after a navigation completes successfully.
|
|
14
|
+
* Fires in sync with the Navigation API's
|
|
15
|
+
* `navigatesuccess` event.
|
|
16
|
+
*/
|
|
17
|
+
readonly onNavigateSuccess?: () => void;
|
|
18
|
+
/**
|
|
19
|
+
* Called when a navigation fails. Receives the error
|
|
20
|
+
* extracted from the Navigation API's `navigateerror`
|
|
21
|
+
* ErrorEvent.
|
|
22
|
+
*
|
|
23
|
+
* @param error - The error that caused the navigation
|
|
24
|
+
* to fail.
|
|
25
|
+
*/
|
|
26
|
+
readonly onNavigateError?: (error: unknown) => void;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Subscribes to the Navigation API's `navigate`,
|
|
30
|
+
* `navigatesuccess`, and `navigateerror` events on the
|
|
31
|
+
* given navigation object. All callbacks are wrapped in
|
|
32
|
+
* `useEffectEvent` so the effects only depend on the
|
|
33
|
+
* navigation instance itself — inline arrow functions
|
|
34
|
+
* from the caller don't cause unnecessary re-subscriptions.
|
|
35
|
+
*
|
|
36
|
+
* Cleans up all listeners on unmount or when the navigation
|
|
37
|
+
* instance changes.
|
|
38
|
+
*
|
|
39
|
+
* @param navigation - The Navigation object to subscribe to.
|
|
40
|
+
* @param handlers - Callbacks for each navigation lifecycle
|
|
41
|
+
* event. All are optional.
|
|
42
|
+
*/
|
|
43
|
+
export declare function useNavigationEvents(navigation: Navigation, handlers: NavigationEventHandlers): void;
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { TransitionFunction, useTransition } from 'react';
|
|
2
|
+
import { PrefetchFunc } from '../router';
|
|
3
|
+
/**
|
|
4
|
+
* Options for creating a precommit handler that forwards
|
|
5
|
+
* route context to the prefetch function.
|
|
6
|
+
*/
|
|
7
|
+
export interface PrecommitHandlerOptions {
|
|
8
|
+
/**
|
|
9
|
+
* The prefetch function from the matched route handler.
|
|
10
|
+
* When undefined, no precommit handler is created.
|
|
11
|
+
*/
|
|
12
|
+
readonly prefetch?: PrefetchFunc;
|
|
13
|
+
/**
|
|
14
|
+
* Dynamic route parameters extracted from the matched
|
|
15
|
+
* URL pattern.
|
|
16
|
+
*/
|
|
17
|
+
readonly params: Record<string, string>;
|
|
18
|
+
/**
|
|
19
|
+
* The destination URL being navigated to.
|
|
20
|
+
*/
|
|
21
|
+
readonly url: URL;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Creates handler functions for the Navigation API's
|
|
25
|
+
* `event.intercept()` method. The precommit handler runs
|
|
26
|
+
* prefetch logic before the URL commits; the handler runs
|
|
27
|
+
* the React state transition after the URL commits.
|
|
28
|
+
*
|
|
29
|
+
* Accepts an optional `transition` tuple to use directly.
|
|
30
|
+
* When omitted, reads from TransitionContext instead. The
|
|
31
|
+
* Router component passes its own transition tuple here to
|
|
32
|
+
* avoid a circular dependency — the Router provides the
|
|
33
|
+
* TransitionContext in its JSX return, but needs the
|
|
34
|
+
* handlers during render before that provider exists in
|
|
35
|
+
* the tree.
|
|
36
|
+
*
|
|
37
|
+
* @param transition - Optional `useTransition()` tuple to
|
|
38
|
+
* use instead of reading from TransitionContext. Pass
|
|
39
|
+
* this when calling from within the component that
|
|
40
|
+
* provides TransitionContext.
|
|
41
|
+
* @throws When no transition tuple is provided and the
|
|
42
|
+
* hook is used outside a TransitionContext provider.
|
|
43
|
+
*/
|
|
44
|
+
export declare function useNavigationHandlers(transition?: ReturnType<typeof useTransition>): {
|
|
45
|
+
createPrecommitHandler: (options: PrecommitHandlerOptions) => ((controller: NavigationPrecommitController) => Promise<void>) | undefined;
|
|
46
|
+
createHandler: (callback: TransitionFunction) => () => Promise<void>;
|
|
47
|
+
};
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Returns the AbortSignal from the current navigation event.
|
|
3
|
+
* The signal aborts when the navigation is cancelled (e.g.,
|
|
4
|
+
* by the user pressing Stop or a new navigation superseding
|
|
5
|
+
* this one). Pass this to fetch calls or other cancellable
|
|
6
|
+
* async operations to avoid stale work.
|
|
7
|
+
*
|
|
8
|
+
* Returns `null` before any navigation event has occurred
|
|
9
|
+
* (i.e. on the initial render).
|
|
10
|
+
*
|
|
11
|
+
* @returns The current AbortSignal or null.
|
|
12
|
+
*/
|
|
13
|
+
export declare function useNavigationSignal(): AbortSignal | null;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Returns the navigation type of the most recent navigation
|
|
3
|
+
* (`push`, `replace`, `reload`, or `traverse`). Useful for
|
|
4
|
+
* varying animations, skipping prefetch on reload, or
|
|
5
|
+
* applying different behavior for back/forward traversals.
|
|
6
|
+
*
|
|
7
|
+
* Returns `null` before any navigation event has occurred
|
|
8
|
+
* (i.e. on the initial render).
|
|
9
|
+
*
|
|
10
|
+
* @returns The current NavigationType or null.
|
|
11
|
+
*/
|
|
12
|
+
export declare function useNavigationType(): NavigationType | null;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { ComponentType } from 'react';
|
|
2
|
+
import { Handler } from '../router';
|
|
3
|
+
import { Matcher, Resolved } from '../../router/matcher';
|
|
4
|
+
/**
|
|
5
|
+
* Options for the `useNextMatch` hook.
|
|
6
|
+
*/
|
|
7
|
+
export interface NextMatchOptions {
|
|
8
|
+
/**
|
|
9
|
+
* Optional matcher override. When omitted the hook reads
|
|
10
|
+
* from `MatcherContext`.
|
|
11
|
+
*/
|
|
12
|
+
matcher?: Matcher<Handler>;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Returns a function that resolves a destination URL into a
|
|
16
|
+
* route match. When no route matches, a fallback `Resolved`
|
|
17
|
+
* is returned using the provided `notFound` component.
|
|
18
|
+
*
|
|
19
|
+
* Used internally by the Router to determine which component
|
|
20
|
+
* to render for an incoming navigation event.
|
|
21
|
+
*
|
|
22
|
+
* @param options - Optional matcher override.
|
|
23
|
+
* @returns A resolver function that takes a destination URL
|
|
24
|
+
* and a not-found component, returning the resolved match.
|
|
25
|
+
*/
|
|
26
|
+
export declare function useNextMatch(options?: NextMatchOptions): (destination: string | null, notFound: ComponentType) => Resolved<Handler>;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Returns the dynamic route parameters extracted from the
|
|
3
|
+
* currently matched URL pattern. The keys correspond to the
|
|
4
|
+
* `:param` names defined in the route pattern, and the values
|
|
5
|
+
* are the matching URL segments.
|
|
6
|
+
*
|
|
7
|
+
* Must be used inside a `<Router>` component tree where
|
|
8
|
+
* `ParamsContext` is provided.
|
|
9
|
+
*
|
|
10
|
+
* @returns A record of parameter names to their string values.
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* ```tsx
|
|
14
|
+
* // Route pattern: "/user/:id"
|
|
15
|
+
* // URL: "/user/42"
|
|
16
|
+
* const { id } = useParams() // id === "42"
|
|
17
|
+
* ```
|
|
18
|
+
*/
|
|
19
|
+
export declare function useParams(): Record<string, string>;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Returns the current URL pathname from the Router's state.
|
|
3
|
+
* The value updates on every navigation and reflects the
|
|
4
|
+
* pathname of the committed destination URL.
|
|
5
|
+
*
|
|
6
|
+
* Useful for active link detection, conditional rendering
|
|
7
|
+
* based on the current route, or building breadcrumbs.
|
|
8
|
+
*
|
|
9
|
+
* Must be used inside a `<Router>` component tree where
|
|
10
|
+
* `PathnameContext` is provided.
|
|
11
|
+
*
|
|
12
|
+
* @returns The current pathname string (e.g. `"/user/42"`).
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* ```tsx
|
|
16
|
+
* function Breadcrumb() {
|
|
17
|
+
* const pathname = usePathname()
|
|
18
|
+
*
|
|
19
|
+
* return <span>{pathname}</span>
|
|
20
|
+
* }
|
|
21
|
+
* ```
|
|
22
|
+
*/
|
|
23
|
+
export declare function usePathname(): string;
|