@termuijs/router
v0.1.3
Published
File-based screen routing for TermUI with typed params and navigation guards
Downloads
183
Maintainers
Readme
@termuijs/router
Screen routing for terminal apps. Register screens by name or point it at a directory and let the file system define your routes.
Install
npm install @termuijs/routerRequires @termuijs/core and @termuijs/widgets.
Manual routing
import { Router } from '@termuijs/router'
const router = new Router()
router.register('home', homeWidget)
router.register('settings', settingsWidget)
router.register('help', helpWidget)
// Navigate
router.push('settings')
router.back()
console.log(router.current) // 'home'File-based routing
Point the router at a directory. Each file becomes a screen:
screens/
index.ts -> /
settings.ts -> /settings
help.ts -> /help
users/
[id].ts -> /users/[id] (dynamic param)const router = new Router({ dir: './screens' })
// Dynamic params are available in the screen
router.push('/users/42')
// screen receives { id: '42' } as paramsRoute params
Dynamic segments use brackets in the filename. Params are typed and available inside the screen component.
// screens/logs/[level].ts
export default function LogScreen({ params }) {
const { level } = params // 'error', 'warn', etc.
return <LogView filter={level} />
}History
The router keeps a navigation stack. push() adds to it, back() pops. You can inspect the full stack with router.history.
Guards
Run a check before entering a route. Return false or a redirect path to prevent navigation.
router.guard('/settings', () => {
if (!isAuthenticated) return '/login'
return true
})Documentation
Full docs at www.termui.io/docs/router/overview.
License
MIT
