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

feat: add scroll prop to <A/> component to control scrolling behavior (closes #2666) #3333

Merged
merged 1 commit into from
Dec 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
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
Loading