Skip to content

Commit 28a8fa5

Browse files
committed
Fix unintended guide chapter scrolling when visit by turbo [skip ci]
When visiting a guide page by turbo, the navHighlight function fires the scrollIntoView API. Then, the chapter scrolls to the end of the chapter list by the updateHighlight function because IntersectionObserver's time origin is the first page loaded. To fix this, add "is the page navigated by directly" to the condition of the call updateHighlight function.
1 parent f9ec1ae commit 28a8fa5

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

guides/assets/javascripts/guides.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
for(var i = 0; i < array.length; i++) callback(array[i]);
2424
}
2525

26-
document.addEventListener("turbo:load", function() {
26+
document.addEventListener("turbo:load", function(turbo_load_event) {
2727
var guidesMenu = document.getElementById("guidesMenu");
2828
var guides = document.getElementById("guides");
2929

@@ -158,11 +158,15 @@
158158

159159
var PAGE_LOAD_BUFFER = 1000;
160160

161+
var isDirectlyVisited = function() {
162+
return Object.keys(turbo_load_event.detail.timing).length === 0;
163+
}
164+
161165
var navHighlight = function (entries) {
162166
entries.forEach(function (entry) {
163167
if (entry.isIntersecting) {
164168
updateHighlight(matchingNavLink(entry.target));
165-
} else if (entry.time >= PAGE_LOAD_BUFFER && belowBottomHalf(entry)) {
169+
} else if (isDirectlyVisited() && entry.time >= PAGE_LOAD_BUFFER && belowBottomHalf(entry)) {
166170
updateHighlight(matchingNavLink(prevElem(entry.target)));
167171
}
168172
});

0 commit comments

Comments
 (0)