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::prelude::*;
14//! use freya_router::prelude::*;
15//!
16//! fn app() -> impl IntoElement {
17//!     router::<Route>(|| RouterConfig::default().with_initial_path(Route::Home))
18//! }
19//!
20//! #[derive(PartialEq)]
21//! struct Layout;
22//! impl Component for Layout {
23//!     fn render(&self) -> impl IntoElement {
24//!         rect().center().expanded().child(outlet::<Route>())
25//!     }
26//! }
27//!
28//! #[derive(PartialEq)]
29//! struct Home;
30//! impl Component for Home {
31//!     fn render(&self) -> impl IntoElement {
32//!         Link::new(Route::Settings).child("Go Settings")
33//!     }
34//! }
35//!
36//! #[derive(PartialEq)]
37//! struct Settings;
38//! impl Component for Settings {
39//!     fn render(&self) -> impl IntoElement {
40//!         Link::new(Route::Home).child("Go Home")
41//!     }
42//! }
43//!
44//! #[derive(Routable, Clone, PartialEq)]
45//! #[rustfmt::skip]
46//! pub enum Route {
47//!     #[layout(Layout)]
48//!         #[route("/")]
49//!         Home,
50//!         #[route("/settings")]
51//!         Settings,
52//! }
53//! ```
54// cannot use forbid, because props derive macro generates #[allow(missing_docs)]
55#![allow(non_snake_case)]
56
57mod memory;
58
59pub mod navigation;
60pub mod routable;
61
62/// Components interacting with the router.
63pub mod components {
64    mod outlet;
65    pub use outlet::*;
66
67    mod router;
68    pub use router::*;
69
70    #[doc(hidden)]
71    pub mod child_router;
72}
73
74mod contexts {
75    pub(crate) mod navigator;
76    pub(crate) mod outlet;
77    pub use outlet::{
78        OutletContext,
79        use_outlet_context,
80    };
81    pub(crate) mod router;
82    pub use navigator::*;
83    pub(crate) use router::*;
84    pub use router::{
85        GenericRouterContext,
86        ParseRouteError,
87        RouterContext,
88    };
89}
90
91mod router_cfg;
92
93/// Hooks for interacting with the router in components.
94pub mod hooks {
95    mod use_route;
96    pub use use_route::*;
97}
98
99/// A collection of useful items most applications might need.
100pub mod prelude {
101    pub use freya_router_macro::Routable;
102
103    pub use crate::{
104        components::{
105            outlet,
106            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}