Skip to content

Commit a445ac7

Browse files
Add event listener for turbo:load event in scroll container to make sure turbo has fully loaded the page before calculating the top value of the scroll to menu
1 parent f96eb87 commit a445ac7

File tree

1 file changed

+9
-6
lines changed

1 file changed

+9
-6
lines changed

app/javascript/controllers/scroll_controller.js

+9-6
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,17 @@ import { Controller } from "@hotwired/stimulus"
33
export default class extends Controller {
44
static targets = ["list", "listItem", "scrollItem", "parent"]
55
currentSelectedIndex = -1;
6-
offsetY = this.parentTarget.getBoundingClientRect().top + window.scrollY;
6+
offsetY = 0;
77

88
connect() {
9-
this.listTarget.style.top = `${this.offsetY}px`;
10-
document.addEventListener("scroll", () => {
11-
this.highlight();
12-
});
13-
this.highlight()
9+
addEventListener('turbo:load', (event) => {
10+
this.offsetY = this.parentTarget.getBoundingClientRect().top + window.scrollY;
11+
this.listTarget.style.top = `${this.offsetY}px`;
12+
document.addEventListener("scroll", () => {
13+
this.highlight();
14+
});
15+
this.highlight()
16+
})
1417
}
1518

1619
disconnect() {

0 commit comments

Comments
 (0)