|
| 1 | +# @mk-singh/web-utils |
| 2 | + |
| 3 | +## Why This Library? |
| 4 | + |
| 5 | +When building React applications, you often need to: |
| 6 | + |
| 7 | +- Manage complex nested routes |
| 8 | +- Handle multi-step forms with navigation |
| 9 | +- Avoid hardcoding paths throughout your app |
| 10 | +- Maintain consistent navigation patterns |
| 11 | +- Build breadcrumbs and back buttons |
| 12 | + |
| 13 | +This library solves these problems by providing a declarative way to define your route structure once and generate all the paths automatically. |
| 14 | + |
| 15 | +## Compatibility |
| 16 | + |
| 17 | +- ✅ **React.js** (Perfect for SPA routing) |
| 18 | +- ✅ **Vue.js** (Works with Vue Router) |
| 19 | +- ✅ **Next.js** (App Router & Pages Router) |
| 20 | +- ✅ **Vanilla JavaScript** (Framework agnostic) |
| 21 | +- ✅ **TypeScript** (Full type support) |
| 22 | +- ✅ **Any JavaScript bundler** (Webpack, Vite, Rollup, etc.)# @mksingh/utils |
| 23 | + |
| 24 | +A collection of JavaScript utility functions designed for modern web development, with special focus on React.js applications. Modular and tree-shakeable for optimal bundle sizes. |
| 25 | + |
| 26 | +## Installation |
| 27 | + |
| 28 | +```javascript |
| 29 | +import { createRoutes } from "@mksingh/utils"; |
| 30 | +``` |
| 31 | + |
| 32 | +## Modular Imports |
| 33 | + |
| 34 | +Import only what you need to keep your bundle size small: |
| 35 | + |
| 36 | +```javascript |
| 37 | +// Import specific utilities |
| 38 | +import { createRoutes } from "@mksingh/utils/factories/route-factory"; |
| 39 | +``` |
| 40 | + |
| 41 | +## Features |
| 42 | + |
| 43 | +### Route Factory |
| 44 | + |
| 45 | +Create nested route structures with automatic path resolution and navigation helpers. |
| 46 | + |
| 47 | +#### `createRoutes(basePath, routeConfig)` |
| 48 | + |
| 49 | +Transforms a route configuration object into a fully resolved route structure with absolute paths. |
| 50 | + |
| 51 | +**Parameters:** |
| 52 | + |
| 53 | +- `basePath` (string): The base path to prepend to all routes |
| 54 | +- `routeConfig` (object): Route configuration with relative paths and navigation data |
| 55 | + |
| 56 | +**Returns:** Object with resolved absolute paths and preserved navigation data |
| 57 | + |
| 58 | +#### React.js Example |
| 59 | + |
| 60 | +```tsx |
| 61 | +import React from 'react'; |
| 62 | +import { BrowserRouter, Routes, Route, Link } from 'react-router-dom'; |
| 63 | +import { createRoutes } from "@mksingh/utils/factories/route-factory"; |
| 64 | + |
| 65 | +// Define your route configuration |
| 66 | +const routeConfig = { |
| 67 | + dashboard: { path: "dashboard" }, |
| 68 | + user: { |
| 69 | + path: "user", |
| 70 | + profile: { |
| 71 | + path: "profile", |
| 72 | + step: 1, |
| 73 | + prevPath: "../dashboard", |
| 74 | + }, |
| 75 | + settings: { |
| 76 | + path: "settings", |
| 77 | + step: 2, |
| 78 | + prevPath: "./profile", |
| 79 | + }, |
| 80 | + }, |
| 81 | + admin: { |
| 82 | + path: "admin", |
| 83 | + step: 3, |
| 84 | + prevPath: "./user/settings", |
| 85 | + }, |
| 86 | +}; |
| 87 | + |
| 88 | +// Generate routes with base path |
| 89 | +const routes = createRoutes("/app", routeConfig); |
| 90 | + |
| 91 | +function App() { |
| 92 | + return ( |
| 93 | + <BrowserRouter> |
| 94 | + <nav> |
| 95 | + <Link to={routes.dashboard.path}>Dashboard</Link> |
| 96 | + <Link to={routes.user.profile.path}>Profile</Link> |
| 97 | + <Link to={routes.user.settings.path}>Settings</Link> |
| 98 | + <Link to={routes.admin.path}>Admin</Link> |
| 99 | + </nav> |
| 100 | + |
| 101 | + <Routes> |
| 102 | + <Route path={routes.dashboard.path} element={<Dashboard />} /> |
| 103 | + <Route path={routes.user.profile.path} element={<Profile />} /> |
| 104 | + <Route path={routes.user.settings.path} element={<Settings />} /> |
| 105 | + <Route path={routes.admin.path} element={<Admin />} /> |
| 106 | + </Routes> |
| 107 | + </BrowserRouter> |
| 108 | + ); |
| 109 | +} |
| 110 | + |
| 111 | +// Use in components for navigation |
| 112 | +function Settings() { |
| 113 | + const handleBack = () => { |
| 114 | + navigate(routes.user.settings.prevPath); // Goes to /app/user/profile |
| 115 | + }; |
| 116 | + |
| 117 | + return ( |
| 118 | + <div> |
| 119 | + <button onClick={handleBack}>← Back</button> |
| 120 | + <h1>Settings (Step {routes.user.settings.step})</h1> |
| 121 | + </div> |
| 122 | + ); |
| 123 | +} |
| 124 | +``` |
| 125 | + |
| 126 | +#### Basic Example |
| 127 | + |
| 128 | +```javascript |
| 129 | +import { createRoutes } from "@mksingh/utils/factories/route-factory"; |
| 130 | + |
| 131 | +const routeConfig = { |
| 132 | + start: { path: "start" }, |
| 133 | + onboarding: { |
| 134 | + path: "onboarding", |
| 135 | + basicDetails: { |
| 136 | + path: "basic-details", |
| 137 | + step: 1, |
| 138 | + prevPath: "../start", |
| 139 | + }, |
| 140 | + businessDetails: { |
| 141 | + path: "business-details", |
| 142 | + step: 2, |
| 143 | + prevPath: "./basicDetails", |
| 144 | + }, |
| 145 | + }, |
| 146 | + review: { |
| 147 | + path: "review", |
| 148 | + step: 3, |
| 149 | + prevPath: "./onboarding/businessDetails", |
| 150 | + }, |
| 151 | +}; |
| 152 | + |
| 153 | +const routes = createRoutes("/client", routeConfig); |
| 154 | + |
| 155 | +console.log(routes); |
| 156 | +// Output: |
| 157 | +// { |
| 158 | +// start: { path: "/client/start" }, |
| 159 | +// onboarding: { |
| 160 | +// path: "/client/onboarding", |
| 161 | +// basicDetails: { |
| 162 | +// path: "/client/onboarding/basic-details", |
| 163 | +// step: 1, |
| 164 | +// prevPath: "/client/start", |
| 165 | +// }, |
| 166 | +// businessDetails: { |
| 167 | +// path: "/client/onboarding/business-details", |
| 168 | +// step: 2, |
| 169 | +// prevPath: "/client/onboarding/basic-details", |
| 170 | +// }, |
| 171 | +// }, |
| 172 | +// review: { |
| 173 | +// path: "/client/review", |
| 174 | +// step: 3, |
| 175 | +// prevPath: "/client/onboarding/business-details", |
| 176 | +// }, |
| 177 | +// } |
| 178 | +``` |
| 179 | + |
| 180 | +#### Path Resolution Rules |
| 181 | + |
| 182 | +The `createRoutes` function intelligently resolves different types of paths in `prevPath`: |
| 183 | + |
| 184 | +- **Relative paths with `../`**: Navigate up one level |
| 185 | + - `../start` from `/client/onboarding/basic-details` → `/client/start` |
| 186 | + |
| 187 | +- **Current directory paths with `./`**: Reference sibling routes |
| 188 | + - `./basicDetails` from `/client/onboarding/business-details` → `/client/onboarding/basic-details` |
| 189 | + |
| 190 | +- **Nested paths**: Navigate to nested routes |
| 191 | + - `./onboarding/businessDetails` from `/client/review` → `/client/onboarding/business-details` |
| 192 | + |
| 193 | +#### Use Cases |
| 194 | + |
| 195 | +Perfect for: |
| 196 | + |
| 197 | +- **React.js Single Page Applications** with complex routing |
| 198 | +- **Multi-step forms and wizards** in React components |
| 199 | +- **Navigation breadcrumbs** with automatic path resolution |
| 200 | +- **Dynamic route generation** for React Router |
| 201 | +- **Type-safe route management** in TypeScript React apps |
| 202 | +- **Centralized route configuration** to avoid hardcoded paths |
| 203 | +- **Back/Next navigation** in form workflows |
| 204 | + |
| 205 | +## API Reference |
| 206 | + |
| 207 | +### Route Factory |
| 208 | + |
| 209 | +```typescript |
| 210 | +createRoutes(basePath: string, routeConfig: RouteConfig): ResolvedRoutes |
| 211 | +``` |
| 212 | + |
| 213 | +## Development |
| 214 | + |
| 215 | +```bash |
| 216 | +# Run tests |
| 217 | +npm test |
| 218 | + |
| 219 | +# Format code |
| 220 | +npm run format |
| 221 | +``` |
| 222 | + |
| 223 | +## License |
| 224 | + |
| 225 | +MIT |
| 226 | + |
| 227 | +## Contributing |
| 228 | + |
| 229 | +Contributions are welcome! Please feel free to submit a Pull Request. |
0 commit comments