fix(dependencies): Updated dependencies and improved documentation
This commit is contained in:
69
readme.md
69
readme.md
@@ -1,10 +1,10 @@
|
||||
# @push.rocks/smartrouter
|
||||
a router for routing on websites
|
||||
A JavaScript library providing routing capabilities for web applications with support for path parameters, query parameters, and programmatic navigation
|
||||
|
||||
## Install
|
||||
To install `@push.rocks/smartrouter`, run the following command in your project directory:
|
||||
```sh
|
||||
npm install @push.rocks/smartrouter --save
|
||||
pnpm install @push.rocks/smartrouter --save
|
||||
```
|
||||
|
||||
This will add `@push.rocks/smartrouter` to your project's dependencies and enable you to use it within your application.
|
||||
@@ -73,45 +73,70 @@ router.pushUrl('/about');
|
||||
|
||||
// Navigate to a user profile with URL parameters
|
||||
router.pushUrl('/user/12345');
|
||||
|
||||
// Navigate with state data
|
||||
router.pushUrl('/dashboard', { previousPage: 'home' });
|
||||
```
|
||||
|
||||
### Managing Query Parameters
|
||||
`@push.rocks/smartrouter` provides methods for managing URL query parameters, enabling dynamic URL manipulation for filter settings, pagination, and other use cases.
|
||||
|
||||
```typescript
|
||||
// Set a query parameter
|
||||
// Set a query parameter (replace by default)
|
||||
router.queryParams.setQueryParam('key', 'value');
|
||||
|
||||
// Set a query parameter with push (adds to history)
|
||||
router.queryParams.setQueryParam('key', 'value', 'push');
|
||||
|
||||
// Get a query parameter
|
||||
const value = router.queryParams.getQueryParam('key');
|
||||
|
||||
// Get all query parameters as an object
|
||||
const allParams = router.queryParams.getAllAsObject();
|
||||
|
||||
// Delete a query parameter
|
||||
router.queryParams.deleteQueryParam('key');
|
||||
|
||||
// Delete with push to history
|
||||
router.queryParams.deleteQueryParam('key', 'push');
|
||||
```
|
||||
|
||||
### Selection Dimensions
|
||||
`@push.rocks/smartrouter` introduces the concept of selection dimensions, allowing you to manage stateful selections across routes. This is especially useful for complex navigation flows that depend on prior selections.
|
||||
### Sub-Routers
|
||||
Create sub-routers with a specific base path for modular routing:
|
||||
|
||||
```typescript
|
||||
await router.createSelectionDimension({
|
||||
routeArg: '/select/:option',
|
||||
keyArg: 'mySelection',
|
||||
options: [
|
||||
{
|
||||
key: 'option1',
|
||||
detail: { /* some data */ },
|
||||
action: async () => { /* action for option1 */ }
|
||||
},
|
||||
{
|
||||
key: 'option2',
|
||||
detail: { /* some data */ },
|
||||
action: async () => { /* action for option2 */ }
|
||||
}
|
||||
]
|
||||
// Create a sub-router for admin routes
|
||||
const adminRouter = router.createSubRouter('/admin');
|
||||
|
||||
// Routes will be prefixed with /admin
|
||||
adminRouter.on('/dashboard', async (routeInfo) => {
|
||||
// This handles /admin/dashboard
|
||||
});
|
||||
|
||||
// Navigate to a selection option
|
||||
router.pushUrl('/select/option1');
|
||||
adminRouter.on('/users', async (routeInfo) => {
|
||||
// This handles /admin/users
|
||||
});
|
||||
```
|
||||
|
||||
### Route Management
|
||||
The `on` method returns a function that can be used to remove the route:
|
||||
|
||||
```typescript
|
||||
// Add a route
|
||||
const removeRoute = router.on('/temporary', async (routeInfo) => {
|
||||
console.log('Temporary route accessed');
|
||||
});
|
||||
|
||||
// Later, remove the route
|
||||
removeRoute();
|
||||
```
|
||||
|
||||
### Cleanup
|
||||
When you're done with a router instance, clean it up properly:
|
||||
|
||||
```typescript
|
||||
// Destroy the router, removing all event listeners and routes
|
||||
router.destroy();
|
||||
```
|
||||
|
||||
This module enables complex routing scenarios, simplifying the handling of navigational logic in modern web applications. By leveraging `@push.rocks/smartrouter`, developers can implement detailed routing mechanisms, manipulate browser history thoughtfully, and maintain cleaner URL structures, enhancing the user experience and making web apps more accessible.
|
||||
|
Reference in New Issue
Block a user