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//! `Navigator` context 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::prelude::*,
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 navigator;
78    pub(crate) mod outlet;
79    pub use outlet::{
80        OutletContext,
81        use_outlet_context,
82    };
83    pub(crate) mod router;
84    pub use navigator::*;
85    pub(crate) use router::*;
86    pub use router::{
87        GenericRouterContext,
88        ParseRouteError,
89        RouterContext,
90    };
91}
92
93mod router_cfg;
94
95/// Hooks for interacting with the router in components.
96pub mod hooks {
97    mod use_route;
98    pub use use_route::*;
99}
100
101/// A collection of useful items most applications might need.
102pub mod prelude {
103    pub use freya_router_macro::Routable;
104
105    pub use crate::{
106        components::{
107            Outlet,
108            Router,
109            use_share_router,
110        },
111        contexts::*,
112        hooks::*,
113        memory::MemoryHistory,
114        navigation::*,
115        routable::*,
116        router_cfg::RouterConfig,
117    };
118}
119
120mod utils {
121    pub(crate) mod use_router_internal;
122}
123
124#[doc(hidden)]
125pub mod exports {
126    pub use urlencoding;
127}