Skip to content

Commit

Permalink
feat: add scroll prop to <A/> component to control scrolling beha…
Browse files Browse the repository at this point in the history
…vior (closes #2666) (#3333)
  • Loading branch information
gbj authored Dec 10, 2024
1 parent 775bea4 commit 49366be
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
7 changes: 6 additions & 1 deletion router/src/link.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,9 @@ pub fn A<H>(
/// 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
Expand All @@ -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 <A/> outside a <Router/>.");
Expand All @@ -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()}
Expand All @@ -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 `'/'`.
Expand Down
3 changes: 2 additions & 1 deletion router/src/location/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
};

Expand Down

0 comments on commit 49366be

Please sign in to comment.