1
- import { h , Fragment , createContext , cloneElement , toChildArray } from 'preact' ;
1
+ import { h , createContext , cloneElement , toChildArray } from 'preact' ;
2
2
import { useContext , useMemo , useReducer , useLayoutEffect , useRef } from 'preact/hooks' ;
3
3
4
4
/**
@@ -117,6 +117,7 @@ export function Router(props) {
117
117
const [ c , update ] = useReducer ( c => c + 1 , 0 ) ;
118
118
119
119
const { url, path, pathParams, searchParams, wasPush } = useLocation ( ) ;
120
+ const { rest = path } = useContext ( RouterContext ) ;
120
121
121
122
const isLoading = useRef ( false ) ;
122
123
const prevRoute = useRef ( path ) ;
@@ -136,7 +137,7 @@ export function Router(props) {
136
137
137
138
let pathRoute , defaultRoute , matchProps ;
138
139
toChildArray ( props . children ) . some ( ( /** @type {VNode<any> } */ vnode ) => {
139
- const matches = exec ( path , vnode . props . path , ( matchProps = { ...vnode . props , path, pathParams, searchParams } ) ) ;
140
+ const matches = exec ( rest , vnode . props . path , ( matchProps = { ...vnode . props , path : rest , pathParams, searchParams } ) ) ;
140
141
if ( matches ) return ( pathRoute = cloneElement ( vnode , matchProps ) ) ;
141
142
if ( vnode . props . default ) defaultRoute = cloneElement ( vnode , matchProps ) ;
142
143
} ) ;
@@ -149,7 +150,7 @@ export function Router(props) {
149
150
const routeChanged = useMemo ( ( ) => {
150
151
prev . current = cur . current ;
151
152
152
- cur . current = /** @type {VNode<any> } */ ( h ( Fragment , { key : path } , incoming ) ) ;
153
+ cur . current = /** @type {VNode<any> } */ ( h ( RouterContext . Provider , { value : matchProps } , incoming ) ) ;
153
154
154
155
// Only mark as an update if the route component changed.
155
156
const outgoing = prev . current && prev . current . props . children ;
@@ -263,6 +264,9 @@ Router.Provider = LocationProvider;
263
264
LocationProvider . ctx = createContext (
264
265
/** @type {import('./router.d.ts').LocationHook & { wasPush: boolean } } */ ( { } )
265
266
) ;
267
+ const RouterContext = createContext (
268
+ /** @type {{ rest: string } } */ ( { } )
269
+ ) ;
266
270
267
271
export const Route = props => h ( props . component , props ) ;
268
272
0 commit comments