Skip to content

Commit 9f61b15

Browse files
authored
Merge pull request #3976 from CNFeffery/dev
Add configurable scroll-to-top behavior to dcc.Link
2 parents f647f2d + b4e041b commit 9f61b15

4 files changed

Lines changed: 85 additions & 3 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ This project adheres to [Semantic Versioning](https://semver.org/).
55
## [Unreleased]
66

77
### Added
8+
- [#3976](https://github.com/plotly/dash/pull/3976) Add a new `scrollToTop` prop to `dcc.Link` to control whether the page scrolls to the top after client-side navigation. It defaults to `True` to preserve the existing behavior. Fixes [#3974](https://github.com/plotly/dash/issues/3974).
89
- [#3947](https://github.com/plotly/dash/pull/3947) Make `plotly-cloud` a default install dependency of Dash instead of an optional extra, so the `plotly` CLI and Dash's cloud integration work out of the box. The `dash[cloud]` extra is kept for backward compatibility.
910
- [#3930](https://github.com/plotly/dash/pull/3930) Add shared storage: a backend-agnostic cross-process state manager (key/value with optional TTL, plus ordered replayable pub/sub) on every app via `dash.ctx.shared_storage`, started lazily and disabled with `shared_storage=None`. Ships `LocalSharedStorage` (default, in-memory with optional disk persistence), `DiskcacheSharedStorage`, and `RedisSharedStorage` for horizontally-scaled deployments; see `.ai/ARCHITECTURE.md`.
1011
- [#3931](https://github.com/plotly/dash/pull/3931) Add streaming callbacks: an `async def` generator callback streams its yields to the browser as they are produced (`dash.Patch` yields apply incrementally). A browser's streams share one SharedWorker-hosted connection so they do not count against the per-host connection limit; closing a tab cancels its streams.

‎components/dash-core-components/src/components/Link.tsx‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,11 @@ type LinkComponentProps = LinkProps & {
1414
* For links with destinations outside the current app, `html.A` is a better
1515
* component to use.
1616
*/
17-
const Link = ({refresh = false, ...props}: LinkComponentProps) => {
17+
const Link = ({
18+
refresh = false,
19+
scrollToTop = true,
20+
...props
21+
}: LinkComponentProps) => {
1822
const {className, style, id, href, children, title, target, setProps} =
1923
props;
2024
const cleanUrl = window.dash_clientside.clean_url;
@@ -39,8 +43,9 @@ const Link = ({refresh = false, ...props}: LinkComponentProps) => {
3943
window.history.pushState({}, '', sanitizedUrl);
4044
window.dispatchEvent(new CustomEvent('_dashprivate_pushstate'));
4145
}
42-
// scroll back to top
43-
window.scrollTo(0, 0);
46+
if (scrollToTop) {
47+
window.scrollTo(0, 0);
48+
}
4449
};
4550

4651
useEffect(() => {

‎components/dash-core-components/src/types.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1514,6 +1514,12 @@ export interface LinkProps {
15141514
*/
15151515
refresh?: boolean;
15161516

1517+
/**
1518+
* Controls whether or not the page will scroll to the top when the link is
1519+
* clicked. Defaults to true.
1520+
*/
1521+
scrollToTop?: boolean;
1522+
15171523
/**
15181524
* Adds the title attribute to your link, which can contain supplementary
15191525
* information.
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import pytest
2+
3+
from dash import Dash, dcc, html
4+
from dash.testing.wait import until
5+
6+
7+
@pytest.mark.parametrize(
8+
"link_props,scrolls_to_top",
9+
[({}, True), ({"scrollToTop": False}, False)],
10+
ids=["default", "disabled"],
11+
)
12+
def test_lisc001_scroll_to_top(dash_dcc, link_props, scrolls_to_top):
13+
behavior = "enabled (default)" if scrolls_to_top else "disabled"
14+
app = Dash(__name__)
15+
app.layout = html.Div(
16+
[
17+
html.Div(
18+
[
19+
html.H1("TOP OF PAGE", id="top-marker"),
20+
html.P("The default Link behavior returns here after a click."),
21+
],
22+
id="top-section",
23+
style={
24+
"height": "100vh",
25+
"padding": "1rem",
26+
"boxSizing": "border-box",
27+
},
28+
),
29+
html.Div(
30+
[
31+
html.H1("BOTTOM OF PAGE", id="bottom-marker"),
32+
html.P(f"scrollToTop is {behavior}."),
33+
dcc.Link(
34+
"Click to navigate",
35+
href="/test-link",
36+
id="test-link",
37+
**link_props,
38+
),
39+
],
40+
id="bottom-section",
41+
style={
42+
"height": "100vh",
43+
"padding": "1rem",
44+
"boxSizing": "border-box",
45+
"borderTop": "1px solid",
46+
},
47+
),
48+
]
49+
)
50+
51+
dash_dcc.start_server(app)
52+
53+
test_link = dash_dcc.wait_for_element("#test-link")
54+
dash_dcc.driver.execute_script(
55+
"document.getElementById('bottom-section').scrollIntoView()"
56+
)
57+
until(lambda: dash_dcc.driver.execute_script("return window.scrollY") > 0, 3)
58+
initial_scroll_position = dash_dcc.driver.execute_script("return window.scrollY")
59+
60+
test_link.click()
61+
62+
until(lambda: dash_dcc.driver.current_url.endswith("/test-link"), 3)
63+
expected_scroll_position = 0 if scrolls_to_top else initial_scroll_position
64+
until(
65+
lambda: dash_dcc.driver.execute_script("return window.scrollY")
66+
== expected_scroll_position,
67+
3,
68+
)
69+
70+
assert dash_dcc.get_logs() == []

0 commit comments

Comments
 (0)