GPUI Navigator
A declarative navigation library for GPUI with support for nested routes, transitions, guards, and middleware.
Features
- 🎯 Declarative Route Definition - Define routes with a fluent builder API
- 🎨 Route Transitions - Built-in fade, slide, and scale animations
- 🔀 Nested Routing - Support for parent/child route hierarchies with
RouterOutlet - 🛡️ Route Guards - Authentication, authorization, and custom guards
- 🔌 Middleware - Before/after hooks for navigation events
- 📝 Named Routes - Navigate using route names instead of paths
- 🔍 Route Matching - Pattern matching with parameters and constraints
- 📊 Performance - Route cache for optimized lookups
- ⚡ Error Handling - Custom 404 and error handlers
Installation
Add to your Cargo.toml:
[]
= "0.1"
= "0.2"
Quick Start
use *;
use gpui-*;
;
Navigation
The library provides a Flutter-style navigation API:
use gpui-Navigator;
// Push new route
push;
// Replace current route
replace;
// Go back
pop;
// Go forward
forward;
// Get current path
let path = current_path;
// Check navigation state
if can_pop
Flutter-style API
// Navigator.of(context) style
of.push.pop;
// Or direct static methods
push;
Route Transitions
The router supports smooth transitions between routes:
use gpui-*;
// Fade transition
new.transition
// Slide transitions
new.transition
new.transition
new.transition
new.transition
// Scale transitions
new.transition
new.transition
new.transition
// No transition
new.transition
Nested Routes
Create hierarchical route structures with RouterOutlet:
use gpui-*;
// Configure nested routes
init_router;
Route Parameters
Extract parameters from route paths:
use gpui-*;
// Define route with parameter
new
// Access parameters in handler
// Navigate with parameters
push;
// Type-safe parameter extraction
let id: = params.get_as;
Route Guards
Protect routes with authentication and authorization:
use gpui-*;
// Authentication guard with custom check function
new
.guard
// Role-based guard
new
.guard
// Permission-based guard
new
.guard
// Custom guard with closure
new
.guard
Middleware
Add before/after hooks to routes:
use gpui-*;
// Create middleware from functions
;
new
.middleware
Named Routes
Navigate using route names:
use gpui-*;
// Define named route
new
.name
// Navigate by name
let mut params = new;
params.set;
push_named;
// Generate URL from name
if let Some = url_for
Error Handling
Custom error and 404 handlers:
use gpui-*;
init_router;
Examples
Run the transition demo:
Architecture
Module Structure
| Module | Description |
|---|---|
context |
Global router state and Navigator API |
route |
Route definition and configuration |
router |
Router state management |
transition |
Animation definitions |
guards |
Authentication/authorization guards |
middleware |
Navigation middleware |
lifecycle |
Route lifecycle hooks |
nested |
Nested routing and RouterOutlet |
params |
Route and query parameters |
error |
Error handling |
widgets |
UI components (RouterLink, RouterOutlet) |
Route Matching
Routes are matched using patterns with support for:
- Static segments:
/about - Dynamic parameters:
/users/:id - Wildcards:
/docs/*path - Constraints:
/users/:id<\d+>(numeric only)
Performance
- Route cache for O(1) lookups after first match
- Efficient parent route resolution
- Minimal allocations during navigation
- Cache statistics for monitoring
Minimum Supported Rust Version (MSRV)
This crate requires Rust 1.75 or later.
License
Licensed under either of:
- Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add some amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
Acknowledgments
- Inspired by Flutter Navigator
- Built for GPUI by Zed Industries