router-kit 0.1.7 → 0.2.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.
@@ -1,2 +1,5 @@
1
- declare const RouterProvider: () => import("react/jsx-runtime").JSX.Element;
1
+ import type { Route } from "../types";
2
+ declare const RouterProvider: ({ routes }: {
3
+ routes: Route[];
4
+ }) => import("react/jsx-runtime").JSX.Element;
2
5
  export default RouterProvider;
@@ -1,10 +1,9 @@
1
1
  import { jsx as _jsx } from "react/jsx-runtime";
2
2
  import { useEffect, useState } from "react";
3
3
  import join from "url-join";
4
- import { routes } from "../core/createRouter";
5
4
  import Page404 from "../pages/404";
6
5
  import RouterContext from "./RouterContext";
7
- const RouterProvider = () => {
6
+ const RouterProvider = ({ routes }) => {
8
7
  const [path, setPath] = useState(window.location.pathname);
9
8
  let fullPathWithParams = "";
10
9
  const pathValidation = (routeFullPath, currentPath) => {
@@ -1,4 +1,3 @@
1
1
  import type { Route } from "../types";
2
2
  declare function createRouter(inputRoutes: Route[]): Route[];
3
- export declare const routes: Route[];
4
3
  export default createRouter;
@@ -1,5 +1,5 @@
1
1
  function normalizeRoutes(inputRoutes) {
2
- const normalizedRoutes = inputRoutes.map((route) => {
2
+ return inputRoutes.map((route) => {
3
3
  var _a, _b;
4
4
  const normalizedPath = ((_a = route.path) === null || _a === void 0 ? void 0 : _a.startsWith("/"))
5
5
  ? route.path.replace(/^\/+/, "")
@@ -13,13 +13,8 @@ function normalizeRoutes(inputRoutes) {
13
13
  }
14
14
  return normalized;
15
15
  });
16
- routes.length = 0;
17
- routes.push(...normalizedRoutes);
18
- return routes;
19
16
  }
20
17
  function createRouter(inputRoutes) {
21
- // normalize and mutate the exported `routes` array in-place
22
18
  return normalizeRoutes(inputRoutes);
23
19
  }
24
- export const routes = [];
25
20
  export default createRouter;
@@ -1,8 +1,40 @@
1
+ /*
2
+ Router-Kit — Common hooks and components (reference)
3
+
4
+ Hooks:
5
+ - useRouter : access router context and helpers
6
+ - useLocation : current location (pathname, search, hash)
7
+ - useNavigate : imperative navigation function
8
+ - useParams : route params for the current route
9
+ - useSearchParams : read/update URL query params
10
+ - useRouteMatch : match the current location against a pattern
11
+ - useBlocker/usePrompt : block navigation with a confirmation
12
+
13
+ Components / APIs:
14
+ - createRouter : factory to create a router instance and Provider
15
+ - RouterProvider : provider component returned by createRouter()
16
+ - Routes : route collection wrapper
17
+ - Route : single route declaration
18
+ - Link : declarative link component
19
+ - NavLink : link with active styling
20
+ - Outlet : render child route content
21
+ - Redirect : declarative redirect helper (if provided)
22
+
23
+ Note: Exact names may vary between versions — update this list to match your installed Router-Kit exports.
24
+ */
1
25
  import { useContext } from "react";
2
26
  import RouterContext from "../context/RouterContext";
3
27
  export function useRouter() {
4
28
  const ctx = useContext(RouterContext);
5
- if (!ctx)
6
- throw new Error("useRouter must be used within a RouterProvider");
29
+ if (!ctx) {
30
+ const message = "Common hooks and components must be used within the RouterProvider returned by createRouter(). Wrap your app with the RouterProvider.";
31
+ if (typeof window !== "undefined" && window.console && console.error) {
32
+ console.error("%cRouterKit%c " + message, "color: #fff; background: #d9534f; font-weight: 700; padding: 2px 6px; border-radius: 3px;", "color: #d9534f;");
33
+ }
34
+ else {
35
+ console.error("RouterKit: " + message);
36
+ }
37
+ throw new Error("RouterKit: " + message);
38
+ }
7
39
  return ctx;
8
40
  }
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": "0.1.7",
8
+ "version": "0.2.1",
9
9
  "description": "A small React routing provider library",
10
10
  "main": "dist/index.js",
11
11
  "types": "dist/index.d.ts",