freya_router/lib.rs
1//! Routing
2//!
3//! High-level routing utilities for Freya applications. This crate provides
4//! components like [Outlet](self::components::Outlet) and [Router](self::components::Router), hooks such as [use_route](self::hooks::use_route), and the
5//! RouterContext to programmatically interact with navigation state.
6//!
7//! # Example
8//!
9//! A minimal router that switches between two routes. See `examples/feature_router.rs`
10//! for a runnable demo.
11//!
12//! ```rust
13//! use freya::{
14//! prelude::*,
15//! router::*,
16//! };
17//!
18//! fn app() -> impl IntoElement {
19//! Router::<Route>::new(|| RouterConfig::default().with_initial_path(Route::Home))
20//! }
21//!
22//! #[derive(PartialEq)]
23//! struct Layout;
24//! impl Component for Layout {
25//! fn render(&self) -> impl IntoElement {
26//! rect().center().expanded().child(Outlet::<Route>::new())
27//! }
28//! }
29//!
30//! #[derive(PartialEq)]
31//! struct Home;
32//! impl Component for Home {
33//! fn render(&self) -> impl IntoElement {
34//! Link::new(Route::Settings).child("Go Settings")
35//! }
36//! }
37//!
38//! #[derive(PartialEq)]
39//! struct Settings;
40//! impl Component for Settings {
41//! fn render(&self) -> impl IntoElement {
42//! Link::new(Route::Home).child("Go Home")
43//! }
44//! }
45//!
46//! #[derive(Routable, Clone, PartialEq)]
47//! #[rustfmt::skip]
48//! pub enum Route {
49//! #[layout(Layout)]
50//! #[route("/")]
51//! Home,
52//! #[route("/settings")]
53//! Settings,
54//! }
55//! ```
56// cannot use forbid, because props derive macro generates #[allow(missing_docs)]
57#![allow(non_snake_case)]
58
59mod memory;
60
61pub mod navigation;
62pub mod routable;
63
64/// Components interacting with the router.
65pub mod components {
66 mod outlet;
67 pub use outlet::*;
68
69 mod router;
70 pub use router::*;
71
72 #[doc(hidden)]
73 pub mod child_router;
74}
75
76mod contexts {
77 pub(crate) mod outlet;
78 pub use outlet::{
79 OutletContext,
80 use_outlet_context,
81 };
82 pub(crate) mod router;
83 pub use router::{
84 GenericRouterContext,
85 ParseRouteError,
86 RouterContext,
87 };
88}
89
90mod router_cfg;
91
92/// Hooks for interacting with the router in components.
93pub mod hooks {
94 mod use_route;
95 pub use use_route::*;
96}
97
98/// A collection of useful items most applications might need.
99pub mod prelude {
100 pub use freya_router_macro::Routable;
101
102 pub use crate::{
103 components::{
104 Outlet,
105 Router,
106 use_share_router,
107 },
108 contexts::*,
109 hooks::*,
110 memory::MemoryHistory,
111 navigation::*,
112 routable::*,
113 router_cfg::RouterConfig,
114 };
115}
116
117mod utils {
118 pub(crate) mod use_router_internal;
119}
120
121#[doc(hidden)]
122pub mod exports {
123 pub use urlencoding;
124}