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

Simplify navigate functions in router #759

Merged
merged 2 commits into from
Nov 17, 2024
Merged
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
61 changes: 22 additions & 39 deletions packages/sycamore-router/src/router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -354,23 +354,11 @@
/// # Panics
/// This function will `panic!()` if a [`Router`] has not yet been created.
pub fn navigate(url: &str) {
PATHNAME.with(|pathname| {
assert!(
pathname.get().is_some(),
"navigate can only be used with a Router"
);

// Update History API.
let history = window().history().unwrap_throw();
history
.push_state_with_url(&JsValue::UNDEFINED, "", Some(url))
.unwrap_throw();
window().scroll_to_with_x_and_y(0.0, 0.0);

let pathname = pathname.get().unwrap_throw();
let path = url.strip_prefix(&base_pathname()).unwrap_or(url);
pathname.set(path.to_string());
});
let history = window().history().unwrap_throw();
history
.push_state_with_url(&JsValue::UNDEFINED, "", Some(url))
.unwrap_throw();
navigate_no_history(url);

Check warning on line 361 in packages/sycamore-router/src/router.rs

View check run for this annotation

Codecov / codecov/patch

packages/sycamore-router/src/router.rs#L357-L361

Added lines #L357 - L361 were not covered by tests
}

/// Navigates to the specified `url` without adding a new history entry. Instead, this replaces the
Expand All @@ -382,23 +370,11 @@
/// # Panics
/// This function will `panic!()` if a [`Router`] has not yet been created.
pub fn navigate_replace(url: &str) {
PATHNAME.with(|pathname| {
assert!(
pathname.get().is_some(),
"navigate_replace can only be used with a Router"
);

// Update History API.
let history = window().history().unwrap_throw();
history
.replace_state_with_url(&JsValue::UNDEFINED, "", Some(url))
.unwrap_throw();
window().scroll_to_with_x_and_y(0.0, 0.0);

let pathname = pathname.get().unwrap_throw();
let path = url.strip_prefix(&base_pathname()).unwrap_or(url);
pathname.set(path.to_string());
});
let history = window().history().unwrap_throw();
history
.replace_state_with_url(&JsValue::UNDEFINED, "", Some(url))
.unwrap_throw();
navigate_no_history(url);

Check warning on line 377 in packages/sycamore-router/src/router.rs

View check run for this annotation

Codecov / codecov/patch

packages/sycamore-router/src/router.rs#L373-L377

Added lines #L373 - L377 were not covered by tests
}

/// Navigates to the specified `url` without touching the history API.
Expand All @@ -408,18 +384,25 @@
/// # Panics
/// This function will `panic!()` if a [`Router`] has not yet been created.
pub fn navigate_no_history(url: &str) {
window().scroll_to_with_x_and_y(0.0, 0.0);
update_pathname(url);
}

Check warning on line 389 in packages/sycamore-router/src/router.rs

View check run for this annotation

Codecov / codecov/patch

packages/sycamore-router/src/router.rs#L387-L389

Added lines #L387 - L389 were not covered by tests

/// Internal function for getting the global pathname variable and updating it with the given `url`.
///
/// # Panics
/// This function will `panic!()` if a [`Router`] has not yet been created.
fn update_pathname(url: &str) {

Check warning on line 395 in packages/sycamore-router/src/router.rs

View check run for this annotation

Codecov / codecov/patch

packages/sycamore-router/src/router.rs#L395

Added line #L395 was not covered by tests
PATHNAME.with(|pathname| {
assert!(
pathname.get().is_some(),
"navigate_no_history can only be used with a Router"
"cannot navigate outside of a Router",

Check warning on line 399 in packages/sycamore-router/src/router.rs

View check run for this annotation

Codecov / codecov/patch

packages/sycamore-router/src/router.rs#L399

Added line #L399 was not covered by tests
);

window().scroll_to_with_x_and_y(0.0, 0.0);

let pathname = pathname.get().unwrap_throw();
let path = url.strip_prefix(&base_pathname()).unwrap_or(url);
pathname.set(path.to_string());
});
})

Check warning on line 405 in packages/sycamore-router/src/router.rs

View check run for this annotation

Codecov / codecov/patch

packages/sycamore-router/src/router.rs#L405

Added line #L405 was not covered by tests
}

/// Preform a "soft" refresh of the current page.
Expand All @@ -433,7 +416,7 @@
PATHNAME.with(|pathname| {
assert!(
pathname.get().is_some(),
"refresh can only be used with a Router"
"cannot refresh outside of a Router"

Check warning on line 419 in packages/sycamore-router/src/router.rs

View check run for this annotation

Codecov / codecov/patch

packages/sycamore-router/src/router.rs#L419

Added line #L419 was not covered by tests
);

window().scroll_to_with_x_and_y(0.0, 0.0);
Expand Down
Loading