Skip to content

Commit aefebf0

Browse files
authored
clean up scroll logic (#1463)
It had a weird bounce to it after recent changes
1 parent 58e244c commit aefebf0

2 files changed

Lines changed: 5 additions & 6 deletions

File tree

app/javascript/application.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ var StoryView = Backbone.NativeView.extend({
136136
if (this.model.get("open")) {
137137
this.el.classList.add("open");
138138
if (storyLead) storyLead.style.display = "none";
139-
window.scrollTo(0, this.el.getBoundingClientRect().top + window.scrollY);
139+
this.el.scrollIntoView({ block: "start" });
140140
} else {
141141
this.el.classList.remove("open");
142142
if (storyLead) storyLead.style.display = "";
@@ -149,10 +149,7 @@ var StoryView = Backbone.NativeView.extend({
149149

150150
itemSelected: function() {
151151
this.el.classList.toggle("cursor", this.model.get("selected"));
152-
var rect = this.el.getBoundingClientRect();
153-
if (rect.top < 0 || rect.bottom > window.innerHeight) {
154-
window.scrollTo(0, rect.top + window.scrollY);
155-
}
152+
requestAnimationFrame(() => { this.el.scrollIntoView({ block: "nearest" }); });
156153
},
157154

158155
render: function() {
@@ -184,7 +181,7 @@ var StoryView = Backbone.NativeView.extend({
184181
if (this.model.shouldSave()) this.model.save(null, { headers: requestHeaders() });
185182
} else {
186183
this.model.toggle();
187-
window.scrollTo(0, this.el.getBoundingClientRect().top + window.scrollY);
184+
this.el.scrollIntoView({ block: "start" });
188185
}
189186
},
190187

spec/javascript/setup.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,8 @@ const templateHTML = [
8686

8787
document.body.insertAdjacentHTML("beforeend", templateHTML);
8888

89+
window.HTMLElement.prototype.scrollIntoView = function () { /* noop */ };
90+
8991
import { Story, StoryView, StoryList, AppView } from "../../app/javascript/application";
9092

9193
globalThis.Story = Story;

0 commit comments

Comments
 (0)