@idealyst/navigation 1.0.39 → 1.0.41
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/CLAUDE.md +417 -0
- package/LLM-ACCESS-GUIDE.md +166 -0
- package/README.md +517 -0
- package/package.json +8 -5
- package/src/context/README.md +329 -0
- package/src/examples/ExampleStackRouter.tsx +1 -1
- package/src/examples/README.md +394 -0
- package/src/layouts/GeneralLayout/README.md +498 -0
- package/src/routing/README.md +421 -0
|
@@ -0,0 +1,329 @@
|
|
|
1
|
+
# Navigation Context
|
|
2
|
+
|
|
3
|
+
The navigation context system provides a unified API for navigation across React and React Native platforms.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- ✅ Cross-platform navigation API
|
|
8
|
+
- ✅ Type-safe navigation parameters
|
|
9
|
+
- ✅ React Context-based state management
|
|
10
|
+
- ✅ Route variable support
|
|
11
|
+
- ✅ Automatic platform detection
|
|
12
|
+
|
|
13
|
+
## Core Components
|
|
14
|
+
|
|
15
|
+
### NavigatorProvider
|
|
16
|
+
|
|
17
|
+
The root provider that wraps your entire application and provides navigation context.
|
|
18
|
+
|
|
19
|
+
```tsx
|
|
20
|
+
import { NavigatorProvider } from '@idealyst/navigation';
|
|
21
|
+
import { RouteParam } from '@idealyst/navigation';
|
|
22
|
+
|
|
23
|
+
const AppRouter: RouteParam = {
|
|
24
|
+
path: "/",
|
|
25
|
+
component: HomeScreen,
|
|
26
|
+
routes: [
|
|
27
|
+
{ path: "profile", component: ProfileScreen },
|
|
28
|
+
{ path: "settings", component: SettingsScreen },
|
|
29
|
+
],
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
export default function App() {
|
|
33
|
+
return (
|
|
34
|
+
<NavigatorProvider route={AppRouter}>
|
|
35
|
+
{/* Your app content */}
|
|
36
|
+
</NavigatorProvider>
|
|
37
|
+
);
|
|
38
|
+
}
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
#### Props
|
|
42
|
+
|
|
43
|
+
| Prop | Type | Required | Description |
|
|
44
|
+
|------|------|----------|-------------|
|
|
45
|
+
| `route` | `RouteParam` | ✅ | Root route configuration |
|
|
46
|
+
| `children` | `ReactNode` | - | App content (usually not needed as router handles content) |
|
|
47
|
+
|
|
48
|
+
### useNavigator Hook
|
|
49
|
+
|
|
50
|
+
Access navigation functionality from any component within the NavigatorProvider.
|
|
51
|
+
|
|
52
|
+
```tsx
|
|
53
|
+
import { useNavigator } from '@idealyst/navigation';
|
|
54
|
+
|
|
55
|
+
function MyComponent() {
|
|
56
|
+
const navigator = useNavigator();
|
|
57
|
+
|
|
58
|
+
const handleNavigate = () => {
|
|
59
|
+
navigator.navigate({
|
|
60
|
+
path: "/profile",
|
|
61
|
+
vars: { userId: "123" },
|
|
62
|
+
});
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
return (
|
|
66
|
+
<Button onPress={handleNavigate}>
|
|
67
|
+
Go to Profile
|
|
68
|
+
</Button>
|
|
69
|
+
);
|
|
70
|
+
}
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
#### Returns
|
|
74
|
+
|
|
75
|
+
| Property | Type | Description |
|
|
76
|
+
|----------|------|-------------|
|
|
77
|
+
| `navigate` | `(params: NavigateParams) => void` | Navigate to a new route |
|
|
78
|
+
|
|
79
|
+
## Navigation Parameters
|
|
80
|
+
|
|
81
|
+
### NavigateParams
|
|
82
|
+
|
|
83
|
+
```tsx
|
|
84
|
+
type NavigateParams = {
|
|
85
|
+
path: string; // Route path to navigate to
|
|
86
|
+
vars: Record<string, string>; // Variables to pass to the route
|
|
87
|
+
};
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
#### Examples
|
|
91
|
+
|
|
92
|
+
```tsx
|
|
93
|
+
// Simple navigation
|
|
94
|
+
navigator.navigate({
|
|
95
|
+
path: "/settings",
|
|
96
|
+
vars: {},
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
// Navigation with parameters
|
|
100
|
+
navigator.navigate({
|
|
101
|
+
path: "/user/:id",
|
|
102
|
+
vars: { id: "123" },
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
// Navigation with multiple parameters
|
|
106
|
+
navigator.navigate({
|
|
107
|
+
path: "/product/:category/:id",
|
|
108
|
+
vars: {
|
|
109
|
+
category: "electronics",
|
|
110
|
+
id: "laptop-123"
|
|
111
|
+
},
|
|
112
|
+
});
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
## Route Configuration
|
|
116
|
+
|
|
117
|
+
### RouteParam
|
|
118
|
+
|
|
119
|
+
The core route configuration object used throughout the navigation system.
|
|
120
|
+
|
|
121
|
+
```tsx
|
|
122
|
+
type RouteParam = {
|
|
123
|
+
path?: string; // Route path (optional for root)
|
|
124
|
+
routes?: RouteParam[]; // Child routes
|
|
125
|
+
component: React.ComponentType; // Component to render for this route
|
|
126
|
+
layout?: LayoutParam; // Layout configuration
|
|
127
|
+
};
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
### LayoutParam
|
|
131
|
+
|
|
132
|
+
Configure the layout type and optional custom layout component.
|
|
133
|
+
|
|
134
|
+
```tsx
|
|
135
|
+
type LayoutParam = {
|
|
136
|
+
type: LayoutType; // Layout type
|
|
137
|
+
component?: React.ComponentType<{ children?: React.ReactNode }>; // Custom layout component
|
|
138
|
+
};
|
|
139
|
+
|
|
140
|
+
type LayoutType = 'stack' | 'tab' | 'drawer' | 'modal';
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
## Usage Examples
|
|
144
|
+
|
|
145
|
+
### Basic Navigation Setup
|
|
146
|
+
|
|
147
|
+
```tsx
|
|
148
|
+
import React from 'react';
|
|
149
|
+
import { NavigatorProvider, useNavigator } from '@idealyst/navigation';
|
|
150
|
+
import { Button, Screen, Text } from '@idealyst/components';
|
|
151
|
+
|
|
152
|
+
// Screens
|
|
153
|
+
const HomeScreen = () => {
|
|
154
|
+
const navigator = useNavigator();
|
|
155
|
+
|
|
156
|
+
return (
|
|
157
|
+
<Screen>
|
|
158
|
+
<Text size="large" weight="bold">Home</Text>
|
|
159
|
+
<Button onPress={() => navigator.navigate({ path: "/about", vars: {} })}>
|
|
160
|
+
Go to About
|
|
161
|
+
</Button>
|
|
162
|
+
</Screen>
|
|
163
|
+
);
|
|
164
|
+
};
|
|
165
|
+
|
|
166
|
+
const AboutScreen = () => (
|
|
167
|
+
<Screen>
|
|
168
|
+
<Text size="large" weight="bold">About</Text>
|
|
169
|
+
<Text>This is the about page</Text>
|
|
170
|
+
</Screen>
|
|
171
|
+
);
|
|
172
|
+
|
|
173
|
+
// Route configuration
|
|
174
|
+
const AppRouter: RouteParam = {
|
|
175
|
+
path: "/",
|
|
176
|
+
component: HomeScreen,
|
|
177
|
+
layout: { type: "stack" },
|
|
178
|
+
routes: [
|
|
179
|
+
{ path: "about", component: AboutScreen },
|
|
180
|
+
],
|
|
181
|
+
};
|
|
182
|
+
|
|
183
|
+
// App setup
|
|
184
|
+
export default function App() {
|
|
185
|
+
return (
|
|
186
|
+
<NavigatorProvider route={AppRouter}>
|
|
187
|
+
{/* Router handles all content rendering */}
|
|
188
|
+
</NavigatorProvider>
|
|
189
|
+
);
|
|
190
|
+
}
|
|
191
|
+
```
|
|
192
|
+
|
|
193
|
+
### Navigation with Parameters
|
|
194
|
+
|
|
195
|
+
```tsx
|
|
196
|
+
const UserListScreen = () => {
|
|
197
|
+
const navigator = useNavigator();
|
|
198
|
+
const users = [
|
|
199
|
+
{ id: "1", name: "John Doe" },
|
|
200
|
+
{ id: "2", name: "Jane Smith" },
|
|
201
|
+
];
|
|
202
|
+
|
|
203
|
+
return (
|
|
204
|
+
<Screen>
|
|
205
|
+
<Text size="large" weight="bold">Users</Text>
|
|
206
|
+
{users.map(user => (
|
|
207
|
+
<Button
|
|
208
|
+
key={user.id}
|
|
209
|
+
onPress={() => navigator.navigate({
|
|
210
|
+
path: "/user/:id",
|
|
211
|
+
vars: { id: user.id }
|
|
212
|
+
})}
|
|
213
|
+
>
|
|
214
|
+
{user.name}
|
|
215
|
+
</Button>
|
|
216
|
+
))}
|
|
217
|
+
</Screen>
|
|
218
|
+
);
|
|
219
|
+
};
|
|
220
|
+
|
|
221
|
+
const UserDetailScreen = () => {
|
|
222
|
+
// Access route parameters through context or props
|
|
223
|
+
return (
|
|
224
|
+
<Screen>
|
|
225
|
+
<Text size="large" weight="bold">User Details</Text>
|
|
226
|
+
{/* User details content */}
|
|
227
|
+
</Screen>
|
|
228
|
+
);
|
|
229
|
+
};
|
|
230
|
+
|
|
231
|
+
const AppRouter: RouteParam = {
|
|
232
|
+
path: "/",
|
|
233
|
+
component: UserListScreen,
|
|
234
|
+
routes: [
|
|
235
|
+
{ path: "user/:id", component: UserDetailScreen },
|
|
236
|
+
],
|
|
237
|
+
};
|
|
238
|
+
```
|
|
239
|
+
|
|
240
|
+
### Nested Routes with Different Layouts
|
|
241
|
+
|
|
242
|
+
```tsx
|
|
243
|
+
const DashboardRouter: RouteParam = {
|
|
244
|
+
path: "/",
|
|
245
|
+
component: DashboardHome,
|
|
246
|
+
layout: { type: "stack" },
|
|
247
|
+
routes: [
|
|
248
|
+
{
|
|
249
|
+
path: "analytics",
|
|
250
|
+
component: AnalyticsSection,
|
|
251
|
+
layout: { type: "tab" },
|
|
252
|
+
routes: [
|
|
253
|
+
{ path: "overview", component: AnalyticsOverview },
|
|
254
|
+
{ path: "reports", component: AnalyticsReports },
|
|
255
|
+
{ path: "insights", component: AnalyticsInsights },
|
|
256
|
+
],
|
|
257
|
+
},
|
|
258
|
+
{
|
|
259
|
+
path: "settings",
|
|
260
|
+
component: SettingsScreen,
|
|
261
|
+
layout: { type: "modal" },
|
|
262
|
+
},
|
|
263
|
+
],
|
|
264
|
+
};
|
|
265
|
+
```
|
|
266
|
+
|
|
267
|
+
## Platform Differences
|
|
268
|
+
|
|
269
|
+
### React Native
|
|
270
|
+
- Uses React Navigation's navigation context internally
|
|
271
|
+
- Supports hardware back button
|
|
272
|
+
- Native navigation animations and gestures
|
|
273
|
+
|
|
274
|
+
### Web
|
|
275
|
+
- Uses React Router's navigation context internally
|
|
276
|
+
- Browser URL integration
|
|
277
|
+
- Browser back/forward button support
|
|
278
|
+
|
|
279
|
+
The navigation context API remains identical across platforms.
|
|
280
|
+
|
|
281
|
+
## Best Practices
|
|
282
|
+
|
|
283
|
+
1. **Single NavigatorProvider**: Use only one NavigatorProvider at your app root
|
|
284
|
+
2. **Route Organization**: Keep route configurations organized and well-typed
|
|
285
|
+
3. **Parameter Validation**: Validate route parameters in target components
|
|
286
|
+
4. **Error Handling**: Handle navigation errors gracefully
|
|
287
|
+
5. **Deep Linking**: Design routes with deep linking in mind
|
|
288
|
+
6. **Testing**: Test navigation flows on both platforms
|
|
289
|
+
|
|
290
|
+
## TypeScript Support
|
|
291
|
+
|
|
292
|
+
The navigation context is fully typed for safety and developer experience:
|
|
293
|
+
|
|
294
|
+
```tsx
|
|
295
|
+
// All navigation parameters are type-checked
|
|
296
|
+
navigator.navigate({
|
|
297
|
+
path: "/user/:id",
|
|
298
|
+
vars: { id: userId }, // TypeScript ensures this matches the route pattern
|
|
299
|
+
});
|
|
300
|
+
|
|
301
|
+
// Route configurations are validated
|
|
302
|
+
const router: RouteParam = {
|
|
303
|
+
path: "/",
|
|
304
|
+
component: HomeScreen, // Must be a valid React component
|
|
305
|
+
routes: [], // Array of RouteParam objects
|
|
306
|
+
};
|
|
307
|
+
```
|
|
308
|
+
|
|
309
|
+
## Error Handling
|
|
310
|
+
|
|
311
|
+
```tsx
|
|
312
|
+
const MyComponent = () => {
|
|
313
|
+
const navigator = useNavigator();
|
|
314
|
+
|
|
315
|
+
const handleNavigate = async () => {
|
|
316
|
+
try {
|
|
317
|
+
navigator.navigate({
|
|
318
|
+
path: "/some-route",
|
|
319
|
+
vars: { param: "value" },
|
|
320
|
+
});
|
|
321
|
+
} catch (error) {
|
|
322
|
+
console.error('Navigation failed:', error);
|
|
323
|
+
// Handle navigation error
|
|
324
|
+
}
|
|
325
|
+
};
|
|
326
|
+
|
|
327
|
+
return <Button onPress={handleNavigate}>Navigate</Button>;
|
|
328
|
+
};
|
|
329
|
+
```
|