router-kit 1.3.2 → 1.3.4

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
@@ -34,6 +34,8 @@ npm install router-kit
34
34
 
35
35
  ### Basic Setup
36
36
 
37
+ **Programmatic Approach (Traditional):**
38
+
37
39
  ```tsx
38
40
  import React from "react";
39
41
  import { createRouter, RouterProvider, Link } from "router-kit";
@@ -56,6 +58,31 @@ function App() {
56
58
  export default App;
57
59
  ```
58
60
 
61
+ **Declarative Approach (New in v1.3.1):**
62
+
63
+ ```tsx
64
+ import React from "react";
65
+ import { Router, Route, Link } from "router-kit";
66
+
67
+ // 1. Define your components
68
+ const Home = () => <h1>Home Page</h1>;
69
+ const About = () => <h1>About Page</h1>;
70
+
71
+ // 2. Use declarative JSX routing
72
+ function App() {
73
+ return (
74
+ <Router>
75
+ <Route path="/" element={<Home />} />
76
+ <Route path="/about" element={<About />} />
77
+ </Router>
78
+ );
79
+ }
80
+
81
+ export default App;
82
+ ```
83
+
84
+ > 🆕 **New in v1.3.1**: Declarative routing with `<Router>` and `<Route>` components! Choose the approach that fits your style.
85
+
59
86
  ### Navigation
60
87
 
61
88
  ```tsx
@@ -259,20 +286,21 @@ const routes = createRouter([
259
286
 
260
287
  ## Feature Matrix
261
288
 
262
- | Feature | Status | Documentation |
263
- | --------------------- | ------ | ------------------------------------------------ |
264
- | Static Routes | ✅ | [Docs](./DOCUMENTATION.md#routes) |
265
- | Dynamic Routes | ✅ | [Docs](./DOCUMENTATION.md#useparams) |
266
- | Nested Routes | ✅ | [Docs](./DOCUMENTATION.md#nested-routes) |
267
- | Multiple Path Aliases | ✅ | [Docs](./DOCUMENTATION.md#multiple-path-aliases) |
268
- | Query Parameters | ✅ | [Docs](./DOCUMENTATION.md#usequery) |
269
- | Navigation State | ✅ | [Docs](./DOCUMENTATION.md#navigation-state) |
270
- | Custom 404 Pages | ✅ | [Docs](./DOCUMENTATION.md#custom-404-pages) |
271
- | TypeScript Support | ✅ | [Docs](./DOCUMENTATION.md#typescript-support) |
272
- | Error Handling | ✅ | [Docs](./DOCUMENTATION.md#error-handling) |
273
- | Dynamic Components | ✅ | [Docs](./API_REFERENCE.md#usedynamiccomponents) |
274
- | Hash Routing | | Planned |
275
- | Regex Routes | ⏳ | Planned |
289
+ | Feature | Status | Documentation |
290
+ | ----------------------- | ---------- | ------------------------------------------------ |
291
+ | Static Routes | ✅ | [Docs](./DOCUMENTATION.md#routes) |
292
+ | Dynamic Routes | ✅ | [Docs](./DOCUMENTATION.md#useparams) |
293
+ | Nested Routes | ✅ | [Docs](./DOCUMENTATION.md#nested-routes) |
294
+ | Multiple Path Aliases | ✅ | [Docs](./DOCUMENTATION.md#multiple-path-aliases) |
295
+ | Query Parameters | ✅ | [Docs](./DOCUMENTATION.md#usequery) |
296
+ | Navigation State | ✅ | [Docs](./DOCUMENTATION.md#navigation-state) |
297
+ | Custom 404 Pages | ✅ | [Docs](./DOCUMENTATION.md#custom-404-pages) |
298
+ | TypeScript Support | ✅ | [Docs](./DOCUMENTATION.md#typescript-support) |
299
+ | Error Handling | ✅ | [Docs](./DOCUMENTATION.md#error-handling) |
300
+ | Dynamic Components | ✅ | [Docs](./API_REFERENCE.md#usedynamiccomponents) |
301
+ | **Declarative Routing** | ✅ **NEW** | [Docs](./DECLARATIVE_ROUTING.md) |
302
+ | Hash Routing | ⏳ | Planned |
303
+ | Regex Routes | ⏳ | Planned |
276
304
 
277
305
  ---
278
306
 
@@ -282,7 +310,7 @@ const routes = createRouter([
282
310
 
283
311
  ```tsx
284
312
  // Core
285
- import { createRouter, RouterProvider } from "router-kit";
313
+ import { createRouter, RouterProvider, Router, Route } from "router-kit";
286
314
 
287
315
  // Components
288
316
  import { Link, NavLink } from "router-kit";
