@arielgonzaguer/michi-router 1.0.0 → 1.0.1
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/README.md
CHANGED
|
@@ -45,6 +45,44 @@ function NavigateButton() {
|
|
|
45
45
|
}
|
|
46
46
|
```
|
|
47
47
|
|
|
48
|
+
### Usando un Layout General
|
|
49
|
+
|
|
50
|
+
Puedes aplicar un layout común a todas tus rutas:
|
|
51
|
+
|
|
52
|
+
```jsx
|
|
53
|
+
import { RouterProvider } from "@arielgonzaguer/michi-router";
|
|
54
|
+
import MainLayout from "./layouts/MainLayout";
|
|
55
|
+
|
|
56
|
+
const routes = [
|
|
57
|
+
{ path: "/", component: <Home /> },
|
|
58
|
+
{ path: "/about", component: <About /> },
|
|
59
|
+
];
|
|
60
|
+
|
|
61
|
+
function App() {
|
|
62
|
+
return (
|
|
63
|
+
<RouterProvider routes={routes} layout={MainLayout}>
|
|
64
|
+
<div>404: Page not found</div>
|
|
65
|
+
</RouterProvider>
|
|
66
|
+
);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
// Ejemplo de un componente Layout
|
|
70
|
+
function MainLayout({ children }) {
|
|
71
|
+
return (
|
|
72
|
+
<div className="layout">
|
|
73
|
+
<header>
|
|
74
|
+
<nav>
|
|
75
|
+
<Link to="/">Home</Link>
|
|
76
|
+
<Link to="/about">About</Link>
|
|
77
|
+
</nav>
|
|
78
|
+
</header>
|
|
79
|
+
<main>{children}</main>
|
|
80
|
+
<footer>Michi-router 😸</footer>
|
|
81
|
+
</div>
|
|
82
|
+
);
|
|
83
|
+
}
|
|
84
|
+
```
|
|
85
|
+
|
|
48
86
|
## API
|
|
49
87
|
|
|
50
88
|
### `<RouterProvider>`
|
|
@@ -55,6 +93,7 @@ Componente principal para el enrutamiento.
|
|
|
55
93
|
|
|
56
94
|
- `routes`: Array de objetos con `path` y `component`
|
|
57
95
|
- `children`: Elemento a mostrar cuando no hay ruta coincidente
|
|
96
|
+
- `layout`: Componente opcional que funciona como layout general para todas las rutas
|
|
58
97
|
|
|
59
98
|
### `<Link>`
|
|
60
99
|
|
|
@@ -1,7 +1,11 @@
|
|
|
1
1
|
import React from 'react';
|
|
2
2
|
import type { RouterContextType, RouterProviderProps, LinkProps } from './types';
|
|
3
3
|
declare const RouterContext: React.Context<RouterContextType>;
|
|
4
|
-
export declare function RouterProvider({ routes, children }: RouterProviderProps
|
|
4
|
+
export declare function RouterProvider({ routes, children, layout: Layout }: RouterProviderProps & {
|
|
5
|
+
layout?: React.ComponentType<{
|
|
6
|
+
children: React.ReactNode;
|
|
7
|
+
}>;
|
|
8
|
+
}): React.JSX.Element;
|
|
5
9
|
export declare const Link: ({ to, children, className }: LinkProps) => React.JSX.Element;
|
|
6
10
|
export declare const useNavigate: () => (to: string) => void;
|
|
7
11
|
export { RouterContext };
|
|
@@ -13,7 +13,7 @@ var RouterContext = (0, react_2.createContext)({
|
|
|
13
13
|
});
|
|
14
14
|
exports.RouterContext = RouterContext;
|
|
15
15
|
function RouterProvider(_a) {
|
|
16
|
-
var routes = _a.routes, children = _a.children;
|
|
16
|
+
var routes = _a.routes, children = _a.children, Layout = _a.layout;
|
|
17
17
|
var _b = (0, react_2.useState)(window.location.pathname), path = _b[0], setPath = _b[1];
|
|
18
18
|
(0, react_2.useEffect)(function () {
|
|
19
19
|
var handlePopState = function () { return setPath(window.location.pathname); };
|
|
@@ -25,7 +25,8 @@ function RouterProvider(_a) {
|
|
|
25
25
|
setPath(to);
|
|
26
26
|
};
|
|
27
27
|
var currentRoute = routes.find(function (route) { return route.path === path; });
|
|
28
|
-
|
|
28
|
+
var routeContent = currentRoute ? currentRoute.component : children;
|
|
29
|
+
return (react_1.default.createElement(RouterContext.Provider, { value: { path: path, navigate: navigate } }, Layout ? (react_1.default.createElement(Layout, null, routeContent)) : (routeContent)));
|
|
29
30
|
}
|
|
30
31
|
exports.RouterProvider = RouterProvider;
|
|
31
32
|
var Link = function (_a) {
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { ReactNode } from
|
|
1
|
+
import { ReactNode } from "react";
|
|
2
2
|
export interface Route {
|
|
3
3
|
path: string;
|
|
4
4
|
component: ReactNode;
|
|
@@ -8,8 +8,14 @@ export interface RouterContextType {
|
|
|
8
8
|
navigate: (to: string) => void;
|
|
9
9
|
}
|
|
10
10
|
export interface RouterProviderProps {
|
|
11
|
-
routes:
|
|
12
|
-
|
|
11
|
+
routes: Array<{
|
|
12
|
+
path: string;
|
|
13
|
+
component: React.ReactNode;
|
|
14
|
+
}>;
|
|
15
|
+
children?: React.ReactNode;
|
|
16
|
+
layout?: React.ComponentType<{
|
|
17
|
+
children: React.ReactNode;
|
|
18
|
+
}>;
|
|
13
19
|
}
|
|
14
20
|
export interface LinkProps {
|
|
15
21
|
to: string;
|