@arielgonzaguer/michi-router 1.0.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.txt +6 -0
- package/README.md +79 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +17 -0
- package/dist/michi-router/Michi-router.d.ts +7 -0
- package/dist/michi-router/Michi-router.js +44 -0
- package/dist/michi-router/index.d.ts +2 -0
- package/dist/michi-router/index.js +7 -0
- package/dist/michi-router/types.d.ts +18 -0
- package/dist/michi-router/types.js +2 -0
- package/package.json +39 -0
package/LICENSE.txt
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
Copyright 2025 ArielGonzAgüer
|
|
2
|
+
Por la presente se concede permiso, libre de cargos, a cualquier persona que obtenga una copia de este software y de los archivos de documentación asociados (el "Software"), a utilizar el Software sin restricción, incluyendo sin limitación los derechos a usar, copiar, modificar, fusionar, publicar, distribuir, sublicenciar, y/o vender copias del Software, y a permitir a las personas a las que se les proporcione el Software a hacer lo mismo, sujeto a las siguientes condiciones:
|
|
3
|
+
|
|
4
|
+
El aviso de copyright anterior y este aviso de permiso se incluirán en todas las copias o partes sustanciales del Software.
|
|
5
|
+
|
|
6
|
+
EL SOFTWARE SE PROPORCIONA "COMO ESTÁ", SIN GARANTÍA DE NINGÚN TIPO, EXPRESA O IMPLÍCITA, INCLUYENDO PERO NO LIMITADO A GARANTÍAS DE COMERCIALIZACIÓN, IDONEIDAD PARA UN PROPÓSITO PARTICULAR E INCUMPLIMIENTO. EN NINGÚN CASO LOS AUTORES O PROPIETARIOS DE LOS DERECHOS DE AUTOR SERÁN RESPONSABLES DE NINGUNA RECLAMACIÓN, DAÑOS U OTRAS RESPONSABILIDADES, YA SEA EN UNA ACCIÓN DE CONTRATO, AGRAVIO O CUALQUIER OTRO MOTIVO, DERIVADAS DE, FUERA DE O EN CONEXIÓN CON EL SOFTWARE O SU USO U OTRO TIPO DE ACCIONES EN EL SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
# Michi Router
|
|
2
|
+
|
|
3
|
+
El router minimalista y simple para React.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @arielgonzaguer/michi-router
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
```jsx
|
|
14
|
+
import { RouterProvider, Link } from "@arielgonzaguer/michi-router";
|
|
15
|
+
|
|
16
|
+
// puede crear rutas en un archivo separado o en el mismo archivo
|
|
17
|
+
const routes = [
|
|
18
|
+
{ path: "/", component: <Home /> },
|
|
19
|
+
{ path: "/about", component: <About /> },
|
|
20
|
+
];
|
|
21
|
+
|
|
22
|
+
function App() {
|
|
23
|
+
return (
|
|
24
|
+
<RouterProvider routes={routes}>
|
|
25
|
+
{/* Esto se muestra si no se encuentra una ruta */}
|
|
26
|
+
<div>404: Page not found</div>
|
|
27
|
+
</RouterProvider>
|
|
28
|
+
);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
// Usando Link
|
|
32
|
+
function NavigationComponent() {
|
|
33
|
+
return (
|
|
34
|
+
<nav>
|
|
35
|
+
<Link to="/">Home</Link>
|
|
36
|
+
<Link to="/about">About</Link>
|
|
37
|
+
</nav>
|
|
38
|
+
);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// Usando useNavigate
|
|
42
|
+
function NavigateButton() {
|
|
43
|
+
const navigate = useNavigate();
|
|
44
|
+
return <button onClick={() => navigate("/about")}>Ir a About</button>;
|
|
45
|
+
}
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
## API
|
|
49
|
+
|
|
50
|
+
### `<RouterProvider>`
|
|
51
|
+
|
|
52
|
+
Componente principal para el enrutamiento.
|
|
53
|
+
|
|
54
|
+
**Props:**
|
|
55
|
+
|
|
56
|
+
- `routes`: Array de objetos con `path` y `component`
|
|
57
|
+
- `children`: Elemento a mostrar cuando no hay ruta coincidente
|
|
58
|
+
|
|
59
|
+
### `<Link>`
|
|
60
|
+
|
|
61
|
+
Componente para navegación declarativa.
|
|
62
|
+
|
|
63
|
+
**Props:**
|
|
64
|
+
|
|
65
|
+
- `to`: Ruta destino
|
|
66
|
+
- `children`: Contenido del enlace
|
|
67
|
+
- `className`: Clases CSS opcionales
|
|
68
|
+
|
|
69
|
+
### `useNavigate()`
|
|
70
|
+
|
|
71
|
+
Hook para navegación programática.
|
|
72
|
+
|
|
73
|
+
**Retorna:**
|
|
74
|
+
|
|
75
|
+
- Función navigate que acepta la ruta como parámetro
|
|
76
|
+
|
|
77
|
+
## Licencia
|
|
78
|
+
|
|
79
|
+
MIT
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './michi-router';
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./michi-router"), exports);
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import type { RouterContextType, RouterProviderProps, LinkProps } from './types';
|
|
3
|
+
declare const RouterContext: React.Context<RouterContextType>;
|
|
4
|
+
export declare function RouterProvider({ routes, children }: RouterProviderProps): React.JSX.Element;
|
|
5
|
+
export declare const Link: ({ to, children, className }: LinkProps) => React.JSX.Element;
|
|
6
|
+
export declare const useNavigate: () => (to: string) => void;
|
|
7
|
+
export { RouterContext };
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.RouterContext = exports.useNavigate = exports.Link = exports.RouterProvider = void 0;
|
|
7
|
+
var react_1 = __importDefault(require("react"));
|
|
8
|
+
var react_2 = require("react");
|
|
9
|
+
// Creación del contexto con un valor inicial
|
|
10
|
+
var RouterContext = (0, react_2.createContext)({
|
|
11
|
+
path: '',
|
|
12
|
+
navigate: function () { }
|
|
13
|
+
});
|
|
14
|
+
exports.RouterContext = RouterContext;
|
|
15
|
+
function RouterProvider(_a) {
|
|
16
|
+
var routes = _a.routes, children = _a.children;
|
|
17
|
+
var _b = (0, react_2.useState)(window.location.pathname), path = _b[0], setPath = _b[1];
|
|
18
|
+
(0, react_2.useEffect)(function () {
|
|
19
|
+
var handlePopState = function () { return setPath(window.location.pathname); };
|
|
20
|
+
window.addEventListener('popstate', handlePopState);
|
|
21
|
+
return function () { return window.removeEventListener('popstate', handlePopState); };
|
|
22
|
+
}, []);
|
|
23
|
+
var navigate = function (to) {
|
|
24
|
+
window.history.pushState({}, '', to);
|
|
25
|
+
setPath(to);
|
|
26
|
+
};
|
|
27
|
+
var currentRoute = routes.find(function (route) { return route.path === path; });
|
|
28
|
+
return (react_1.default.createElement(RouterContext.Provider, { value: { path: path, navigate: navigate } }, currentRoute ? currentRoute.component : children));
|
|
29
|
+
}
|
|
30
|
+
exports.RouterProvider = RouterProvider;
|
|
31
|
+
var Link = function (_a) {
|
|
32
|
+
var to = _a.to, children = _a.children, className = _a.className;
|
|
33
|
+
var navigate = (0, react_2.useContext)(RouterContext).navigate;
|
|
34
|
+
return (react_1.default.createElement("a", { href: to, className: className, style: { marginRight: '1rem' }, onClick: function (e) {
|
|
35
|
+
e.preventDefault();
|
|
36
|
+
navigate(to);
|
|
37
|
+
} }, children));
|
|
38
|
+
};
|
|
39
|
+
exports.Link = Link;
|
|
40
|
+
var useNavigate = function () {
|
|
41
|
+
var navigate = (0, react_2.useContext)(RouterContext).navigate;
|
|
42
|
+
return navigate;
|
|
43
|
+
};
|
|
44
|
+
exports.useNavigate = useNavigate;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.useNavigate = exports.Link = exports.RouterProvider = void 0;
|
|
4
|
+
var Michi_router_1 = require("./Michi-router");
|
|
5
|
+
Object.defineProperty(exports, "RouterProvider", { enumerable: true, get: function () { return Michi_router_1.RouterProvider; } });
|
|
6
|
+
Object.defineProperty(exports, "Link", { enumerable: true, get: function () { return Michi_router_1.Link; } });
|
|
7
|
+
Object.defineProperty(exports, "useNavigate", { enumerable: true, get: function () { return Michi_router_1.useNavigate; } });
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { ReactNode } from 'react';
|
|
2
|
+
export interface Route {
|
|
3
|
+
path: string;
|
|
4
|
+
component: ReactNode;
|
|
5
|
+
}
|
|
6
|
+
export interface RouterContextType {
|
|
7
|
+
path: string;
|
|
8
|
+
navigate: (to: string) => void;
|
|
9
|
+
}
|
|
10
|
+
export interface RouterProviderProps {
|
|
11
|
+
routes: Route[];
|
|
12
|
+
children?: ReactNode;
|
|
13
|
+
}
|
|
14
|
+
export interface LinkProps {
|
|
15
|
+
to: string;
|
|
16
|
+
children: ReactNode;
|
|
17
|
+
className?: string;
|
|
18
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@arielgonzaguer/michi-router",
|
|
3
|
+
"private": false,
|
|
4
|
+
"version": "1.0.0",
|
|
5
|
+
"description": "The minimalist and simple router for React",
|
|
6
|
+
"main": "dist/index.js",
|
|
7
|
+
"types": "dist/index.d.ts",
|
|
8
|
+
"files": [
|
|
9
|
+
"dist"
|
|
10
|
+
],
|
|
11
|
+
"scripts": {
|
|
12
|
+
"build": "tsc",
|
|
13
|
+
"prepare": "npm run build",
|
|
14
|
+
"clean": "rimraf dist",
|
|
15
|
+
"prebuild": "npm run clean",
|
|
16
|
+
"prepublishOnly": "npm run build"
|
|
17
|
+
},
|
|
18
|
+
"keywords": [
|
|
19
|
+
"react",
|
|
20
|
+
"router",
|
|
21
|
+
"typescript",
|
|
22
|
+
"michi",
|
|
23
|
+
"cats"
|
|
24
|
+
],
|
|
25
|
+
"peerDependencies": {
|
|
26
|
+
"react": ">=16.8.0",
|
|
27
|
+
"react-dom": ">=16.8.0"
|
|
28
|
+
},
|
|
29
|
+
"devDependencies": {
|
|
30
|
+
"@types/react": "^18.3.18",
|
|
31
|
+
"@types/react-dom": "^18.3.5",
|
|
32
|
+
"react": "^18.3.1",
|
|
33
|
+
"react-dom": "^18.3.1",
|
|
34
|
+
"rimraf": "^6.0.1",
|
|
35
|
+
"typescript": "^4.9.5"
|
|
36
|
+
},
|
|
37
|
+
"author": "Ariel González Agüero",
|
|
38
|
+
"license": "MIT"
|
|
39
|
+
}
|