Skip to content

Commit d5a71c8

Browse files
committed
fix: prevent scroll jump when clicking on cell output
Add scroll-margin CSS to data-el-cell-body to reduce scroll jumps when the cell body receives focus (e.g., when clicking on cell output). Add mousedown handler in cell.js to preserve scroll position when clicking on non-editor areas of the cell body.
1 parent 5ded53d commit d5a71c8

2 files changed

Lines changed: 26 additions & 0 deletions

File tree

assets/css/js_interop.css

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@ we hide/show certain elements. This way we don't have to engage the
66
server in solely client-side operations.
77
*/
88

9+
/* Prevent scroll jump when cell body is focused (e.g., clicking on output) */
10+
[data-el-cell-body] {
11+
scroll-margin-top: 50px;
12+
scroll-margin-bottom: 50px;
13+
}
14+
915
/* Hooks */
1016

1117
[phx-hook="Dropzone"][data-js-dragging] {

assets/js/hooks/cell.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,26 @@ const Cell = {
6868
this.el.setAttribute("data-js-hover", "");
6969
});
7070

71+
// Prevent scroll jump when clicking on cell output by handling mousedown
72+
// on the cell body. This ensures clicking on output doesn't trigger
73+
// unwanted scroll behavior when the cell body receives focus.
74+
const cellBody = this.el.querySelector(`[data-el-cell-body]`);
75+
if (cellBody) {
76+
cellBody.addEventListener("mousedown", (event) => {
77+
// If clicking on output (not an editor), prevent default focus scroll
78+
if (!event.target.closest(`[data-el-editor-container]`)) {
79+
// Save scroll position before focus changes
80+
const scrollX = window.scrollX;
81+
const scrollY = window.scrollY;
82+
83+
// After focus is applied, restore scroll position
84+
requestAnimationFrame(() => {
85+
window.scrollTo(scrollX, scrollY);
86+
});
87+
}
88+
});
89+
}
90+
7191
this.el.addEventListener("mouseleave", (event) => {
7292
this.el.removeAttribute("data-js-hover");
7393
});

0 commit comments

Comments
 (0)