Skip to content

Commit

Permalink
Router refresh (#751)
Browse files Browse the repository at this point in the history
* feat(router): refresh and navigate_no_history

* docs(router): add refresh and navigate_no_history

* docs(router): rephrase

Co-authored-by: Luke <[email protected]>

* fix(router): scroll to top inside refresh and navigate_no_history

---------

Co-authored-by: Luke <[email protected]>
  • Loading branch information
davidon-top and lukechu10 authored Nov 3, 2024
1 parent ed3cabc commit 47ca5f1
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions packages/sycamore-router/src/router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,46 @@ pub fn navigate_replace(url: &str) {
});
}

/// Navigates to the specified `url` without touching the history API.
///
/// This means that the url will not be updated and will continue to show the previous value.
///
/// # Panics
/// This function will `panic!()` if a [`Router`] has not yet been created.
pub fn navigate_no_history(url: &str) {
PATHNAME.with(|pathname| {
assert!(
pathname.get().is_some(),
"navigate_no_history can only be used with a Router"
);

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

window().scroll_to_with_x_and_y(0.0, 0.0);
});
}

/// Preform a "soft" refresh of the current page.
///
/// Unlike a "hard" refresh which corresponds to clicking on the refresh button, this simply forces a re-render of the view for the current page.
///
/// # Panic
/// This function will `panic!()` if a [`Router`] has not yet been created.
pub fn refresh() {
PATHNAME.with(|pathname| {
assert!(
pathname.get().is_some(),
"refresh can only be used with a Router"
);

pathname.get().unwrap_throw().update(|_| {});

window().scroll_to_with_x_and_y(0.0, 0.0);
});
}

fn meta_keys_pressed(kb_event: &KeyboardEvent) -> bool {
kb_event.meta_key() || kb_event.ctrl_key() || kb_event.shift_key() || kb_event.alt_key()
}
Expand Down

0 comments on commit 47ca5f1

Please sign in to comment.