@@ -311,6 +339,8 @@ import { RouterErrorCode, RouterErrors, createRouterError } from "router-kit";
311
339
 
312
340
  ### Route Patterns
313
341
 
342
+ **Programmatic Approach:**
343
+
314
344
  ```tsx
315
345
  // Static route
316
346
  { path: "about", component: <About /> }
@@ -337,6 +367,30 @@ import { RouterErrorCode, RouterErrors, createRouterError } from "router-kit";
337
367
  { path: "/404", component: <NotFound /> }
338
368
  ```
339
369
 
370
+ **Declarative Approach:**
371
+
372
+ ```tsx
373
+ // Static route
374
+ <Route path="/about" element={<About />} />
375
+
376
+ // Dynamic parameter
377
+ <Route path="/users/:id" element={<UserProfile />} />
378
+
379
+ // Multiple parameters
380
+ <Route path="/posts/:category/:slug" element={<BlogPost />} />
381
+
382
+ // Multiple paths
383
+ <Route path={["/about", "/about-us"]} element={<About />} />
384
+
385
+ // Nested routes
386
+ <Route path="/dashboard" element={<Dashboard />}>
387
+ <Route path="settings" element={<Settings />} />
388
+ </Route>
389
+
390
+ // 404 page
391
+ <Route path="/404" element={<NotFound />} />
392
+ ```
393
+
340
394
  ### Hook Usage
341
395
 
