clear-react-router 1.0.3 → 1.0.5

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,5 +1,5 @@
1
- import { ReactNode } from 'react';
2
- import { ActionsContextValue, DataContextValue, NavigationContextValue } from '../context/RouterContext';
1
+ import { type ReactNode } from 'react';
2
+ import { type ActionsContextValue, type DataContextValue, type NavigationContextValue } from '../context/RouterContext';
3
3
  type RouterProviderProps = NavigationContextValue & ActionsContextValue & DataContextValue & {
4
4
  children: ReactNode;
5
5
  };
@@ -1,4 +1,4 @@
1
- import { ComponentType, ReactElement } from 'react';
1
+ import type { ComponentType, ReactElement } from 'react';
2
2
  export type LazyComponent = () => Promise<{
3
3
  default: ComponentType<unknown>;
4
4
  }>;
@@ -1,4 +1,4 @@
1
- import { ComponentType, ReactElement } from 'react';
1
+ import { type ComponentType, type ReactElement } from 'react';
2
2
  export declare const createLazyComponent: (importFn: () => Promise<{
3
3
  default: ComponentType<unknown>;
4
4
  }>, fallback?: ReactElement | (() => ReactElement)) => (() => ReactElement);
@@ -1,2 +1,2 @@
1
- import { ReactElement } from 'react';
1
+ import { type ReactElement } from 'react';
2
2
  export declare const renderElement: (Component?: (() => ReactElement) | ReactElement) => import("react").JSX.Element | null;
@@ -1,4 +1,4 @@
1
- import { ClientRouteItem, Location, RouteItem } from '../types/global.ts';
1
+ import type { ClientRouteItem, Location, RouteItem } from '../types/global.ts';
2
2
  export declare const createRouter: (clientList: ClientRouteItem[]) => RouteItem[];
3
3
  export declare const getParamsObject: (params: RouteItem["params"]) => {};
4
4
  export declare const parseWindowLocation: (location: typeof window.location) => Location;
package/package.json CHANGED
@@ -1,32 +1,13 @@
1
1
  {
2
2
  "name": "clear-react-router",
3
- "version": "1.0.3",
3
+ "version": "1.0.5",
4
4
  "description": "A lightweight, type-safe routing library for React applications",
5
- "repository": {
6
- "type": "git",
7
- "url": "https://github.com/AndrewBubnov/clear-react-router"
8
- },
9
- "sideEffects": false,
10
- "type": "module",
5
+ "author": "Andrew Bubnov",
11
6
  "main": "./dist/index.js",
12
7
  "module": "./dist/index.js",
13
8
  "types": "./dist/index.d.ts",
14
- "exports": {
15
- ".": {
16
- "types": "./dist/index.d.ts",
17
- "import": "./dist/index.js",
18
- "require": "./dist/index.js"
19
- }
20
- },
21
- "files": [
22
- "dist"
23
- ],
24
9
  "scripts": {
25
- "build": "vite build"
26
- },
27
- "peerDependencies": {
28
- "react": ">=16.8.0",
29
- "react-dom": ">=16.8.0"
10
+ "build": "vite build && tsc --emitDeclarationOnly"
30
11
  },
31
12
  "keywords": [
32
13
  "react",
@@ -36,15 +17,33 @@
36
17
  "spa",
37
18
  "nested-routes"
38
19
  ],
20
+ "repository": {
21
+ "type": "git",
22
+ "url": "https://github.com/AndrewBubnov/clear-react-router"
23
+ },
24
+ "sideEffects": false,
25
+ "peerDependencies": {
26
+ "react": ">=16.8.0",
27
+ "react-dom": ">=16.8.0"
28
+ },
29
+ "files": [
30
+ "dist"
31
+ ],
39
32
  "license": "MIT",
40
- "author": "Andrew Bubnov",
33
+ "type": "module",
34
+ "exports": {
35
+ ".": {
36
+ "types": "./dist/index.d.ts",
37
+ "import": "./dist/index.js",
38
+ "require": "./dist/index.js"
39
+ }
40
+ },
41
41
  "devDependencies": {
42
42
  "@types/react": "^19.2.17",
43
- "@vitejs/plugin-react": "^4.3.4",
43
+ "@vitejs/plugin-react": "^6.0.2",
44
44
  "react": "^19.2.7",
45
45
  "react-dom": "^19.2.7",
46
46
  "typescript": "^6.0.3",
47
- "vite": "^6.4.3",
48
- "vite-plugin-dts": "^5.0.2"
47
+ "vite": "^8.0.16"
49
48
  }
50
49
  }
