From 49366be2a5298388f0bf8e0874ca859b04ab489a Mon Sep 17 00:00:00 2001 From: Greg Johnston Date: Tue, 10 Dec 2024 16:26:11 -0500 Subject: [PATCH] feat: add `scroll` prop to `` component to control scrolling behavior (closes #2666) (#3333) --- router/src/link.rs | 7 ++++++- router/src/location/mod.rs | 3 ++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/router/src/link.rs b/router/src/link.rs index c4dfb10e68..76c05ef7e6 100644 --- a/router/src/link.rs +++ b/router/src/link.rs @@ -92,6 +92,9 @@ pub fn A( /// a trailing slash. #[prop(optional)] strict_trailing_slash: bool, + /// If `true`, the router will scroll to the top of the window at the end of navigation. Defaults to `true`. + #[prop(default = true)] + scroll: bool, /// The nodes or elements to be shown inside the link. children: Children, ) -> impl IntoView @@ -104,6 +107,7 @@ where exact: bool, children: Children, strict_trailing_slash: bool, + scroll: bool, ) -> impl IntoView { let RouterContext { current_url, .. } = use_context().expect("tried to use outside a ."); @@ -129,6 +133,7 @@ where href=move || href.get().unwrap_or_default() target=target aria-current=move || if is_active() { Some("page") } else { None } + data-noscroll=!scroll > {children()} @@ -137,7 +142,7 @@ where } let href = use_resolved_path(move || href.to_href()()); - inner(href, target, exact, children, strict_trailing_slash) + inner(href, target, exact, children, strict_trailing_slash, scroll) } // Test if `href` is active for `location`. Assumes _both_ `href` and `location` begin with a `'/'`. diff --git a/router/src/location/mod.rs b/router/src/location/mod.rs index cedaf4f444..6244870b12 100644 --- a/router/src/location/mod.rs +++ b/router/src/location/mod.rs @@ -359,7 +359,8 @@ where let change = LocationChange { value: to, replace, - scroll: true, + scroll: !a.has_attribute("noscroll") + && !a.has_attribute("data-noscroll"), state: State::new(state), };