GPUI Navigator
A declarative navigation library for GPUI with smooth transitions, nested routing, and beautiful default error pages.
Features
- 🎨 Smooth Transitions - Fade, slide animations with dual enter/exit support
- 🔀 Nested Routing - Parent/child route hierarchies with
RouterOutlet - 🎯 Simple API - Intuitive route definition with closures
- 🖼️ Beautiful Defaults - Pre-styled 404 and error pages out of the box
- 🔗 RouterLink Widget - Navigation links with active state styling
- 🛡️ Route Guards - Authentication and authorization (optional)
- 🔌 Middleware - Before/after navigation hooks (optional)
- 📝 Named Routes - Navigate by name instead of path
Why GPUI Navigator?
Unlike other GPUI routers:
- Zero Boilerplate - Define routes with simple closures, no complex builders
- Smooth Animations - Dual-animation system for professional transitions
- Production Ready - Beautiful error pages included, not placeholder text
- Developer Experience - Clean API inspired by modern web frameworks
Installation
[]
= "0.1"
= "0.2"
Quick Start
use *;
use *;
use *;
Route Builders
GPUI Navigator provides three ergonomic methods for defining routes:
Route::view() - Stateless Pages
For simple pages that don't need state or parameters:
view
Route::component() - Stateful Pages
For pages with internal state that persists across navigation. The component is automatically cached using window.use_keyed_state():
use *;
use *;
// Route definition - component state persists across navigation!
component
Benefits:
- ✅ State persists when navigating away and back
- ✅ Automatic Entity caching
- ✅ Clean, concise API
Route::component_with_params() - Stateful Pages with Route Params
For pages that need route parameters and maintain state:
// Each unique user_id gets its own cached component instance
component_with_params
Route::new() - Full Control
For advanced use cases when you need full control over the builder function:
new
Navigation
Programmatic Navigation
use Navigator;
// Push new route
push;
// Replace current route
replace;
// Go back
pop;
// Go forward
forward;
// Get current path
let path = current_path;
// Check if can go back
if can_pop
RouterLink Widget
Create clickable navigation links with automatic active state:
use *;
RouterLink features:
- ✅ Instant navigation with immediate UI updates
- ✅ Automatic active state detection
- ✅ Customizable active styling
- ✅ Works with nested routes
Route Transitions
Add smooth animations between pages:
use *;
// Fade transition
new
.transition
// Slide transitions
new
.transition
new
.transition
new
.transition
new
.transition
// No transition
new
.transition
Dual Animation System: GPUI Navigator uses the new route's transition for both exit and enter animations, creating smooth, professional transitions.
Route Parameters
Extract dynamic values from URLs:
use *;
// Define route with parameter
router.add_route;
// Navigate with parameter
push;
Nested Routes
Create layouts with child routes:
use *;
router.add_route;
Access nested routes:
/dashboard- Shows dashboard layout/dashboard/overview- Shows overview inside layout/dashboard/settings- Shows settings inside layout
Error Handling
Default Error Pages
GPUI Navigator includes beautiful, pre-styled error pages:
- 404 Page - Shown when no route matches (styled with red badge)
- Loading Page - Optional loading state
- Error Page - Generic error display
These work automatically - no configuration needed!
Custom Error Pages
Override defaults if desired:
use *;
let default_pages = new
.with_not_found;
Named Routes
Navigate by name instead of hardcoded paths:
// Define named route
router.add_route;
// Navigate by name
let mut params = new;
params.set;
push_named;
Optional Features
Enable advanced features in Cargo.toml:
[]
= { = "0.1", = ["guard", "middleware", "cache"] }
Route Guards
Protect routes with authentication:
use *;
new
.guard
Middleware
Add hooks before/after navigation:
use *;
;
new
.middleware
Examples
Run the included examples:
# Transition animations demo
# RouterLink and error handling demo
API Summary
| Function/Type | Description |
|---|---|
init_router(cx, |router| {...}) |
Initialize the router with routes |
Route::new(path, handler) |
Create a new route |
.transition(Transition::fade(ms)) |
Add transition animation |
.name("route-name") |
Name the route for reference |
.children(vec![...]) |
Add child routes |
Navigator::push(cx, path) |
Navigate to path |
Navigator::pop(cx) |
Go back |
RouterOutlet::new() |
Render current/child routes |
RouterLink::new(path) |
Create navigation link |
RouteParams::get("key") |
Get route parameter |
Architecture
GPUI Navigator is built with a clean, modular architecture:
- Core: Route matching, state management, navigation
- Widgets: RouterOutlet (route renderer), RouterLink (nav links)
- Optional: Guards, middleware, caching (feature-gated)
- Defaults: Beautiful error pages included
Minimum Supported Rust Version
Rust 1.75 or later.
License
Licensed under either of:
- Apache License, Version 2.0 (LICENSE-APACHE)
- MIT license (LICENSE-MIT)
at your option.
Contributing
Contributions welcome! Please:
- Fork the repository
- Create your feature branch
- Add tests for new features
- Ensure all tests pass:
cargo test - Submit a Pull Request
Acknowledgments
- Built for GPUI by Zed Industries
- Inspired by modern web routing libraries