A lightweight TypeScript router for web applications that leverages the Navigation API for seamless client-side routing.
- Modern routing with the Navigation API
- Configurable route matching and rendering
- Support for nested routes and child components
- Custom error handling
- Metadata-driven component registration
- History management with back/forward navigation
npm install @ayu-sh-kr/dota-router
# or
pnpm add @ayu-sh-kr/dota-router
# or
yarn add @ayu-sh-kr/dota-router
import { DomNavigationRouter, RouteConfig } from '@ayu-sh-kr/dota-router';
import { HomePage, AboutPage, NotFoundPage } from './components';
// Define your routes
const routes: RouteConfig[] = [
{
path: '/',
component: HomePage,
default: true
},
{
path: '/about',
component: AboutPage
}
];
// Create and initialize router
const router = new DomNavigationRouter({
routes,
errorRoute: {
path: '/error',
component: NotFoundPage
}
});
Navigate between pages using the static route method:
// Navigate to a path
DomNavigationRouter.route('/about');
// Navigate with options
DomNavigationRouter.route('/products', {
category: 'electronics',
sort: 'price'
});
const routes = [
{
path: '/dashboard',
component: DashboardComponent,
render: (path) => {
// Custom rendering logic
document.querySelector('#app-root').innerHTML = '<dashboard-view></dashboard-view>';
// Initialize other resources
}
}
];
const routes = [
{
path: '/admin',
component: AdminLayout,
children: [
{
path: '/users',
component: UsersComponent
},
{
path: '/settings',
component: SettingsComponent
}
]
}
];
The router automatically redirects to the error route when a path is not found:
// This will redirect to the error route if '/unknown' is not defined
DomNavigationRouter.route('/unknown');
The library provides utility functions for navigation:
import { RouterUtils } from '@ayu-sh-kr/dota-router';
// Get the previous path
const previousPath = RouterUtils.getPreviousPath();
// Get the current path
const currentPath = RouterUtils.getCurrentPath();
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature
) - Run tests (
npm test
) - Commit your changes using Changesets
- Push to the branch (
git push origin feature/amazing-feature
) - Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.