package/README.md DELETED
@@ -1,169 +0,0 @@
1
- A lightweight, type-safe routing library for React applications with nested routes, data loading, navigation blocking, and prefetching.
2
-
3
- ## Features
4
-
5
- - 🧩 **Nested Routes** - Organize your UI with nested layouts and routes
6
- - ⚡ **Data Loading** - Built-in loaders with caching and stale-while-revalidate strategy
7
- - 🔒 **Navigation Blocking** - Prevent accidental navigation with `useBlocker`
8
- - 🎯 **Type-safe Redirects** - Redirect from loaders and beforeLoad hooks
9
- - 📦 **Prefetching** - Preload data on hover for instant navigation
10
- - 🚀 **Lazy Loading** - Code-split your routes with dynamic imports for optimal performance
11
- - 🎨 **Flexible API** - Use components or hooks as you prefer
12
- - 📱 **Browser History** - Full support for browser back/forward buttons
13
- - 🧠 **Context-aware** - Pass and update context through routes
14
-
15
- ## API
16
-
17
- ### `Router`
18
-
19
- Main component that renders the application based on current URL.
20
-
21
- | Prop | Type | Description |
22
- |------|------|-------------|
23
- | `routeList` | `RouteItem[]` | Array of route configurations |
24
- | `context` | `object` | Optional initial context (user, theme, etc.) |
25
-
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
- ### `Link`
40
-
41
- Component for client-side navigation with prefetch support.
42
-
43
- | Prop | Type | Default |
44
- |------|------|---------|
45
- | `to` | `string` | required |
46
- | `prefetch` | `boolean` | `true` |
47
- | `children` | `ReactElement` | required |
48
-
49
- ## Hooks
50
-
51
- ### `useNavigate()`
52
-
53
- Returns function to navigate programmatically:
54
-
55
- - `navigate({ pathname: '/about' })` - navigate to path
56
- - `navigate({ pathname: '/user/123', state: { fromDashboard: true } })` - navigate with state
57
- - `navigate(-1)` - go back
58
-
59
- **Note:** Navigation state can be accessed via `useLocation()`:
60
-
61
- ```
62
- const navigate = useNavigate();
63
- navigate({ pathname: '/profile', state: { userId: 123 } });
64
-
65
- // In Profile component
66
- const { state } = useLocation();
67
- console.log(state); // { userId: 123 }
68
- ```
69
-
70
- ### `useParams<T>()`
71
-
72
- Returns route parameters object.
73
-
74
- const params = useParams<{ userId: string }>();
75
- // URL: /user/123 → params.userId === '123'
76
-
77
- ### `useLocation()`
78
-
79
- Returns current location `{ pathname, search, state }`.
80
- ```
81
- const { pathname, search, state } = useLocation();
82
- ```
83
-
84
- ### `useLoaderState()`
85
-
86
- Returns loaderState from current route's loader.
87
-
88
- ### `useBlocker(callback)`
89
-
90
- Blocks navigation when callback returns `true`.
91
-
92
- **Returns:**
93
-
94
- | Property | Type | Description |
95
- |----------|------|-------------|
96
- | `state` | `'unblocked' \| 'charged' \| 'blocked'` | Current blocker state |
97
- | `process()` | `() => void` | Confirm navigation and proceed |
98
- | `reset()` | `() => void` | Cancel navigation |
99
-
100
- ```
101
- const { state, process, reset } = useBlocker(() => hasUnsavedChanges);
102
-
103
- useEffect(() => {
104
- if (state === 'blocked') {
105
- // Show your custom modal
106
- if (confirm('Leave without saving?')) {
107
- process();
108
- } else {
109
- reset();
110
- }
111
- }
112
- }, [state, process, reset]);
113
- ```
114
-
115
- ### `useBeforeUnload(callback?)`
116
-
117
- Executes a callback when the page is about to be closed or reloaded. Perfect for auto-saving data at the last moment.
118
-
119
- **Parameters:**
120
-
121
- | Parameter | Type | Description |
122
- |-----------|------|-------------|
123
- | `callback` | `() => void | undefined` | Function to execute before page unload (e.g., auto-save) |
124
-
125
- **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
-
127
- ```
128
- const [text, setText] = useState('');
129
- const onSave = useCallback(() => {
130
- localStorage.setItem('draft', text);
131
- }, [text]);
132
-
133
- // Auto-save when user tries to close/reload the page
134
- useBeforeUnload(text ? onSave : undefined);
135
- ```
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
-
153
- ## Lazy Loading
154
-
155
- Clear Router supports code-splitting out of the box. Simply pass a function that returns a dynamic import:
156
- ```
157
- {
158
- path: '/heavy-page',
159
- element: () => import('./pages/HeavyComponent'),
160
- fallback: () => <div>Loading...</div>,
161
- }
162
- ```
163
-
164
- ## Requirements
165
- - React 16.6+ (for React.lazy and Suspense)
166
- - Use `default` export for your lazy-loaded components
167
-
168
- ## License
169
- MIT
@@ -1,2 +0,0 @@
1
- declare const _default: import('vite').UserConfig;
2
- export default _default;