342
396
  ```tsx
@@ -0,0 +1,23 @@
1
+ import { type ReactNode } from "react";
2
+ /**
3
+ * Router component for declarative routing
4
+ * Provides an alternative JSX-based approach to defining routes
5
+ *
6
+ * @example
7
+ * ```tsx
8
+ * <Router>
9
+ * <Route path="/" element={<Home />} />
10
+ * <Route path="/about" element={<About />} />
11
+ * <Route path="/users/:id" element={<UserProfile />} />
12
+ * <Route path="/dashboard" element={<Dashboard />}>
13
+ * <Route path="settings" element={<Settings />} />
14
+ * <Route path="profile" element={<Profile />} />
15
+ * </Route>
16
+ * <Route path="/404" element={<NotFound />} />
17
+ * </Router>
18
+ * ```
19
+ */
20
+ declare const Router: ({ children }: {
21
+ children: ReactNode;
22
+ }) => import("react/jsx-runtime").JSX.Element;
23
+ export default Router;
@@ -0,0 +1,61 @@
1
+ import { jsx as _jsx } from "react/jsx-runtime";
2
+ import { Children, isValidElement } from "react";
3
+ import RouterProvider from "../context/RouterProvider";
4
+ import createRouter from "../core/createRouter";
5
+ import { Route } from "./route";
6
+ /**
7
+ * Extracts route configurations from JSX Route elements
8
+ * @param children React children containing Route elements
9
+ * @returns Array of route configurations
10
+ */
11
+ function extractRoutesFromJSX(children) {
12
+ const routes = [];
13
+ Children.forEach(children, (child) => {
14
+ var _a;
15
+ if (isValidElement(child)) {
16
+ // Check if this is a Route component
17
+ const isRouteComponent = child.type === Route ||
18
+ ((_a = child.type) === null || _a === void 0 ? void 0 : _a.displayName) === "Route" ||
19
+ (typeof child.type === "function" && child.type.name === "Route");
20
+ if (isRouteComponent) {
21
+ const props = child.props;
22
+ const route = {
23
+ path: props.path,
24
+ component: props.element,
25
+ };
26
+ // Handle nested routes
27
+ if (props.children) {
28
+ const childRoutes = extractRoutesFromJSX(props.children);
29
+ if (childRoutes.length > 0) {
30
+ route.children = childRoutes;
31
+ }
32
+ }
33
+ routes.push(route);
34
+ }
35
+ }
36
+ });
37
+ return routes;
38
+ }
39
+ /**
40
+ * Router component for declarative routing
41
+ * Provides an alternative JSX-based approach to defining routes
42
+ *
43
+ * @example
44
+ * ```tsx
45
+ * <Router>
46
+ * <Route path="/" element={<Home />} />
47
+ * <Route path="/about" element={<About />} />
48
+ * <Route path="/users/:id" element={<UserProfile />} />
49
+ * <Route path="/dashboard" element={<Dashboard />}>
50
+ * <Route path="settings" element={<Settings />} />
51
+ * <Route path="profile" element={<Profile />} />
52
+ * </Route>
53
+ * <Route path="/404" element={<NotFound />} />
54
+ * </Router>
55
+ * ```
56
+ */
57
+ const Router = ({ children }) => {
58
+ const routes = extractRoutesFromJSX(children);
59
+ return _jsx(RouterProvider, { routes: createRouter(routes) });
60
+ };
61
+ export default Router;
@@ -0,0 +1,27 @@
1
+ import { ReactElement } from "react";
2
+ /**
3
+ * Route component props for declarative routing
4
+ */
5
+ export interface RouteProps {
6
+ path: string | string[];
7
+ element: ReactElement;
8
+ children?: ReactElement<RouteProps> | ReactElement<RouteProps>[];
9
+ }
10
+ /**
11
+ * Route component - used for declarative route definitions
12
+ * This is a placeholder component that will be processed by the Router
13
+ *
14
+ * @example
15
+ * ```tsx
16
+ * <Route path="/users/:id" element={<UserProfile />} />
17
+ * <Route path={["/about", "/about-us"]} element={<About />} />
18
+ * <Route path="/dashboard" element={<Dashboard />}>
19
+ * <Route path="settings" element={<Settings />} />
20
+ * </Route>
21
+ * ```
22
+ */
23
+ export declare function Route(props: RouteProps): null;
24
+ export declare namespace Route {
25
+ var displayName: string;
26
+ }
27
+ export default Route;
@@ -0,0 +1,21 @@
1
+ /**
2
+ * Route component - used for declarative route definitions
3
+ * This is a placeholder component that will be processed by the Router
4
+ *
5
+ * @example
6
+ * ```tsx
7
+ * <Route path="/users/:id" element={<UserProfile />} />
8
+ * <Route path={["/about", "/about-us"]} element={<About />} />
9
+ * <Route path="/dashboard" element={<Dashboard />}>
10
+ * <Route path="settings" element={<Settings />} />
11
+ * </Route>
12
+ * ```
13
+ */
14
+ export function Route(props) {
15
+ // This component doesn't render anything directly
16
+ // It's used as a declarative way to define routes that will be processed by Router
17
+ return null;
18
+ }
19
+ // Add displayName for better debugging and component recognition
20
+ Route.displayName = "Route";
21
+ export default Route;
package/dist/index.d.ts CHANGED
@@ -1,10 +1,14 @@
1
1
  export { default as Link } from "./components/Link";
2
2
  export { default as NavLink } from "./components/NavLink";
3
+ export { default as Route } from "./components/route";
4
+ export { default as Router } from "./components/Router";
3
5
  export { default as RouterProvider } from "./context/RouterProvider";
4
6
  export { default as createRouter } from "./core/createRouter";
5
7
  export { useDynamicComponents } from "./hooks/useDynamicComponents";
6
8
  export { useLocation } from "./hooks/useLocation";
7
9
  export { useParams } from "./hooks/useParams";
8
10
  export { useQuery } from "./hooks/useQuery";
9
- export type { DynamicComponents, GetComponent, Location, NavigateOptions, Route, RouterContextType, RouterError, RouterKitError, Routes, } from "./types/index";
11
+ export { useRouter } from "./hooks/useRouter";
12
+ export type { RouteProps } from "./components/route";
13
+ export type { DynamicComponents, GetComponent, Location, NavigateOptions, RouterContextType, RouterError, RouterKitError, Routes, Route as RouteType, } from "./types/index";
10
14
  export { createRouterError, RouterErrorCode, RouterErrors, } from "./utils/error/errors";
package/dist/index.js CHANGED
@@ -1,9 +1,12 @@
1
1
  export { default as Link } from "./components/Link";
2
2
  export { default as NavLink } from "./components/NavLink";
3
+ export { default as Route } from "./components/route";
4
+ export { default as Router } from "./components/Router";
3
5
  export { default as RouterProvider } from "./context/RouterProvider";
4
6
  export { default as createRouter } from "./core/createRouter";
5
7
  export { useDynamicComponents } from "./hooks/useDynamicComponents";
6
8
  export { useLocation } from "./hooks/useLocation";
7
9
  export { useParams } from "./hooks/useParams";
8
10
  export { useQuery } from "./hooks/useQuery";
11
+ export { useRouter } from "./hooks/useRouter";
9
12
  export { createRouterError, RouterErrorCode, RouterErrors, } from "./utils/error/errors";
File without changes
@@ -0,0 +1 @@
1
+ "use strict";
package/package.json CHANGED
@@ -5,7 +5,7 @@
5
5
  "email": "mohammed.bencheikh.dev@gmail.com",
6
6
  "url": "https://mohammedbencheikh.com/"
7
7
  },
8
- "version": "1.3.2",
8
+ "version": "1.3.4",
9
9
  "description": "A small React routing provider library",
10
10
  "main": "dist/index.js",
11
11
  "types": "dist/index.d.ts",