feat: enable layout element

This commit is contained in:
Valerio Ageno
2024-05-12 17:13:41 +02:00
parent 4e1d0b6252
commit b557820b6e
7 changed files with 36 additions and 22 deletions
@@ -1,6 +1,7 @@
import * as React from 'react'
import { useRouter } from '../hooks/useRouter'
import { useRouterStore } from '../hooks/useRouterStore'
import { RouteMatch } from './RouteMatch'
import NotFound from './NotFound'
export const Matches = React.memo(function () {
@@ -13,9 +14,5 @@ export const Matches = React.memo(function () {
return <NotFound />
}
if (route.options.hasHandler) {
console.log('Has rust handler')
}
return route.options.component()
return <RouteMatch route={route} />
})
@@ -1,12 +1,15 @@
import { useRouter } from '../hooks/useRouter'
import { RouteMatch } from './RouteMatch'
import Link from './Link'
export default function NotFound(): JSX.Element {
const router = useRouter()
const custom404Route = router.routesById['/404']
// Check if exists a custom 404 error page
if (router.routesById['/404']) {
return router.routesById['/404'].options.component()
if (custom404Route) {
return <RouteMatch route={custom404Route} />
}
return (
@@ -1,7 +0,0 @@
import * as React from 'react'
const Outlet = React.memo(function Outlet() {
return <h1>Outlet</h1>
})
export default Outlet
@@ -0,0 +1,23 @@
import type { Route } from '../route'
interface MatchProps {
route: Route
}
/**
* Returns the route match with the root element if exists
*/
export const RouteMatch = ({ route }: MatchProps): JSX.Element => {
if (route.options.hasHandler) {
console.log('Has rust handler')
}
if (!route.isRoot) {
console.log(route.options)
return route.options.getParentRoute().component({
children: route.options.component(),
})
}
return route.options.component()
}
@@ -44,7 +44,6 @@ interface RouterProviderProps {
const initRouterStore = (): void => {
const updateLocation = useRouterStore((st) => st.updateLocation)
console.log(window.location)
useLayoutEffect(() => {
const { pathname, hash, href, search } = window.location
updateLocation({
+5 -5
View File
@@ -1,4 +1,3 @@
import type { ReactNode } from 'react'
import type { RouterType } from './router'
import { trimPathLeft, joinPaths } from './utils'
@@ -6,7 +5,7 @@ interface RouteOptions {
isRoot?: boolean
getParentRoute?: () => Route
path?: string
component: ReactNode
component: () => JSX.Element
}
export function createRoute(options: RouteOptions): Route {
@@ -26,10 +25,10 @@ export class Route {
router: RouterType
isRoot: boolean
originalIndex: number
component: ReactNode
component: () => JSX.Element
constructor(options: RouteOptions) {
this.isRoot = options.isRoot || !options.getParentRoute
this.isRoot = options.isRoot ?? typeof options.getParentRoute !== 'function'
this.options = options
;(this as any).$$typeof = Symbol.for('react.memo')
@@ -45,7 +44,7 @@ export class Route {
this.parentRoute = this.options?.getParentRoute?.()
if (isRoot) {
this.path = rootRouteId as TPath
this.path = rootRouteId
}
let path: undefined | string = isRoot ? rootRouteId : this.options.path
@@ -90,6 +89,7 @@ export class Route {
update = (options: RouteOptions): this => {
Object.assign(this.options, options)
this.isRoot = options.isRoot || !options.getParentRoute
return this
}
+1 -2
View File
@@ -1,6 +1,5 @@
import { trimPath, trimPathRight } from './utils'
import type { Route } from './route'
import type { ReactNode } from 'react'
type RouteTree = any
@@ -11,7 +10,7 @@ interface CreateRouter {
}
interface RouteOptions {
component?: () => ReactNode
component?: () => JSX.Element
hasHandler?: boolean
routeTree?: RouteTree
}