Skip to content

Commit f942221

Browse files
authored
Merge pull request rails#53283 from unasuke/fix-unintended-scrolling-in-chapters
Fix unintended guide chapter scrolling when visit by turbo [skip ci]
2 parents f9ec1ae + 28a8fa5 commit f942221

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)