Skip to content

Commit 776a085

Browse files
authored
Merge pull request plone#1339 from plone/patch-scrollToActive
Add patch scrollToActive (active main navigation item)
2 parents 77491ab + 0f792aa commit 776a085

File tree

2 files changed

+49
-2
lines changed

2 files changed

+49
-2
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/**
2+
* Patch of scrollToActive of sphinxbook theme
3+
* Scroll to active navigation item
4+
*/
5+
6+
/**
7+
* A helper function to load scripts when the DOM is loaded.
8+
* This waits for everything to be on the page first before running, since
9+
* some functionality doesn't behave properly until everything is ready.
10+
*/
11+
var sbRunWhenDOMLoaded = (cb) => {
12+
if (document.readyState != "loading") {
13+
cb();
14+
} else if (document.addEventListener) {
15+
document.addEventListener("DOMContentLoaded", cb);
16+
} else {
17+
document.attachEvent("onreadystatechange", function () {
18+
if (document.readyState == "complete") cb();
19+
});
20+
}
21+
};
22+
23+
/**
24+
* Sidebar scroll on load.
25+
*
26+
* Detect the active page in the sidebar, and scroll so that it is centered on
27+
* the screen.
28+
*/
29+
30+
var scrollToActive = () => {
31+
let navbar_scrollable = $("#site-navigation").children()[0];
32+
let active_navigation_item = $("#site-navigation .active").last();
33+
if (active_navigation_item) {
34+
if (active_navigation_item.offset().top > $(window).height() * 0.5) {
35+
navbar_scrollable.scrollTop = active_navigation_item.offset().top - $(window).height() * 0.2;
36+
}
37+
}
38+
};
39+
40+
41+
sbRunWhenDOMLoaded(scrollToActive);

docs/conf.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,10 @@
127127
"plone.restapi/src",
128128
]
129129

130+
html_js_files = [
131+
"patch_scrollToActive.js",
132+
]
133+
130134
html_extra_path = [
131135
"robots.txt",
132136
]
@@ -291,11 +295,13 @@ def source_replace(app, docname, source):
291295
result = result.replace(key, app.config.source_replacements[key])
292296
source[0] = result
293297

298+
294299
# Dict of replacements.
295300
source_replacements = {
296301
"{PLONE_BACKEND_VERSION}": "6.0.0b2",
297302
}
298303

304+
299305
def setup(app):
300-
app.add_config_value('source_replacements', {}, True)
301-
app.connect('source-read', source_replace)
306+
app.add_config_value("source_replacements", {}, True)
307+
app.connect("source-read", source_replace)

0 commit comments

Comments
 (0)