Add scroll preserving snippet
This commit is contained in:
parent
09da4afc05
commit
4cdb4bde6f
@ -1 +1,31 @@
|
||||
export * from "./controllers.js";
|
||||
|
||||
(function enableScrollPreservation() {
|
||||
/* https://github.com/hotwired/turbo/issues/37#issuecomment-1422200868 */
|
||||
Turbo.scrollTop = 0;
|
||||
|
||||
Turbo.shouldPreserveScroll = false;
|
||||
|
||||
let shouldPreserveScroll = 0;
|
||||
|
||||
document.addEventListener("turbo:click", function (event) {
|
||||
if (event.target.hasAttribute("data-turbo-preserve-scroll")) {
|
||||
shouldPreserveScroll = true;
|
||||
} else {
|
||||
shouldPreserveScroll = false;
|
||||
}
|
||||
});
|
||||
|
||||
document.addEventListener("turbo:visit", function (event) {
|
||||
if (shouldPreserveScroll) {
|
||||
Turbo.scrollTop = document.documentElement.scrollTop;
|
||||
} else {
|
||||
Turbo.scrollTop = 0;
|
||||
}
|
||||
});
|
||||
|
||||
addEventListener("turbo:visit", () => {
|
||||
Turbo.navigator.currentVisit.scrolled = true;
|
||||
document.documentElement.scrollTop = Turbo.scrollTop;
|
||||
});
|
||||
})();
|
||||
|
Loading…
x
Reference in New Issue
Block a user