clear-react-router 1.0.3 → 1.0.6

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
@@ -1,3 +1,5 @@
1
+ [![npm version](https://badge.fury.io/js/clear-react-router.svg)](https://www.npmjs.com/package/clear-react-router)
2
+
1
3
  A lightweight, type-safe routing library for React applications with nested routes, data loading, navigation blocking, and prefetching.
2
4
 
3
5
  ## Features
@@ -14,6 +16,23 @@ A lightweight, type-safe routing library for React applications with nested rout
14
16
 
15
17
  ## API
16
18
 
19
+ ### `createRouter(routes)`
20
+
21
+ Normalizes route configuration. Handles wildcard `*` routes, extracts dynamic params, builds nested paths.
22
+
23
+ | Property | Type | Description |
24
+ |----------|------|-------------|
25
+ | `path` | `string` | Route path, e.g., `/user/:userId` |
26
+ | `element` | `ReactElement \| () => ReactElement \| LazyComponent` | Component to render |
27
+ | `loader` | `() => Promise<unknown>` | Fetch data |
28
+ | `beforeLoad` | `(context) => Promise<void>` | Auth checks, redirects |
29
+ | `afterLoad` | `(context) => Promise<void>` | Analytics, side effects |
30
+ | `fallback` | `ReactElement \| () => ReactElement` | Loading fallback (for lazy loading) |
31
+ | `loaderFallback` | `ReactElement \| () => ReactElement` | Loading fallback (for loader) |
32
+ | `errorElement` | `ReactElement \| () => ReactElement` | Error fallback |
33
+ | `staleTime` | `number` | Cache duration in ms for loader data |
34
+ | `children` | `RouteItem[]` | Nested routes |
35
+
17
36
  ### `Router`
18
37
 
19
38
  Main component that renders the application based on current URL.
@@ -23,19 +42,6 @@ Main component that renders the application based on current URL.
23
42
  | `routeList` | `RouteItem[]` | Array of route configurations |
24
43
  | `context` | `object` | Optional initial context (user, theme, etc.) |
25
44
 
26
- ### `createRouter(routes)`
27
-
28
- Normalizes route configuration. Handles wildcard `*` routes, extracts dynamic params, builds nested paths.
29
-
30
- ### `redirect(url, search?)`
31
-
32
- Redirects from `beforeLoad`.
33
- ```
34
- beforeLoad: context => {
35
- if (!context.isAuthorized) return redirect('/');
36
- }
37
- ```
38
-
39
45
  ### `Link`
40
46
 
41
47
  Component for client-side navigation with prefetch support.
@@ -46,6 +52,15 @@ Component for client-side navigation with prefetch support.
46
52
  | `prefetch` | `boolean` | `true` |
47
53
  | `children` | `ReactElement` | required |
48
54
 
55
+ ### `redirect(url, search?)`
56
+
57
+ Redirects from `beforeLoad`.
58
+ ```
59
+ beforeLoad: context => {
60
+ if (!context.isAuthorized) return redirect('/');
61
+ }
62
+ ```
63
+
49
64
  ## Hooks
50
65
 
51
66
  ### `useNavigate()`
@@ -120,7 +135,7 @@ Executes a callback when the page is about to be closed or reloaded. Perfect for
120
135
 
121
136
  | Parameter | Type | Description |
122
137
  |-----------|------|-------------|
123
- | `callback` | `() => void | undefined` | Function to execute before page unload (e.g., auto-save) |
138
+ | `callback` | `() => void \| undefined` | Function to execute before page unload (e.g., auto-save) |
124
139
 
125
140
  **Note:** This hook does not show a browser confirmation dialog. It silently executes the callback, allowing you to save user data in the background before the page closes.
126
141
 
@@ -133,22 +148,6 @@ const onSave = useCallback(() => {
133
148
  // Auto-save when user tries to close/reload the page
134
149
  useBeforeUnload(text ? onSave : undefined);
135
150
  ```
136
- ## Route Configuration
137
-
138
- ### `RouteItem`
139
-
140
- | Property | Type | Description |
141
- |----------|------|-------------|
142
- | `path` | `string` | Route path, e.g., `/user/:userId` |
143
- | `element` | `ReactElement \| () => ReactElement \| LazyComponent` | Component to render |
144
- | `loader` | `() => Promise<unknown>` | Fetch data |
145
- | `beforeLoad` | `(context) => Promise<void>` | Auth checks, redirects |
146
- | `afterLoad` | `(context) => Promise<void>` | Analytics, side effects |
147
- | `fallback` | `ReactElement \| () => ReactElement` | Loading fallback (for lazy loading) |
148
- | `loaderFallback` | `ReactElement \| () => ReactElement` | Loading fallback (for loader) |
149
- | `errorElement` | `ReactElement \| () => ReactElement` | Error fallback |
150
- | `staleTime` | `number` | Cache duration in ms for loader data |
151
- | `children` | `RouteItem[]` | Nested routes |
152
151
 
153
152
  ## Lazy Loading
154
153
 
@@ -1,4 +1,4 @@
1
- import { ReactElement, MouseEvent, CSSProperties } from 'react';
1
+ import { type ReactElement, type MouseEvent, type CSSProperties } from 'react';
2
2
  type LinkProps = {
3
3
  to: string;
4
4
  children: ReactElement<{
@@ -1,4 +1,4 @@
1
- import { RouteItem } from '../types/global.ts';
1
+ import type { RouteItem } from '../types/global.ts';
2
2
  type RouterProps = {
3
3
  routeList: RouteItem[];
4
4
  context?: Record<string, unknown>;
@@ -1,4 +1,4 @@
1
- import { BlockerState, Location, UpdateBlockedRouteProps } from '../types/global.ts';
1
+ import type { BlockerState, Location, UpdateBlockedRouteProps } from '../types/global';
2
2
  export type NavigationContextValue = {
3
3
  location: Location;
4
4
  params: Record<string, string>;
@@ -11,9 +11,9 @@ export type ActionsContextValue = {
11
11
  setContext(arg: object): void;
12
12
  };
13
13
  export type DataContextValue = {
14
- loaderCache: Record<string, unknown>;
14
+ loaderCache: unknown;
15
15
  context: Record<string, unknown>;
16
16
  };
17
- export declare const ActionsContext: import('react').Context<ActionsContextValue>;
18
- export declare const DataContext: import('react').Context<DataContextValue>;
19
- export declare const NavigationContext: import('react').Context<NavigationContextValue>;
17
+ export declare const ActionsContext: import("react").Context<ActionsContextValue>;
18
+ export declare const DataContext: import("react").Context<DataContextValue>;
19
+ export declare const NavigationContext: import("react").Context<NavigationContextValue>;
@@ -1,4 +1,4 @@
1
- import { BlockerState } from '../types/global.ts';
1
+ import type { BlockerState } from '../types/global.ts';
2
2
  type UseBlockerReturnValue = {
3
3
  state: BlockerState;
4
4
  process(): void;
@@ -1,4 +1,4 @@
1
- import { BlockerState, Location, RouteItem, UpdateBlockedRouteProps } from '../types/global.ts';
1
+ import type { BlockerState, Location, RouteItem, UpdateBlockedRouteProps } from '../types/global.ts';
2
2
  type UseHandleNavigation = {
3
3
  routeList: RouteItem[];
4
4
  setLocation: (arg: Location) => void;
@@ -1 +1 @@
1
- export declare const useLatest: <T>(value: T) => import('react').RefObject<T>;
1
+ export declare const useLatest: <T>(value: T) => import("react").RefObject<T>;
@@ -1,8 +1,8 @@
1
- import { RouteItem } from '../types/global.ts';
1
+ import type { RouteItem } from '../types/global.ts';
2
2
  export declare const useLoader: (routeList: RouteItem[]) => {
3
- loaderCache: Record<string, unknown>;
3
+ loaderCache: unknown;
4
4
  loaderError: boolean;
5
5
  prefetchLoader: (pathname: string) => Promise<void>;
6
6
  revalidateCache: (routeItem?: RouteItem) => Promise<void>;
7
- isLoadingMap: Record<string, boolean>;
7
+ isLoading: boolean;
8
8
  };
@@ -1 +1 @@
1
- export declare const useLocation: () => import('..').Location;
1
+ export declare const useLocation: () => import("../types/global").Location;
@@ -1,2 +1,2 @@
1
- import { Location } from '../types/global.ts';
1
+ import type { Location } from '../types/global.ts';
2
2
  export declare const useNavigate: () => (arg: Location | -1) => Promise<void>;
@@ -1,3 +1,3 @@
1
- export declare const useNavigationState: () => import('../context/RouterContext').NavigationContextValue;
2
- export declare const useRouterActions: () => import('../context/RouterContext').ActionsContextValue;
3
- export declare const useRouterData: () => import('../context/RouterContext').DataContextValue;
1
+ export declare const useNavigationState: () => import("../context/RouterContext").NavigationContextValue;
2
+ export declare const useRouterActions: () => import("../context/RouterContext").ActionsContextValue;
3
+ export declare const useRouterData: () => import("../context/RouterContext").DataContextValue;
package/dist/index.d.ts CHANGED
@@ -7,6 +7,5 @@ export { useLoaderState } from './hooks/useLoaderState';
7
7
  export { useBlocker } from './hooks/useBlocker';
8
8
  export { useBeforeUnload } from './hooks/useBeforeUnload';
9
9
  export { useRouterContext } from './hooks/useRouterContext';
10
- export { createRouter, parseWindowLocation, comparePaths } from './utils/utils';
10
+ export { createRouter } from './utils/utils';
11
11
  export { redirect } from './utils/redirect';
12
- export type { ClientRouteItem, RouteItem, Location, BlockerState } from './types/global';