Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gloo-history based yew-router #2239

Merged
merged 18 commits into from
Dec 19, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add Navigator Kind.
  • Loading branch information
futursolo committed Dec 6, 2021
commit 8ddfdf0f531f08cf6bf4cc6520b8bf46139e21fa
11 changes: 10 additions & 1 deletion packages/yew-router/src/components/link.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use wasm_bindgen::UnwrapThrowExt;
use yew::prelude::*;
use yew::virtual_dom::AttrValue;

use crate::navigator::NavigatorKind;
use crate::scope_ext::RouterScopeExt;
use crate::Routable;

Expand Down Expand Up @@ -98,7 +99,15 @@ where
.link()
.navigator()
.expect_throw("failed to get navigator");
let href: AttrValue = navigator.route_to_url(to).into();
let href: AttrValue = {
let href = navigator.route_to_url(to);

match navigator.kind() {
NavigatorKind::Hash => format!("#{}", href).into(),
_ => href,
}
.into()
};
html! {
<a class={classes}
{href}
Expand Down
20 changes: 20 additions & 0 deletions packages/yew-router/src/navigator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,17 @@ use crate::routable::Routable;
pub type NavigationError = HistoryError;
pub type NavigationResult<T> = HistoryResult<T>;

/// The kind of Navigator Provider.
#[derive(Debug, PartialEq, Clone, Copy)]
pub enum NavigatorKind {
/// Browser History.
Browser,
/// Hash History.
Hash,
/// Memory History.
Memory,
}

#[derive(Debug, PartialEq, Clone)]
pub struct Navigator {
inner: AnyHistory,
Expand Down Expand Up @@ -118,6 +129,15 @@ impl Navigator {
.replace_with_query_and_state(self.route_to_url(route), query, state)
}

/// Returns the Navigator kind.
pub fn kind(&self) -> NavigatorKind {
match &self.inner {
AnyHistory::Browser(_) => NavigatorKind::Browser,
AnyHistory::Hash(_) => NavigatorKind::Hash,
AnyHistory::Memory(_) => NavigatorKind::Memory,
}
}

pub(crate) fn route_to_url(&self, route: impl Routable) -> Cow<'static, str> {
let url = route.to_path();

Expand Down