1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
//! A router for Yew, supporting nesting.
//!
//! ## Usage
//!
//! The nested router makes use of Yew's `context` features. It injects a routing context, tied to
//! a type implementing the [`target::Target`] trait. It then is possible to scope/translate between
//! levels.
//!
//! ### Targets
//!
//! "Targets" are the route targets, things the page can point to. They must be an enum,
//! implementing the [`target::Target`] trait. This can easily be done using the `Target` derive:
//!
//! ```
//! # use yew_nested_router::prelude::*;
//! #[derive(Clone, Debug, PartialEq, Eq, Target)]
//! pub enum AppRoute {
//!   #[target(index)]
//!   Index,
//!   Foo,
//!   Bar,
//! }
//! ```
//!
//! This created a target enum with three paths (`/`, `/foo`, `/bar`).
//!
//! ### Main router
//!
//! Each application needs a main entry point for the router ([`Router`]). This simply injects the
//! routing context, and provides the necessary information internally. All children of the
//! component will simply be rendered.
//!
//! ```
//! # use yew::prelude::*;
//! # use yew_nested_router::prelude::*;
//! # #[derive(Clone, Debug, PartialEq, Eq, Target)]
//! # pub enum AppRoute { Index };
//! # #[function_component(MyContent)] fn my_content() -> Html { html!() }
//! #[function_component(MyApp)]
//! pub fn my_app() -> Html {
//!   html!(
//!     <Router<AppRoute>>
//!       <MyContent/>
//!     </Router<AppRoute>>
//!   )
//! }
//! ```
//!
//! ### Switching content
//!
//! Having the route context available, allows to switch based on its state. This is done using the
//! [`Switch`] component, which searches (upwards) for a matching routing context (of the target
//! type).
//!
//! ```
//! # use yew::prelude::*;
//! # use yew_nested_router::prelude::*;
//! # #[derive(Clone, Debug, PartialEq, Eq, Target)]
//! # pub enum AppRoute {
//! #   #[target(index)]
//! #   Index,
//! #   Foo,
//! #   Bar,
//! # }
//! # #[function_component(Index)] fn index() -> Html { html!() }
//! # #[function_component(Foo)] fn foo() -> Html { html!() }
//! # #[function_component(Bar)] fn bar() -> Html { html!() }
//! #[function_component(MyContent)]
//! pub fn my_content() -> Html {
//!   html!(
//!     <Switch<AppRoute> render={|target|match target {
//!       AppRoute::Index => html!(<Index/>),
//!       AppRoute::Foo => html!(<Foo/>),
//!       AppRoute::Bar => html!(<Bar/>),
//!     }}/>
//!   )
//! }
//! ```
//!
//! The `Switch` component does not have any children, as its content is evaluated from the `render`
//! callback.
//!
//! If no target matched, then none of the switches will match either. If is possible to define a
//! default target on the router.
//!
//! ### Nesting
//!
//! When nesting, first the structure must be declared. Let's adapt the example from above:
//!
//! ```
//! # use yew_nested_router_macros::Target;
//! #[derive(Clone, Debug, PartialEq, Eq, Target)]
//! pub enum AppRoute {
//!   #[target(index)]
//!   Index,
//!   Foo(#[target(default)] Details),
//!   Bar {
//!     id: String,
//!     #[target(nested, default)]
//!     details: Details,
//!   },
//! }
//!
//! #[derive(Clone, Debug, PartialEq, Eq, Target)]
//! pub enum Details {
//!   Overview,
//!   Code,
//!   Metrics,
//! }
//!
//! impl Default for Details {
//!   fn default() -> Self {
//!     Self::Overview
//!   }
//! }
//! ```
//!
//! This changed the following:
//!
//! * It added a nested layer to `Foo`, which by default will use the `Details::Overview` target.
//! * It added a nested layer to `Bar`
//!   * Capturing a path variable into `id`
//!   * Then using a nested layer to `Details`, again using the default target.
//!
//!  This will process the following routes
//!
//! | Path | Target |
//! | ---- | ------ |
//! | `/` | `AppRoute::Index` |
//! | `/foo`, `/foo/overview` | `AppRoute::Foo({id}::Overview)` |
//! | `/foo/code` | `AppRoute::Foo(Details::Code)` |
//! | `/foo/metrics` | `AppRoute::Foo({id}::Metrics)` |
//! | `/bar` | no match |
//! | `/bar/{id}`, `/foo/{id}/overview` | `AppRoute::Bar {id: "id", details: Details::Overview}` |
//! | `/foo/{id}/code` | `AppRoute::Bar {id: "id", details: Details::Code}` |
//! | `/foo/{id}/metrics` | `AppRoute::Bar {id: "id", details: Details::Metrics}` |
//!
//! ### Scoping/Translating
//!
//! The main router will only insert an routing context for the `AppRoutes` context. Now we need to
//! translate down the next level. This is done using the [`Scope`] component, which translates
//! "from" -> "to", or `P`arent -> `C`hild.
//!
//! ```
//! # use yew::prelude::*;
//! # use yew_nested_router::prelude::*;
//! # #[derive(Clone, Debug, PartialEq, Eq, Target)]
//! # pub enum AppRoute {
//! #   #[target(index)]
//! #   Index,
//! #   Foo(Details),
//! #   Bar,
//! # }
//! # #[derive(Clone, Debug, PartialEq, Eq, Target)]
//! # pub enum Details {
//! #   Overview,
//! #   Code,
//! #   Metrics,
//! # }
//! #[function_component(Foo)]
//! pub fn foo() -> Html {
//!   html! (
//!     <Scope<AppRoute, Details> mapper={AppRoute::mapper_foo}>
//!       <Switch<Details> render={|target|html!(/* ... */)}/>
//!     </Scope<AppRoute, Details>>
//!   )
//! }
//! ```
//!
//! The `AppRoute::mapper_foo` function was automatically created by the `Target` derive. It
//! translates upwards and downwards between the two levels.
//!
//! **NOTE:** Targets having additional information do not get a mapper automatically created, as
//! that information might not be known on the lower levels.
//!
//! For a more complete example on nesting, see the full example in the `examples` folder.
//!
//! ### Navigating
//!
//! There is an out-of-the-box component named [`components::Link`], which allows to navigate to a
//! target. It is also possible to achieve the same, using the routing context, which can be
//! acquired using [`prelude::use_router`].
//!
//! ```
//! # use yew::prelude::*;
//! # use yew_nested_router::prelude::*;
//! # #[derive(Clone, Debug, PartialEq, Eq, Target)]
//! # pub enum AppRoute {
//! #   #[target(index)]
//! #   Index,
//! #   Foo(Details),
//! #   Bar{id: String, #[target(nested)] details: Details},
//! # }
//! # #[derive(Clone, Debug, PartialEq, Eq, Target)]
//! # pub enum Details {
//! #   Overview,
//! #   Code,
//! #   Metrics,
//! # }
//! # #[function_component(Index)] fn index() -> Html { html!() }
//! # #[function_component(Foo)] fn foo() -> Html { html!() }
//! # #[function_component(Bar)] fn bar() -> Html { html!() }
//! use yew_nested_router::components::Link;
//!
//! #[function_component(MyContent)]
//! pub fn my_content() -> Html {
//!   html!(
//!     <>
//!       <ul>
//!         <li><Link<AppRoute> target={AppRoute::Index}>{"Index"}</Link<AppRoute>></li>
//!         <li><Link<AppRoute> target={AppRoute::Foo(Details::Overview)}>{"Foo"}</Link<AppRoute>></li>
//!         <li><Link<AppRoute> target={AppRoute::Bar{ id: "".into(), details: Details::Overview}}>{"Bar"}</Link<AppRoute>></li>
//!       </ul>
//!       <Switch<AppRoute> render={|target|match target {
//!         AppRoute::Index => html!(<Index/>),
//!         AppRoute::Foo(_) => html!(<Foo/>),
//!         AppRoute::Bar{..} => html!(<Bar/>),
//!       }}/>
//!     </>
//!   )
//! }
//! ```
//!
//! ## More examples
//!
//! See the `examples` folder.

pub mod components;
pub mod target;

mod base;
mod router;
mod scope;
mod switch;

pub use router::Router;
pub use scope::Scope;
pub use switch::Switch;
pub use yew_nested_router_macros::Target;

/// Common includes.
pub mod prelude {
    pub use super::router::*;
    pub use super::scope::*;
    pub use super::switch::*;
    pub use super::target::*;

    pub use yew_nested_router_macros::Target;
}

/// Re-export the version of gloo_history we use
pub use gloo_history as history;
/// Re-export the history implementation we use
pub use gloo_history::BrowserHistory as History;