From 40382356047127406600f7fb5c78374c8e03ed30 Mon Sep 17 00:00:00 2001 From: Thomas Sibley Date: Tue, 29 Mar 2022 16:30:50 -0700 Subject: [PATCH] Make the nav sidebar's scroll-to-current-page behaviour less jumpy This avoids scrolling the sidebar when the link is already in view by using "nearest" for the vertical axis ("block") instead of the default of "start". This behaviour is much better when clicking on items in the current viewport, especially if the current viewport matches the initial viewport. It's still a little jumpy when the current link isn't visible in the initial viewport, because the sidebar position still changes across page loads. But the new behaviour solves the big problem on initial home page loads with a long sidebar where the logo and project title would be immediately scrolled out of view. Related to #824. --- src/theme.js | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/theme.js b/src/theme.js index 504fa642b..144e5b0eb 100644 --- a/src/theme.js +++ b/src/theme.js @@ -159,7 +159,15 @@ function ThemeNav () { .addClass('current') .attr('aria-expanded','true'); } - link[0].scrollIntoView(); + /* Scroll the link's top-level parent into view first. Then + * scroll the link itself into view, which will only have an + * effect if the top-level parent is taller than the viewport + * and the link is still outside the viewport after the first + * scroll. (This seems likely to be rare.) + */ + link.closest('li.toctree-l1')[0] + .scrollIntoView({block: "nearest"}); + link[0].scrollIntoView({block: "nearest"}); } } catch (err) {